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 | |
4cbe18b8 EM |
12 | /** |
13 | * Class api_v3_ContactTypeTest | |
acb109b7 | 14 | * @group headless |
4cbe18b8 | 15 | */ |
6a488035 TO |
16 | class api_v3_ContactTypeTest extends CiviUnitTestCase { |
17 | protected $_apiversion; | |
b7c9bc4c | 18 | |
00be9182 | 19 | public function setUp() { |
6a488035 | 20 | parent::setUp(); |
77a09a77 | 21 | $this->useTransaction(TRUE); |
9099cab3 | 22 | $params = [ |
6a488035 TO |
23 | 'label' => 'sub_individual', |
24 | 'name' => 'sub_individual', | |
25 | // Individual | |
26 | 'parent_id' => 1, | |
27 | 'is_active' => 1, | |
9099cab3 | 28 | ]; |
6a488035 TO |
29 | $result = CRM_Contact_BAO_ContactType::add($params); |
30 | $this->subTypeIndividual = $params['name']; | |
6a488035 | 31 | |
9099cab3 | 32 | $params = [ |
6a488035 TO |
33 | 'label' => 'sub_organization', |
34 | 'name' => 'sub_organization', | |
35 | // Organization | |
36 | 'parent_id' => 3, | |
37 | 'is_active' => 1, | |
9099cab3 | 38 | ]; |
6a488035 TO |
39 | $result = CRM_Contact_BAO_ContactType::add($params); |
40 | $this->subTypeOrganization = $params['name']; | |
6a488035 | 41 | |
9099cab3 | 42 | $params = [ |
6a488035 TO |
43 | 'label' => 'sub_household', |
44 | 'name' => 'sub_household', | |
45 | // Household | |
46 | 'parent_id' => 2, | |
47 | 'is_active' => 1, | |
9099cab3 | 48 | ]; |
6a488035 TO |
49 | $result = CRM_Contact_BAO_ContactType::add($params); |
50 | $this->subTypeHousehold = $params['name']; | |
6a488035 TO |
51 | } |
52 | ||
c490a46a | 53 | /** |
eceb18cc | 54 | * Test add methods with valid data. |
c490a46a | 55 | * success expected |
2d932085 CW |
56 | * @param int $version |
57 | * @dataProvider versionThreeAndFour | |
c490a46a | 58 | */ |
2d932085 CW |
59 | public function testContactCreate($version) { |
60 | $this->_apiversion = $version; | |
6a488035 TO |
61 | |
62 | // check for Type:Individual Subtype:sub_individual | |
9099cab3 | 63 | $contactParams = [ |
6a488035 TO |
64 | 'first_name' => 'Anne', |
65 | 'last_name' => 'Grant', | |
66 | 'contact_type' => 'Individual', | |
67 | 'contact_sub_type' => $this->subTypeIndividual, | |
9099cab3 | 68 | ]; |
fc928539 | 69 | $contact = $this->callAPISuccess('contact', 'create', $contactParams); |
9099cab3 | 70 | $params = [ |
6a488035 | 71 | 'contact_id' => $contact['id'], |
9099cab3 | 72 | ]; |
fc928539 | 73 | $result = $this->callAPISuccess('contact', 'get', $params); |
6a488035 TO |
74 | $this->assertEquals($result['values'][$contact['id']]['first_name'], $contactParams['first_name'], "In line " . __LINE__); |
75 | $this->assertEquals($result['values'][$contact['id']]['last_name'], $contactParams['last_name'], "In line " . __LINE__); | |
76 | $this->assertEquals($result['values'][$contact['id']]['contact_type'], $contactParams['contact_type'], "In line " . __LINE__); | |
77 | $this->assertEquals(end($result['values'][$contact['id']]['contact_sub_type']), $contactParams['contact_sub_type'], "In line " . __LINE__); | |
fc928539 | 78 | $this->callAPISuccess('contact', 'delete', $params); |
6a488035 TO |
79 | |
80 | // check for Type:Organization Subtype:sub_organization | |
9099cab3 | 81 | $contactParams = [ |
6a488035 TO |
82 | 'organization_name' => 'Compumentor', |
83 | 'contact_type' => 'Organization', | |
84 | 'contact_sub_type' => $this->subTypeOrganization, | |
9099cab3 | 85 | ]; |
fc928539 | 86 | $contact = $this->callAPISuccess('contact', 'create', $contactParams); |
6a488035 | 87 | |
9099cab3 | 88 | $params = [ |
6a488035 | 89 | 'contact_id' => $contact['id'], |
9099cab3 | 90 | ]; |
fc928539 | 91 | $getContacts = $this->callAPISuccess('contact', 'get', $params); |
6a488035 TO |
92 | $result = $getContacts['values'][$contact['id']]; |
93 | $this->assertEquals($result['organization_name'], $contactParams['organization_name'], "In line " . __LINE__); | |
94 | $this->assertEquals($result['contact_type'], $contactParams['contact_type'], "In line " . __LINE__); | |
95 | $this->assertEquals(end($result['contact_sub_type']), $contactParams['contact_sub_type'], "In line " . __LINE__); | |
fc928539 | 96 | $this->callAPISuccess('contact', 'delete', $params); |
6a488035 TO |
97 | } |
98 | ||
c490a46a | 99 | /** |
eceb18cc | 100 | * Test add with invalid data. |
2d932085 CW |
101 | * @param int $version |
102 | * @dataProvider versionThreeAndFour | |
c490a46a | 103 | */ |
2d932085 CW |
104 | public function testContactAddInvalidData($version) { |
105 | $this->_apiversion = $version; | |
6a488035 TO |
106 | |
107 | // check for Type:Individual Subtype:sub_household | |
9099cab3 | 108 | $contactParams = [ |
6a488035 TO |
109 | 'first_name' => 'Anne', |
110 | 'last_name' => 'Grant', | |
111 | 'contact_type' => 'Individual', | |
112 | 'contact_sub_type' => $this->subTypeHousehold, | |
9099cab3 | 113 | ]; |
d0e1eff2 | 114 | $contact = $this->callAPIFailure('contact', 'create', $contactParams); |
6a488035 TO |
115 | |
116 | // check for Type:Organization Subtype:sub_individual | |
9099cab3 | 117 | $contactParams = [ |
6a488035 TO |
118 | 'organization_name' => 'Compumentor', |
119 | 'contact_type' => 'Organization', | |
120 | 'contact_sub_type' => $this->subTypeIndividual, | |
9099cab3 | 121 | ]; |
d0e1eff2 | 122 | $contact = $this->callAPIFailure('contact', 'create', $contactParams); |
6a488035 TO |
123 | } |
124 | ||
c490a46a | 125 | /** |
eceb18cc | 126 | * Test update with no subtype to valid subtype. |
c490a46a | 127 | * success expected |
2d932085 CW |
128 | * @param int $version |
129 | * @dataProvider versionThreeAndFour | |
c490a46a | 130 | */ |
2d932085 CW |
131 | public function testContactUpdateNoSubtypeValid($version) { |
132 | $this->_apiversion = $version; | |
6a488035 TO |
133 | |
134 | // check for Type:Individual | |
9099cab3 | 135 | $contactParams = [ |
6a488035 TO |
136 | 'first_name' => 'Anne', |
137 | 'last_name' => 'Grant', | |
138 | 'contact_type' => 'Individual', | |
9099cab3 | 139 | ]; |
fc928539 | 140 | $contact = $this->callAPISuccess('contact', 'create', $contactParams); |
6a488035 | 141 | // subype:sub_individual |
9099cab3 | 142 | $updateParams = [ |
6a488035 TO |
143 | 'first_name' => 'John', |
144 | 'last_name' => 'Grant', | |
145 | 'contact_id' => $contact['id'], | |
146 | 'contact_type' => 'Individual', | |
147 | 'contact_sub_type' => $this->subTypeIndividual, | |
9099cab3 | 148 | ]; |
fc928539 | 149 | $updateContact = $this->callAPISuccess('contact', 'create', $updateParams); |
6a488035 TO |
150 | $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__); |
151 | ||
9099cab3 | 152 | $params = [ |
6a488035 | 153 | 'contact_id' => $contact['id'], |
9099cab3 | 154 | ]; |
fc928539 | 155 | $getContacts = $this->callAPISuccess('contact', 'get', $params); |
6a488035 TO |
156 | $result = $getContacts['values'][$contact['id']]; |
157 | ||
158 | $this->assertEquals($result['first_name'], $updateParams['first_name'], "In line " . __LINE__); | |
159 | $this->assertEquals($result['last_name'], $updateParams['last_name'], "In line " . __LINE__); | |
160 | $this->assertEquals($result['contact_type'], $updateParams['contact_type'], "In line " . __LINE__); | |
161 | $this->assertEquals(end($result['contact_sub_type']), $updateParams['contact_sub_type'], "In line " . __LINE__); | |
fc928539 | 162 | $this->callAPISuccess('contact', 'delete', $params); |
6a488035 TO |
163 | |
164 | // check for Type:Organization | |
9099cab3 | 165 | $contactParams = [ |
6a488035 TO |
166 | 'organization_name' => 'Compumentor', |
167 | 'contact_type' => 'Organization', | |
9099cab3 | 168 | ]; |
fc928539 | 169 | $contact = $this->callAPISuccess('contact', 'create', $contactParams); |
6a488035 TO |
170 | |
171 | // subype:sub_organization | |
9099cab3 | 172 | $updateParams = [ |
6a488035 TO |
173 | 'organization_name' => 'Intel Arts', |
174 | 'contact_id' => $contact['id'], | |
175 | 'contact_type' => 'Organization', | |
176 | 'contact_sub_type' => $this->subTypeOrganization, | |
9099cab3 | 177 | ]; |
fc928539 | 178 | $updateContact = $this->callAPISuccess('contact', 'create', $updateParams); |
6a488035 TO |
179 | $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__); |
180 | ||
9099cab3 | 181 | $params = [ |
6a488035 | 182 | 'contact_id' => $contact['id'], |
9099cab3 | 183 | ]; |
fc928539 | 184 | $getContacts = $this->callAPISuccess('contact', 'get', $params); |
6a488035 TO |
185 | $result = $getContacts['values'][$contact['id']]; |
186 | ||
187 | $this->assertEquals($result['organization_name'], $updateParams['organization_name'], "In line " . __LINE__); | |
188 | $this->assertEquals($result['contact_type'], $updateParams['contact_type'], "In line " . __LINE__); | |
189 | $this->assertEquals(end($result['contact_sub_type']), $updateParams['contact_sub_type'], "In line " . __LINE__); | |
fc928539 | 190 | $this->callAPISuccess('contact', 'delete', $params); |
6a488035 TO |
191 | } |
192 | ||
c490a46a | 193 | /** |
eceb18cc | 194 | * Test update with no subtype to invalid subtype. |
2d932085 CW |
195 | * @param int $version |
196 | * @dataProvider versionThreeAndFour | |
c490a46a | 197 | */ |
2d932085 CW |
198 | public function testContactUpdateNoSubtypeInvalid($version) { |
199 | $this->_apiversion = $version; | |
6a488035 TO |
200 | |
201 | // check for Type:Individual | |
9099cab3 | 202 | $contactParams = [ |
6a488035 TO |
203 | 'first_name' => 'Anne', |
204 | 'last_name' => 'Grant', | |
205 | 'contact_type' => 'Individual', | |
9099cab3 | 206 | ]; |
fc928539 | 207 | $contact = $this->callAPISuccess('contact', 'create', $contactParams); |
6a488035 TO |
208 | |
209 | // subype:sub_household | |
9099cab3 | 210 | $updateParams = [ |
6a488035 TO |
211 | 'first_name' => 'John', |
212 | 'last_name' => 'Grant', | |
213 | 'contact_id' => $contact['id'], | |
214 | 'contact_type' => 'Individual', | |
215 | 'contact_sub_type' => $this->subTypeHousehold, | |
9099cab3 | 216 | ]; |
d0e1eff2 | 217 | $updateContact = $this->callAPIFailure('contact', 'create', $updateParams); |
9099cab3 | 218 | $params = [ |
6a488035 | 219 | 'contact_id' => $contact['id'], |
9099cab3 | 220 | ]; |
fc928539 | 221 | $this->callAPISuccess('contact', 'delete', $params); |
6a488035 TO |
222 | |
223 | // check for Type:Organization | |
9099cab3 | 224 | $contactParams = [ |
6a488035 TO |
225 | 'organization_name' => 'Compumentor', |
226 | 'contact_type' => 'Organization', | |
9099cab3 | 227 | ]; |
fc928539 | 228 | $contact = $this->callAPISuccess('contact', 'create', $contactParams); |
6a488035 | 229 | |
9099cab3 | 230 | $updateParams = [ |
6a488035 TO |
231 | 'organization_name' => 'Intel Arts', |
232 | 'contact_id' => $contact['id'], | |
233 | 'contact_type' => 'Organization', | |
234 | 'contact_sub_type' => $this->subTypeIndividual, | |
9099cab3 | 235 | ]; |
d0e1eff2 | 236 | $updateContact = $this->callAPIFailure('contact', 'create', $updateParams); |
9099cab3 | 237 | $params = [ |
6a488035 | 238 | 'contact_id' => $contact['id'], |
9099cab3 | 239 | ]; |
fc928539 | 240 | $this->callAPISuccess('contact', 'delete', $params); |
6a488035 TO |
241 | } |
242 | ||
c490a46a | 243 | /** |
eceb18cc | 244 | * Test update with no subtype to valid subtype. |
c490a46a | 245 | * success expected |
2d932085 CW |
246 | * @param int $version |
247 | * @dataProvider versionThreeAndFour | |
c490a46a | 248 | */ |
2d932085 CW |
249 | public function testContactUpdateSubtypeValid($version) { |
250 | $this->_apiversion = $version; | |
6a488035 | 251 | |
9099cab3 | 252 | $params = [ |
6a488035 TO |
253 | 'label' => 'sub2_individual', |
254 | 'name' => 'sub2_individual', | |
255 | // Individual | |
256 | 'parent_id' => 1, | |
257 | 'is_active' => 1, | |
9099cab3 | 258 | ]; |
6a488035 TO |
259 | $getSubtype = CRM_Contact_BAO_ContactType::add($params); |
260 | $subtype = $params['name']; | |
261 | ||
262 | // check for Type:Individual subype:sub_individual | |
9099cab3 | 263 | $contactParams = [ |
6a488035 TO |
264 | 'first_name' => 'Anne', |
265 | 'last_name' => 'Grant', | |
266 | 'contact_type' => 'Individual', | |
267 | 'contact_sub_type' => $this->subTypeIndividual, | |
9099cab3 | 268 | ]; |
fc928539 | 269 | $contact = $this->callAPISuccess('contact', 'create', $contactParams); |
6a488035 | 270 | // subype:sub2_individual |
9099cab3 | 271 | $updateParams = [ |
6a488035 TO |
272 | 'id' => $contact['id'], |
273 | 'first_name' => 'John', | |
274 | 'last_name' => 'Grant', | |
275 | 'contact_id' => $contact['id'], | |
276 | 'contact_type' => 'Individual', | |
277 | 'contact_sub_type' => $subtype, | |
9099cab3 | 278 | ]; |
fc928539 | 279 | $updateContact = $this->callAPISuccess('contact', 'create', $updateParams); |
6a488035 | 280 | |
6a488035 TO |
281 | $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__); |
282 | ||
9099cab3 | 283 | $params = [ |
6a488035 | 284 | 'contact_id' => $contact['id'], |
9099cab3 | 285 | ]; |
fc928539 | 286 | $getContacts = $this->callAPISuccess('contact', 'get', $params); |
6a488035 TO |
287 | $result = $getContacts['values'][$contact['id']]; |
288 | ||
289 | $this->assertEquals($result['first_name'], $updateParams['first_name'], "In line " . __LINE__); | |
290 | $this->assertEquals($result['last_name'], $updateParams['last_name'], "In line " . __LINE__); | |
291 | $this->assertEquals($result['contact_type'], $updateParams['contact_type'], "In line " . __LINE__); | |
292 | $this->assertEquals(end($result['contact_sub_type']), $updateParams['contact_sub_type'], "In line " . __LINE__); | |
fc928539 | 293 | $this->callAPISuccess('contact', 'delete', $params); |
6a488035 | 294 | |
9099cab3 | 295 | $params = [ |
6a488035 TO |
296 | 'label' => 'sub2_organization', |
297 | 'name' => 'sub2_organization', | |
298 | // Organization | |
299 | 'parent_id' => 3, | |
300 | 'is_active' => 1, | |
9099cab3 | 301 | ]; |
6a488035 TO |
302 | $getSubtype = CRM_Contact_BAO_ContactType::add($params); |
303 | $subtype = $params['name']; | |
304 | ||
305 | // check for Type:Organization subype:sub_organization | |
9099cab3 | 306 | $contactParams = [ |
6a488035 TO |
307 | 'organization_name' => 'Compumentor', |
308 | 'contact_type' => 'Organization', | |
309 | 'contact_sub_type' => $this->subTypeOrganization, | |
9099cab3 | 310 | ]; |
fc928539 | 311 | $contact = $this->callAPISuccess('contact', 'create', $contactParams); |
6a488035 TO |
312 | |
313 | // subype:sub2_organization | |
9099cab3 | 314 | $updateParams = [ |
6a488035 TO |
315 | 'organization_name' => 'Intel Arts', |
316 | 'contact_id' => $contact['id'], | |
317 | 'contact_type' => 'Organization', | |
318 | 'contact_sub_type' => $subtype, | |
9099cab3 | 319 | ]; |
fc928539 | 320 | $updateContact = $this->callAPISuccess('contact', 'create', $updateParams); |
6a488035 TO |
321 | $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__); |
322 | ||
9099cab3 | 323 | $params = [ |
6a488035 | 324 | 'contact_id' => $contact['id'], |
9099cab3 | 325 | ]; |
fc928539 | 326 | $getContacts = $this->callAPISuccess('contact', 'get', $params); |
6a488035 TO |
327 | $result = $getContacts['values'][$contact['id']]; |
328 | ||
329 | $this->assertEquals($result['organization_name'], $updateParams['organization_name'], "In line " . __LINE__); | |
330 | $this->assertEquals($result['contact_type'], $updateParams['contact_type'], "In line " . __LINE__); | |
331 | $this->assertEquals(end($result['contact_sub_type']), $updateParams['contact_sub_type'], "In line " . __LINE__); | |
fc928539 | 332 | $this->callAPISuccess('contact', 'delete', $params); |
6a488035 TO |
333 | } |
334 | ||
c490a46a | 335 | /** |
eceb18cc | 336 | * Test update with no subtype to invalid subtype. |
2d932085 CW |
337 | * @param int $version |
338 | * @dataProvider versionThreeAndFour | |
c490a46a | 339 | */ |
2d932085 CW |
340 | public function testContactUpdateSubtypeInvalid($version) { |
341 | $this->_apiversion = $version; | |
6a488035 TO |
342 | |
343 | // check for Type:Individual subtype:sub_individual | |
9099cab3 | 344 | $contactParams = [ |
6a488035 TO |
345 | 'first_name' => 'Anne', |
346 | 'last_name' => 'Grant', | |
347 | 'contact_type' => 'Individual', | |
348 | 'contact_sub_type' => $this->subTypeIndividual, | |
9099cab3 | 349 | ]; |
fc928539 | 350 | $contact = $this->callAPISuccess('contact', 'create', $contactParams); |
6a488035 TO |
351 | |
352 | // subype:sub_household | |
9099cab3 | 353 | $updateParams = [ |
6a488035 TO |
354 | 'first_name' => 'John', |
355 | 'last_name' => 'Grant', | |
356 | 'contact_id' => $contact['id'], | |
357 | 'contact_type' => 'Individual', | |
358 | 'contact_sub_type' => $this->subTypeHousehold, | |
9099cab3 | 359 | ]; |
d0e1eff2 | 360 | $updateContact = $this->callAPIFailure('contact', 'create', $updateParams); |
9099cab3 | 361 | $params = [ |
6a488035 | 362 | 'contact_id' => $contact['id'], |
9099cab3 | 363 | ]; |
fc928539 | 364 | $this->callAPISuccess('contact', 'delete', $params); |
6a488035 TO |
365 | |
366 | // check for Type:Organization subtype: | |
9099cab3 | 367 | $contactParams = [ |
6a488035 TO |
368 | 'organization_name' => 'Compumentor', |
369 | 'contact_type' => 'Organization', | |
370 | 'contact_sub_type' => $this->subTypeOrganization, | |
9099cab3 | 371 | ]; |
fc928539 | 372 | $contact = $this->callAPISuccess('contact', 'create', $contactParams); |
6a488035 | 373 | |
9099cab3 | 374 | $updateParams = [ |
6a488035 TO |
375 | 'organization_name' => 'Intel Arts', |
376 | 'contact_id' => $contact['id'], | |
377 | 'contact_sub_type' => $this->subTypeIndividual, | |
9099cab3 | 378 | ]; |
d0e1eff2 | 379 | $updateContact = $this->callAPIFailure('contact', 'create', $updateParams); |
9099cab3 | 380 | $params = [ |
6a488035 | 381 | 'contact_id' => $contact['id'], |
9099cab3 | 382 | ]; |
fc928539 | 383 | $this->callAPISuccess('contact', 'delete', $params); |
6a488035 | 384 | } |
96025800 | 385 | |
6a488035 | 386 | } |