Merge pull request #9187 from sqweets/ExportHeadersRelationships
[civicrm-core.git] / tests / phpunit / api / v3 / GroupNestingTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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 * Test class for GroupNesting API - civicrm_group_nesting_*
30 *
31 * @package CiviCRM
32 * @group headless
33 */
34 class api_v3_GroupNestingTest extends CiviUnitTestCase {
35 protected $_apiversion;
36
37 /**
38 * Sets up the fixture, for example, opens a network connection.
39 * This method is called before a test is executed.
40 */
41 protected function setUp() {
42 $this->_apiversion = 3;
43 parent::setUp();
44
45 // Insert a row in civicrm_group creating option group
46 // from_email_address group
47 $op = new PHPUnit_Extensions_Database_Operation_Insert();
48 $op->execute($this->_dbconn,
49 $this->createXMLDataSet(
50 dirname(__FILE__) . '/dataset/group_admins.xml'
51 )
52 );
53
54 // Insert a row in civicrm_group creating option group
55 // from_email_address group
56 $op = new PHPUnit_Extensions_Database_Operation_Insert();
57 $op->execute($this->_dbconn,
58 $this->createXMLDataSet(
59 dirname(__FILE__) . '/dataset/group_subscribers.xml'
60 )
61 );
62
63 // Insert a row in civicrm_group creating option group
64 // from_email_address group
65 $op = new PHPUnit_Extensions_Database_Operation_Insert();
66 $op->execute($this->_dbconn,
67 $this->createXMLDataSet(
68 dirname(__FILE__) . '/dataset/group_nesting.xml'
69 )
70 );
71 }
72
73 /**
74 * Tears down the fixture, for example, closes a network connection.
75 * This method is called after a test is executed.
76 */
77 protected function tearDown() {
78 // Truncate the tables
79 $this->quickCleanup(
80 array(
81 'civicrm_group',
82 'civicrm_group_nesting',
83 'civicrm_contact',
84 'civicrm_uf_group',
85 'civicrm_uf_join',
86 'civicrm_uf_match',
87 )
88 );
89 }
90
91 ///////////////// civicrm_group_nesting_get methods
92
93 /**
94 * Test civicrm_group_nesting_get.
95 */
96 public function testGet() {
97 $params = array(
98 'parent_group_id' => 1,
99 'child_group_id' => 2,
100 );
101
102 $result = $this->callAPIAndDocument('group_nesting', 'get', $params, __FUNCTION__, __FILE__);
103 // expected data loaded in setUp
104 $expected = array(
105 1 => array(
106 'id' => 1,
107 'child_group_id' => 2,
108 'parent_group_id' => 1,
109 ),
110 );
111
112 $this->assertEquals($expected, $result['values']);
113 }
114
115 /**
116 * Test civicrm_group_nesting_get with just one
117 * param (child_group_id).
118 */
119 public function testGetWithChildGroupId() {
120 $params = array(
121 'child_group_id' => 4,
122 );
123
124 $result = $this->callAPISuccess('group_nesting', 'get', $params);
125
126 // expected data loaded in setUp
127 $expected = array(
128 3 => array(
129 '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
152 $result = $this->callAPISuccess('group_nesting', 'get', $params);
153
154 // expected data loaded in setUp
155 $expected = array(
156 1 => array(
157 'id' => 1,
158 'child_group_id' => 2,
159 'parent_group_id' => 1,
160 ),
161 2 => array(
162 'id' => 2,
163 'child_group_id' => 3,
164 'parent_group_id' => 1,
165 ),
166 3 => array(
167 'id' => 3,
168 'child_group_id' => 4,
169 'parent_group_id' => 1,
170 ),
171 );
172
173 $this->assertEquals($expected, $result['values']);
174 }
175
176 /**
177 * Test civicrm_group_nesting_get for no records results.
178 * Success expected. (these tests are of marginal value as are in syntax conformance,
179 * don't copy & paste
180 */
181 public function testGetEmptyResults() {
182 $params = array(
183 'parent_group_id' => 1,
184 'child_group_id' => 700,
185 );
186 $this->callAPISuccess('group_nesting', 'get', $params);
187 }
188
189 ///////////////// civicrm_group_nesting_create methods
190
191 /**
192 * Test civicrm_group_nesting_create.
193 */
194 public function testCreate() {
195 // groups id=1 and id=2 loaded in setUp
196 $params = array(
197 'parent_group_id' => 1,
198 'child_group_id' => 3,
199 );
200
201 $this->callAPIAndDocument('group_nesting', 'create', $params, __FUNCTION__, __FILE__);
202 $this->callAPISuccessGetCount('GroupNesting', $params, 1);
203 }
204
205 /**
206 * Test civicrm_group_nesting_remove.
207 */
208 public function testDelete() {
209 // groups id=1 and id=2 loaded in setUp
210 $getparams = array(
211 'parent_group_id' => 1,
212 'child_group_id' => 2,
213 );
214
215 $result = $this->callAPISuccess('group_nesting', 'get', $getparams);
216 $params = array('id' => $result['id']);
217 $this->callAPIAndDocument('group_nesting', 'delete', $params, __FUNCTION__, __FILE__);
218 $this->assertEquals(0, $this->callAPISuccess('group_nesting', 'getcount', $getparams));
219 }
220
221 /**
222 * Test civicrm_group_nesting_remove with empty parameter array.
223 *
224 * Error expected.
225 */
226 public function testDeleteWithEmptyParams() {
227 $this->callAPIFailure('group_nesting', 'delete', array());
228 }
229
230 }