Merge pull request #4696 from colemanw/CRM-15669
[civicrm-core.git] / tests / phpunit / api / v3 / GroupNestingTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 and the CiviCRM Licensing Exception. |
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 and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28
29 require_once 'CiviTest/CiviUnitTestCase.php';
30
31 /**
32 * Test class for GroupNesting API - civicrm_group_nesting_*
33 *
34 * @package CiviCRM
35 */
36 class api_v3_GroupNestingTest extends CiviUnitTestCase {
37 protected $_apiversion;
38
39 /**
40 * Sets up the fixture, for example, opens a network connection.
41 * This method is called before a test is executed.
42 *
43 */
44 protected function setUp() {
45 $this->_apiversion = 3;
46 parent::setUp();
47
48 // Insert a row in civicrm_group creating option group
49 // from_email_address group
50 $op = new PHPUnit_Extensions_Database_Operation_Insert();
51 $op->execute($this->_dbconn,
52 $this->createXMLDataSet(
53 dirname(__FILE__) . '/dataset/group_admins.xml'
54 )
55 );
56
57 // Insert a row in civicrm_group creating option group
58 // from_email_address group
59 $op = new PHPUnit_Extensions_Database_Operation_Insert();
60 $op->execute($this->_dbconn,
61 $this->createXMLDataSet(
62 dirname(__FILE__) . '/dataset/group_subscribers.xml'
63 )
64 );
65
66 // Insert a row in civicrm_group creating option group
67 // from_email_address group
68 $op = new PHPUnit_Extensions_Database_Operation_Insert();
69 $op->execute($this->_dbconn,
70 $this->createXMLDataSet(
71 dirname(__FILE__) . '/dataset/group_nesting.xml'
72 )
73 );
74 }
75
76 /**
77 * Tears down the fixture, for example, closes a network connection.
78 * This method is called after a test is executed.
79 *
80 */
81 protected function tearDown() {
82 // Truncate the tables
83 $this->quickCleanup(
84 array(
85 'civicrm_group',
86 'civicrm_group_nesting',
87 'civicrm_contact',
88 'civicrm_uf_group',
89 'civicrm_uf_join',
90 'civicrm_uf_match',
91 )
92 );
93 }
94
95 ///////////////// civicrm_group_nesting_get methods
96
97 /**
98 * Test civicrm_group_nesting_get.
99 */
100 public function testGet() {
101 $params = array(
102 'parent_group_id' => 1,
103 'child_group_id' => 2,
104 );
105
106 $result = $this->callAPIAndDocument('group_nesting', 'get', $params, __FUNCTION__, __FILE__);
107 // expected data loaded in setUp
108 $expected = array(
109 1 => array('id' => 1,
110 'child_group_id' => 2,
111 'parent_group_id' => 1,
112 ));
113
114 $this->assertEquals($expected, $result['values']);
115 }
116
117 /**
118 * Test civicrm_group_nesting_get with just one
119 * param (child_group_id).
120 */
121 public function testGetWithChildGroupId() {
122 $params = array(
123 'child_group_id' => 4, );
124
125 $result = $this->callAPISuccess('group_nesting', 'get', $params);
126
127 // expected data loaded in setUp
128 $expected = array(
129 3 => array('id' => 3,
130 'child_group_id' => 4,
131 'parent_group_id' => 1,
132 ),
133 4 => array(
134 'id' => 4,
135 'child_group_id' => 4,
136 'parent_group_id' => 2,
137 ),
138 );
139
140 $this->assertEquals($expected, $result['values']);
141 }
142
143 /**
144 * Test civicrm_group_nesting_get with just one
145 * param (parent_group_id).
146 */
147 public function testGetWithParentGroupId() {
148 $params = array(
149 'parent_group_id' => 1, );
150
151 $result = $this->callAPISuccess('group_nesting', 'get', $params);
152
153 // expected data loaded in setUp
154 $expected = array(
155 1 => array('id' => 1,
156 'child_group_id' => 2,
157 'parent_group_id' => 1,
158 ),
159 2 => array(
160 'id' => 2,
161 'child_group_id' => 3,
162 'parent_group_id' => 1,
163 ),
164 3 => array(
165 'id' => 3,
166 'child_group_id' => 4,
167 'parent_group_id' => 1,
168 ),
169 );
170
171 $this->assertEquals($expected, $result['values']);
172 }
173
174 /**
175 * Test civicrm_group_nesting_get for no records results.
176 * Success expected. (these tests are of marginal value as are in syntax conformance,
177 * don't copy & paste
178 */
179 public function testGetEmptyResults() {
180 $params = array(
181 'parent_group_id' => 1,
182 'child_group_id' => 700,
183 );
184 $result = $this->callAPISuccess('group_nesting', 'get', $params);
185 }
186
187 ///////////////// civicrm_group_nesting_create methods
188
189 /**
190 * Test civicrm_group_nesting_create.
191 */
192 public function testCreate() {
193 // groups id=1 and id=2 loaded in setUp
194 $params = array(
195 'parent_group_id' => 1,
196 'child_group_id' => 3,
197 );
198
199 $result = $this->callAPIAndDocument('group_nesting', 'create', $params, __FUNCTION__, __FILE__);
200
201 // we have 4 group nesting records in the example
202 // data, expecting next number to be the id for newly created
203 $id = 5;
204 $this->assertDBState('CRM_Contact_DAO_GroupNesting', $id, $params);
205 }
206
207 /**
208 * Test civicrm_group_nesting_create with empty parameter array.
209 * Error expected.
210 */
211 public function testCreateWithEmptyParams() {
212 $result = $this->callAPIFailure('group_nesting', 'create', array());
213 }
214
215 ///////////////// civicrm_group_nesting_remove methods
216
217 /**
218 * Test civicrm_group_nesting_remove.
219 */
220 public function testDelete() {
221 // groups id=1 and id=2 loaded in setUp
222 $getparams = array(
223 'parent_group_id' => 1,
224 'child_group_id' => 2, );
225
226 $result = $this->callAPISuccess('group_nesting', 'get', $getparams);
227 $params = array('id' => $result['id']);
228 $result = $this->callAPIAndDocument('group_nesting', 'delete', $params, __FUNCTION__, __FILE__);
229 $this->assertEquals(0, $this->callAPISuccess('group_nesting', 'getcount', $getparams));
230 }
231
232 /**
233 * Test civicrm_group_nesting_remove with empty parameter array.
234 * Error expected.
235 */
236 public function testDeleteWithEmptyParams() {
237 $result = $this->callAPIFailure('group_nesting', 'delete', array());
238 }
239 }