Merge pull request #15764 from civicrm/5.20
[civicrm-core.git] / tests / phpunit / api / v3 / GroupNestingTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
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
36 /**
37 * Sets up the fixture, for example, opens a network connection.
38 *
39 * This method is called before a test is executed.
40 */
41 protected function setUp() {
42 parent::setUp();
43
44 $this->ids['Group'] = [];
45 $this->ids['Group']['parent'] = $this->callAPISuccess('Group', 'create', [
46 'name' => 'Administrators',
47 'title' => 'Administrators',
48 ])['id'];
49 $this->ids['Group']['child'] = $this->callAPISuccess('Group', 'create', [
50 'name' => 'Newsletter Subscribers',
51 'title' => 'Newsletter Subscribers',
52 'parents' => $this->ids['Group']['parent'],
53 ])['id'];
54 $this->ids['Group']['child2'] = $this->callAPISuccess('Group', 'create', [
55 'name' => 'Another Newsletter Subscribers',
56 'title' => 'Another Newsletter Subscribers',
57 'parents' => $this->ids['Group']['parent'],
58 ])['id'];
59 $this->ids['Group']['child3'] = $this->callAPISuccess('Group', 'create', [
60 'name' => 'Super Special Newsletter Subscribers',
61 'title' => 'Super Special Newsletter Subscribers',
62 'parents' => [$this->ids['Group']['parent'], $this->ids['Group']['child']],
63 ])['id'];
64
65 }
66
67 /**
68 * Tears down the fixture.
69 *
70 * This method is called after a test is executed.
71 *
72 * @throws \Exception
73 */
74 protected function tearDown() {
75 $this->quickCleanup(
76 [
77 'civicrm_group',
78 'civicrm_group_nesting',
79 'civicrm_contact',
80 'civicrm_uf_group',
81 'civicrm_uf_join',
82 'civicrm_uf_match',
83 ]
84 );
85 parent::tearDown();
86 }
87
88 /**
89 * Test civicrm_group_nesting_get.
90 *
91 * @dataProvider versionThreeAndFour
92 */
93 public function testGet() {
94 $params = [
95 'parent_group_id' => $this->ids['Group']['parent'],
96 'child_group_id' => $this->ids['Group']['child'],
97 ];
98
99 $result = $this->callAPIAndDocument('group_nesting', 'get', $params, __FUNCTION__, __FILE__);
100 $expected = [
101 1 => [
102 'id' => 1,
103 'child_group_id' => $this->ids['Group']['child'],
104 'parent_group_id' => $this->ids['Group']['parent'],
105 ],
106 ];
107
108 $this->assertEquals($expected, $result['values']);
109 }
110
111 /**
112 * Test civicrm_group_nesting_get with just one param (child_group_id).
113 *
114 * @dataProvider versionThreeAndFour
115 */
116 public function testGetWithChildGroupId() {
117 $params = [
118 'child_group_id' => $this->ids['Group']['child3'],
119 ];
120
121 $result = $this->callAPISuccess('group_nesting', 'get', $params);
122
123 // expected data loaded in setUp
124 $expected = [
125 3 => [
126 'id' => 3,
127 'child_group_id' => $this->ids['Group']['child3'],
128 'parent_group_id' => $this->ids['Group']['parent'],
129 ],
130 4 => [
131 'id' => 4,
132 'child_group_id' => $this->ids['Group']['child3'],
133 'parent_group_id' => $this->ids['Group']['child'],
134 ],
135 ];
136
137 $this->assertEquals($expected, $result['values']);
138 }
139
140 /**
141 * Test civicrm_group_nesting_get with just one param (parent_group_id).
142 *
143 * @dataProvider versionThreeAndFour
144 */
145 public function testGetWithParentGroupId() {
146 $params = [
147 'parent_group_id' => $this->ids['Group']['parent'],
148 ];
149
150 $result = $this->callAPISuccess('group_nesting', 'get', $params);
151
152 // expected data loaded in setUp
153 $expected = [
154 1 => [
155 'id' => 1,
156 'child_group_id' => $this->ids['Group']['child'],
157 'parent_group_id' => $this->ids['Group']['parent'],
158 ],
159 2 => [
160 'id' => 2,
161 'child_group_id' => $this->ids['Group']['child2'],
162 'parent_group_id' => $this->ids['Group']['parent'],
163 ],
164 3 => [
165 'id' => 3,
166 'child_group_id' => $this->ids['Group']['child3'],
167 'parent_group_id' => $this->ids['Group']['parent'],
168 ],
169 ];
170
171 $this->assertEquals($expected, $result['values']);
172 }
173
174 /**
175 * Test civicrm_group_nesting_get for no records results.
176 *
177 * Success expected. (these tests are of marginal value as are in syntax conformance,
178 * don't copy & paste.
179 *
180 * @dataProvider versionThreeAndFour
181 */
182 public function testGetEmptyResults() {
183 $params = [
184 'parent_group_id' => $this->ids['Group']['parent'],
185 'child_group_id' => 700,
186 ];
187 $this->callAPISuccess('group_nesting', 'get', $params);
188 }
189
190 /**
191 * Test civicrm_group_nesting_create.
192 *
193 * @throws \Exception
194 *
195 * @dataProvider versionThreeAndFour
196 */
197 public function testCreate() {
198 $params = [
199 'parent_group_id' => $this->ids['Group']['parent'],
200 'child_group_id' => $this->ids['Group']['child2'],
201 ];
202
203 $this->callAPIAndDocument('group_nesting', 'create', $params, __FUNCTION__, __FILE__);
204 $this->callAPISuccessGetCount('GroupNesting', $params, 1);
205 }
206
207 /**
208 * Test civicrm_group_nesting_remove.
209 *
210 * @dataProvider versionThreeAndFour
211 */
212 public function testDelete() {
213 $params = [
214 'parent_group_id' => $this->ids['Group']['parent'],
215 'child_group_id' => $this->ids['Group']['child'],
216 ];
217
218 $result = $this->callAPISuccess('group_nesting', 'get', $params);
219 $params = ['id' => $result['id']];
220 $this->callAPIAndDocument('group_nesting', 'delete', $params, __FUNCTION__, __FILE__);
221 $this->assertEquals(0, $this->callAPISuccess('group_nesting', 'getcount', $params));
222 }
223
224 /**
225 * Test civicrm_group_nesting_remove with empty parameter array.
226 *
227 * Error expected.
228 *
229 * @dataProvider versionThreeAndFour
230 */
231 public function testDeleteWithEmptyParams() {
232 $this->callAPIFailure('group_nesting', 'delete', []);
233 }
234
235 }