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 = $this->callAPIFailure('profile', 'get', $params);
81 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: profile_id');
85 * check with no invalid profile Id
87 function testProfileGetInvalidProfileId() {
93 $result = $this->callAPIFailure('profile', 'get', $params);
99 function testProfileGet() {
100 $pofileFieldValues = $this->_createIndividualContact();
101 $expected = current($pofileFieldValues);
102 $contactId = key($pofileFieldValues);
105 'contact_id' => $contactId,
109 $result = civicrm_api('profile', 'get', $params);
110 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
112 $this->assertEquals(0, $result['is_error'], "In line " . __LINE__
. " error message: " . CRM_Utils_Array
::value('error_message', $result)
115 foreach ($expected as $profileField => $value) {
116 $this->assertEquals($value, CRM_Utils_Array
::value($profileField, $result['values']), "In line " . __LINE__
. " error message: " . "missing/mismatching value for {$profileField}"
120 $this->quickCleanup(array('civicrm_uf_field', 'civicrm_uf_join', 'civicrm_uf_group', 'civicrm_contact'));
124 * check contact activity profile without activity id
126 function testContactActivityGetWithoutActivityId() {
127 list($params, $expected) = $this->_createContactWithActivity();
129 unset($params['activity_id']);
130 $result = $this->callAPIFailure('profile', 'get', $params);
131 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: activity_id');
135 * check contact activity profile wrong activity id
137 function testContactActivityGetWrongActivityId() {
138 list($params, $expected) = $this->_createContactWithActivity();
140 $params['activity_id'] = 100001;
141 $result = $this->callAPIFailure('profile', 'get', $params);
142 $this->assertEquals($result['error_message'], 'Invalid Activity Id (aid).');
144 $this->quickCleanup(array('civicrm_uf_field', 'civicrm_uf_join', 'civicrm_uf_group', 'civicrm_custom_field', 'civicrm_custom_group', 'civicrm_contact'));
148 * check contact activity profile with wrong activity type
150 function testContactActivityGetWrongActivityType() {
151 //flush cache by calling with reset
152 $activityTypes = CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, TRUE, 'name', TRUE);
154 $sourceContactId = $this->householdCreate();
156 $activityparams = array(
157 'source_contact_id' => $sourceContactId,
158 'activity_type_id' => '2',
159 'subject' => 'Test activity',
160 'activity_date_time' => '20110316',
162 'location' => 'Pensulvania',
163 'details' => 'a test activity',
166 'priority_id' => '1',
169 $activity = civicrm_api('activity', 'create', $activityparams);
170 $this->assertEquals(0, $activity['is_error'], "In line " . __LINE__
. " error message: " . CRM_Utils_Array
::value('error_message', $activity)
173 $activityValues = array_pop($activity['values']);
175 list($params, $expected) = $this->_createContactWithActivity();
177 $params['activity_id'] = $activityValues['id'];
178 $result = $this->callAPIFailure('profile', 'get', $params);
179 $this->assertEquals($result['error_message'], 'This activity cannot be edited or viewed via this profile.');
181 $this->quickCleanup(array('civicrm_uf_field', 'civicrm_uf_join', 'civicrm_uf_group', 'civicrm_custom_field', 'civicrm_custom_group', 'civicrm_contact'));
185 * check contact activity profile with success
187 function testContactActivityGetSuccess() {
188 list($params, $expected) = $this->_createContactWithActivity();
190 $result = civicrm_api('profile', 'get', $params);
192 $this->assertEquals(0, $result['is_error'], "In line " . __LINE__
. " error message: " . CRM_Utils_Array
::value('error_message', $result)
195 foreach ($expected as $profileField => $value) {
196 $this->assertEquals($value, CRM_Utils_Array
::value($profileField, $result['values']), "In line " . __LINE__
. " error message: " . "missing/mismatching value for {$profileField}"
200 $this->quickCleanup(array('civicrm_uf_field', 'civicrm_uf_join', 'civicrm_uf_group', 'civicrm_custom_field', 'civicrm_custom_group', 'civicrm_contact'));
203 /////////////// test civicrm_api3_profile_set //////////////////
206 * check with no array
208 function testProfileSetNoArray() {
210 $result = $this->callAPIFailure('profile', 'set', $params);
211 $this->assertEquals($result['error_message'], 'Input variable `params` is not an array');
215 * check Without ProfileId
217 function testProfileSetWithoutProfileId() {
222 $result = $this->callAPIFailure('profile', 'set', $params);
223 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: profile_id');
227 * check with no invalid profile Id
229 function testProfileSetInvalidProfileId() {
232 'profile_id' => 1000,
235 $result = $this->callAPIFailure('profile', 'set', $params);
239 * check with missing required field in profile
241 function testProfileSetCheckProfileRequired() {
242 $pofileFieldValues = $this->_createIndividualContact();
243 current($pofileFieldValues);
244 $contactId = key($pofileFieldValues);
245 $updateParams = array(
246 'first_name' => 'abc2',
247 'last_name' => 'xyz2',
248 'phone-1-1' => '022 321 826',
249 'country-1' => '1013',
250 'state_province-1' => '1000',
253 $params = array_merge(array('profile_id' => 25, 'contact_id' => $contactId, 'version' => 3),
257 $result = $this->callAPIFailure('profile', 'set', $params);
258 $this->assertEquals($result['error_message'], 'Missing required parameters for profile id 25: email-Primary');
260 $this->quickCleanup(array('civicrm_uf_field', 'civicrm_uf_join', 'civicrm_uf_group', 'civicrm_contact'));
266 function testProfileSet() {
267 $pofileFieldValues = $this->_createIndividualContact();
268 current($pofileFieldValues);
269 $contactId = key($pofileFieldValues);
271 $updateParams = array(
272 'first_name' => 'abc2',
273 'last_name' => 'xyz2',
274 'email-Primary' => 'abc2.xyz2@gmail.com',
275 'phone-1-1' => '022 321 826',
276 'country-1' => '1013',
277 'state_province-1' => '1000',
280 $params = array_merge(array(
282 'contact_id' => $contactId,
286 $result = civicrm_api('profile', 'set', $params);
287 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
289 $this->assertEquals(0, $result['is_error'], "In line " . __LINE__
. " error message: " . CRM_Utils_Array
::value('error_message', $result)
294 'contact_id' => $contactId,
297 $profileDetails = civicrm_api('profile', 'get', $getParams);
298 $this->assertEquals(0, $profileDetails['is_error'], "In line " . __LINE__
. " error message: " . CRM_Utils_Array
::value('error_message', $profileDetails)
301 foreach ($updateParams as $profileField => $value) {
302 $this->assertEquals($value, CRM_Utils_Array
::value($profileField, $profileDetails['values']), "In line " . __LINE__
. " error message: " . "missing/mismatching value for {$profileField}"
308 * check contact activity profile without activity id
310 function testContactActivitySetWithoutActivityId() {
311 list($params, $expected) = $this->_createContactWithActivity();
313 $params = array_merge($params, $expected);
314 unset($params['activity_id']);
315 $result = $this->callAPIFailure('profile', 'set', $params);
316 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: activity_id');
318 $this->quickCleanup(array('civicrm_uf_field', 'civicrm_uf_join', 'civicrm_uf_group', 'civicrm_custom_field', 'civicrm_custom_group', 'civicrm_contact'));
322 * check contact activity profile wrong activity id
324 function testContactActivitySetWrongActivityId() {
325 list($params, $expected) = $this->_createContactWithActivity();
327 $params = array_merge($params, $expected);
328 $params['activity_id'] = 100001;
329 $result = $this->callAPIFailure('profile', 'set', $params);
330 $this->assertEquals($result['error_message'], 'Invalid Activity Id (aid).');
332 $this->quickCleanup(array('civicrm_uf_field', 'civicrm_uf_join', 'civicrm_uf_group', 'civicrm_custom_field', 'civicrm_custom_group', 'civicrm_contact'));
336 * check contact activity profile with wrong activity type
338 function testContactActivitySetWrongActivityType() {
339 //flush cache by calling with reset
340 $activityTypes = CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, TRUE, 'name', TRUE);
342 $sourceContactId = $this->householdCreate();
344 $activityparams = array(
345 'source_contact_id' => $sourceContactId,
346 'activity_type_id' => '2',
347 'subject' => 'Test activity',
348 'activity_date_time' => '20110316',
350 'location' => 'Pensulvania',
351 'details' => 'a test activity',
354 'priority_id' => '1',
357 $activity = civicrm_api('activity', 'create', $activityparams);
358 $this->assertEquals(0, $activity['is_error'], "In line " . __LINE__
. " error message: " . CRM_Utils_Array
::value('error_message', $activity)
361 $activityValues = array_pop($activity['values']);
363 list($params, $expected) = $this->_createContactWithActivity();
365 $params = array_merge($params, $expected);
366 $params['activity_id'] = $activityValues['id'];
367 $result = $this->callAPIFailure('profile', 'set', $params);
368 $this->assertEquals($result['error_message'], 'This activity cannot be edited or viewed via this profile.');
370 $this->quickCleanup(array('civicrm_uf_field', 'civicrm_uf_join', 'civicrm_uf_group', 'civicrm_custom_field', 'civicrm_custom_group', 'civicrm_contact'));
374 * check contact activity profile with success
376 function testContactActivitySetSuccess() {
377 list($params, $expected) = $this->_createContactWithActivity();
379 $updateParams = array(
380 'first_name' => 'abc2',
381 'last_name' => 'xyz2',
382 'email-Primary' => 'abc2.xyz2@yahoo.com',
383 'activity_subject' => 'Test Meeting',
384 'activity_details' => 'a test activity details',
385 'activity_duration' => '100',
386 'activity_date_time' => '03/08/2010',
387 'activity_status_id' => '2',
389 $profileParams = array_merge($params, $updateParams);
390 $profile = civicrm_api('profile', 'set', $profileParams);
391 $result = civicrm_api('profile', 'get', $params);
392 $this->assertEquals(0, $result['is_error'], "In line " . __LINE__
. " error message: " . CRM_Utils_Array
::value('error_message', $result)
395 foreach ($updateParams as $profileField => $value) {
396 $this->assertEquals($value, CRM_Utils_Array
::value($profileField, $result['values']), "In line " . __LINE__
. " error message: " . "missing/mismatching value for {$profileField}"
400 $this->quickCleanup(array(
404 'civicrm_custom_field',
405 'civicrm_custom_group',
412 /////////////// test civicrm_api3_profile_apply //////////////////
415 * check Without ProfileId
417 function testProfileApplyWithoutProfileId() {
422 $result = $this->callAPIFailure('profile', 'apply', $params);
423 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: profile_id');
427 * check with no invalid profile Id
429 function testProfileApplyInvalidProfileId() {
432 'profile_id' => 1000,
435 $result = $this->callAPIFailure('profile', 'apply', $params);
441 function testProfileApply() {
442 $pofileFieldValues = $this->_createIndividualContact();
443 current($pofileFieldValues);
444 $contactId = key($pofileFieldValues);
448 'contact_id' => $contactId,
450 'first_name' => 'abc2',
451 'last_name' => 'xyz2',
452 'email-Primary' => 'abc2.xyz2@gmail.com',
453 'phone-1-1' => '022 321 826',
454 'country-1' => '1013',
455 'state_province-1' => '1000',
458 $result = civicrm_api('profile', 'apply', $params);
459 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
461 $this->assertEquals(0, $result['is_error'], "In line " . __LINE__
. " error message: " . CRM_Utils_Array
::value('error_message', $result)
464 // Expected field values
465 $expected['contact'] = array(
466 'contact_id' => $contactId,
467 'contact_type' => 'Individual',
468 'first_name' => 'abc2',
469 'last_name' => 'xyz2',
471 $expected['email'] = array(
472 'location_type_id' => 1,
474 'email' => 'abc2.xyz2@gmail.com',
477 $expected['phone'] = array(
478 'location_type_id' => 1,
480 'phone_type_id' => 1,
481 'phone' => '022 321 826',
483 $expected['address'] = array(
484 'location_type_id' => 1,
486 'country_id' => 1013,
487 'state_province_id' => 1000,
490 foreach ($expected['contact'] as $field => $value) {
491 $this->assertEquals($value, CRM_Utils_Array
::value($field, $result['values']), "In line " . __LINE__
. " error message: " . "missing/mismatching value for {$field}"
496 'email', 'phone', 'address') as $fieldType) {
497 $typeValues = array_pop($result['values'][$fieldType]);
498 foreach ($expected[$fieldType] as $field => $value) {
499 $this->assertEquals($value, CRM_Utils_Array
::value($field, $typeValues), "In line " . __LINE__
. " error message: " . "missing/mismatching value for {$field} ({$fieldType})"
506 * Helper function to create an Individual with address/email/phone info. Import UF Group and UF Fields
508 function _createIndividualContact() {
509 $contactParams = array(
510 'first_name' => 'abc1',
511 'last_name' => 'xyz1',
512 'contact_type' => 'Individual',
513 'email' => 'abc1.xyz1@yahoo.com',
515 'api.address.create' => array(
516 'location_type_id' => 1,
518 'name' => 'Saint Helier St',
520 'country' => 'United States',
521 'state_province' => 'Michigan',
522 'supplemental_address_1' => 'Hallmark Ct',
523 'supplemental_address_2' => 'Jersey Village',
525 'api.phone.create' => array(
526 'location_type_id' => '1',
527 'phone' => '021 512 755',
528 'phone_type_id' => '1',
533 $contact = civicrm_api('contact', 'create', $contactParams);
535 $this->assertEquals(0, $contact['is_error'], "In line " . __LINE__
. " error message: " . CRM_Utils_Array
::value('error_message', $contact)
538 $keys = array_keys($contact['values']);
539 $contactId = array_pop($keys);
541 $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'])
543 $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'])
546 // Create new profile having group_type: Contact,Individual
547 $op = new PHPUnit_Extensions_Database_Operation_Insert();
548 $op->execute($this->_dbconn
,
549 new PHPUnit_Extensions_Database_DataSet_XMLDataSet(
550 dirname(__FILE__
) . "/dataset/uf_group_25.xml"
553 // Create Contact + Idividual fields for profile
554 $op = new PHPUnit_Extensions_Database_Operation_Insert();
555 $op->execute($this->_dbconn
,
556 new PHPUnit_Extensions_Database_DataSet_XMLDataSet(
557 dirname(__FILE__
) . "/dataset/uf_field_uf_group_25.xml"
562 // expected result of above created profile with contact Id $contactId
563 $profileData[$contactId] = array(
564 'first_name' => 'abc1',
565 'last_name' => 'xyz1',
566 'email-Primary' => 'abc1.xyz1@yahoo.com',
567 'phone-1-1' => '021 512 755',
568 'country-1' => '1228',
569 'state_province-1' => '1021',
575 function _createContactWithActivity() {
576 // @TODO: Create profile with custom fields
577 $op = new PHPUnit_Extensions_Database_Operation_Insert();
578 $op->execute($this->_dbconn
,
579 new PHPUnit_Extensions_Database_DataSet_FlatXMLDataSet(
580 dirname(__FILE__
) . '/dataset/uf_group_contact_activity_26.xml'
583 // hack: xml data set do not accept \ 1 (CRM_Core_DAO::VALUE_SEPARATOR)
584 CRM_Core_DAO
::setFieldValue('CRM_Core_DAO_UFGroup', '26', 'group_type', 'Individual,Contact,Activity' . CRM_Core_DAO
::VALUE_SEPARATOR
. 'ActivityType:1');
586 $sourceContactId = $this->individualCreate();
587 $contactParams = array(
588 'first_name' => 'abc1',
589 'last_name' => 'xyz1',
590 'contact_type' => 'Individual',
591 'email' => 'abc1.xyz1@yahoo.com',
593 'api.address.create' => array(
594 'location_type_id' => 1,
596 'name' => 'Saint Helier St',
598 'country' => 'United States',
599 'state_province' => 'Michigan',
600 'supplemental_address_1' => 'Hallmark Ct',
601 'supplemental_address_2' => 'Jersey Village',
605 $contact = civicrm_api('contact', 'create', $contactParams);
607 $this->assertEquals(0, $contact['is_error'], "In line " . __LINE__
. " error message: " . CRM_Utils_Array
::value('error_message', $contact)
610 $keys = array_keys($contact['values']);
611 $contactId = array_pop($keys);
613 $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'])
616 $activityParams = array(
617 'source_contact_id' => $sourceContactId,
618 'assignee_contact_id' => $contactId,
619 'activity_type_id' => '1',
620 'subject' => 'Make-it-Happen Meeting',
621 'activity_date_time' => '20110316',
623 'location' => 'Pensulvania',
624 'details' => 'a test activity',
627 'priority_id' => '1',
629 $activity = civicrm_api('activity', 'create', $activityParams);
630 $this->assertEquals(0, $activity['is_error'], "In line " . __LINE__
. " error message: " . CRM_Utils_Array
::value('error_message', $activity)
633 $activityValues = array_pop($activity['values']);
635 // valid parameters for above profile
636 $profileParams = array(
638 'contact_id' => $contactId,
639 'activity_id' => $activityValues['id'],
643 // expected result of above created profile
645 'first_name' => 'abc1',
646 'last_name' => 'xyz1',
647 'email-Primary' => 'abc1.xyz1@yahoo.com',
648 'activity_subject' => 'Make-it-Happen Meeting',
649 'activity_details' => 'a test activity',
650 'activity_duration' => '120',
651 'activity_date_time_time' => '12:00AM',
652 'activity_date_time' => '03/16/2011',
653 'activity_status_id' => '1',
656 return array($profileParams, $expected);