Add 'name' field to pseudoconstant schema CRM-12464
[civicrm-core.git] / tests / phpunit / api / v3 / GroupNestingTest.php
CommitLineData
6a488035 1<?php
6a488035
TO
2
3/*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27*/
28
29
30require_once 'CiviTest/CiviUnitTestCase.php';
31
32/**
33 * Test class for GroupNesting API - civicrm_group_nesting_*
34 *
35 * @package CiviCRM
36 */
37class api_v3_GroupNestingTest extends CiviUnitTestCase {
38 protected $_apiversion;
39 public $_eNoticeCompliant = True;
40
41 /**
42 * Sets up the fixture, for example, opens a network connection.
43 * This method is called before a test is executed.
44 *
45 * @access protected
46 */
47 protected function setUp() {
48 $this->_apiversion = 3;
49 parent::setUp();
50
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__) . '/dataset/group_admins.xml'
57 )
58 );
59
60 // Insert a row in civicrm_group creating option group
61 // from_email_address group
62 $op = new PHPUnit_Extensions_Database_Operation_Insert();
63 $op->execute($this->_dbconn,
64 new PHPUnit_Extensions_Database_DataSet_XMLDataSet(
65 dirname(__FILE__) . '/dataset/group_subscribers.xml'
66 )
67 );
68
69 // Insert a row in civicrm_group creating option group
70 // from_email_address group
71 $op = new PHPUnit_Extensions_Database_Operation_Insert();
72 $op->execute($this->_dbconn,
73 new PHPUnit_Extensions_Database_DataSet_XMLDataSet(
74 dirname(__FILE__) . '/dataset/group_nesting.xml'
75 )
76 );
77 }
78
79 /**
80 * Tears down the fixture, for example, closes a network connection.
81 * This method is called after a test is executed.
82 *
83 * @access protected
84 */
85 protected function tearDown() {
86 // Truncate the tables
87 $this->quickCleanup(
88 array(
89 'civicrm_group',
90 'civicrm_group_nesting',
91 'civicrm_contact',
92 'civicrm_uf_group',
93 'civicrm_uf_join',
94 'civicrm_uf_match',
95 )
96 );
97 }
98
99 ///////////////// civicrm_group_nesting_get methods
100
101 /**
102 * Test civicrm_group_nesting_get.
103 */
104 public function testGet() {
105 $params = array(
106 'parent_group_id' => 1,
107 'child_group_id' => 2,
108 'version' => $this->_apiversion,
109 );
110
111 $result = civicrm_api('group_nesting', 'get', $params);
112 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
113 // expected data loaded in setUp
114 $expected = array(
115 1 => array('id' => 1,
116 'child_group_id' => 2,
117 'parent_group_id' => 1,
118 ));
119
120 $this->assertEquals($expected, $result['values']);
121 }
122
123 /**
124 * Test civicrm_group_nesting_get with just one
125 * param (child_group_id).
126 */
127 public function testGetWithChildGroupId() {
128 $params = array(
129 'child_group_id' => 4,
130 'version' => $this->_apiversion,
131 );
132
133 $result = civicrm_api('group_nesting', 'get', $params);
134
135 // expected data loaded in setUp
136 $expected = array(
137 3 => array('id' => 3,
138 'child_group_id' => 4,
139 'parent_group_id' => 1,
140 ),
141 4 => array(
142 'id' => 4,
143 'child_group_id' => 4,
144 'parent_group_id' => 2,
145 ),
146 );
147
148 $this->assertEquals($expected, $result['values']);
149 }
150
151 /**
152 * Test civicrm_group_nesting_get with just one
153 * param (parent_group_id).
154 */
155 public function testGetWithParentGroupId() {
156 $params = array(
157 'parent_group_id' => 1,
158 'version' => $this->_apiversion,
159 );
160
161 $result = civicrm_api('group_nesting', 'get', $params);
162
163 // expected data loaded in setUp
164 $expected = array(
165 1 => array('id' => 1,
166 'child_group_id' => 2,
167 'parent_group_id' => 1,
168 ),
169 2 => array(
170 'id' => 2,
171 'child_group_id' => 3,
172 'parent_group_id' => 1,
173 ),
174 3 => array(
175 'id' => 3,
176 'child_group_id' => 4,
177 'parent_group_id' => 1,
178 ),
179 );
180
181 $this->assertEquals($expected, $result['values']);
182 }
183
184 /**
185 * Test civicrm_group_nesting_get for no records results.
186 * Error expected.
187 */
188 public function testGetEmptyResults() {
189 // no such record in the db
190 $params = array(
191 'parent_group_id' => 1,
192 'child_group_id' => 700,
193 );
194
195 $result = civicrm_api('group_nesting', 'get', $params);
196 $this->assertEquals($result['is_error'], 1,
197 "In line " . __LINE__
198 );
199 }
200
201 /**
202 * Test civicrm_group_nesting_get with empty params.
203 * Error expected.
204 */
205 public function testGetWithEmptyParams() {
206 $params = array();
207
208 $result = civicrm_api('group_nesting', 'get', $params);
209 $this->assertEquals($result['is_error'], 1,
210 "In line " . __LINE__
211 );
212 }
213
214 /**
215 * Test civicrm_group_nesting_get with wrong parameters type.
216 * Error expected.
217 */
218 public function testGetWithWrongParamsType() {
219 $params = 'a string';
220
221 $result = civicrm_api('group_nesting', 'get', $params);
222 $this->assertEquals($result['is_error'], 1,
223 "In line " . __LINE__
224 );
225 }
226
227 ///////////////// civicrm_group_nesting_create methods
228
229 /**
230 * Test civicrm_group_nesting_create.
231 */
232 public function testCreate() {
233 // groups id=1 and id=2 loaded in setUp
234 $params = array(
235 'parent_group_id' => 1,
236 'child_group_id' => 3,
237 'version' => $this->_apiversion,
238 );
239
240 $result = civicrm_api('group_nesting', 'create', $params);
241 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
242 $this->assertEquals($result['is_error'], 0);
243
244 // we have 4 group nesting records in the example
245 // data, expecting next number to be the id for newly created
246 $id = 5;
247 unset($params['version']);
248 $this->assertDBState('CRM_Contact_DAO_GroupNesting', $id, $params);
249 }
250
251 /**
252 * Test civicrm_group_nesting_create with empty parameter array.
253 * Error expected.
254 */
255 public function testCreateWithEmptyParams() {
256 $params = array();
257
258 $result = civicrm_api('group_nesting', 'create', $params);
259 $this->assertEquals($result['is_error'], 1,
260 "In line " . __LINE__
261 );
262 }
263
264 /**
265 * Test civicrm_group_nesting_create with wrong parameter type.
266 * Error expected.
267 */
268 public function testCreateWithWrongParamsType() {
269 $params = 'a string';
270
271 $result = civicrm_api('group_nesting', 'create', $params);
272 $this->assertEquals($result['is_error'], 1,
273 "In line " . __LINE__
274 );
275 }
276
277 ///////////////// civicrm_group_nesting_remove methods
278
279 /**
280 * Test civicrm_group_nesting_remove.
281 */
282 public function testDelete() {
283 // groups id=1 and id=2 loaded in setUp
284 $getparams = array(
285 'parent_group_id' => 1,
286 'child_group_id' => 2,
287 'version' => $this->_apiversion,
288 );
289
290 $result = civicrm_api('group_nesting', 'get', $getparams);
291 $params = array('version' => 3, 'id' => $result['id']);
292 $result = civicrm_api('group_nesting', 'delete', $params);
293 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
294 $this->assertAPISuccess($result, 'in line ' . __LINE__);
295 $this->assertEquals(0, civicrm_api('group_nesting', 'getcount', $getparams));
296 }
297
298 /**
299 * Test civicrm_group_nesting_remove with empty parameter array.
300 * Error expected.
301 */
302 public function testDeleteWithEmptyParams() {
303 $params = array();
304
305 $result = civicrm_api('group_nesting', 'delete', $params);
306 $this->assertEquals($result['is_error'], 1,
307 "In line " . __LINE__
308 );
309 }
310
311 /**
312 * Test civicrm_group_nesting_remove with wrong parameter type.
313 * Error expected.
314 */
315 public function testDeleteWithWrongParamsType() {
316 $params = 'a string';
317
318 $result = civicrm_api('group_nesting', 'delete', $params);
319 $this->assertEquals($result['is_error'], 1,
320 "In line " . __LINE__
321 );
322 }
323}
324