CRM-13179 minor test addition
[civicrm-core.git] / tests / phpunit / api / v3 / ProfileTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.4 |
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 * Include class definitions
31 */
32 require_once 'tests/phpunit/CiviTest/CiviUnitTestCase.php';
33
34 /**
35 * Test APIv3 civicrm_profile_* functions
36 *
37 * @package CiviCRM
38 */
39 class api_v3_ProfileTest extends CiviUnitTestCase {
40 protected $_apiversion;
41 function get_info() {
42 return array(
43 'name' => 'Profile Test',
44 'description' => 'Test all profile API methods.',
45 'group' => 'CiviCRM API Tests',
46 );
47 }
48
49 function setUp() {
50 $this->_apiversion = 3;
51 parent::setUp();
52 $config = CRM_Core_Config::singleton();
53 $config->countryLimit[1] = 1013;
54 $config->stateLimit[1] = 1013;
55 }
56
57 function tearDown() {
58
59 $this->quickCleanup(array(
60 'civicrm_contact',
61 'civicrm_phone',
62 'civicrm_address',
63 ), TRUE);
64 // ok can't be bothered wring an api to do this & truncating is crazy
65 CRM_Core_DAO::executeQuery(' DELETE FROM civicrm_uf_group WHERE id IN (25, 26)');
66 }
67
68 ////////////// test $this->callAPISuccess3_profile_get //////////////////
69
70 /**
71 * check Without ProfileId
72 */
73 function testProfileGetWithoutProfileId() {
74 $params = array(
75 'contact_id' => 1,
76 );
77 $result = $this->callAPIFailure('profile', 'get', $params,
78 'Mandatory key(s) missing from params array: profile_id'
79 );
80 }
81
82 /**
83 * check with no invalid profile Id
84 */
85 function testProfileGetInvalidProfileId() {
86 $params = array(
87 'contact_id' => 1,
88 'profile_id' => 1000,
89 );
90 $result = $this->callAPIFailure('profile', 'get', $params);
91 }
92
93 /**
94 * check with success
95 */
96 function testProfileGet() {
97 $pofileFieldValues = $this->_createIndividualContact();
98 $expected = current($pofileFieldValues);
99 $contactId = key($pofileFieldValues);
100 $params = array(
101 'profile_id' => 25,
102 'contact_id' => $contactId,
103 );
104 $result = $this->callAPISuccess('profile', 'get', $params);
105 foreach ($expected as $profileField => $value) {
106 $this->assertEquals($value, CRM_Utils_Array::value($profileField, $result['values']), "In line " . __LINE__ . " error message: " . "missing/mismatching value for {$profileField}"
107 );
108 }
109 }
110
111 function testProfileGetMultiple() {
112 $pofileFieldValues = $this->_createIndividualContact();
113 $expected = current($pofileFieldValues);
114 $contactId = key($pofileFieldValues);
115 $params = array(
116 'profile_id' => array(25, 1, 'Billing'),
117 'contact_id' => $contactId,
118 );
119
120 $result = $this->callAPIAndDocument('profile', 'get', $params, __FUNCTION__, __FILE__);
121 foreach ($expected as $profileField => $value) {
122 $this->assertEquals($value, CRM_Utils_Array::value($profileField, $result['values'][25]), " error message: " . "missing/mismatching value for {$profileField}");
123 }
124 $this->assertEquals('abc1', $result['values'][1]['first_name'], " error message: " . "missing/mismatching value for {$profileField}");
125 $this->assertFalse(array_key_exists('email-Primary', $result['values'][1]), 'profile 1 doesn not include email');
126 $this->assertEquals($result['values']['Billing'], array(
127 'billing_first_name' => 'abc1',
128 'billing_middle_name' => 'J.',
129 'billing_last_name' => 'xyz1',
130 'billing_street_address-5' => '5 Saint Helier St',
131 'billing_city-5' => 'Gotham City',
132 'billing_state_province_id-5' => '1021',
133 'billing_country_id-5' => '',
134 'billing-email-5' => 'abc1.xyz1@yahoo.com',
135 'billing_postal_code-5' => '90210',
136 'billing-email-5' => 'abc1.xyz1@yahoo.com',
137 'email-5' => 'abc1.xyz1@yahoo.com',
138 ));
139 }
140
141 function testProfileGetBillingUseIsBillingLocation() {
142 $individual = $this->_createIndividualContact();
143 $contactId = key($individual);
144 $this->callAPISuccess('address', 'create', array(
145 'is_billing' => 1,
146 'street_address' => 'is billing st',
147 'location_type_id' => 2,
148 'contact_id' => $contactId,
149 ));
150
151 $expected = current($individual);
152
153 $params = array(
154 'profile_id' => array(25, 1, 'Billing'),
155 'contact_id' => $contactId,
156 );
157
158 $result = $this->callAPISuccess('profile', 'get', $params);
159 $this->assertEquals('abc1', $result['values'][1]['first_name']);
160 $this->assertEquals(array(
161 'billing_first_name' => 'abc1',
162 'billing_middle_name' => 'J.',
163 'billing_last_name' => 'xyz1',
164 'billing_street_address-5' => 'is billing st',
165 'billing_city-5' => '',
166 'billing_state_province_id-5' => '',
167 'billing_country_id-5' => '',
168 'billing-email-5' => 'abc1.xyz1@yahoo.com',
169 'email-5' => 'abc1.xyz1@yahoo.com',
170 'billing_postal_code-5' => '',
171 ), $result['values']['Billing']);
172 }
173
174 function testProfileGetMultipleHasBillingLocation() {
175 $individual = $this->_createIndividualContact();
176 $contactId = key($individual);
177 $this->callAPISuccess('address', 'create', array('contact_id' => $contactId , 'street_address' => '25 Big Street', 'city' => 'big city', 'location_type_id' => 5));
178 $this->callAPISuccess('email', 'create', array('contact_id' => $contactId , 'email' => 'big@once.com', 'location_type_id' => 2, 'is_billing' => 1));
179
180 $expected = current($individual);
181
182 $params = array(
183 'profile_id' => array(25, 1, 'Billing'),
184 'contact_id' => $contactId,
185 );
186
187 $result = $this->callAPISuccess('profile', 'get', $params, __FUNCTION__, __FILE__);
188 $this->assertEquals('abc1', $result['values'][1]['first_name']);
189 $this->assertEquals($result['values']['Billing'], array(
190 'billing_first_name' => 'abc1',
191 'billing_middle_name' => 'J.',
192 'billing_last_name' => 'xyz1',
193 'billing_street_address-5' => '25 Big Street',
194 'billing_city-5' => 'big city',
195 'billing_state_province_id-5' => '',
196 'billing_country_id-5' => '',
197 'billing-email-5' => 'big@once.com',
198 'email-5' => 'big@once.com',
199 'billing_postal_code-5' => '',
200 ));
201 }
202
203
204 /**
205 * check contact activity profile without activity id
206 */
207 function testContactActivityGetWithoutActivityId() {
208 list($params, $expected) = $this->_createContactWithActivity();
209
210 unset($params['activity_id']);
211 $result = $this->callAPIFailure('profile', 'get', $params,
212 'Mandatory key(s) missing from params array: activity_id');
213 }
214
215 /**
216 * check contact activity profile wrong activity id
217 */
218 function testContactActivityGetWrongActivityId() {
219 list($params, $expected) = $this->_createContactWithActivity();
220
221 $params['activity_id'] = 100001;
222 $result = $this->callAPIFailure('profile', 'get', $params,
223 'Invalid Activity Id (aid).');
224 }
225
226 /*
227 * check contact activity profile with wrong activity type
228 */
229 function testContactActivityGetWrongActivityType() {
230 //flush cache by calling with reset
231 $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, TRUE, 'name', TRUE);
232
233 $sourceContactId = $this->householdCreate();
234
235 $activityparams = array(
236 'source_contact_id' => $sourceContactId,
237 'activity_type_id' => '2',
238 'subject' => 'Test activity',
239 'activity_date_time' => '20110316',
240 'duration' => '120',
241 'location' => 'Pensulvania',
242 'details' => 'a test activity',
243 'status_id' => '1',
244 'priority_id' => '1',
245 );
246
247 $activity = $this->callAPISuccess('activity', 'create', $activityparams);
248
249 $activityValues = array_pop($activity['values']);
250
251 list($params, $expected) = $this->_createContactWithActivity();
252
253 $params['activity_id'] = $activityValues['id'];
254 $result = $this->callAPIFailure('profile', 'get', $params,
255 'This activity cannot be edited or viewed via this profile.'
256 );
257 }
258
259 /*
260 * check contact activity profile with success
261 */
262 function testContactActivityGetSuccess() {
263 list($params, $expected) = $this->_createContactWithActivity();
264
265 $result = $this->callAPISuccess('profile', 'get', $params);
266
267 foreach ($expected as $profileField => $value) {
268 $this->assertEquals($value, CRM_Utils_Array::value($profileField, $result['values']), "In line " . __LINE__ . " error message: " . "missing/mismatching value for {$profileField}"
269 );
270 }
271 }
272
273 /////////////// test $this->callAPISuccess3_profile_set //////////////////
274
275 /**
276 * check with no array
277 */
278 function testProfileSetNoArray() {
279 $params = NULL;
280 $result = $this->callAPIFailure('profile', 'set', $params);
281 $this->assertEquals($result['error_message'], 'Input variable `params` is not an array');
282 }
283
284 /**
285 * check Without ProfileId
286 */
287 function testProfileSetWithoutProfileId() {
288 $params = array(
289 'contact_id' => 1,
290 );
291 $result = $this->callAPIFailure('profile', 'set', $params,
292 'Mandatory key(s) missing from params array: profile_id'
293 );
294 }
295
296 /**
297 * check with no invalid profile Id
298 */
299 function testProfileSetInvalidProfileId() {
300 $params = array(
301 'contact_id' => 1,
302 'profile_id' => 1000,
303 );
304 $result = $this->callAPIFailure('profile', 'set', $params);
305 }
306
307 /**
308 * check with missing required field in profile
309 */
310 function testProfileSetCheckProfileRequired() {
311 $pofileFieldValues = $this->_createIndividualContact();
312 current($pofileFieldValues);
313 $contactId = key($pofileFieldValues);
314 $updateParams = array(
315 'first_name' => 'abc2',
316 'last_name' => 'xyz2',
317 'phone-1-1' => '022 321 826',
318 'country-1' => '1013',
319 'state_province-1' => '1000',
320 );
321
322 $params = array_merge(array('profile_id' => 25, 'contact_id' => $contactId),
323 $updateParams
324 );
325
326 $result = $this->callAPIFailure('profile', 'set', $params,
327 'Missing required parameters for profile id 25: email-Primary'
328 );
329 }
330
331 /**
332 * check with success
333 */
334 function testProfileSet() {
335 $pofileFieldValues = $this->_createIndividualContact();
336 current($pofileFieldValues);
337 $contactId = key($pofileFieldValues);
338
339 $updateParams = array(
340 'first_name' => 'abc2',
341 'last_name' => 'xyz2',
342 'email-Primary' => 'abc2.xyz2@gmail.com',
343 'phone-1-1' => '022 321 826',
344 'country-1' => '1013',
345 'state_province-1' => '1000',
346 );
347
348 $params = array_merge(array(
349 'profile_id' => 25,
350 'contact_id' => $contactId,
351 ), $updateParams);
352
353 $result = $this->callAPIAndDocument('profile', 'set', $params, __FUNCTION__, __FILE__);
354
355 $getParams = array(
356 'profile_id' => 25,
357 'contact_id' => $contactId,
358 );
359 $profileDetails = $this->callAPISuccess('profile', 'get', $getParams);
360
361 foreach ($updateParams as $profileField => $value) {
362 $this->assertEquals($value, CRM_Utils_Array::value($profileField, $profileDetails['values']), "In line " . __LINE__ . " error message: " . "missing/mismatching value for {$profileField}"
363 );
364 }
365 }
366
367 /*
368 * check contact activity profile without activity id
369 */
370 function testContactActivitySetWithoutActivityId() {
371 list($params, $expected) = $this->_createContactWithActivity();
372
373 $params = array_merge($params, $expected);
374 unset($params['activity_id']);
375 $result = $this->callAPIFailure('profile', 'set', $params);
376 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: activity_id');
377
378 $this->quickCleanup(array('civicrm_uf_field', 'civicrm_uf_join', 'civicrm_uf_group', 'civicrm_custom_field', 'civicrm_custom_group', 'civicrm_contact'));
379 }
380
381 /*
382 * check contact activity profile wrong activity id
383 */
384 function testContactActivitySetWrongActivityId() {
385 list($params, $expected) = $this->_createContactWithActivity();
386
387 $params = array_merge($params, $expected);
388 $params['activity_id'] = 100001;
389 $result = $this->callAPIFailure('profile', 'set', $params);
390 $this->assertEquals($result['error_message'], 'Invalid Activity Id (aid).');
391
392 $this->quickCleanup(array('civicrm_uf_field', 'civicrm_uf_join', 'civicrm_uf_group', 'civicrm_custom_field', 'civicrm_custom_group', 'civicrm_contact'));
393 }
394
395 /*
396 * check contact activity profile with wrong activity type
397 */
398 function testContactActivitySetWrongActivityType() {
399 //flush cache by calling with reset
400 $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, TRUE, 'name', TRUE);
401
402 $sourceContactId = $this->householdCreate();
403
404 $activityparams = array(
405 'source_contact_id' => $sourceContactId,
406 'activity_type_id' => '2',
407 'subject' => 'Test activity',
408 'activity_date_time' => '20110316',
409 'duration' => '120',
410 'location' => 'Pensulvania',
411 'details' => 'a test activity',
412 'status_id' => '1',
413 'priority_id' => '1',
414 );
415
416 $activity = $this->callAPISuccess('activity', 'create', $activityparams);
417
418 $activityValues = array_pop($activity['values']);
419
420 list($params, $expected) = $this->_createContactWithActivity();
421
422 $params = array_merge($params, $expected);
423 $params['activity_id'] = $activityValues['id'];
424 $result = $this->callAPIFailure('profile', 'set', $params,
425 'This activity cannot be edited or viewed via this profile.');
426 }
427
428 /*
429 * check contact activity profile with success
430 */
431 function testContactActivitySetSuccess() {
432 list($params, $expected) = $this->_createContactWithActivity();
433
434 $updateParams = array(
435 'first_name' => 'abc2',
436 'last_name' => 'xyz2',
437 'email-Primary' => 'abc2.xyz2@yahoo.com',
438 'activity_subject' => 'Test Meeting',
439 'activity_details' => 'a test activity details',
440 'activity_duration' => '100',
441 'activity_date_time' => '03/08/2010',
442 'activity_status_id' => '2',
443 );
444 $profileParams = array_merge($params, $updateParams);
445 $profile = $this->callAPISuccess('profile', 'set', $profileParams);
446 $result = $this->callAPISuccess('profile', 'get', $params);
447
448 foreach ($updateParams as $profileField => $value) {
449 $this->assertEquals($value, CRM_Utils_Array::value($profileField, $result['values']), "In line " . __LINE__ . " error message: " . "missing/mismatching value for {$profileField}"
450 );
451 }
452 }
453
454 /**
455 * check profile apply Without ProfileId
456 */
457 function testProfileApplyWithoutProfileId() {
458 $params = array(
459 'contact_id' => 1,
460 );
461 $result = $this->callAPIFailure('profile', 'apply', $params,
462 'Mandatory key(s) missing from params array: profile_id');
463 }
464
465 /**
466 * check profile apply with no invalid profile Id
467 */
468 function testProfileApplyInvalidProfileId() {
469 $params = array(
470 'contact_id' => 1,
471 'profile_id' => 1000,
472 );
473 $result = $this->callAPIFailure('profile', 'apply', $params);
474 }
475
476 /**
477 * check with success
478 */
479 function testProfileApply() {
480 $pofileFieldValues = $this->_createIndividualContact();
481 current($pofileFieldValues);
482 $contactId = key($pofileFieldValues);
483
484 $params = array(
485 'profile_id' => 25,
486 'contact_id' => $contactId,
487 'first_name' => 'abc2',
488 'last_name' => 'xyz2',
489 'email-Primary' => 'abc2.xyz2@gmail.com',
490 'phone-1-1' => '022 321 826',
491 'country-1' => '1013',
492 'state_province-1' => '1000',
493 );
494
495 $result = $this->callAPIAndDocument('profile', 'apply', $params, __FUNCTION__, __FILE__);
496
497 // Expected field values
498 $expected['contact'] = array(
499 'contact_id' => $contactId,
500 'contact_type' => 'Individual',
501 'first_name' => 'abc2',
502 'last_name' => 'xyz2',
503 );
504 $expected['email'] = array(
505 'location_type_id' => 1,
506 'is_primary' => 1,
507 'email' => 'abc2.xyz2@gmail.com',
508 );
509
510 $expected['phone'] = array(
511 'location_type_id' => 1,
512 'is_primary' => 1,
513 'phone_type_id' => 1,
514 'phone' => '022 321 826',
515 );
516 $expected['address'] = array(
517 'location_type_id' => 1,
518 'is_primary' => 1,
519 'country_id' => 1013,
520 'state_province_id' => 1000,
521 );
522
523 foreach ($expected['contact'] as $field => $value) {
524 $this->assertEquals($value, CRM_Utils_Array::value($field, $result['values']), "In line " . __LINE__ . " error message: " . "missing/mismatching value for {$field}"
525 );
526 }
527
528 foreach (array(
529 'email', 'phone', 'address') as $fieldType) {
530 $typeValues = array_pop($result['values'][$fieldType]);
531 foreach ($expected[$fieldType] as $field => $value) {
532 $this->assertEquals($value, CRM_Utils_Array::value($field, $typeValues), "In line " . __LINE__ . " error message: " . "missing/mismatching value for {$field} ({$fieldType})"
533 );
534 }
535 }
536 }
537
538 /*
539 * Helper function to create an Individual with address/email/phone info. Import UF Group and UF Fields
540 */
541 function _createIndividualContact($params = array()) {
542 $contactParams = array_merge(array(
543 'first_name' => 'abc1',
544 'last_name' => 'xyz1',
545 'email' => 'abc1.xyz1@yahoo.com',
546 'api.address.create' => array(
547 'location_type_id' => 1,
548 'is_primary' => 1,
549 'street_address' => '5 Saint Helier St',
550 'county' => 'Marin',
551 'country' => 'United States',
552 'state_province' => 'Michigan',
553 'supplemental_address_1' => 'Hallmark Ct',
554 'supplemental_address_2' => 'Jersey Village',
555 'postal_code' => '90210',
556 'city' => 'Gotham City',
557 'is_billing' => 0,
558 ),
559 'api.phone.create' => array(
560 'location_type_id' => '1',
561 'phone' => '021 512 755',
562 'phone_type_id' => '1',
563 'is_primary' => '1',
564 ),
565 ), $params
566 );
567
568 $contactID = $this->individualCreate($contactParams);
569
570 // Create new profile having group_type: Contact,Individual
571 $op = new PHPUnit_Extensions_Database_Operation_Insert();
572 $op->execute($this->_dbconn,
573 new PHPUnit_Extensions_Database_DataSet_XMLDataSet(
574 dirname(__FILE__) . "/dataset/uf_group_25.xml"
575 )
576 );
577 // Create Contact + Idividual fields for profile
578 $op = new PHPUnit_Extensions_Database_Operation_Insert();
579 $op->execute($this->_dbconn,
580 new PHPUnit_Extensions_Database_DataSet_XMLDataSet(
581 dirname(__FILE__) . "/dataset/uf_field_uf_group_25.xml"
582 )
583 );
584
585
586 // expected result of above created profile with contact Id $contactId
587 $profileData[$contactID] = array(
588 'first_name' => 'abc1',
589 'last_name' => 'xyz1',
590 'email-Primary' => 'abc1.xyz1@yahoo.com',
591 'phone-1-1' => '021 512 755',
592 'country-1' => '1228',
593 'state_province-1' => '1021',
594 );
595
596 return $profileData;
597 }
598
599 function _createContactWithActivity() {
600 // @TODO: Create profile with custom fields
601 $op = new PHPUnit_Extensions_Database_Operation_Insert();
602 $op->execute($this->_dbconn,
603 new PHPUnit_Extensions_Database_DataSet_FlatXMLDataSet(
604 dirname(__FILE__) . '/dataset/uf_group_contact_activity_26.xml'
605 )
606 );
607 // hack: xml data set do not accept \ 1 (CRM_Core_DAO::VALUE_SEPARATOR)
608 CRM_Core_DAO::setFieldValue('CRM_Core_DAO_UFGroup', '26', 'group_type', 'Individual,Contact,Activity' . CRM_Core_DAO::VALUE_SEPARATOR . 'ActivityType:1');
609
610 $sourceContactId = $this->individualCreate();
611 $contactParams = array(
612 'first_name' => 'abc1',
613 'last_name' => 'xyz1',
614 'contact_type' => 'Individual',
615 'email' => 'abc1.xyz1@yahoo.com',
616 'api.address.create' => array(
617 'location_type_id' => 1,
618 'is_primary' => 1,
619 'name' => 'Saint Helier St',
620 'county' => 'Marin',
621 'country' => 'United States',
622 'state_province' => 'Michigan',
623 'supplemental_address_1' => 'Hallmark Ct',
624 'supplemental_address_2' => 'Jersey Village',
625 ),
626 );
627
628 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
629
630 $keys = array_keys($contact['values']);
631 $contactId = array_pop($keys);
632
633 $this->assertEquals(0, $contact['values'][$contactId]['api.address.create']['is_error'], "In line " . __LINE__ . " error message: " . CRM_Utils_Array::value('error_message', $contact['values'][$contactId]['api.address.create'])
634 );
635
636 $activityParams = array(
637 'source_contact_id' => $sourceContactId,
638 'assignee_contact_id' => $contactId,
639 'activity_type_id' => '1',
640 'subject' => 'Make-it-Happen Meeting',
641 'activity_date_time' => '20110316',
642 'duration' => '120',
643 'location' => 'Pensulvania',
644 'details' => 'a test activity',
645 'status_id' => '1',
646 'priority_id' => '1',
647 );
648 $activity = $this->callAPISuccess('activity', 'create', $activityParams);
649
650 $activityValues = array_pop($activity['values']);
651
652 // valid parameters for above profile
653 $profileParams = array(
654 'profile_id' => 26,
655 'contact_id' => $contactId,
656 'activity_id' => $activityValues['id'],
657 );
658
659 // expected result of above created profile
660 $expected = array(
661 'first_name' => 'abc1',
662 'last_name' => 'xyz1',
663 'email-Primary' => 'abc1.xyz1@yahoo.com',
664 'activity_subject' => 'Make-it-Happen Meeting',
665 'activity_details' => 'a test activity',
666 'activity_duration' => '120',
667 'activity_date_time_time' => '12:00AM',
668 'activity_date_time' => '03/16/2011',
669 'activity_status_id' => '1',
670 );
671
672 return array($profileParams, $expected);
673 }
674 }
675