Merge pull request #1020 from lcdservices/CRM-12814
[civicrm-core.git] / tests / phpunit / api / v3 / ProfileTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
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. |
14 | |
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. |
19 | |
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 +--------------------------------------------------------------------+
27 */
28
29 /**
30 * Include class definitions
31 */
32 require_once 'tests/phpunit/CiviTest/CiviUnitTestCase.php';
33
34 /**
35 * Test APIv3 civicrm_profile_* functions
36 *
37 * @package CiviCRM
38 */
39 class api_v3_ProfileTest extends CiviUnitTestCase {
40 protected $_apiversion;
41 function get_info() {
42 return array(
43 'name' => 'Profile Test',
44 'description' => 'Test all profile API methods.',
45 'group' => 'CiviCRM API Tests',
46 );
47 }
48
49 function setUp() {
50 $this->_apiversion = 3;
51 parent::setUp();
52 $config = CRM_Core_Config::singleton();
53 $config->countryLimit[1] = 1013;
54 $config->stateLimit[1] = 1013;
55 }
56
57 function tearDown() {
58 $this->quickCleanup(array(
59 'civicrm_uf_field',
60 'civicrm_uf_join',
61 'civicrm_uf_group',
62 'civicrm_custom_field',
63 'civicrm_custom_group',
64 'civicrm_contact',
65 'civicrm_phone',
66 'civicrm_address',
67 ));
68 }
69
70 ////////////// test civicrm_api3_profile_get //////////////////
71
72 /**
73 * check Without ProfileId
74 */
75 function testProfileGetWithoutProfileId() {
76 $params = array(
77 'contact_id' => 1,
78 'version' => 3,
79 );
80 $result = $this->callAPIFailure('profile', 'get', $params);
81 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: profile_id');
82 }
83
84 /**
85 * check with no invalid profile Id
86 */
87 function testProfileGetInvalidProfileId() {
88 $params = array(
89 'contact_id' => 1,
90 'profile_id' => 1000,
91 'version' => 3,
92 );
93 $result = $this->callAPIFailure('profile', 'get', $params);
94 }
95
96 /**
97 * check with success
98 */
99 function testProfileGet() {
100 $pofileFieldValues = $this->_createIndividualContact();
101 $expected = current($pofileFieldValues);
102 $contactId = key($pofileFieldValues);
103 $params = array(
104 'profile_id' => 25,
105 'contact_id' => $contactId,
106 'version' => 3,
107 );
108
109 $result = civicrm_api('profile', 'get', $params);
110 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
111
112 $this->assertEquals(0, $result['is_error'], "In line " . __LINE__ . " error message: " . CRM_Utils_Array::value('error_message', $result)
113 );
114
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}"
117 );
118 }
119
120 $this->quickCleanup(array('civicrm_uf_field', 'civicrm_uf_join', 'civicrm_uf_group', 'civicrm_contact'));
121 }
122
123 /*
124 * check contact activity profile without activity id
125 */
126 function testContactActivityGetWithoutActivityId() {
127 list($params, $expected) = $this->_createContactWithActivity();
128
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');
132 }
133
134 /*
135 * check contact activity profile wrong activity id
136 */
137 function testContactActivityGetWrongActivityId() {
138 list($params, $expected) = $this->_createContactWithActivity();
139
140 $params['activity_id'] = 100001;
141 $result = $this->callAPIFailure('profile', 'get', $params);
142 $this->assertEquals($result['error_message'], 'Invalid Activity Id (aid).');
143
144 $this->quickCleanup(array('civicrm_uf_field', 'civicrm_uf_join', 'civicrm_uf_group', 'civicrm_custom_field', 'civicrm_custom_group', 'civicrm_contact'));
145 }
146
147 /*
148 * check contact activity profile with wrong activity type
149 */
150 function testContactActivityGetWrongActivityType() {
151 //flush cache by calling with reset
152 $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, TRUE, 'name', TRUE);
153
154 $sourceContactId = $this->householdCreate();
155
156 $activityparams = array(
157 'source_contact_id' => $sourceContactId,
158 'activity_type_id' => '2',
159 'subject' => 'Test activity',
160 'activity_date_time' => '20110316',
161 'duration' => '120',
162 'location' => 'Pensulvania',
163 'details' => 'a test activity',
164 'status_id' => '1',
165 'version' => '3',
166 'priority_id' => '1',
167 );
168
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)
171 );
172
173 $activityValues = array_pop($activity['values']);
174
175 list($params, $expected) = $this->_createContactWithActivity();
176
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.');
180
181 $this->quickCleanup(array('civicrm_uf_field', 'civicrm_uf_join', 'civicrm_uf_group', 'civicrm_custom_field', 'civicrm_custom_group', 'civicrm_contact'));
182 }
183
184 /*
185 * check contact activity profile with success
186 */
187 function testContactActivityGetSuccess() {
188 list($params, $expected) = $this->_createContactWithActivity();
189
190 $result = civicrm_api('profile', 'get', $params);
191
192 $this->assertEquals(0, $result['is_error'], "In line " . __LINE__ . " error message: " . CRM_Utils_Array::value('error_message', $result)
193 );
194
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}"
197 );
198 }
199
200 $this->quickCleanup(array('civicrm_uf_field', 'civicrm_uf_join', 'civicrm_uf_group', 'civicrm_custom_field', 'civicrm_custom_group', 'civicrm_contact'));
201 }
202
203 /////////////// test civicrm_api3_profile_set //////////////////
204
205 /**
206 * check with no array
207 */
208 function testProfileSetNoArray() {
209 $params = NULL;
210 $result = $this->callAPIFailure('profile', 'set', $params);
211 $this->assertEquals($result['error_message'], 'Input variable `params` is not an array');
212 }
213
214 /**
215 * check Without ProfileId
216 */
217 function testProfileSetWithoutProfileId() {
218 $params = array(
219 'contact_id' => 1,
220 'version' => 3,
221 );
222 $result = $this->callAPIFailure('profile', 'set', $params);
223 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: profile_id');
224 }
225
226 /**
227 * check with no invalid profile Id
228 */
229 function testProfileSetInvalidProfileId() {
230 $params = array(
231 'contact_id' => 1,
232 'profile_id' => 1000,
233 'version' => 3,
234 );
235 $result = $this->callAPIFailure('profile', 'set', $params);
236 }
237
238 /**
239 * check with missing required field in profile
240 */
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',
251 );
252
253 $params = array_merge(array('profile_id' => 25, 'contact_id' => $contactId, 'version' => 3),
254 $updateParams
255 );
256
257 $result = $this->callAPIFailure('profile', 'set', $params);
258 $this->assertEquals($result['error_message'], 'Missing required parameters for profile id 25: email-Primary');
259
260 $this->quickCleanup(array('civicrm_uf_field', 'civicrm_uf_join', 'civicrm_uf_group', 'civicrm_contact'));
261 }
262
263 /**
264 * check with success
265 */
266 function testProfileSet() {
267 $pofileFieldValues = $this->_createIndividualContact();
268 current($pofileFieldValues);
269 $contactId = key($pofileFieldValues);
270
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',
278 );
279
280 $params = array_merge(array(
281 'profile_id' => 25,
282 'contact_id' => $contactId,
283 'version' => 3,
284 ), $updateParams);
285
286 $result = civicrm_api('profile', 'set', $params);
287 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
288
289 $this->assertEquals(0, $result['is_error'], "In line " . __LINE__ . " error message: " . CRM_Utils_Array::value('error_message', $result)
290 );
291
292 $getParams = array(
293 'profile_id' => 25,
294 'contact_id' => $contactId,
295 'version' => 3,
296 );
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)
299 );
300
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}"
303 );
304 }
305 }
306
307 /*
308 * check contact activity profile without activity id
309 */
310 function testContactActivitySetWithoutActivityId() {
311 list($params, $expected) = $this->_createContactWithActivity();
312
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');
317
318 $this->quickCleanup(array('civicrm_uf_field', 'civicrm_uf_join', 'civicrm_uf_group', 'civicrm_custom_field', 'civicrm_custom_group', 'civicrm_contact'));
319 }
320
321 /*
322 * check contact activity profile wrong activity id
323 */
324 function testContactActivitySetWrongActivityId() {
325 list($params, $expected) = $this->_createContactWithActivity();
326
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).');
331
332 $this->quickCleanup(array('civicrm_uf_field', 'civicrm_uf_join', 'civicrm_uf_group', 'civicrm_custom_field', 'civicrm_custom_group', 'civicrm_contact'));
333 }
334
335 /*
336 * check contact activity profile with wrong activity type
337 */
338 function testContactActivitySetWrongActivityType() {
339 //flush cache by calling with reset
340 $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, TRUE, 'name', TRUE);
341
342 $sourceContactId = $this->householdCreate();
343
344 $activityparams = array(
345 'source_contact_id' => $sourceContactId,
346 'activity_type_id' => '2',
347 'subject' => 'Test activity',
348 'activity_date_time' => '20110316',
349 'duration' => '120',
350 'location' => 'Pensulvania',
351 'details' => 'a test activity',
352 'status_id' => '1',
353 'version' => '3',
354 'priority_id' => '1',
355 );
356
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)
359 );
360
361 $activityValues = array_pop($activity['values']);
362
363 list($params, $expected) = $this->_createContactWithActivity();
364
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.');
369
370 $this->quickCleanup(array('civicrm_uf_field', 'civicrm_uf_join', 'civicrm_uf_group', 'civicrm_custom_field', 'civicrm_custom_group', 'civicrm_contact'));
371 }
372
373 /*
374 * check contact activity profile with success
375 */
376 function testContactActivitySetSuccess() {
377 list($params, $expected) = $this->_createContactWithActivity();
378
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',
388 );
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)
393 );
394
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}"
397 );
398 }
399
400 $this->quickCleanup(array(
401 'civicrm_uf_field',
402 'civicrm_uf_join',
403 'civicrm_uf_group',
404 'civicrm_custom_field',
405 'civicrm_custom_group',
406 'civicrm_contact',
407 ),
408 TRUE
409 );
410 }
411
412 /////////////// test civicrm_api3_profile_apply //////////////////
413
414 /**
415 * check Without ProfileId
416 */
417 function testProfileApplyWithoutProfileId() {
418 $params = array(
419 'contact_id' => 1,
420 'version' => 3,
421 );
422 $result = $this->callAPIFailure('profile', 'apply', $params);
423 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: profile_id');
424 }
425
426 /**
427 * check with no invalid profile Id
428 */
429 function testProfileApplyInvalidProfileId() {
430 $params = array(
431 'contact_id' => 1,
432 'profile_id' => 1000,
433 'version' => 3,
434 );
435 $result = $this->callAPIFailure('profile', 'apply', $params);
436 }
437
438 /**
439 * check with success
440 */
441 function testProfileApply() {
442 $pofileFieldValues = $this->_createIndividualContact();
443 current($pofileFieldValues);
444 $contactId = key($pofileFieldValues);
445
446 $params = array(
447 'profile_id' => 25,
448 'contact_id' => $contactId,
449 'version' => 3,
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',
456 );
457
458 $result = civicrm_api('profile', 'apply', $params);
459 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
460
461 $this->assertEquals(0, $result['is_error'], "In line " . __LINE__ . " error message: " . CRM_Utils_Array::value('error_message', $result)
462 );
463
464 // Expected field values
465 $expected['contact'] = array(
466 'contact_id' => $contactId,
467 'contact_type' => 'Individual',
468 'first_name' => 'abc2',
469 'last_name' => 'xyz2',
470 );
471 $expected['email'] = array(
472 'location_type_id' => 1,
473 'is_primary' => 1,
474 'email' => 'abc2.xyz2@gmail.com',
475 );
476
477 $expected['phone'] = array(
478 'location_type_id' => 1,
479 'is_primary' => 1,
480 'phone_type_id' => 1,
481 'phone' => '022 321 826',
482 );
483 $expected['address'] = array(
484 'location_type_id' => 1,
485 'is_primary' => 1,
486 'country_id' => 1013,
487 'state_province_id' => 1000,
488 );
489
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}"
492 );
493 }
494
495 foreach (array(
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})"
500 );
501 }
502 }
503 }
504
505 /*
506 * Helper function to create an Individual with address/email/phone info. Import UF Group and UF Fields
507 */
508 function _createIndividualContact() {
509 $contactParams = array(
510 'first_name' => 'abc1',
511 'last_name' => 'xyz1',
512 'contact_type' => 'Individual',
513 'email' => 'abc1.xyz1@yahoo.com',
514 'version' => '3',
515 'api.address.create' => array(
516 'location_type_id' => 1,
517 'is_primary' => 1,
518 'name' => 'Saint Helier St',
519 'county' => 'Marin',
520 'country' => 'United States',
521 'state_province' => 'Michigan',
522 'supplemental_address_1' => 'Hallmark Ct',
523 'supplemental_address_2' => 'Jersey Village',
524 ),
525 'api.phone.create' => array(
526 'location_type_id' => '1',
527 'phone' => '021 512 755',
528 'phone_type_id' => '1',
529 'is_primary' => '1',
530 ),
531 );
532
533 $contact = civicrm_api('contact', 'create', $contactParams);
534
535 $this->assertEquals(0, $contact['is_error'], "In line " . __LINE__ . " error message: " . CRM_Utils_Array::value('error_message', $contact)
536 );
537
538 $keys = array_keys($contact['values']);
539 $contactId = array_pop($keys);
540
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'])
542 );
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'])
544 );
545
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"
551 )
552 );
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"
558 )
559 );
560
561
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',
570 );
571
572 return $profileData;
573 }
574
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'
581 )
582 );
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');
585
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',
592 'version' => '3',
593 'api.address.create' => array(
594 'location_type_id' => 1,
595 'is_primary' => 1,
596 'name' => 'Saint Helier St',
597 'county' => 'Marin',
598 'country' => 'United States',
599 'state_province' => 'Michigan',
600 'supplemental_address_1' => 'Hallmark Ct',
601 'supplemental_address_2' => 'Jersey Village',
602 ),
603 );
604
605 $contact = civicrm_api('contact', 'create', $contactParams);
606
607 $this->assertEquals(0, $contact['is_error'], "In line " . __LINE__ . " error message: " . CRM_Utils_Array::value('error_message', $contact)
608 );
609
610 $keys = array_keys($contact['values']);
611 $contactId = array_pop($keys);
612
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'])
614 );
615
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',
622 'duration' => '120',
623 'location' => 'Pensulvania',
624 'details' => 'a test activity',
625 'status_id' => '1',
626 'version' => '3',
627 'priority_id' => '1',
628 );
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)
631 );
632
633 $activityValues = array_pop($activity['values']);
634
635 // valid parameters for above profile
636 $profileParams = array(
637 'profile_id' => 26,
638 'contact_id' => $contactId,
639 'activity_id' => $activityValues['id'],
640 'version' => 3,
641 );
642
643 // expected result of above created profile
644 $expected = array(
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',
654 );
655
656 return array($profileParams, $expected);
657 }
658 }
659