Import from SVN (r45945, r596)
[civicrm-core.git] / tests / phpunit / api / v3 / ContactTypeTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27 */
28
29
30
31
32
33 require_once 'CiviTest/CiviUnitTestCase.php';
34 class api_v3_ContactTypeTest extends CiviUnitTestCase {
35 protected $_apiversion;
36 public $_eNoticeCompliant = TRUE;
37 function setUp() {
38 parent::setUp();
39 $this->_apiversion = 3;
40 $params = array(
41 'label' => 'sub_individual',
42 'name' => 'sub_individual',
43 // Individual
44 'parent_id' => 1,
45 'is_active' => 1,
46 );
47 $result = CRM_Contact_BAO_ContactType::add($params);
48 $this->subTypeIndividual = $params['name'];
49 $this->_subTypeIndividualId = $result->id;
50
51 $params = array(
52 'label' => 'sub_organization',
53 'name' => 'sub_organization',
54 // Organization
55 'parent_id' => 3,
56 'is_active' => 1,
57 );
58 $result = CRM_Contact_BAO_ContactType::add($params);
59 $this->subTypeOrganization = $params['name'];
60 $this->_subTypeOrganizationId = $result->id;
61
62 $params = array(
63 'label' => 'sub_household',
64 'name' => 'sub_household',
65 // Household
66 'parent_id' => 2,
67 'is_active' => 1,
68 );
69 $result = CRM_Contact_BAO_ContactType::add($params);
70 $this->subTypeHousehold = $params['name'];
71 $this->_subTypeHouseholdId = $result->id;
72 }
73
74 /**
75 * Tears down the fixture, for example, closes a network connection.
76 * This method is called after a test is executed.
77 *
78 */
79 function tearDown() {
80 $contactTypeIds = array(
81 $this->_subTypeIndividualId,
82 $this->_subTypeOrganizationId,
83 $this->_subTypeHouseholdId,
84 );
85 foreach ($contactTypeIds as $typeId) {
86 $this->contactTypeDelete($typeId);
87 }
88 }
89
90 /*
91 * test add methods with valid data
92 * success expected
93 */
94 function testContactCreate() {
95
96 // check for Type:Individual Subtype:sub_individual
97 $contactParams = array(
98 'first_name' => 'Anne',
99 'last_name' => 'Grant',
100 'contact_type' => 'Individual',
101 'contact_sub_type' => $this->subTypeIndividual,
102 'version' => $this->_apiversion,
103 );
104 $contact = civicrm_api('contact', 'create', $contactParams);
105 $this->assertEquals($contact['is_error'], 0, "In line " . __LINE__);
106 $params = array(
107 'contact_id' => $contact['id'],
108 'version' => $this->_apiversion,
109 );
110 $result = civicrm_api('contact', 'get', $params);
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__);
115 civicrm_api('contact', 'delete', $params);
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,
122 'version' => $this->_apiversion,
123 );
124 $contact = civicrm_api('contact', 'create', $contactParams);
125 $this->assertEquals($contact['is_error'], 0, "In line " . __LINE__);
126
127 $params = array(
128 'contact_id' => $contact['id'],
129 'version' => $this->_apiversion,
130 );
131 $getContacts = civicrm_api('contact', 'get', $params);
132 $result = $getContacts['values'][$contact['id']];
133 $this->assertEquals($result['organization_name'], $contactParams['organization_name'], "In line " . __LINE__);
134 $this->assertEquals($result['contact_type'], $contactParams['contact_type'], "In line " . __LINE__);
135 $this->assertEquals(end($result['contact_sub_type']), $contactParams['contact_sub_type'], "In line " . __LINE__);
136 civicrm_api('contact', 'delete', $params);
137 }
138
139
140 /*
141 * test add with invalid data
142 */
143 function testContactAddInvalidData() {
144
145 // check for Type:Individual Subtype:sub_household
146 $contactParams = array(
147 'first_name' => 'Anne',
148 'last_name' => 'Grant',
149 'contact_type' => 'Individual',
150 'contact_sub_type' => $this->subTypeHousehold,
151 'version' => $this->_apiversion,
152 );
153 $contact = civicrm_api('contact', 'create', $contactParams);
154 $this->assertEquals($contact['is_error'], 1, "In line " . __LINE__);
155
156 // check for Type:Organization Subtype:sub_individual
157 $contactParams = array(
158 'organization_name' => 'Compumentor',
159 'contact_type' => 'Organization',
160 'contact_sub_type' => $this->subTypeIndividual,
161 'version' => $this->_apiversion,
162 );
163 $contact = civicrm_api('contact', 'create', $contactParams);
164 $this->assertEquals($contact['is_error'], 1, "In line " . __LINE__);
165 }
166
167
168 /*
169 * test update with no subtype to valid subtype
170 * success expected
171 */
172 function testContactUpdateNoSubtypeValid() {
173
174 // check for Type:Individual
175 $contactParams = array(
176 'first_name' => 'Anne',
177 'last_name' => 'Grant',
178 'contact_type' => 'Individual',
179 'version' => $this->_apiversion,
180 );
181 $contact = civicrm_api('contact', 'create', $contactParams);
182 // subype:sub_individual
183 $updateParams = array(
184 'first_name' => 'John',
185 'last_name' => 'Grant',
186 'contact_id' => $contact['id'],
187 'contact_type' => 'Individual',
188 'contact_sub_type' => $this->subTypeIndividual,
189 'version' => $this->_apiversion,
190 );
191 $updateContact = civicrm_api('contact', 'create', $updateParams);
192 $this->assertEquals($updateContact['is_error'], 0, "In line " . __LINE__);
193 $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__);
194
195 $params = array(
196 'contact_id' => $contact['id'],
197 'version' => $this->_apiversion,
198 );
199 $getContacts = civicrm_api('contact', 'get', $params);
200 $result = $getContacts['values'][$contact['id']];
201
202 $this->assertEquals($result['first_name'], $updateParams['first_name'], "In line " . __LINE__);
203 $this->assertEquals($result['last_name'], $updateParams['last_name'], "In line " . __LINE__);
204 $this->assertEquals($result['contact_type'], $updateParams['contact_type'], "In line " . __LINE__);
205 $this->assertEquals(end($result['contact_sub_type']), $updateParams['contact_sub_type'], "In line " . __LINE__);
206 civicrm_api('contact', 'delete', $params);
207
208 // check for Type:Organization
209 $contactParams = array(
210 'organization_name' => 'Compumentor',
211 'contact_type' => 'Organization',
212 'version' => $this->_apiversion,
213 );
214 $contact = civicrm_api('contact', 'create', $contactParams);
215
216 // subype:sub_organization
217 $updateParams = array(
218 'organization_name' => 'Intel Arts',
219 'contact_id' => $contact['id'],
220 'contact_type' => 'Organization',
221 'contact_sub_type' => $this->subTypeOrganization,
222 'version' => $this->_apiversion,
223 );
224 $updateContact = civicrm_api('contact', 'create', $updateParams);
225
226 $this->assertEquals($updateContact['is_error'], 0, "In line " . __LINE__);
227 $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__);
228
229 $params = array(
230 'contact_id' => $contact['id'],
231 'version' => $this->_apiversion,
232 );
233 $getContacts = civicrm_api('contact', 'get', $params);
234 $result = $getContacts['values'][$contact['id']];
235
236 $this->assertEquals($result['organization_name'], $updateParams['organization_name'], "In line " . __LINE__);
237 $this->assertEquals($result['contact_type'], $updateParams['contact_type'], "In line " . __LINE__);
238 $this->assertEquals(end($result['contact_sub_type']), $updateParams['contact_sub_type'], "In line " . __LINE__);
239 civicrm_api('contact', 'delete', $params);
240 }
241
242
243 /*
244 * test update with no subtype to invalid subtype
245 */
246 function testContactUpdateNoSubtypeInvalid() {
247
248 // check for Type:Individual
249 $contactParams = array(
250 'first_name' => 'Anne',
251 'last_name' => 'Grant',
252 'contact_type' => 'Individual',
253 'version' => $this->_apiversion,
254 );
255 $contact = civicrm_api('contact', 'create', $contactParams);
256
257 // subype:sub_household
258 $updateParams = array(
259 'first_name' => 'John',
260 'last_name' => 'Grant',
261 'contact_id' => $contact['id'],
262 'contact_type' => 'Individual',
263 'contact_sub_type' => $this->subTypeHousehold,
264 'version' => $this->_apiversion,
265 );
266 $updateContact = civicrm_api('contact', 'create', $updateParams);
267
268 $this->assertEquals($updateContact['is_error'], 1, "In line " . __LINE__);
269 $params = array(
270 'contact_id' => $contact['id'],
271 'version' => $this->_apiversion,
272 );
273 civicrm_api('contact', 'delete', $params);
274
275 // check for Type:Organization
276 $contactParams = array(
277 'organization_name' => 'Compumentor',
278 'contact_type' => 'Organization',
279 'version' => $this->_apiversion,
280 );
281 $contact = civicrm_api('contact', 'create', $contactParams);
282
283 $updateParams = array(
284 'organization_name' => 'Intel Arts',
285 'contact_id' => $contact['id'],
286 'contact_type' => 'Organization',
287 'contact_sub_type' => $this->subTypeIndividual,
288 'version' => $this->_apiversion,
289 );
290 $updateContact = civicrm_api('contact', 'create', $updateParams);
291
292 $this->assertEquals($updateContact['is_error'], 1, "In line " . __LINE__);
293 $params = array(
294 'contact_id' => $contact['id'],
295 'version' => $this->_apiversion,
296 );
297 civicrm_api('contact', 'delete', $params);
298 }
299
300 /*
301 * test update with no subtype to valid subtype
302 * success expected
303 */
304 function testContactUpdateSubtypeValid() {
305
306 $params = array(
307 'label' => 'sub2_individual',
308 'name' => 'sub2_individual',
309 // Individual
310 'parent_id' => 1,
311 'is_active' => 1,
312 );
313 $getSubtype = CRM_Contact_BAO_ContactType::add($params);
314 $subtype = $params['name'];
315
316 // check for Type:Individual subype:sub_individual
317 $contactParams = array(
318 'first_name' => 'Anne',
319 'last_name' => 'Grant',
320 'contact_type' => 'Individual',
321 'contact_sub_type' => $this->subTypeIndividual,
322 'version' => $this->_apiversion,
323 );
324 $contact = civicrm_api('contact', 'create', $contactParams);
325 // subype:sub2_individual
326 $updateParams = array(
327 'id' => $contact['id'],
328 'first_name' => 'John',
329 'last_name' => 'Grant',
330 'contact_id' => $contact['id'],
331 'contact_type' => 'Individual',
332 'contact_sub_type' => $subtype,
333 'version' => $this->_apiversion,
334 );
335 $updateContact = civicrm_api('contact', 'create', $updateParams);
336
337 $this->assertEquals($updateContact['is_error'], 0, "In line " . __LINE__);
338 $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__);
339
340 $params = array(
341 'contact_id' => $contact['id'],
342 'version' => $this->_apiversion,
343 );
344 $getContacts = civicrm_api('contact', 'get', $params);
345 $result = $getContacts['values'][$contact['id']];
346
347 $this->assertEquals($result['first_name'], $updateParams['first_name'], "In line " . __LINE__);
348 $this->assertEquals($result['last_name'], $updateParams['last_name'], "In line " . __LINE__);
349 $this->assertEquals($result['contact_type'], $updateParams['contact_type'], "In line " . __LINE__);
350 $this->assertEquals(end($result['contact_sub_type']), $updateParams['contact_sub_type'], "In line " . __LINE__);
351 civicrm_api('contact', 'delete', $params);
352
353
354 $params = array(
355 'label' => 'sub2_organization',
356 'name' => 'sub2_organization',
357 // Organization
358 'parent_id' => 3,
359 'is_active' => 1,
360 );
361 $getSubtype = CRM_Contact_BAO_ContactType::add($params);
362 $subtype = $params['name'];
363
364 // check for Type:Organization subype:sub_organization
365 $contactParams = array(
366 'organization_name' => 'Compumentor',
367 'contact_type' => 'Organization',
368 'contact_sub_type' => $this->subTypeOrganization,
369 'version' => $this->_apiversion,
370 );
371 $contact = civicrm_api('contact', 'create', $contactParams);
372
373 // subype:sub2_organization
374 $updateParams = array(
375 'organization_name' => 'Intel Arts',
376 'contact_id' => $contact['id'],
377 'contact_type' => 'Organization',
378 'contact_sub_type' => $subtype,
379 'version' => $this->_apiversion,
380 );
381 $updateContact = civicrm_api('contact', 'create', $updateParams);
382
383 $this->assertEquals($updateContact['is_error'], 0, "In line " . __LINE__);
384 $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__);
385
386 $params = array(
387 'contact_id' => $contact['id'],
388 'version' => $this->_apiversion,
389 );
390 $getContacts = civicrm_api('contact', 'get', $params);
391 $result = $getContacts['values'][$contact['id']];
392
393 $this->assertEquals($result['organization_name'], $updateParams['organization_name'], "In line " . __LINE__);
394 $this->assertEquals($result['contact_type'], $updateParams['contact_type'], "In line " . __LINE__);
395 $this->assertEquals(end($result['contact_sub_type']), $updateParams['contact_sub_type'], "In line " . __LINE__);
396 civicrm_api('contact', 'delete', $params);
397 }
398
399 /*
400 * test update with no subtype to invalid subtype
401 */
402 function testContactUpdateSubtypeInvalid() {
403
404 // check for Type:Individual subtype:sub_individual
405 $contactParams = array(
406 'first_name' => 'Anne',
407 'last_name' => 'Grant',
408 'contact_type' => 'Individual',
409 'contact_sub_type' => $this->subTypeIndividual,
410 'version' => $this->_apiversion,
411 );
412 $contact = civicrm_api('contact', 'create', $contactParams);
413
414 // subype:sub_household
415 $updateParams = array(
416 'first_name' => 'John',
417 'last_name' => 'Grant',
418 'contact_id' => $contact['id'],
419 'contact_type' => 'Individual',
420 'contact_sub_type' => $this->subTypeHousehold,
421 'version' => $this->_apiversion,
422 );
423 $updateContact = civicrm_api('contact', 'create', $updateParams);
424
425 $this->assertEquals($updateContact['is_error'], 1, "In line " . __LINE__);
426 $params = array(
427 'contact_id' => $contact['id'],
428 'version' => $this->_apiversion,
429 );
430 civicrm_api('contact', 'delete', $params);
431
432 // check for Type:Organization subtype:
433 $contactParams = array(
434 'organization_name' => 'Compumentor',
435 'contact_type' => 'Organization',
436 'contact_sub_type' => $this->subTypeOrganization,
437 'version' => $this->_apiversion,
438 );
439 $contact = civicrm_api('contact', 'create', $contactParams);
440
441 $updateParams = array(
442 'organization_name' => 'Intel Arts',
443 'contact_id' => $contact['id'],
444 'contact_sub_type' => $this->subTypeIndividual,
445 'version' => $this->_apiversion,
446 );
447 $updateContact = civicrm_api('contact', 'create', $updateParams);
448
449 $this->assertEquals($updateContact['is_error'], 1, "In line " . __LINE__);
450 $params = array(
451 'contact_id' => $contact['id'],
452 'version' => $this->_apiversion,
453 );
454 civicrm_api('contact', 'delete', $params);
455 }
456 }
457