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;
58 $this->quickCleanup(array(
62 'civicrm_custom_field',
63 'civicrm_custom_group',
70 ////////////// test civicrm_api3_profile_get //////////////////
73 * check Without ProfileId
75 function testProfileGetWithoutProfileId() {
80 $result = civicrm_api('profile', 'get', $params);
81 $this->assertEquals($result['is_error'], 1);
82 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: profile_id');
86 * check with no invalid profile Id
88 function testProfileGetInvalidProfileId() {
94 $result = civicrm_api('profile', 'get', $params);
95 $this->assertEquals($result['is_error'], 1);
101 function testProfileGet() {
102 $pofileFieldValues = $this->_createIndividualContact();
103 $expected = current($pofileFieldValues);
104 $contactId = key($pofileFieldValues);
107 'contact_id' => $contactId,
111 $result = civicrm_api('profile', 'get', $params);
112 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
114 $this->assertEquals(0, $result['is_error'], "In line " . __LINE__
. " error message: " . CRM_Utils_Array
::value('error_message', $result)
117 foreach ($expected as $profileField => $value) {
118 $this->assertEquals($value, CRM_Utils_Array
::value($profileField, $result['values']), "In line " . __LINE__
. " error message: " . "missing/mismatching value for {$profileField}"
122 $this->quickCleanup(array('civicrm_uf_field', 'civicrm_uf_join', 'civicrm_uf_group', 'civicrm_contact'));
126 * check contact activity profile without activity id
128 function testContactActivityGetWithoutActivityId() {
129 list($params, $expected) = $this->_createContactWithActivity();
131 unset($params['activity_id']);
132 $result = civicrm_api('profile', 'get', $params);
134 $this->assertEquals($result['is_error'], 1);
135 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: activity_id');
139 * check contact activity profile wrong activity id
141 function testContactActivityGetWrongActivityId() {
142 list($params, $expected) = $this->_createContactWithActivity();
144 $params['activity_id'] = 100001;
145 $result = civicrm_api('profile', 'get', $params);
147 $this->assertEquals($result['is_error'], 1);
148 $this->assertEquals($result['error_message'], 'Invalid Activity Id (aid).');
150 $this->quickCleanup(array('civicrm_uf_field', 'civicrm_uf_join', 'civicrm_uf_group', 'civicrm_custom_field', 'civicrm_custom_group', 'civicrm_contact'));
154 * check contact activity profile with wrong activity type
156 function testContactActivityGetWrongActivityType() {
157 //flush cache by calling with reset
158 $activityTypes = CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, TRUE, 'name', TRUE);
160 $sourceContactId = $this->householdCreate();
162 $activityparams = array(
163 'source_contact_id' => $sourceContactId,
164 'activity_type_id' => '2',
165 'subject' => 'Test activity',
166 'activity_date_time' => '20110316',
168 'location' => 'Pensulvania',
169 'details' => 'a test activity',
172 'priority_id' => '1',
175 $activity = civicrm_api('activity', 'create', $activityparams);
176 $this->assertEquals(0, $activity['is_error'], "In line " . __LINE__
. " error message: " . CRM_Utils_Array
::value('error_message', $activity)
179 $activityValues = array_pop($activity['values']);
181 list($params, $expected) = $this->_createContactWithActivity();
183 $params['activity_id'] = $activityValues['id'];
184 $result = civicrm_api('profile', 'get', $params);
186 $this->assertEquals($result['is_error'], 1);
187 $this->assertEquals($result['error_message'], 'This activity cannot be edited or viewed via this profile.');
189 $this->quickCleanup(array('civicrm_uf_field', 'civicrm_uf_join', 'civicrm_uf_group', 'civicrm_custom_field', 'civicrm_custom_group', 'civicrm_contact'));
193 * check contact activity profile with success
195 function testContactActivityGetSuccess() {
196 list($params, $expected) = $this->_createContactWithActivity();
198 $result = civicrm_api('profile', 'get', $params);
200 $this->assertEquals(0, $result['is_error'], "In line " . __LINE__
. " error message: " . CRM_Utils_Array
::value('error_message', $result)
203 foreach ($expected as $profileField => $value) {
204 $this->assertEquals($value, CRM_Utils_Array
::value($profileField, $result['values']), "In line " . __LINE__
. " error message: " . "missing/mismatching value for {$profileField}"
208 $this->quickCleanup(array('civicrm_uf_field', 'civicrm_uf_join', 'civicrm_uf_group', 'civicrm_custom_field', 'civicrm_custom_group', 'civicrm_contact'));
211 /////////////// test civicrm_api3_profile_set //////////////////
214 * check with no array
216 function testProfileSetNoArray() {
218 $result = civicrm_api('profile', 'set', $params);
219 $this->assertEquals($result['is_error'], 1);
220 $this->assertEquals($result['error_message'], 'Input variable `params` is not an array');
224 * check Without ProfileId
226 function testProfileSetWithoutProfileId() {
231 $result = civicrm_api('profile', 'set', $params);
232 $this->assertEquals($result['is_error'], 1);
233 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: profile_id');
237 * check with no invalid profile Id
239 function testProfileSetInvalidProfileId() {
242 'profile_id' => 1000,
245 $result = civicrm_api('profile', 'set', $params);
246 $this->assertEquals($result['is_error'], 1);
250 * check with missing required field in profile
252 function testProfileSetCheckProfileRequired() {
253 $pofileFieldValues = $this->_createIndividualContact();
254 current($pofileFieldValues);
255 $contactId = key($pofileFieldValues);
256 $updateParams = array(
257 'first_name' => 'abc2',
258 'last_name' => 'xyz2',
259 'phone-1-1' => '022 321 826',
260 'country-1' => '1013',
261 'state_province-1' => '1000',
264 $params = array_merge(array('profile_id' => 25, 'contact_id' => $contactId, 'version' => 3),
268 $result = civicrm_api('profile', 'set', $params);
270 $this->assertEquals($result['is_error'], 1);
271 $this->assertEquals($result['error_message'], 'Missing required parameters for profile id 25: email-Primary');
273 $this->quickCleanup(array('civicrm_uf_field', 'civicrm_uf_join', 'civicrm_uf_group', 'civicrm_contact'));
279 function testProfileSet() {
280 $pofileFieldValues = $this->_createIndividualContact();
281 current($pofileFieldValues);
282 $contactId = key($pofileFieldValues);
284 $updateParams = array(
285 'first_name' => 'abc2',
286 'last_name' => 'xyz2',
287 'email-Primary' => 'abc2.xyz2@gmail.com',
288 'phone-1-1' => '022 321 826',
289 'country-1' => '1013',
290 'state_province-1' => '1000',
293 $params = array_merge(array(
295 'contact_id' => $contactId,
299 $result = civicrm_api('profile', 'set', $params);
300 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
302 $this->assertEquals(0, $result['is_error'], "In line " . __LINE__
. " error message: " . CRM_Utils_Array
::value('error_message', $result)
307 'contact_id' => $contactId,
310 $profileDetails = civicrm_api('profile', 'get', $getParams);
311 $this->assertEquals(0, $profileDetails['is_error'], "In line " . __LINE__
. " error message: " . CRM_Utils_Array
::value('error_message', $profileDetails)
314 foreach ($updateParams as $profileField => $value) {
315 $this->assertEquals($value, CRM_Utils_Array
::value($profileField, $profileDetails['values']), "In line " . __LINE__
. " error message: " . "missing/mismatching value for {$profileField}"
321 * check contact activity profile without activity id
323 function testContactActivitySetWithoutActivityId() {
324 list($params, $expected) = $this->_createContactWithActivity();
326 $params = array_merge($params, $expected);
327 unset($params['activity_id']);
328 $result = civicrm_api('profile', 'set', $params);
330 $this->assertEquals($result['is_error'], 1);
331 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: activity_id');
333 $this->quickCleanup(array('civicrm_uf_field', 'civicrm_uf_join', 'civicrm_uf_group', 'civicrm_custom_field', 'civicrm_custom_group', 'civicrm_contact'));
337 * check contact activity profile wrong activity id
339 function testContactActivitySetWrongActivityId() {
340 list($params, $expected) = $this->_createContactWithActivity();
342 $params = array_merge($params, $expected);
343 $params['activity_id'] = 100001;
344 $result = civicrm_api('profile', 'set', $params);
346 $this->assertEquals($result['is_error'], 1);
347 $this->assertEquals($result['error_message'], 'Invalid Activity Id (aid).');
349 $this->quickCleanup(array('civicrm_uf_field', 'civicrm_uf_join', 'civicrm_uf_group', 'civicrm_custom_field', 'civicrm_custom_group', 'civicrm_contact'));
353 * check contact activity profile with wrong activity type
355 function testContactActivitySetWrongActivityType() {
356 //flush cache by calling with reset
357 $activityTypes = CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, TRUE, 'name', TRUE);
359 $sourceContactId = $this->householdCreate();
361 $activityparams = array(
362 'source_contact_id' => $sourceContactId,
363 'activity_type_id' => '2',
364 'subject' => 'Test activity',
365 'activity_date_time' => '20110316',
367 'location' => 'Pensulvania',
368 'details' => 'a test activity',
371 'priority_id' => '1',
374 $activity = civicrm_api('activity', 'create', $activityparams);
375 $this->assertEquals(0, $activity['is_error'], "In line " . __LINE__
. " error message: " . CRM_Utils_Array
::value('error_message', $activity)
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 = civicrm_api('profile', 'set', $params);
386 $this->assertEquals($result['is_error'], 1);
387 $this->assertEquals($result['error_message'], 'This activity cannot be edited or viewed via this profile.');
389 $this->quickCleanup(array('civicrm_uf_field', 'civicrm_uf_join', 'civicrm_uf_group', 'civicrm_custom_field', 'civicrm_custom_group', 'civicrm_contact'));
393 * check contact activity profile with success
395 function testContactActivitySetSuccess() {
396 list($params, $expected) = $this->_createContactWithActivity();
398 $updateParams = array(
399 'first_name' => 'abc2',
400 'last_name' => 'xyz2',
401 'email-Primary' => 'abc2.xyz2@yahoo.com',
402 'activity_subject' => 'Test Meeting',
403 'activity_details' => 'a test activity details',
404 'activity_duration' => '100',
405 'activity_date_time' => '03/08/2010',
406 'activity_status_id' => '2',
408 $profileParams = array_merge($params, $updateParams);
409 $profile = civicrm_api('profile', 'set', $profileParams);
410 $result = civicrm_api('profile', 'get', $params);
411 $this->assertEquals(0, $result['is_error'], "In line " . __LINE__
. " error message: " . CRM_Utils_Array
::value('error_message', $result)
414 foreach ($updateParams as $profileField => $value) {
415 $this->assertEquals($value, CRM_Utils_Array
::value($profileField, $result['values']), "In line " . __LINE__
. " error message: " . "missing/mismatching value for {$profileField}"
419 $this->quickCleanup(array(
423 'civicrm_custom_field',
424 'civicrm_custom_group',
431 /////////////// test civicrm_api3_profile_apply //////////////////
434 * check Without ProfileId
436 function testProfileApplyWithoutProfileId() {
441 $result = civicrm_api('profile', 'apply', $params);
442 $this->assertEquals($result['is_error'], 1);
443 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: profile_id');
447 * check with no invalid profile Id
449 function testProfileApplyInvalidProfileId() {
452 'profile_id' => 1000,
455 $result = civicrm_api('profile', 'apply', $params);
456 $this->assertEquals($result['is_error'], 1);
462 function testProfileApply() {
463 $pofileFieldValues = $this->_createIndividualContact();
464 current($pofileFieldValues);
465 $contactId = key($pofileFieldValues);
469 'contact_id' => $contactId,
471 'first_name' => 'abc2',
472 'last_name' => 'xyz2',
473 'email-Primary' => 'abc2.xyz2@gmail.com',
474 'phone-1-1' => '022 321 826',
475 'country-1' => '1013',
476 'state_province-1' => '1000',
479 $result = civicrm_api('profile', 'apply', $params);
480 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
482 $this->assertEquals(0, $result['is_error'], "In line " . __LINE__
. " error message: " . CRM_Utils_Array
::value('error_message', $result)
485 // Expected field values
486 $expected['contact'] = array(
487 'contact_id' => $contactId,
488 'contact_type' => 'Individual',
489 'first_name' => 'abc2',
490 'last_name' => 'xyz2',
492 $expected['email'] = array(
493 'location_type_id' => 1,
495 'email' => 'abc2.xyz2@gmail.com',
498 $expected['phone'] = array(
499 'location_type_id' => 1,
501 'phone_type_id' => 1,
502 'phone' => '022 321 826',
504 $expected['address'] = array(
505 'location_type_id' => 1,
507 'country_id' => 1013,
508 'state_province_id' => 1000,
511 foreach ($expected['contact'] as $field => $value) {
512 $this->assertEquals($value, CRM_Utils_Array
::value($field, $result['values']), "In line " . __LINE__
. " error message: " . "missing/mismatching value for {$field}"
517 'email', 'phone', 'address') as $fieldType) {
518 $typeValues = array_pop($result['values'][$fieldType]);
519 foreach ($expected[$fieldType] as $field => $value) {
520 $this->assertEquals($value, CRM_Utils_Array
::value($field, $typeValues), "In line " . __LINE__
. " error message: " . "missing/mismatching value for {$field} ({$fieldType})"
527 * Helper function to create an Individual with address/email/phone info. Import UF Group and UF Fields
529 function _createIndividualContact() {
530 $contactParams = array(
531 'first_name' => 'abc1',
532 'last_name' => 'xyz1',
533 'contact_type' => 'Individual',
534 'email' => 'abc1.xyz1@yahoo.com',
536 'api.address.create' => array(
537 'location_type_id' => 1,
539 'name' => 'Saint Helier St',
541 'country' => 'United States',
542 'state_province' => 'Michigan',
543 'supplemental_address_1' => 'Hallmark Ct',
544 'supplemental_address_2' => 'Jersey Village',
546 'api.phone.create' => array(
547 'location_type_id' => '1',
548 'phone' => '021 512 755',
549 'phone_type_id' => '1',
554 $contact = civicrm_api('contact', 'create', $contactParams);
556 $this->assertEquals(0, $contact['is_error'], "In line " . __LINE__
. " error message: " . CRM_Utils_Array
::value('error_message', $contact)
559 $keys = array_keys($contact['values']);
560 $contactId = array_pop($keys);
562 $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'])
564 $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'])
567 // Create new profile having group_type: Contact,Individual
568 $op = new PHPUnit_Extensions_Database_Operation_Insert();
569 $op->execute($this->_dbconn
,
570 new PHPUnit_Extensions_Database_DataSet_XMLDataSet(
571 dirname(__FILE__
) . "/dataset/uf_group_25.xml"
574 // Create Contact + Idividual fields for profile
575 $op = new PHPUnit_Extensions_Database_Operation_Insert();
576 $op->execute($this->_dbconn
,
577 new PHPUnit_Extensions_Database_DataSet_XMLDataSet(
578 dirname(__FILE__
) . "/dataset/uf_field_uf_group_25.xml"
583 // expected result of above created profile with contact Id $contactId
584 $profileData[$contactId] = array(
585 'first_name' => 'abc1',
586 'last_name' => 'xyz1',
587 'email-Primary' => 'abc1.xyz1@yahoo.com',
588 'phone-1-1' => '021 512 755',
589 'country-1' => '1228',
590 'state_province-1' => '1021',
596 function _createContactWithActivity() {
597 // @TODO: Create profile with custom fields
598 $op = new PHPUnit_Extensions_Database_Operation_Insert();
599 $op->execute($this->_dbconn
,
600 new PHPUnit_Extensions_Database_DataSet_FlatXMLDataSet(
601 dirname(__FILE__
) . '/dataset/uf_group_contact_activity_26.xml'
604 // hack: xml data set do not accept \ 1 (CRM_Core_DAO::VALUE_SEPARATOR)
605 CRM_Core_DAO
::setFieldValue('CRM_Core_DAO_UFGroup', '26', 'group_type', 'Individual,Contact,Activity' . CRM_Core_DAO
::VALUE_SEPARATOR
. 'ActivityType:1');
607 $sourceContactId = $this->individualCreate();
608 $contactParams = array(
609 'first_name' => 'abc1',
610 'last_name' => 'xyz1',
611 'contact_type' => 'Individual',
612 'email' => 'abc1.xyz1@yahoo.com',
614 'api.address.create' => array(
615 'location_type_id' => 1,
617 'name' => 'Saint Helier St',
619 'country' => 'United States',
620 'state_province' => 'Michigan',
621 'supplemental_address_1' => 'Hallmark Ct',
622 'supplemental_address_2' => 'Jersey Village',
626 $contact = civicrm_api('contact', 'create', $contactParams);
628 $this->assertEquals(0, $contact['is_error'], "In line " . __LINE__
. " error message: " . CRM_Utils_Array
::value('error_message', $contact)
631 $keys = array_keys($contact['values']);
632 $contactId = array_pop($keys);
634 $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'])
637 $activityParams = array(
638 'source_contact_id' => $sourceContactId,
639 'assignee_contact_id' => $contactId,
640 'activity_type_id' => '1',
641 'subject' => 'Make-it-Happen Meeting',
642 'activity_date_time' => '20110316',
644 'location' => 'Pensulvania',
645 'details' => 'a test activity',
648 'priority_id' => '1',
650 $activity = civicrm_api('activity', 'create', $activityParams);
651 $this->assertEquals(0, $activity['is_error'], "In line " . __LINE__
. " error message: " . CRM_Utils_Array
::value('error_message', $activity)
654 $activityValues = array_pop($activity['values']);
656 // valid parameters for above profile
657 $profileParams = array(
659 'contact_id' => $contactId,
660 'activity_id' => $activityValues['id'],
664 // expected result of above created profile
666 'first_name' => 'abc1',
667 'last_name' => 'xyz1',
668 'email-Primary' => 'abc1.xyz1@yahoo.com',
669 'activity_subject' => 'Make-it-Happen Meeting',
670 'activity_details' => 'a test activity',
671 'activity_duration' => '120',
672 'activity_date_time_time' => '12:00AM',
673 'activity_date_time' => '03/16/2011',
674 'activity_status_id' => '1',
677 return array($profileParams, $expected);