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