Merge pull request #16469 from civicrm/5.22
[civicrm-core.git] / tests / phpunit / api / v3 / GroupNestingTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Test class for GroupNesting API - civicrm_group_nesting_*
14 *
15 * @package CiviCRM
16 * @group headless
17 */
18 class api_v3_GroupNestingTest extends CiviUnitTestCase {
19
20 /**
21 * Sets up the fixture, for example, opens a network connection.
22 *
23 * This method is called before a test is executed.
24 */
25 protected function setUp() {
26 parent::setUp();
27
28 $this->ids['Group'] = [];
29 $this->ids['Group']['parent'] = $this->callAPISuccess('Group', 'create', [
30 'name' => 'Administrators',
31 'title' => 'Administrators',
32 ])['id'];
33 $this->ids['Group']['child'] = $this->callAPISuccess('Group', 'create', [
34 'name' => 'Newsletter Subscribers',
35 'title' => 'Newsletter Subscribers',
36 'parents' => $this->ids['Group']['parent'],
37 ])['id'];
38 $this->ids['Group']['child2'] = $this->callAPISuccess('Group', 'create', [
39 'name' => 'Another Newsletter Subscribers',
40 'title' => 'Another Newsletter Subscribers',
41 'parents' => $this->ids['Group']['parent'],
42 ])['id'];
43 $this->ids['Group']['child3'] = $this->callAPISuccess('Group', 'create', [
44 'name' => 'Super Special Newsletter Subscribers',
45 'title' => 'Super Special Newsletter Subscribers',
46 'parents' => [$this->ids['Group']['parent'], $this->ids['Group']['child']],
47 ])['id'];
48
49 }
50
51 /**
52 * Tears down the fixture.
53 *
54 * This method is called after a test is executed.
55 *
56 * @throws \Exception
57 */
58 protected function tearDown() {
59 $this->quickCleanup(
60 [
61 'civicrm_group',
62 'civicrm_group_nesting',
63 'civicrm_contact',
64 'civicrm_uf_group',
65 'civicrm_uf_join',
66 'civicrm_uf_match',
67 ]
68 );
69 parent::tearDown();
70 }
71
72 /**
73 * Test civicrm_group_nesting_get.
74 *
75 * @dataProvider versionThreeAndFour
76 */
77 public function testGet() {
78 $params = [
79 'parent_group_id' => $this->ids['Group']['parent'],
80 'child_group_id' => $this->ids['Group']['child'],
81 ];
82
83 $result = $this->callAPIAndDocument('group_nesting', 'get', $params, __FUNCTION__, __FILE__);
84 $expected = [
85 1 => [
86 'id' => 1,
87 'child_group_id' => $this->ids['Group']['child'],
88 'parent_group_id' => $this->ids['Group']['parent'],
89 ],
90 ];
91
92 $this->assertEquals($expected, $result['values']);
93 }
94
95 /**
96 * Test civicrm_group_nesting_get with just one param (child_group_id).
97 *
98 * @dataProvider versionThreeAndFour
99 */
100 public function testGetWithChildGroupId() {
101 $params = [
102 'child_group_id' => $this->ids['Group']['child3'],
103 ];
104
105 $result = $this->callAPISuccess('group_nesting', 'get', $params);
106
107 // expected data loaded in setUp
108 $expected = [
109 3 => [
110 'id' => 3,
111 'child_group_id' => $this->ids['Group']['child3'],
112 'parent_group_id' => $this->ids['Group']['parent'],
113 ],
114 4 => [
115 'id' => 4,
116 'child_group_id' => $this->ids['Group']['child3'],
117 'parent_group_id' => $this->ids['Group']['child'],
118 ],
119 ];
120
121 $this->assertEquals($expected, $result['values']);
122 }
123
124 /**
125 * Test civicrm_group_nesting_get with just one param (parent_group_id).
126 *
127 * @dataProvider versionThreeAndFour
128 */
129 public function testGetWithParentGroupId() {
130 $params = [
131 'parent_group_id' => $this->ids['Group']['parent'],
132 ];
133
134 $result = $this->callAPISuccess('group_nesting', 'get', $params);
135
136 // expected data loaded in setUp
137 $expected = [
138 1 => [
139 'id' => 1,
140 'child_group_id' => $this->ids['Group']['child'],
141 'parent_group_id' => $this->ids['Group']['parent'],
142 ],
143 2 => [
144 'id' => 2,
145 'child_group_id' => $this->ids['Group']['child2'],
146 'parent_group_id' => $this->ids['Group']['parent'],
147 ],
148 3 => [
149 'id' => 3,
150 'child_group_id' => $this->ids['Group']['child3'],
151 'parent_group_id' => $this->ids['Group']['parent'],
152 ],
153 ];
154
155 $this->assertEquals($expected, $result['values']);
156 }
157
158 /**
159 * Test civicrm_group_nesting_get for no records results.
160 *
161 * Success expected. (these tests are of marginal value as are in syntax conformance,
162 * don't copy & paste.
163 *
164 * @dataProvider versionThreeAndFour
165 */
166 public function testGetEmptyResults() {
167 $params = [
168 'parent_group_id' => $this->ids['Group']['parent'],
169 'child_group_id' => 700,
170 ];
171 $this->callAPISuccess('group_nesting', 'get', $params);
172 }
173
174 /**
175 * Test civicrm_group_nesting_create.
176 *
177 * @throws \Exception
178 *
179 * @dataProvider versionThreeAndFour
180 */
181 public function testCreate() {
182 $params = [
183 'parent_group_id' => $this->ids['Group']['parent'],
184 'child_group_id' => $this->ids['Group']['child2'],
185 ];
186
187 $this->callAPIAndDocument('group_nesting', 'create', $params, __FUNCTION__, __FILE__);
188 $this->callAPISuccessGetCount('GroupNesting', $params, 1);
189 }
190
191 /**
192 * Test civicrm_group_nesting_remove.
193 *
194 * @dataProvider versionThreeAndFour
195 */
196 public function testDelete() {
197 $params = [
198 'parent_group_id' => $this->ids['Group']['parent'],
199 'child_group_id' => $this->ids['Group']['child'],
200 ];
201
202 $result = $this->callAPISuccess('group_nesting', 'get', $params);
203 $params = ['id' => $result['id']];
204 $this->callAPIAndDocument('group_nesting', 'delete', $params, __FUNCTION__, __FILE__);
205 $this->assertEquals(0, $this->callAPISuccess('group_nesting', 'getcount', $params));
206 }
207
208 /**
209 * Test civicrm_group_nesting_remove with empty parameter array.
210 *
211 * Error expected.
212 *
213 * @dataProvider versionThreeAndFour
214 */
215 public function testDeleteWithEmptyParams() {
216 $this->callAPIFailure('group_nesting', 'delete', []);
217 }
218
219 }