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(
69 ////////////// test $this->callAPISuccess3_profile_get //////////////////
72 * check Without ProfileId
74 function testProfileGetWithoutProfileId() {
78 $result = $this->callAPIFailure('profile', 'get', $params,
79 'Mandatory key(s) missing from params array: profile_id'
84 * check with no invalid profile Id
86 function testProfileGetInvalidProfileId() {
91 $result = $this->callAPIFailure('profile', 'get', $params);
97 function testProfileGet() {
98 $pofileFieldValues = $this->_createIndividualContact();
99 $expected = current($pofileFieldValues);
100 $contactId = key($pofileFieldValues);
103 'contact_id' => $contactId,
106 $result = $this->callAPIAndDocument('profile', 'get', $params, __FUNCTION__
, __FILE__
);
108 foreach ($expected as $profileField => $value) {
109 $this->assertEquals($value, CRM_Utils_Array
::value($profileField, $result['values']), "In line " . __LINE__
. " error message: " . "missing/mismatching value for {$profileField}"
115 * check contact activity profile without activity id
117 function testContactActivityGetWithoutActivityId() {
118 list($params, $expected) = $this->_createContactWithActivity();
120 unset($params['activity_id']);
121 $result = $this->callAPIFailure('profile', 'get', $params,
122 'Mandatory key(s) missing from params array: activity_id');
126 * check contact activity profile wrong activity id
128 function testContactActivityGetWrongActivityId() {
129 list($params, $expected) = $this->_createContactWithActivity();
131 $params['activity_id'] = 100001;
132 $result = $this->callAPIFailure('profile', 'get', $params,
133 'Invalid Activity Id (aid).');
137 * check contact activity profile with wrong activity type
139 function testContactActivityGetWrongActivityType() {
140 //flush cache by calling with reset
141 $activityTypes = CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, TRUE, 'name', TRUE);
143 $sourceContactId = $this->householdCreate();
145 $activityparams = array(
146 'source_contact_id' => $sourceContactId,
147 'activity_type_id' => '2',
148 'subject' => 'Test activity',
149 'activity_date_time' => '20110316',
151 'location' => 'Pensulvania',
152 'details' => 'a test activity',
154 'priority_id' => '1',
157 $activity = $this->callAPISuccess('activity', 'create', $activityparams);
159 $activityValues = array_pop($activity['values']);
161 list($params, $expected) = $this->_createContactWithActivity();
163 $params['activity_id'] = $activityValues['id'];
164 $result = $this->callAPIFailure('profile', 'get', $params,
165 'This activity cannot be edited or viewed via this profile.'
170 * check contact activity profile with success
172 function testContactActivityGetSuccess() {
173 list($params, $expected) = $this->_createContactWithActivity();
175 $result = $this->callAPISuccess('profile', 'get', $params);
177 foreach ($expected as $profileField => $value) {
178 $this->assertEquals($value, CRM_Utils_Array
::value($profileField, $result['values']), "In line " . __LINE__
. " error message: " . "missing/mismatching value for {$profileField}"
183 /////////////// test $this->callAPISuccess3_profile_set //////////////////
186 * check with no array
188 function testProfileSetNoArray() {
190 $result = $this->callAPIFailure('profile', 'set', $params);
191 $this->assertEquals($result['error_message'], 'Input variable `params` is not an array');
195 * check Without ProfileId
197 function testProfileSetWithoutProfileId() {
201 $result = $this->callAPIFailure('profile', 'set', $params,
202 'Mandatory key(s) missing from params array: profile_id'
207 * check with no invalid profile Id
209 function testProfileSetInvalidProfileId() {
212 'profile_id' => 1000,
214 $result = $this->callAPIFailure('profile', 'set', $params);
218 * check with missing required field in profile
220 function testProfileSetCheckProfileRequired() {
221 $pofileFieldValues = $this->_createIndividualContact();
222 current($pofileFieldValues);
223 $contactId = key($pofileFieldValues);
224 $updateParams = array(
225 'first_name' => 'abc2',
226 'last_name' => 'xyz2',
227 'phone-1-1' => '022 321 826',
228 'country-1' => '1013',
229 'state_province-1' => '1000',
232 $params = array_merge(array('profile_id' => 25, 'contact_id' => $contactId),
236 $result = $this->callAPIFailure('profile', 'set', $params,
237 'Missing required parameters for profile id 25: email-Primary'
244 function testProfileSet() {
245 $pofileFieldValues = $this->_createIndividualContact();
246 current($pofileFieldValues);
247 $contactId = key($pofileFieldValues);
249 $updateParams = array(
250 'first_name' => 'abc2',
251 'last_name' => 'xyz2',
252 'email-Primary' => 'abc2.xyz2@gmail.com',
253 'phone-1-1' => '022 321 826',
254 'country-1' => '1013',
255 'state_province-1' => '1000',
258 $params = array_merge(array(
260 'contact_id' => $contactId,
263 $result = $this->callAPIAndDocument('profile', 'set', $params, __FUNCTION__
, __FILE__
);
267 'contact_id' => $contactId,
269 $profileDetails = $this->callAPISuccess('profile', 'get', $getParams);
271 foreach ($updateParams as $profileField => $value) {
272 $this->assertEquals($value, CRM_Utils_Array
::value($profileField, $profileDetails['values']), "In line " . __LINE__
. " error message: " . "missing/mismatching value for {$profileField}"
278 * check contact activity profile without activity id
280 function testContactActivitySetWithoutActivityId() {
281 list($params, $expected) = $this->_createContactWithActivity();
283 $params = array_merge($params, $expected);
284 unset($params['activity_id']);
285 $result = $this->callAPIFailure('profile', 'set', $params);
286 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: activity_id');
288 $this->quickCleanup(array('civicrm_uf_field', 'civicrm_uf_join', 'civicrm_uf_group', 'civicrm_custom_field', 'civicrm_custom_group', 'civicrm_contact'));
292 * check contact activity profile wrong activity id
294 function testContactActivitySetWrongActivityId() {
295 list($params, $expected) = $this->_createContactWithActivity();
297 $params = array_merge($params, $expected);
298 $params['activity_id'] = 100001;
299 $result = $this->callAPIFailure('profile', 'set', $params);
300 $this->assertEquals($result['error_message'], 'Invalid Activity Id (aid).');
302 $this->quickCleanup(array('civicrm_uf_field', 'civicrm_uf_join', 'civicrm_uf_group', 'civicrm_custom_field', 'civicrm_custom_group', 'civicrm_contact'));
306 * check contact activity profile with wrong activity type
308 function testContactActivitySetWrongActivityType() {
309 //flush cache by calling with reset
310 $activityTypes = CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, TRUE, 'name', TRUE);
312 $sourceContactId = $this->householdCreate();
314 $activityparams = array(
315 'source_contact_id' => $sourceContactId,
316 'activity_type_id' => '2',
317 'subject' => 'Test activity',
318 'activity_date_time' => '20110316',
320 'location' => 'Pensulvania',
321 'details' => 'a test activity',
323 'priority_id' => '1',
326 $activity = $this->callAPISuccess('activity', 'create', $activityparams);
328 $activityValues = array_pop($activity['values']);
330 list($params, $expected) = $this->_createContactWithActivity();
332 $params = array_merge($params, $expected);
333 $params['activity_id'] = $activityValues['id'];
334 $result = $this->callAPIFailure('profile', 'set', $params,
335 'This activity cannot be edited or viewed via this profile.');
339 * check contact activity profile with success
341 function testContactActivitySetSuccess() {
342 list($params, $expected) = $this->_createContactWithActivity();
344 $updateParams = array(
345 'first_name' => 'abc2',
346 'last_name' => 'xyz2',
347 'email-Primary' => 'abc2.xyz2@yahoo.com',
348 'activity_subject' => 'Test Meeting',
349 'activity_details' => 'a test activity details',
350 'activity_duration' => '100',
351 'activity_date_time' => '03/08/2010',
352 'activity_status_id' => '2',
354 $profileParams = array_merge($params, $updateParams);
355 $profile = $this->callAPISuccess('profile', 'set', $profileParams);
356 $result = $this->callAPISuccess('profile', 'get', $params);
358 foreach ($updateParams as $profileField => $value) {
359 $this->assertEquals($value, CRM_Utils_Array
::value($profileField, $result['values']), "In line " . __LINE__
. " error message: " . "missing/mismatching value for {$profileField}"
365 * check profile apply Without ProfileId
367 function testProfileApplyWithoutProfileId() {
371 $result = $this->callAPIFailure('profile', 'apply', $params,
372 'Mandatory key(s) missing from params array: profile_id');
376 * check profile apply with no invalid profile Id
378 function testProfileApplyInvalidProfileId() {
381 'profile_id' => 1000,
383 $result = $this->callAPIFailure('profile', 'apply', $params);
389 function testProfileApply() {
390 $pofileFieldValues = $this->_createIndividualContact();
391 current($pofileFieldValues);
392 $contactId = key($pofileFieldValues);
396 'contact_id' => $contactId,
397 'first_name' => 'abc2',
398 'last_name' => 'xyz2',
399 'email-Primary' => 'abc2.xyz2@gmail.com',
400 'phone-1-1' => '022 321 826',
401 'country-1' => '1013',
402 'state_province-1' => '1000',
405 $result = $this->callAPIAndDocument('profile', 'apply', $params, __FUNCTION__
, __FILE__
);
407 // Expected field values
408 $expected['contact'] = array(
409 'contact_id' => $contactId,
410 'contact_type' => 'Individual',
411 'first_name' => 'abc2',
412 'last_name' => 'xyz2',
414 $expected['email'] = array(
415 'location_type_id' => 1,
417 'email' => 'abc2.xyz2@gmail.com',
420 $expected['phone'] = array(
421 'location_type_id' => 1,
423 'phone_type_id' => 1,
424 'phone' => '022 321 826',
426 $expected['address'] = array(
427 'location_type_id' => 1,
429 'country_id' => 1013,
430 'state_province_id' => 1000,
433 foreach ($expected['contact'] as $field => $value) {
434 $this->assertEquals($value, CRM_Utils_Array
::value($field, $result['values']), "In line " . __LINE__
. " error message: " . "missing/mismatching value for {$field}"
439 'email', 'phone', 'address') as $fieldType) {
440 $typeValues = array_pop($result['values'][$fieldType]);
441 foreach ($expected[$fieldType] as $field => $value) {
442 $this->assertEquals($value, CRM_Utils_Array
::value($field, $typeValues), "In line " . __LINE__
. " error message: " . "missing/mismatching value for {$field} ({$fieldType})"
449 * Helper function to create an Individual with address/email/phone info. Import UF Group and UF Fields
451 function _createIndividualContact() {
452 $contactParams = array(
453 'first_name' => 'abc1',
454 'last_name' => 'xyz1',
455 'contact_type' => 'Individual',
456 'email' => 'abc1.xyz1@yahoo.com',
457 'api.address.create' => array(
458 'location_type_id' => 1,
460 'name' => 'Saint Helier St',
462 'country' => 'United States',
463 'state_province' => 'Michigan',
464 'supplemental_address_1' => 'Hallmark Ct',
465 'supplemental_address_2' => 'Jersey Village',
467 'api.phone.create' => array(
468 'location_type_id' => '1',
469 'phone' => '021 512 755',
470 'phone_type_id' => '1',
475 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
477 $keys = array_keys($contact['values']);
478 $contactId = array_pop($keys);
480 $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'])
482 $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'])
485 // Create new profile having group_type: Contact,Individual
486 $op = new PHPUnit_Extensions_Database_Operation_Insert();
487 $op->execute($this->_dbconn
,
488 new PHPUnit_Extensions_Database_DataSet_XMLDataSet(
489 dirname(__FILE__
) . "/dataset/uf_group_25.xml"
492 // Create Contact + Idividual fields for profile
493 $op = new PHPUnit_Extensions_Database_Operation_Insert();
494 $op->execute($this->_dbconn
,
495 new PHPUnit_Extensions_Database_DataSet_XMLDataSet(
496 dirname(__FILE__
) . "/dataset/uf_field_uf_group_25.xml"
501 // expected result of above created profile with contact Id $contactId
502 $profileData[$contactId] = array(
503 'first_name' => 'abc1',
504 'last_name' => 'xyz1',
505 'email-Primary' => 'abc1.xyz1@yahoo.com',
506 'phone-1-1' => '021 512 755',
507 'country-1' => '1228',
508 'state_province-1' => '1021',
514 function _createContactWithActivity() {
515 // @TODO: Create profile with custom fields
516 $op = new PHPUnit_Extensions_Database_Operation_Insert();
517 $op->execute($this->_dbconn
,
518 new PHPUnit_Extensions_Database_DataSet_FlatXMLDataSet(
519 dirname(__FILE__
) . '/dataset/uf_group_contact_activity_26.xml'
522 // hack: xml data set do not accept \ 1 (CRM_Core_DAO::VALUE_SEPARATOR)
523 CRM_Core_DAO
::setFieldValue('CRM_Core_DAO_UFGroup', '26', 'group_type', 'Individual,Contact,Activity' . CRM_Core_DAO
::VALUE_SEPARATOR
. 'ActivityType:1');
525 $sourceContactId = $this->individualCreate();
526 $contactParams = array(
527 'first_name' => 'abc1',
528 'last_name' => 'xyz1',
529 'contact_type' => 'Individual',
530 'email' => 'abc1.xyz1@yahoo.com',
531 'api.address.create' => array(
532 'location_type_id' => 1,
534 'name' => 'Saint Helier St',
536 'country' => 'United States',
537 'state_province' => 'Michigan',
538 'supplemental_address_1' => 'Hallmark Ct',
539 'supplemental_address_2' => 'Jersey Village',
543 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
545 $keys = array_keys($contact['values']);
546 $contactId = array_pop($keys);
548 $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'])
551 $activityParams = array(
552 'source_contact_id' => $sourceContactId,
553 'assignee_contact_id' => $contactId,
554 'activity_type_id' => '1',
555 'subject' => 'Make-it-Happen Meeting',
556 'activity_date_time' => '20110316',
558 'location' => 'Pensulvania',
559 'details' => 'a test activity',
561 'priority_id' => '1',
563 $activity = $this->callAPISuccess('activity', 'create', $activityParams);
565 $activityValues = array_pop($activity['values']);
567 // valid parameters for above profile
568 $profileParams = array(
570 'contact_id' => $contactId,
571 'activity_id' => $activityValues['id'],
574 // expected result of above created profile
576 'first_name' => 'abc1',
577 'last_name' => 'xyz1',
578 'email-Primary' => 'abc1.xyz1@yahoo.com',
579 'activity_subject' => 'Make-it-Happen Meeting',
580 'activity_details' => 'a test activity',
581 'activity_duration' => '120',
582 'activity_date_time_time' => '12:00AM',
583 'activity_date_time' => '03/16/2011',
584 'activity_status_id' => '1',
587 return array($profileParams, $expected);