/tests/phpunit/api/v2/GroupNestingTest.php

https://github.com/ksecor/civicrm · PHP · 284 lines · 137 code · 42 blank · 105 comment · 0 complexity · e283d40336f2e671fe511e6e5af8639f MD5 · raw file

  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 3.1 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2009 |
  7. +--------------------------------------------------------------------+
  8. | This file is a part of CiviCRM. |
  9. | |
  10. | CiviCRM is free software; you can copy, modify, and distribute it |
  11. | under the terms of the GNU Affero General Public License |
  12. | Version 3, 19 November 2007. |
  13. | |
  14. | CiviCRM is distributed in the hope that it will be useful, but |
  15. | WITHOUT ANY WARRANTY; without even the implied warranty of |
  16. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
  17. | See the GNU Affero General Public License for more details. |
  18. | |
  19. | You should have received a copy of the GNU Affero General Public |
  20. | License along with this program; if not, contact CiviCRM LLC |
  21. | at info[AT]civicrm[DOT]org. If you have questions about the |
  22. | GNU Affero General Public License or the licensing of CiviCRM, |
  23. | see the CiviCRM license FAQ at http://civicrm.org/licensing |
  24. +--------------------------------------------------------------------+
  25. */
  26. require_once 'CiviTest/CiviUnitTestCase.php';
  27. require_once 'api/v2/GroupNesting.php';
  28. /**
  29. * Test class for GroupNesting API - civicrm_group_nesting_*
  30. *
  31. * @package CiviCRM
  32. */
  33. class api_v2_GroupNestingTest extends CiviUnitTestCase
  34. {
  35. /**
  36. * Sets up the fixture, for example, opens a network connection.
  37. * This method is called before a test is executed.
  38. *
  39. * @access protected
  40. */
  41. protected function setUp()
  42. {
  43. parent::setUp();
  44. // Insert a row in civicrm_group creating option group
  45. // from_email_address group
  46. $op = new PHPUnit_Extensions_Database_Operation_Insert( );
  47. $op->execute( $this->_dbconn,
  48. new PHPUnit_Extensions_Database_DataSet_XMLDataSet(
  49. dirname(__FILE__)
  50. . '/dataset/group_admins.xml') );
  51. // Insert a row in civicrm_group creating option group
  52. // from_email_address group
  53. $op = new PHPUnit_Extensions_Database_Operation_Insert( );
  54. $op->execute( $this->_dbconn,
  55. new PHPUnit_Extensions_Database_DataSet_XMLDataSet(
  56. dirname(__FILE__)
  57. . '/dataset/group_subscribers.xml') );
  58. // Insert a row in civicrm_group creating option group
  59. // from_email_address group
  60. $op = new PHPUnit_Extensions_Database_Operation_Insert( );
  61. $op->execute( $this->_dbconn,
  62. new PHPUnit_Extensions_Database_DataSet_XMLDataSet(
  63. dirname(__FILE__)
  64. . '/dataset/group_nesting.xml') );
  65. }
  66. /**
  67. * Tears down the fixture, for example, closes a network connection.
  68. * This method is called after a test is executed.
  69. *
  70. * @access protected
  71. */
  72. protected function tearDown()
  73. {
  74. }
  75. ///////////////// civicrm_group_nesting_get methods
  76. /**
  77. * Test civicrm_group_nesting_get.
  78. */
  79. public function testGet()
  80. {
  81. $params = array( 'parent_group_id' => 1,
  82. 'child_group_id' => 2 );
  83. $result =& civicrm_group_nesting_get($params);
  84. // expected data loaded in setUp
  85. $expected = array( 1 => array( 'id' => 1,
  86. 'child_group_id' => 2,
  87. 'parent_group_id' => 1 ) );
  88. $expected['is_error'] = 0;
  89. $this->assertEquals( $expected, $result );
  90. }
  91. /**
  92. * Test civicrm_group_nesting_get with just one
  93. * param (child_group_id).
  94. */
  95. public function testGetWithChildGroupId()
  96. {
  97. $params = array( 'child_group_id' => 4 );
  98. $result =& civicrm_group_nesting_get($params);
  99. // expected data loaded in setUp
  100. $expected = array( 3 => array( 'id' => 3,
  101. 'child_group_id' => 4,
  102. 'parent_group_id' => 1 ),
  103. 4 => array( 'id' => 4,
  104. 'child_group_id' => 4,
  105. 'parent_group_id' => 2) );
  106. $expected['is_error'] = 0;
  107. $this->assertEquals( $expected, $result );
  108. }
  109. /**
  110. * Test civicrm_group_nesting_get with just one
  111. * param (parent_group_id).
  112. */
  113. public function testGetWithParentGroupId()
  114. {
  115. $params = array( 'parent_group_id' => 1 );
  116. $result =& civicrm_group_nesting_get($params);
  117. // expected data loaded in setUp
  118. $expected = array( 1 => array( 'id' => 1,
  119. 'child_group_id' => 2,
  120. 'parent_group_id' => 1 ),
  121. 2 => array( 'id' => 2,
  122. 'child_group_id' => 3,
  123. 'parent_group_id' => 1 ),
  124. 3 => array( 'id' => 3,
  125. 'child_group_id' => 4,
  126. 'parent_group_id' => 1) );
  127. $expected['is_error'] = 0;
  128. $this->assertEquals( $expected, $result );
  129. }
  130. /**
  131. * Test civicrm_group_nesting_get for no records results.
  132. * Error expected.
  133. */
  134. public function testGetEmptyResults()
  135. {
  136. // no such record in the db
  137. $params = array( 'parent_group_id' => 1,
  138. 'child_group_id' => 700 );
  139. $result =& civicrm_group_nesting_get($params);
  140. $this->assertEquals( $result['is_error'], 1,
  141. "In line " . __LINE__ );
  142. }
  143. /**
  144. * Test civicrm_group_nesting_get with empty params.
  145. * Error expected.
  146. */
  147. public function testGetWithEmptyParams()
  148. {
  149. $params = array( );
  150. $result =& civicrm_group_nesting_get($params);
  151. $this->assertEquals( $result['is_error'], 1,
  152. "In line " . __LINE__ );
  153. }
  154. /**
  155. * Test civicrm_group_nesting_get with wrong parameters type.
  156. * Error expected.
  157. */
  158. public function testGetWithWrongParamsType()
  159. {
  160. $params = 'a string';
  161. $result =& civicrm_group_nesting_get($params);
  162. $this->assertEquals( $result['is_error'], 1,
  163. "In line " . __LINE__ );
  164. }
  165. ///////////////// civicrm_group_nesting_create methods
  166. /**
  167. * Test civicrm_group_nesting_create.
  168. */
  169. public function testCreate()
  170. {
  171. // groups id=1 and id=2 loaded in setUp
  172. $params = array( 'parent_group_id' => 1,
  173. 'child_group_id' => 3 );
  174. $result = civicrm_group_nesting_create( $params );
  175. $this->assertEquals( $result['is_error'], 0 );
  176. // we have 4 group nesting records in the example
  177. // data, expecting next number to be the id for newly created
  178. $id = 5;
  179. $this->assertDBState( 'CRM_Contact_DAO_GroupNesting', $id, $params );
  180. }
  181. /**
  182. * Test civicrm_group_nesting_create with empty parameter array.
  183. * Error expected.
  184. */
  185. public function testCreateWithEmptyParams()
  186. {
  187. $params = array( );
  188. $result =& civicrm_group_nesting_create($params);
  189. $this->assertEquals( $result['is_error'], 1,
  190. "In line " . __LINE__ );
  191. }
  192. /**
  193. * Test civicrm_group_nesting_create with wrong parameter type.
  194. * Error expected.
  195. */
  196. public function testCreateWithWrongParamsType()
  197. {
  198. $params = 'a string';
  199. $result =& civicrm_group_nesting_create($params);
  200. $this->assertEquals( $result['is_error'], 1,
  201. "In line " . __LINE__ );
  202. }
  203. ///////////////// civicrm_group_nesting_remove methods
  204. /**
  205. * Test civicrm_group_nesting_remove.
  206. */
  207. public function testRemove()
  208. {
  209. // groups id=1 and id=2 loaded in setUp
  210. $params = array( 'parent_group_id' => 1,
  211. 'child_group_id' => 2 );
  212. $result =& civicrm_group_nesting_remove($params);
  213. $this->assertEquals( $result['is_error'], 0 );
  214. // group nesting record with above combo of params
  215. // has id = 1, asserting it's gone
  216. $id = 1;
  217. $this->assertDBState( 'CRM_Contact_DAO_GroupNesting', $id, $params, true );
  218. }
  219. /**
  220. * Test civicrm_group_nesting_remove with empty parameter array.
  221. * Error expected.
  222. */
  223. public function testRemoveWithEmptyParams()
  224. {
  225. $params = array( );
  226. $result =& civicrm_group_nesting_remove($params);
  227. $this->assertEquals( $result['is_error'], 1,
  228. "In line " . __LINE__ );
  229. }
  230. /**
  231. * Test civicrm_group_nesting_remove with wrong parameter type.
  232. * Error expected.
  233. */
  234. public function testRemoveWithWrongParamsType()
  235. {
  236. $params = 'a string';
  237. $result =& civicrm_group_nesting_remove($params);
  238. $this->assertEquals( $result['is_error'], 1,
  239. "In line " . __LINE__ );
  240. }
  241. }
  242. ?>