Merge branch 'crm-13089-CRM_Core_BAO_CustomField_overrides_name' of git://github...
[civicrm-core.git] / tests / phpunit / api / v3 / ProfileTest.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 * 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' => '',
129 'billing_last_name' => 'xyz1',
130 'billing_street_address-5' => '',
131 'billing_city-5' => '',
132 'billing_state_province_id-5' => '',
133 'billing_country_id-5' => '',
134 'billing-email-5' => 'abc1.xyz1@yahoo.com',
135 ));
136 }
137
138 function testProfileGetMultipleHasBillingLocation() {
139 $individual = $this->_createIndividualContact();
140 $contactId = key($individual);
141 $this->callAPISuccess('address', 'create', array('contact_id' => $contactId , 'street_address' => '25 Big Street', 'city' => 'big city', 'location_type_id' => 5));
142 $this->callAPISuccess('email', 'create', array('contact_id' => $contactId , 'email' => 'big@once.com', 'location_type_id' => 2, 'is_billing' => 1));
143
144 $expected = current($individual);
145
146 $params = array(
147 'profile_id' => array(25, 1, 'Billing'),
148 'contact_id' => $contactId,
149 );
150
151 $result = $this->callAPISuccess('profile', 'get', $params, __FUNCTION__, __FILE__);
152 $this->assertEquals('abc1', $result['values'][1]['first_name']);
153 $this->assertEquals($result['values']['Billing'], array(
154 'billing_first_name' => 'abc1',
155 'billing_middle_name' => '',
156 'billing_last_name' => 'xyz1',
157 'billing_street_address-5' => '25 Big Street',
158 'billing_city-5' => 'big city',
159 'billing_state_province_id-5' => '',
160 'billing_country_id-5' => '',
161 'billing-email-5' => 'big@once.com',
162 ));
163 }
164 /**
165 * check contact activity profile without activity id
166 */
167 function testContactActivityGetWithoutActivityId() {
168 list($params, $expected) = $this->_createContactWithActivity();
169
170 unset($params['activity_id']);
171 $result = $this->callAPIFailure('profile', 'get', $params,
172 'Mandatory key(s) missing from params array: activity_id');
173 }
174
175 /**
176 * check contact activity profile wrong activity id
177 */
178 function testContactActivityGetWrongActivityId() {
179 list($params, $expected) = $this->_createContactWithActivity();
180
181 $params['activity_id'] = 100001;
182 $result = $this->callAPIFailure('profile', 'get', $params,
183 'Invalid Activity Id (aid).');
184 }
185
186 /*
187 * check contact activity profile with wrong activity type
188 */
189 function testContactActivityGetWrongActivityType() {
190 //flush cache by calling with reset
191 $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, TRUE, 'name', TRUE);
192
193 $sourceContactId = $this->householdCreate();
194
195 $activityparams = array(
196 'source_contact_id' => $sourceContactId,
197 'activity_type_id' => '2',
198 'subject' => 'Test activity',
199 'activity_date_time' => '20110316',
200 'duration' => '120',
201 'location' => 'Pensulvania',
202 'details' => 'a test activity',
203 'status_id' => '1',
204 'priority_id' => '1',
205 );
206
207 $activity = $this->callAPISuccess('activity', 'create', $activityparams);
208
209 $activityValues = array_pop($activity['values']);
210
211 list($params, $expected) = $this->_createContactWithActivity();
212
213 $params['activity_id'] = $activityValues['id'];
214 $result = $this->callAPIFailure('profile', 'get', $params,
215 'This activity cannot be edited or viewed via this profile.'
216 );
217 }
218
219 /*
220 * check contact activity profile with success
221 */
222 function testContactActivityGetSuccess() {
223 list($params, $expected) = $this->_createContactWithActivity();
224
225 $result = $this->callAPISuccess('profile', 'get', $params);
226
227 foreach ($expected as $profileField => $value) {
228 $this->assertEquals($value, CRM_Utils_Array::value($profileField, $result['values']), "In line " . __LINE__ . " error message: " . "missing/mismatching value for {$profileField}"
229 );
230 }
231 }
232
233 /////////////// test $this->callAPISuccess3_profile_set //////////////////
234
235 /**
236 * check with no array
237 */
238 function testProfileSetNoArray() {
239 $params = NULL;
240 $result = $this->callAPIFailure('profile', 'set', $params);
241 $this->assertEquals($result['error_message'], 'Input variable `params` is not an array');
242 }
243
244 /**
245 * check Without ProfileId
246 */
247 function testProfileSetWithoutProfileId() {
248 $params = array(
249 'contact_id' => 1,
250 );
251 $result = $this->callAPIFailure('profile', 'set', $params,
252 'Mandatory key(s) missing from params array: profile_id'
253 );
254 }
255
256 /**
257 * check with no invalid profile Id
258 */
259 function testProfileSetInvalidProfileId() {
260 $params = array(
261 'contact_id' => 1,
262 'profile_id' => 1000,
263 );
264 $result = $this->callAPIFailure('profile', 'set', $params);
265 }
266
267 /**
268 * check with missing required field in profile
269 */
270 function testProfileSetCheckProfileRequired() {
271 $pofileFieldValues = $this->_createIndividualContact();
272 current($pofileFieldValues);
273 $contactId = key($pofileFieldValues);
274 $updateParams = array(
275 'first_name' => 'abc2',
276 'last_name' => 'xyz2',
277 'phone-1-1' => '022 321 826',
278 'country-1' => '1013',
279 'state_province-1' => '1000',
280 );
281
282 $params = array_merge(array('profile_id' => 25, 'contact_id' => $contactId),
283 $updateParams
284 );
285
286 $result = $this->callAPIFailure('profile', 'set', $params,
287 'Missing required parameters for profile id 25: email-Primary'
288 );
289 }
290
291 /**
292 * check with success
293 */
294 function testProfileSet() {
295 $pofileFieldValues = $this->_createIndividualContact();
296 current($pofileFieldValues);
297 $contactId = key($pofileFieldValues);
298
299 $updateParams = array(
300 'first_name' => 'abc2',
301 'last_name' => 'xyz2',
302 'email-Primary' => 'abc2.xyz2@gmail.com',
303 'phone-1-1' => '022 321 826',
304 'country-1' => '1013',
305 'state_province-1' => '1000',
306 );
307
308 $params = array_merge(array(
309 'profile_id' => 25,
310 'contact_id' => $contactId,
311 ), $updateParams);
312
313 $result = $this->callAPIAndDocument('profile', 'set', $params, __FUNCTION__, __FILE__);
314
315 $getParams = array(
316 'profile_id' => 25,
317 'contact_id' => $contactId,
318 );
319 $profileDetails = $this->callAPISuccess('profile', 'get', $getParams);
320
321 foreach ($updateParams as $profileField => $value) {
322 $this->assertEquals($value, CRM_Utils_Array::value($profileField, $profileDetails['values']), "In line " . __LINE__ . " error message: " . "missing/mismatching value for {$profileField}"
323 );
324 }
325 }
326
327 /*
328 * check contact activity profile without activity id
329 */
330 function testContactActivitySetWithoutActivityId() {
331 list($params, $expected) = $this->_createContactWithActivity();
332
333 $params = array_merge($params, $expected);
334 unset($params['activity_id']);
335 $result = $this->callAPIFailure('profile', 'set', $params);
336 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: activity_id');
337
338 $this->quickCleanup(array('civicrm_uf_field', 'civicrm_uf_join', 'civicrm_uf_group', 'civicrm_custom_field', 'civicrm_custom_group', 'civicrm_contact'));
339 }
340
341 /*
342 * check contact activity profile wrong activity id
343 */
344 function testContactActivitySetWrongActivityId() {
345 list($params, $expected) = $this->_createContactWithActivity();
346
347 $params = array_merge($params, $expected);
348 $params['activity_id'] = 100001;
349 $result = $this->callAPIFailure('profile', 'set', $params);
350 $this->assertEquals($result['error_message'], 'Invalid Activity Id (aid).');
351
352 $this->quickCleanup(array('civicrm_uf_field', 'civicrm_uf_join', 'civicrm_uf_group', 'civicrm_custom_field', 'civicrm_custom_group', 'civicrm_contact'));
353 }
354
355 /*
356 * check contact activity profile with wrong activity type
357 */
358 function testContactActivitySetWrongActivityType() {
359 //flush cache by calling with reset
360 $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, TRUE, 'name', TRUE);
361
362 $sourceContactId = $this->householdCreate();
363
364 $activityparams = array(
365 'source_contact_id' => $sourceContactId,
366 'activity_type_id' => '2',
367 'subject' => 'Test activity',
368 'activity_date_time' => '20110316',
369 'duration' => '120',
370 'location' => 'Pensulvania',
371 'details' => 'a test activity',
372 'status_id' => '1',
373 'priority_id' => '1',
374 );
375
376 $activity = $this->callAPISuccess('activity', 'create', $activityparams);
377
378 $activityValues = array_pop($activity['values']);
379
380 list($params, $expected) = $this->_createContactWithActivity();
381
382 $params = array_merge($params, $expected);
383 $params['activity_id'] = $activityValues['id'];
384 $result = $this->callAPIFailure('profile', 'set', $params,
385 'This activity cannot be edited or viewed via this profile.');
386 }
387
388 /*
389 * check contact activity profile with success
390 */
391 function testContactActivitySetSuccess() {
392 list($params, $expected) = $this->_createContactWithActivity();
393
394 $updateParams = array(
395 'first_name' => 'abc2',
396 'last_name' => 'xyz2',
397 'email-Primary' => 'abc2.xyz2@yahoo.com',
398 'activity_subject' => 'Test Meeting',
399 'activity_details' => 'a test activity details',
400 'activity_duration' => '100',
401 'activity_date_time' => '03/08/2010',
402 'activity_status_id' => '2',
403 );
404 $profileParams = array_merge($params, $updateParams);
405 $profile = $this->callAPISuccess('profile', 'set', $profileParams);
406 $result = $this->callAPISuccess('profile', 'get', $params);
407
408 foreach ($updateParams as $profileField => $value) {
409 $this->assertEquals($value, CRM_Utils_Array::value($profileField, $result['values']), "In line " . __LINE__ . " error message: " . "missing/mismatching value for {$profileField}"
410 );
411 }
412 }
413
414 /**
415 * check profile apply Without ProfileId
416 */
417 function testProfileApplyWithoutProfileId() {
418 $params = array(
419 'contact_id' => 1,
420 );
421 $result = $this->callAPIFailure('profile', 'apply', $params,
422 'Mandatory key(s) missing from params array: profile_id');
423 }
424
425 /**
426 * check profile apply with no invalid profile Id
427 */
428 function testProfileApplyInvalidProfileId() {
429 $params = array(
430 'contact_id' => 1,
431 'profile_id' => 1000,
432 );
433 $result = $this->callAPIFailure('profile', 'apply', $params);
434 }
435
436 /**
437 * check with success
438 */
439 function testProfileApply() {
440 $pofileFieldValues = $this->_createIndividualContact();
441 current($pofileFieldValues);
442 $contactId = key($pofileFieldValues);
443
444 $params = array(
445 'profile_id' => 25,
446 'contact_id' => $contactId,
447 'first_name' => 'abc2',
448 'last_name' => 'xyz2',
449 'email-Primary' => 'abc2.xyz2@gmail.com',
450 'phone-1-1' => '022 321 826',
451 'country-1' => '1013',
452 'state_province-1' => '1000',
453 );
454
455 $result = $this->callAPIAndDocument('profile', 'apply', $params, __FUNCTION__, __FILE__);
456
457 // Expected field values
458 $expected['contact'] = array(
459 'contact_id' => $contactId,
460 'contact_type' => 'Individual',
461 'first_name' => 'abc2',
462 'last_name' => 'xyz2',
463 );
464 $expected['email'] = array(
465 'location_type_id' => 1,
466 'is_primary' => 1,
467 'email' => 'abc2.xyz2@gmail.com',
468 );
469
470 $expected['phone'] = array(
471 'location_type_id' => 1,
472 'is_primary' => 1,
473 'phone_type_id' => 1,
474 'phone' => '022 321 826',
475 );
476 $expected['address'] = array(
477 'location_type_id' => 1,
478 'is_primary' => 1,
479 'country_id' => 1013,
480 'state_province_id' => 1000,
481 );
482
483 foreach ($expected['contact'] as $field => $value) {
484 $this->assertEquals($value, CRM_Utils_Array::value($field, $result['values']), "In line " . __LINE__ . " error message: " . "missing/mismatching value for {$field}"
485 );
486 }
487
488 foreach (array(
489 'email', 'phone', 'address') as $fieldType) {
490 $typeValues = array_pop($result['values'][$fieldType]);
491 foreach ($expected[$fieldType] as $field => $value) {
492 $this->assertEquals($value, CRM_Utils_Array::value($field, $typeValues), "In line " . __LINE__ . " error message: " . "missing/mismatching value for {$field} ({$fieldType})"
493 );
494 }
495 }
496 }
497
498 /*
499 * Helper function to create an Individual with address/email/phone info. Import UF Group and UF Fields
500 */
501 function _createIndividualContact() {
502 $contactParams = array(
503 'first_name' => 'abc1',
504 'last_name' => 'xyz1',
505 'contact_type' => 'Individual',
506 'email' => 'abc1.xyz1@yahoo.com',
507 'api.address.create' => array(
508 'location_type_id' => 1,
509 'is_primary' => 1,
510 'name' => 'Saint Helier St',
511 'county' => 'Marin',
512 'country' => 'United States',
513 'state_province' => 'Michigan',
514 'supplemental_address_1' => 'Hallmark Ct',
515 'supplemental_address_2' => 'Jersey Village',
516 ),
517 'api.phone.create' => array(
518 'location_type_id' => '1',
519 'phone' => '021 512 755',
520 'phone_type_id' => '1',
521 'is_primary' => '1',
522 ),
523 );
524
525 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
526
527 $keys = array_keys($contact['values']);
528 $contactId = array_pop($keys);
529
530 $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'])
531 );
532 $this->assertEquals(0, $contact['values'][$contactId]['api.phone.create']['is_error'], "In line " . __LINE__ . " error message: " . CRM_Utils_Array::value('error_message', $contact['values'][$contactId]['api.phone.create'])
533 );
534
535 // Create new profile having group_type: Contact,Individual
536 $op = new PHPUnit_Extensions_Database_Operation_Insert();
537 $op->execute($this->_dbconn,
538 new PHPUnit_Extensions_Database_DataSet_XMLDataSet(
539 dirname(__FILE__) . "/dataset/uf_group_25.xml"
540 )
541 );
542 // Create Contact + Idividual fields for profile
543 $op = new PHPUnit_Extensions_Database_Operation_Insert();
544 $op->execute($this->_dbconn,
545 new PHPUnit_Extensions_Database_DataSet_XMLDataSet(
546 dirname(__FILE__) . "/dataset/uf_field_uf_group_25.xml"
547 )
548 );
549
550
551 // expected result of above created profile with contact Id $contactId
552 $profileData[$contactId] = array(
553 'first_name' => 'abc1',
554 'last_name' => 'xyz1',
555 'email-Primary' => 'abc1.xyz1@yahoo.com',
556 'phone-1-1' => '021 512 755',
557 'country-1' => '1228',
558 'state_province-1' => '1021',
559 );
560
561 return $profileData;
562 }
563
564 function _createContactWithActivity() {
565 // @TODO: Create profile with custom fields
566 $op = new PHPUnit_Extensions_Database_Operation_Insert();
567 $op->execute($this->_dbconn,
568 new PHPUnit_Extensions_Database_DataSet_FlatXMLDataSet(
569 dirname(__FILE__) . '/dataset/uf_group_contact_activity_26.xml'
570 )
571 );
572 // hack: xml data set do not accept \ 1 (CRM_Core_DAO::VALUE_SEPARATOR)
573 CRM_Core_DAO::setFieldValue('CRM_Core_DAO_UFGroup', '26', 'group_type', 'Individual,Contact,Activity' . CRM_Core_DAO::VALUE_SEPARATOR . 'ActivityType:1');
574
575 $sourceContactId = $this->individualCreate();
576 $contactParams = array(
577 'first_name' => 'abc1',
578 'last_name' => 'xyz1',
579 'contact_type' => 'Individual',
580 'email' => 'abc1.xyz1@yahoo.com',
581 'api.address.create' => array(
582 'location_type_id' => 1,
583 'is_primary' => 1,
584 'name' => 'Saint Helier St',
585 'county' => 'Marin',
586 'country' => 'United States',
587 'state_province' => 'Michigan',
588 'supplemental_address_1' => 'Hallmark Ct',
589 'supplemental_address_2' => 'Jersey Village',
590 ),
591 );
592
593 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
594
595 $keys = array_keys($contact['values']);
596 $contactId = array_pop($keys);
597
598 $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'])
599 );
600
601 $activityParams = array(
602 'source_contact_id' => $sourceContactId,
603 'assignee_contact_id' => $contactId,
604 'activity_type_id' => '1',
605 'subject' => 'Make-it-Happen Meeting',
606 'activity_date_time' => '20110316',
607 'duration' => '120',
608 'location' => 'Pensulvania',
609 'details' => 'a test activity',
610 'status_id' => '1',
611 'priority_id' => '1',
612 );
613 $activity = $this->callAPISuccess('activity', 'create', $activityParams);
614
615 $activityValues = array_pop($activity['values']);
616
617 // valid parameters for above profile
618 $profileParams = array(
619 'profile_id' => 26,
620 'contact_id' => $contactId,
621 'activity_id' => $activityValues['id'],
622 );
623
624 // expected result of above created profile
625 $expected = array(
626 'first_name' => 'abc1',
627 'last_name' => 'xyz1',
628 'email-Primary' => 'abc1.xyz1@yahoo.com',
629 'activity_subject' => 'Make-it-Happen Meeting',
630 'activity_details' => 'a test activity',
631 'activity_duration' => '120',
632 'activity_date_time_time' => '12:00AM',
633 'activity_date_time' => '03/16/2011',
634 'activity_status_id' => '1',
635 );
636
637 return array($profileParams, $expected);
638 }
639 }
640