4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.4 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
30 * Include class definitions
32 require_once 'tests/phpunit/CiviTest/CiviUnitTestCase.php';
35 * Test APIv3 civicrm_profile_* functions
39 class api_v3_ProfileTest
extends CiviUnitTestCase
{
40 protected $_apiversion;
43 'name' => 'Profile Test',
44 'description' => 'Test all profile API methods.',
45 'group' => 'CiviCRM API Tests',
50 $this->_apiversion
= 3;
52 $config = CRM_Core_Config
::singleton();
53 $config->countryLimit
[1] = 1013;
54 $config->stateLimit
[1] = 1013;
59 $this->quickCleanup(array(
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)');
68 ////////////// test $this->callAPISuccess3_profile_get //////////////////
71 * check Without ProfileId
73 function testProfileGetWithoutProfileId() {
77 $result = $this->callAPIFailure('profile', 'get', $params,
78 'Mandatory key(s) missing from params array: profile_id'
83 * check with no invalid profile Id
85 function testProfileGetInvalidProfileId() {
90 $result = $this->callAPIFailure('profile', 'get', $params);
96 function testProfileGet() {
97 $pofileFieldValues = $this->_createIndividualContact();
98 $expected = current($pofileFieldValues);
99 $contactId = key($pofileFieldValues);
102 'contact_id' => $contactId,
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}"
111 function testProfileGetMultiple() {
112 $pofileFieldValues = $this->_createIndividualContact();
113 $expected = current($pofileFieldValues);
114 $contactId = key($pofileFieldValues);
116 'profile_id' => array(25, 1, 'Billing'),
117 'contact_id' => $contactId,
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}");
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',
141 function testProfileGetBillingUseIsBillingLocation() {
142 $individual = $this->_createIndividualContact();
143 $contactId = key($individual);
144 $this->callAPISuccess('address', 'create', array(
146 'street_address' => 'is billing st',
147 'location_type_id' => 2,
148 'contact_id' => $contactId,
151 $expected = current($individual);
154 'profile_id' => array(25, 1, 'Billing'),
155 'contact_id' => $contactId,
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']);
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));
180 $expected = current($individual);
183 'profile_id' => array(25, 1, 'Billing'),
184 'contact_id' => $contactId,
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' => '',
205 * check contact activity profile without activity id
207 function testContactActivityGetWithoutActivityId() {
208 list($params, $expected) = $this->_createContactWithActivity();
210 unset($params['activity_id']);
211 $result = $this->callAPIFailure('profile', 'get', $params,
212 'Mandatory key(s) missing from params array: activity_id');
216 * check contact activity profile wrong activity id
218 function testContactActivityGetWrongActivityId() {
219 list($params, $expected) = $this->_createContactWithActivity();
221 $params['activity_id'] = 100001;
222 $result = $this->callAPIFailure('profile', 'get', $params,
223 'Invalid Activity Id (aid).');
227 * check contact activity profile with wrong activity type
229 function testContactActivityGetWrongActivityType() {
230 //flush cache by calling with reset
231 $activityTypes = CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, TRUE, 'name', TRUE);
233 $sourceContactId = $this->householdCreate();
235 $activityparams = array(
236 'source_contact_id' => $sourceContactId,
237 'activity_type_id' => '2',
238 'subject' => 'Test activity',
239 'activity_date_time' => '20110316',
241 'location' => 'Pensulvania',
242 'details' => 'a test activity',
244 'priority_id' => '1',
247 $activity = $this->callAPISuccess('activity', 'create', $activityparams);
249 $activityValues = array_pop($activity['values']);
251 list($params, $expected) = $this->_createContactWithActivity();
253 $params['activity_id'] = $activityValues['id'];
254 $result = $this->callAPIFailure('profile', 'get', $params,
255 'This activity cannot be edited or viewed via this profile.'
260 * check contact activity profile with success
262 function testContactActivityGetSuccess() {
263 list($params, $expected) = $this->_createContactWithActivity();
265 $result = $this->callAPISuccess('profile', 'get', $params);
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}"
273 /////////////// test $this->callAPISuccess3_profile_set //////////////////
276 * check with no array
278 function testProfileSetNoArray() {
280 $result = $this->callAPIFailure('profile', 'set', $params);
281 $this->assertEquals($result['error_message'], 'Input variable `params` is not an array');
285 * check Without ProfileId
287 function testProfileSetWithoutProfileId() {
291 $result = $this->callAPIFailure('profile', 'set', $params,
292 'Mandatory key(s) missing from params array: profile_id'
297 * check with no invalid profile Id
299 function testProfileSetInvalidProfileId() {
302 'profile_id' => 1000,
304 $result = $this->callAPIFailure('profile', 'set', $params);
308 * check with missing required field in profile
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',
322 $params = array_merge(array('profile_id' => 25, 'contact_id' => $contactId),
326 $result = $this->callAPIFailure('profile', 'set', $params,
327 'Missing required parameters for profile id 25: email-Primary'
334 function testProfileSet() {
335 $pofileFieldValues = $this->_createIndividualContact();
336 current($pofileFieldValues);
337 $contactId = key($pofileFieldValues);
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',
348 $params = array_merge(array(
350 'contact_id' => $contactId,
353 $result = $this->callAPIAndDocument('profile', 'set', $params, __FUNCTION__
, __FILE__
);
357 'contact_id' => $contactId,
359 $profileDetails = $this->callAPISuccess('profile', 'get', $getParams);
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}"
368 * check contact activity profile without activity id
370 function testContactActivitySetWithoutActivityId() {
371 list($params, $expected) = $this->_createContactWithActivity();
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');
378 $this->quickCleanup(array('civicrm_uf_field', 'civicrm_uf_join', 'civicrm_uf_group', 'civicrm_custom_field', 'civicrm_custom_group', 'civicrm_contact'));
382 * check contact activity profile wrong activity id
384 function testContactActivitySetWrongActivityId() {
385 list($params, $expected) = $this->_createContactWithActivity();
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).');
392 $this->quickCleanup(array('civicrm_uf_field', 'civicrm_uf_join', 'civicrm_uf_group', 'civicrm_custom_field', 'civicrm_custom_group', 'civicrm_contact'));
396 * check contact activity profile with wrong activity type
398 function testContactActivitySetWrongActivityType() {
399 //flush cache by calling with reset
400 $activityTypes = CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, TRUE, 'name', TRUE);
402 $sourceContactId = $this->householdCreate();
404 $activityparams = array(
405 'source_contact_id' => $sourceContactId,
406 'activity_type_id' => '2',
407 'subject' => 'Test activity',
408 'activity_date_time' => '20110316',
410 'location' => 'Pensulvania',
411 'details' => 'a test activity',
413 'priority_id' => '1',
416 $activity = $this->callAPISuccess('activity', 'create', $activityparams);
418 $activityValues = array_pop($activity['values']);
420 list($params, $expected) = $this->_createContactWithActivity();
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.');
429 * check contact activity profile with success
431 function testContactActivitySetSuccess() {
432 list($params, $expected) = $this->_createContactWithActivity();
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',
444 $profileParams = array_merge($params, $updateParams);
445 $profile = $this->callAPISuccess('profile', 'set', $profileParams);
446 $result = $this->callAPISuccess('profile', 'get', $params);
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}"
455 * check profile apply Without ProfileId
457 function testProfileApplyWithoutProfileId() {
461 $result = $this->callAPIFailure('profile', 'apply', $params,
462 'Mandatory key(s) missing from params array: profile_id');
466 * check profile apply with no invalid profile Id
468 function testProfileApplyInvalidProfileId() {
471 'profile_id' => 1000,
473 $result = $this->callAPIFailure('profile', 'apply', $params);
479 function testProfileApply() {
480 $pofileFieldValues = $this->_createIndividualContact();
481 current($pofileFieldValues);
482 $contactId = key($pofileFieldValues);
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',
495 $result = $this->callAPIAndDocument('profile', 'apply', $params, __FUNCTION__
, __FILE__
);
497 // Expected field values
498 $expected['contact'] = array(
499 'contact_id' => $contactId,
500 'contact_type' => 'Individual',
501 'first_name' => 'abc2',
502 'last_name' => 'xyz2',
504 $expected['email'] = array(
505 'location_type_id' => 1,
507 'email' => 'abc2.xyz2@gmail.com',
510 $expected['phone'] = array(
511 'location_type_id' => 1,
513 'phone_type_id' => 1,
514 'phone' => '022 321 826',
516 $expected['address'] = array(
517 'location_type_id' => 1,
519 'country_id' => 1013,
520 'state_province_id' => 1000,
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}"
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})"
539 * Helper function to create an Individual with address/email/phone info. Import UF Group and UF Fields
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,
549 'street_address' => '5 Saint Helier St',
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',
559 'api.phone.create' => array(
560 'location_type_id' => '1',
561 'phone' => '021 512 755',
562 'phone_type_id' => '1',
568 $contactID = $this->individualCreate($contactParams);
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"
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"
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',
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'
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');
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,
619 'name' => 'Saint Helier St',
621 'country' => 'United States',
622 'state_province' => 'Michigan',
623 'supplemental_address_1' => 'Hallmark Ct',
624 'supplemental_address_2' => 'Jersey Village',
628 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
630 $keys = array_keys($contact['values']);
631 $contactId = array_pop($keys);
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'])
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',
643 'location' => 'Pensulvania',
644 'details' => 'a test activity',
646 'priority_id' => '1',
648 $activity = $this->callAPISuccess('activity', 'create', $activityParams);
650 $activityValues = array_pop($activity['values']);
652 // valid parameters for above profile
653 $profileParams = array(
655 'contact_id' => $contactId,
656 'activity_id' => $activityValues['id'],
659 // expected result of above created profile
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',
672 return array($profileParams, $expected);