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