Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
7d61e75f | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
7d61e75f TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 | 11 | |
6a488035 TO |
12 | /** |
13 | * Test class for GroupNesting API - civicrm_group_nesting_* | |
14 | * | |
6c6e6187 | 15 | * @package CiviCRM |
acb109b7 | 16 | * @group headless |
6a488035 TO |
17 | */ |
18 | class api_v3_GroupNestingTest extends CiviUnitTestCase { | |
6a488035 TO |
19 | |
20 | /** | |
21 | * Sets up the fixture, for example, opens a network connection. | |
f9a5aadd | 22 | * |
6a488035 | 23 | * This method is called before a test is executed. |
6a488035 TO |
24 | */ |
25 | protected function setUp() { | |
6a488035 TO |
26 | parent::setUp(); |
27 | ||
f9a5aadd | 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']; | |
6a488035 | 48 | |
6a488035 TO |
49 | } |
50 | ||
51 | /** | |
f9a5aadd | 52 | * Tears down the fixture. |
53 | * | |
6a488035 | 54 | * This method is called after a test is executed. |
f9a5aadd | 55 | * |
56 | * @throws \Exception | |
6a488035 TO |
57 | */ |
58 | protected function tearDown() { | |
6a488035 | 59 | $this->quickCleanup( |
f9a5aadd | 60 | [ |
6a488035 TO |
61 | 'civicrm_group', |
62 | 'civicrm_group_nesting', | |
63 | 'civicrm_contact', | |
64 | 'civicrm_uf_group', | |
65 | 'civicrm_uf_join', | |
66 | 'civicrm_uf_match', | |
f9a5aadd | 67 | ] |
6a488035 | 68 | ); |
fda18dc3 | 69 | parent::tearDown(); |
6a488035 TO |
70 | } |
71 | ||
6a488035 TO |
72 | /** |
73 | * Test civicrm_group_nesting_get. | |
0158c2b6 SL |
74 | * |
75 | * @dataProvider versionThreeAndFour | |
6a488035 TO |
76 | */ |
77 | public function testGet() { | |
f9a5aadd | 78 | $params = [ |
79 | 'parent_group_id' => $this->ids['Group']['parent'], | |
80 | 'child_group_id' => $this->ids['Group']['child'], | |
81 | ]; | |
6a488035 | 82 | |
f3e19c4e | 83 | $result = $this->callAPIAndDocument('group_nesting', 'get', $params, __FUNCTION__, __FILE__); |
f9a5aadd | 84 | $expected = [ |
85 | 1 => [ | |
92915c55 | 86 | 'id' => 1, |
f9a5aadd | 87 | 'child_group_id' => $this->ids['Group']['child'], |
88 | 'parent_group_id' => $this->ids['Group']['parent'], | |
89 | ], | |
90 | ]; | |
6a488035 TO |
91 | |
92 | $this->assertEquals($expected, $result['values']); | |
93 | } | |
94 | ||
95 | /** | |
f9a5aadd | 96 | * Test civicrm_group_nesting_get with just one param (child_group_id). |
0158c2b6 SL |
97 | * |
98 | * @dataProvider versionThreeAndFour | |
6a488035 TO |
99 | */ |
100 | public function testGetWithChildGroupId() { | |
f9a5aadd | 101 | $params = [ |
102 | 'child_group_id' => $this->ids['Group']['child3'], | |
103 | ]; | |
6a488035 | 104 | |
f3e19c4e | 105 | $result = $this->callAPISuccess('group_nesting', 'get', $params); |
6a488035 TO |
106 | |
107 | // expected data loaded in setUp | |
f9a5aadd | 108 | $expected = [ |
109 | 3 => [ | |
92915c55 | 110 | 'id' => 3, |
f9a5aadd | 111 | 'child_group_id' => $this->ids['Group']['child3'], |
112 | 'parent_group_id' => $this->ids['Group']['parent'], | |
113 | ], | |
114 | 4 => [ | |
6a488035 | 115 | 'id' => 4, |
f9a5aadd | 116 | 'child_group_id' => $this->ids['Group']['child3'], |
117 | 'parent_group_id' => $this->ids['Group']['child'], | |
118 | ], | |
119 | ]; | |
6a488035 TO |
120 | |
121 | $this->assertEquals($expected, $result['values']); | |
122 | } | |
123 | ||
124 | /** | |
f9a5aadd | 125 | * Test civicrm_group_nesting_get with just one param (parent_group_id). |
0158c2b6 SL |
126 | * |
127 | * @dataProvider versionThreeAndFour | |
6a488035 TO |
128 | */ |
129 | public function testGetWithParentGroupId() { | |
f9a5aadd | 130 | $params = [ |
131 | 'parent_group_id' => $this->ids['Group']['parent'], | |
132 | ]; | |
6a488035 | 133 | |
f3e19c4e | 134 | $result = $this->callAPISuccess('group_nesting', 'get', $params); |
6a488035 TO |
135 | |
136 | // expected data loaded in setUp | |
f9a5aadd | 137 | $expected = [ |
138 | 1 => [ | |
92915c55 | 139 | 'id' => 1, |
f9a5aadd | 140 | 'child_group_id' => $this->ids['Group']['child'], |
141 | 'parent_group_id' => $this->ids['Group']['parent'], | |
142 | ], | |
143 | 2 => [ | |
6a488035 | 144 | 'id' => 2, |
f9a5aadd | 145 | 'child_group_id' => $this->ids['Group']['child2'], |
146 | 'parent_group_id' => $this->ids['Group']['parent'], | |
147 | ], | |
148 | 3 => [ | |
6a488035 | 149 | 'id' => 3, |
f9a5aadd | 150 | 'child_group_id' => $this->ids['Group']['child3'], |
151 | 'parent_group_id' => $this->ids['Group']['parent'], | |
152 | ], | |
153 | ]; | |
6a488035 TO |
154 | |
155 | $this->assertEquals($expected, $result['values']); | |
156 | } | |
157 | ||
158 | /** | |
159 | * Test civicrm_group_nesting_get for no records results. | |
f9a5aadd | 160 | * |
f3e19c4e | 161 | * Success expected. (these tests are of marginal value as are in syntax conformance, |
0158c2b6 SL |
162 | * don't copy & paste. |
163 | * | |
164 | * @dataProvider versionThreeAndFour | |
6a488035 TO |
165 | */ |
166 | public function testGetEmptyResults() { | |
f9a5aadd | 167 | $params = [ |
168 | 'parent_group_id' => $this->ids['Group']['parent'], | |
6a488035 | 169 | 'child_group_id' => 700, |
f9a5aadd | 170 | ]; |
4b31f991 | 171 | $this->callAPISuccess('group_nesting', 'get', $params); |
6a488035 TO |
172 | } |
173 | ||
6a488035 TO |
174 | /** |
175 | * Test civicrm_group_nesting_create. | |
f9a5aadd | 176 | * |
177 | * @throws \Exception | |
0158c2b6 SL |
178 | * |
179 | * @dataProvider versionThreeAndFour | |
6a488035 TO |
180 | */ |
181 | public function testCreate() { | |
f9a5aadd | 182 | $params = [ |
183 | 'parent_group_id' => $this->ids['Group']['parent'], | |
184 | 'child_group_id' => $this->ids['Group']['child2'], | |
185 | ]; | |
6a488035 | 186 | |
4b31f991 | 187 | $this->callAPIAndDocument('group_nesting', 'create', $params, __FUNCTION__, __FILE__); |
188 | $this->callAPISuccessGetCount('GroupNesting', $params, 1); | |
6a488035 TO |
189 | } |
190 | ||
6a488035 TO |
191 | /** |
192 | * Test civicrm_group_nesting_remove. | |
0158c2b6 SL |
193 | * |
194 | * @dataProvider versionThreeAndFour | |
6a488035 TO |
195 | */ |
196 | public function testDelete() { | |
f9a5aadd | 197 | $params = [ |
198 | 'parent_group_id' => $this->ids['Group']['parent'], | |
199 | 'child_group_id' => $this->ids['Group']['child'], | |
200 | ]; | |
6a488035 | 201 | |
f9a5aadd | 202 | $result = $this->callAPISuccess('group_nesting', 'get', $params); |
203 | $params = ['id' => $result['id']]; | |
4b31f991 | 204 | $this->callAPIAndDocument('group_nesting', 'delete', $params, __FUNCTION__, __FILE__); |
f9a5aadd | 205 | $this->assertEquals(0, $this->callAPISuccess('group_nesting', 'getcount', $params)); |
6a488035 TO |
206 | } |
207 | ||
208 | /** | |
209 | * Test civicrm_group_nesting_remove with empty parameter array. | |
4b31f991 | 210 | * |
6a488035 | 211 | * Error expected. |
0158c2b6 SL |
212 | * |
213 | * @dataProvider versionThreeAndFour | |
6a488035 TO |
214 | */ |
215 | public function testDeleteWithEmptyParams() { | |
f9a5aadd | 216 | $this->callAPIFailure('group_nesting', 'delete', []); |
6a488035 | 217 | } |
96025800 | 218 | |
6a488035 | 219 | } |