4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
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' => '',
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',
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));
144 $expected = current($individual);
147 'profile_id' => array(25, 1, 'Billing'),
148 'contact_id' => $contactId,
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',
165 * check contact activity profile without activity id
167 function testContactActivityGetWithoutActivityId() {
168 list($params, $expected) = $this->_createContactWithActivity();
170 unset($params['activity_id']);
171 $result = $this->callAPIFailure('profile', 'get', $params,
172 'Mandatory key(s) missing from params array: activity_id');
176 * check contact activity profile wrong activity id
178 function testContactActivityGetWrongActivityId() {
179 list($params, $expected) = $this->_createContactWithActivity();
181 $params['activity_id'] = 100001;
182 $result = $this->callAPIFailure('profile', 'get', $params,
183 'Invalid Activity Id (aid).');
187 * check contact activity profile with wrong activity type
189 function testContactActivityGetWrongActivityType() {
190 //flush cache by calling with reset
191 $activityTypes = CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, TRUE, 'name', TRUE);
193 $sourceContactId = $this->householdCreate();
195 $activityparams = array(
196 'source_contact_id' => $sourceContactId,
197 'activity_type_id' => '2',
198 'subject' => 'Test activity',
199 'activity_date_time' => '20110316',
201 'location' => 'Pensulvania',
202 'details' => 'a test activity',
204 'priority_id' => '1',
207 $activity = $this->callAPISuccess('activity', 'create', $activityparams);
209 $activityValues = array_pop($activity['values']);
211 list($params, $expected) = $this->_createContactWithActivity();
213 $params['activity_id'] = $activityValues['id'];
214 $result = $this->callAPIFailure('profile', 'get', $params,
215 'This activity cannot be edited or viewed via this profile.'
220 * check contact activity profile with success
222 function testContactActivityGetSuccess() {
223 list($params, $expected) = $this->_createContactWithActivity();
225 $result = $this->callAPISuccess('profile', 'get', $params);
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}"
233 /////////////// test $this->callAPISuccess3_profile_set //////////////////
236 * check with no array
238 function testProfileSetNoArray() {
240 $result = $this->callAPIFailure('profile', 'set', $params);
241 $this->assertEquals($result['error_message'], 'Input variable `params` is not an array');
245 * check Without ProfileId
247 function testProfileSetWithoutProfileId() {
251 $result = $this->callAPIFailure('profile', 'set', $params,
252 'Mandatory key(s) missing from params array: profile_id'
257 * check with no invalid profile Id
259 function testProfileSetInvalidProfileId() {
262 'profile_id' => 1000,
264 $result = $this->callAPIFailure('profile', 'set', $params);
268 * check with missing required field in profile
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',
282 $params = array_merge(array('profile_id' => 25, 'contact_id' => $contactId),
286 $result = $this->callAPIFailure('profile', 'set', $params,
287 'Missing required parameters for profile id 25: email-Primary'
294 function testProfileSet() {
295 $pofileFieldValues = $this->_createIndividualContact();
296 current($pofileFieldValues);
297 $contactId = key($pofileFieldValues);
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',
308 $params = array_merge(array(
310 'contact_id' => $contactId,
313 $result = $this->callAPIAndDocument('profile', 'set', $params, __FUNCTION__
, __FILE__
);
317 'contact_id' => $contactId,
319 $profileDetails = $this->callAPISuccess('profile', 'get', $getParams);
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}"
328 * check contact activity profile without activity id
330 function testContactActivitySetWithoutActivityId() {
331 list($params, $expected) = $this->_createContactWithActivity();
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');
338 $this->quickCleanup(array('civicrm_uf_field', 'civicrm_uf_join', 'civicrm_uf_group', 'civicrm_custom_field', 'civicrm_custom_group', 'civicrm_contact'));
342 * check contact activity profile wrong activity id
344 function testContactActivitySetWrongActivityId() {
345 list($params, $expected) = $this->_createContactWithActivity();
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).');
352 $this->quickCleanup(array('civicrm_uf_field', 'civicrm_uf_join', 'civicrm_uf_group', 'civicrm_custom_field', 'civicrm_custom_group', 'civicrm_contact'));
356 * check contact activity profile with wrong activity type
358 function testContactActivitySetWrongActivityType() {
359 //flush cache by calling with reset
360 $activityTypes = CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, TRUE, 'name', TRUE);
362 $sourceContactId = $this->householdCreate();
364 $activityparams = array(
365 'source_contact_id' => $sourceContactId,
366 'activity_type_id' => '2',
367 'subject' => 'Test activity',
368 'activity_date_time' => '20110316',
370 'location' => 'Pensulvania',
371 'details' => 'a test activity',
373 'priority_id' => '1',
376 $activity = $this->callAPISuccess('activity', 'create', $activityparams);
378 $activityValues = array_pop($activity['values']);
380 list($params, $expected) = $this->_createContactWithActivity();
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.');
389 * check contact activity profile with success
391 function testContactActivitySetSuccess() {
392 list($params, $expected) = $this->_createContactWithActivity();
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',
404 $profileParams = array_merge($params, $updateParams);
405 $profile = $this->callAPISuccess('profile', 'set', $profileParams);
406 $result = $this->callAPISuccess('profile', 'get', $params);
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}"
415 * check profile apply Without ProfileId
417 function testProfileApplyWithoutProfileId() {
421 $result = $this->callAPIFailure('profile', 'apply', $params,
422 'Mandatory key(s) missing from params array: profile_id');
426 * check profile apply with no invalid profile Id
428 function testProfileApplyInvalidProfileId() {
431 'profile_id' => 1000,
433 $result = $this->callAPIFailure('profile', 'apply', $params);
439 function testProfileApply() {
440 $pofileFieldValues = $this->_createIndividualContact();
441 current($pofileFieldValues);
442 $contactId = key($pofileFieldValues);
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',
455 $result = $this->callAPIAndDocument('profile', 'apply', $params, __FUNCTION__
, __FILE__
);
457 // Expected field values
458 $expected['contact'] = array(
459 'contact_id' => $contactId,
460 'contact_type' => 'Individual',
461 'first_name' => 'abc2',
462 'last_name' => 'xyz2',
464 $expected['email'] = array(
465 'location_type_id' => 1,
467 'email' => 'abc2.xyz2@gmail.com',
470 $expected['phone'] = array(
471 'location_type_id' => 1,
473 'phone_type_id' => 1,
474 'phone' => '022 321 826',
476 $expected['address'] = array(
477 'location_type_id' => 1,
479 'country_id' => 1013,
480 'state_province_id' => 1000,
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}"
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})"
499 * Helper function to create an Individual with address/email/phone info. Import UF Group and UF Fields
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,
510 'name' => 'Saint Helier St',
512 'country' => 'United States',
513 'state_province' => 'Michigan',
514 'supplemental_address_1' => 'Hallmark Ct',
515 'supplemental_address_2' => 'Jersey Village',
517 'api.phone.create' => array(
518 'location_type_id' => '1',
519 'phone' => '021 512 755',
520 'phone_type_id' => '1',
525 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
527 $keys = array_keys($contact['values']);
528 $contactId = array_pop($keys);
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'])
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'])
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"
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"
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',
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'
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');
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,
584 'name' => 'Saint Helier St',
586 'country' => 'United States',
587 'state_province' => 'Michigan',
588 'supplemental_address_1' => 'Hallmark Ct',
589 'supplemental_address_2' => 'Jersey Village',
593 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
595 $keys = array_keys($contact['values']);
596 $contactId = array_pop($keys);
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'])
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',
608 'location' => 'Pensulvania',
609 'details' => 'a test activity',
611 'priority_id' => '1',
613 $activity = $this->callAPISuccess('activity', 'create', $activityParams);
615 $activityValues = array_pop($activity['values']);
617 // valid parameters for above profile
618 $profileParams = array(
620 'contact_id' => $contactId,
621 'activity_id' => $activityValues['id'],
624 // expected result of above created profile
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',
637 return array($profileParams, $expected);