CRM-13014 support userLoginFinalize() for UF classes (fix civimobile)
[civicrm-core.git] / tests / phpunit / api / v3 / ProfileTest.php
CommitLineData
6a488035
TO
1<?php
2// $Id$
3
4/**
5 * Include class definitions
6 */
7require_once 'tests/phpunit/CiviTest/CiviUnitTestCase.php';
8
9/**
10 * Test APIv3 civicrm_profile_* functions
11 *
12 * @package CiviCRM
13 */
14class api_v3_ProfileTest extends CiviUnitTestCase {
15 protected $_apiversion;
16 function get_info() {
17 return array(
18 'name' => 'Profile Test',
19 'description' => 'Test all profile API methods.',
20 'group' => 'CiviCRM API Tests',
21 );
22 }
23
24 function setUp() {
25 $this->_apiversion = 3;
26 parent::setUp();
27 $config = CRM_Core_Config::singleton();
28 $config->countryLimit[1] = 1013;
29 $config->stateLimit[1] = 1013;
30 }
31
32 function tearDown() {
33 $this->quickCleanup(array(
34 'civicrm_uf_field',
35 'civicrm_uf_join',
36 'civicrm_uf_group',
37 'civicrm_custom_field',
38 'civicrm_custom_group',
39 'civicrm_contact',
40 'civicrm_phone',
41 'civicrm_address',
42 ));
43 }
44
45 ////////////// test civicrm_api3_profile_get //////////////////
46
47 /**
48 * check Without ProfileId
49 */
50 function testProfileGetWithoutProfileId() {
51 $params = array(
52 'contact_id' => 1,
53 'version' => 3,
54 );
55 $result = civicrm_api('profile', 'get', $params);
56 $this->assertEquals($result['is_error'], 1);
57 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: profile_id');
58 }
59
60 /**
61 * check with no invalid profile Id
62 */
63 function testProfileGetInvalidProfileId() {
64 $params = array(
65 'contact_id' => 1,
66 'profile_id' => 1000,
67 'version' => 3,
68 );
69 $result = civicrm_api('profile', 'get', $params);
70 $this->assertEquals($result['is_error'], 1);
71 }
72
73 /**
74 * check with success
75 */
76 function testProfileGet() {
77 $pofileFieldValues = $this->_createIndividualContact();
78 $expected = current($pofileFieldValues);
79 $contactId = key($pofileFieldValues);
80 $params = array(
81 'profile_id' => 25,
82 'contact_id' => $contactId,
83 'version' => 3,
84 );
85
86 $result = civicrm_api('profile', 'get', $params);
87 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
88
89 $this->assertEquals(0, $result['is_error'], "In line " . __LINE__ . " error message: " . CRM_Utils_Array::value('error_message', $result)
90 );
91
92 foreach ($expected as $profileField => $value) {
93 $this->assertEquals($value, CRM_Utils_Array::value($profileField, $result['values']), "In line " . __LINE__ . " error message: " . "missing/mismatching value for {$profileField}"
94 );
95 }
96
97 $this->quickCleanup(array('civicrm_uf_field', 'civicrm_uf_join', 'civicrm_uf_group', 'civicrm_contact'));
98 }
99
100 /*
101 * check contact activity profile without activity id
102 */
103 function testContactActivityGetWithoutActivityId() {
104 list($params, $expected) = $this->_createContactWithActivity();
105
106 unset($params['activity_id']);
107 $result = civicrm_api('profile', 'get', $params);
108
109 $this->assertEquals($result['is_error'], 1);
110 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: activity_id');
111 }
112
113 /*
114 * check contact activity profile wrong activity id
115 */
116 function testContactActivityGetWrongActivityId() {
117 list($params, $expected) = $this->_createContactWithActivity();
118
119 $params['activity_id'] = 100001;
120 $result = civicrm_api('profile', 'get', $params);
121
122 $this->assertEquals($result['is_error'], 1);
123 $this->assertEquals($result['error_message'], 'Invalid Activity Id (aid).');
124
125 $this->quickCleanup(array('civicrm_uf_field', 'civicrm_uf_join', 'civicrm_uf_group', 'civicrm_custom_field', 'civicrm_custom_group', 'civicrm_contact'));
126 }
127
128 /*
129 * check contact activity profile with wrong activity type
130 */
131 function testContactActivityGetWrongActivityType() {
132 //flush cache by calling with reset
133 $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, TRUE, 'name', TRUE);
134
135 $sourceContactId = $this->householdCreate();
136
137 $activityparams = array(
138 'source_contact_id' => $sourceContactId,
139 'activity_type_id' => '2',
140 'subject' => 'Test activity',
141 'activity_date_time' => '20110316',
142 'duration' => '120',
143 'location' => 'Pensulvania',
144 'details' => 'a test activity',
145 'status_id' => '1',
146 'version' => '3',
147 'priority_id' => '1',
148 );
149
150 $activity = civicrm_api('activity', 'create', $activityparams);
151 $this->assertEquals(0, $activity['is_error'], "In line " . __LINE__ . " error message: " . CRM_Utils_Array::value('error_message', $activity)
152 );
153
154 $activityValues = array_pop($activity['values']);
155
156 list($params, $expected) = $this->_createContactWithActivity();
157
158 $params['activity_id'] = $activityValues['id'];
159 $result = civicrm_api('profile', 'get', $params);
160
161 $this->assertEquals($result['is_error'], 1);
162 $this->assertEquals($result['error_message'], 'This activity cannot be edited or viewed via this profile.');
163
164 $this->quickCleanup(array('civicrm_uf_field', 'civicrm_uf_join', 'civicrm_uf_group', 'civicrm_custom_field', 'civicrm_custom_group', 'civicrm_contact'));
165 }
166
167 /*
168 * check contact activity profile with success
169 */
170 function testContactActivityGetSuccess() {
171 list($params, $expected) = $this->_createContactWithActivity();
172
173 $result = civicrm_api('profile', 'get', $params);
174
175 $this->assertEquals(0, $result['is_error'], "In line " . __LINE__ . " error message: " . CRM_Utils_Array::value('error_message', $result)
176 );
177
178 foreach ($expected as $profileField => $value) {
179 $this->assertEquals($value, CRM_Utils_Array::value($profileField, $result['values']), "In line " . __LINE__ . " error message: " . "missing/mismatching value for {$profileField}"
180 );
181 }
182
183 $this->quickCleanup(array('civicrm_uf_field', 'civicrm_uf_join', 'civicrm_uf_group', 'civicrm_custom_field', 'civicrm_custom_group', 'civicrm_contact'));
184 }
185
186 /////////////// test civicrm_api3_profile_set //////////////////
187
188 /**
189 * check with no array
190 */
191 function testProfileSetNoArray() {
192 $params = NULL;
193 $result = civicrm_api('profile', 'set', $params);
194 $this->assertEquals($result['is_error'], 1);
195 $this->assertEquals($result['error_message'], 'Input variable `params` is not an array');
196 }
197
198 /**
199 * check Without ProfileId
200 */
201 function testProfileSetWithoutProfileId() {
202 $params = array(
203 'contact_id' => 1,
204 'version' => 3,
205 );
206 $result = civicrm_api('profile', 'set', $params);
207 $this->assertEquals($result['is_error'], 1);
208 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: profile_id');
209 }
210
211 /**
212 * check with no invalid profile Id
213 */
214 function testProfileSetInvalidProfileId() {
215 $params = array(
216 'contact_id' => 1,
217 'profile_id' => 1000,
218 'version' => 3,
219 );
220 $result = civicrm_api('profile', 'set', $params);
221 $this->assertEquals($result['is_error'], 1);
222 }
223
224 /**
225 * check with missing required field in profile
226 */
227 function testProfileSetCheckProfileRequired() {
228 $pofileFieldValues = $this->_createIndividualContact();
229 current($pofileFieldValues);
230 $contactId = key($pofileFieldValues);
231 $updateParams = array(
232 'first_name' => 'abc2',
233 'last_name' => 'xyz2',
234 'phone-1-1' => '022 321 826',
235 'country-1' => '1013',
236 'state_province-1' => '1000',
237 );
238
239 $params = array_merge(array('profile_id' => 25, 'contact_id' => $contactId, 'version' => 3),
240 $updateParams
241 );
242
243 $result = civicrm_api('profile', 'set', $params);
244
245 $this->assertEquals($result['is_error'], 1);
246 $this->assertEquals($result['error_message'], 'Missing required parameters for profile id 25: email-Primary');
247
248 $this->quickCleanup(array('civicrm_uf_field', 'civicrm_uf_join', 'civicrm_uf_group', 'civicrm_contact'));
249 }
250
251 /**
252 * check with success
253 */
254 function testProfileSet() {
255 $pofileFieldValues = $this->_createIndividualContact();
256 current($pofileFieldValues);
257 $contactId = key($pofileFieldValues);
258
259 $updateParams = array(
260 'first_name' => 'abc2',
261 'last_name' => 'xyz2',
262 'email-Primary' => 'abc2.xyz2@gmail.com',
263 'phone-1-1' => '022 321 826',
264 'country-1' => '1013',
265 'state_province-1' => '1000',
266 );
267
268 $params = array_merge(array(
269 'profile_id' => 25,
270 'contact_id' => $contactId,
271 'version' => 3,
272 ), $updateParams);
273
274 $result = civicrm_api('profile', 'set', $params);
275 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
276
277 $this->assertEquals(0, $result['is_error'], "In line " . __LINE__ . " error message: " . CRM_Utils_Array::value('error_message', $result)
278 );
279
280 $getParams = array(
281 'profile_id' => 25,
282 'contact_id' => $contactId,
283 'version' => 3,
284 );
285 $profileDetails = civicrm_api('profile', 'get', $getParams);
286 $this->assertEquals(0, $profileDetails['is_error'], "In line " . __LINE__ . " error message: " . CRM_Utils_Array::value('error_message', $profileDetails)
287 );
288
289 foreach ($updateParams as $profileField => $value) {
290 $this->assertEquals($value, CRM_Utils_Array::value($profileField, $profileDetails['values']), "In line " . __LINE__ . " error message: " . "missing/mismatching value for {$profileField}"
291 );
292 }
293 }
294
295 /*
296 * check contact activity profile without activity id
297 */
298 function testContactActivitySetWithoutActivityId() {
299 list($params, $expected) = $this->_createContactWithActivity();
300
301 $params = array_merge($params, $expected);
302 unset($params['activity_id']);
303 $result = civicrm_api('profile', 'set', $params);
304
305 $this->assertEquals($result['is_error'], 1);
306 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: activity_id');
307
308 $this->quickCleanup(array('civicrm_uf_field', 'civicrm_uf_join', 'civicrm_uf_group', 'civicrm_custom_field', 'civicrm_custom_group', 'civicrm_contact'));
309 }
310
311 /*
312 * check contact activity profile wrong activity id
313 */
314 function testContactActivitySetWrongActivityId() {
315 list($params, $expected) = $this->_createContactWithActivity();
316
317 $params = array_merge($params, $expected);
318 $params['activity_id'] = 100001;
319 $result = civicrm_api('profile', 'set', $params);
320
321 $this->assertEquals($result['is_error'], 1);
322 $this->assertEquals($result['error_message'], 'Invalid Activity Id (aid).');
323
324 $this->quickCleanup(array('civicrm_uf_field', 'civicrm_uf_join', 'civicrm_uf_group', 'civicrm_custom_field', 'civicrm_custom_group', 'civicrm_contact'));
325 }
326
327 /*
328 * check contact activity profile with wrong activity type
329 */
330 function testContactActivitySetWrongActivityType() {
331 //flush cache by calling with reset
332 $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, TRUE, 'name', TRUE);
333
334 $sourceContactId = $this->householdCreate();
335
336 $activityparams = array(
337 'source_contact_id' => $sourceContactId,
338 'activity_type_id' => '2',
339 'subject' => 'Test activity',
340 'activity_date_time' => '20110316',
341 'duration' => '120',
342 'location' => 'Pensulvania',
343 'details' => 'a test activity',
344 'status_id' => '1',
345 'version' => '3',
346 'priority_id' => '1',
347 );
348
349 $activity = civicrm_api('activity', 'create', $activityparams);
350 $this->assertEquals(0, $activity['is_error'], "In line " . __LINE__ . " error message: " . CRM_Utils_Array::value('error_message', $activity)
351 );
352
353 $activityValues = array_pop($activity['values']);
354
355 list($params, $expected) = $this->_createContactWithActivity();
356
357 $params = array_merge($params, $expected);
358 $params['activity_id'] = $activityValues['id'];
359 $result = civicrm_api('profile', 'set', $params);
360
361 $this->assertEquals($result['is_error'], 1);
362 $this->assertEquals($result['error_message'], 'This activity cannot be edited or viewed via this profile.');
363
364 $this->quickCleanup(array('civicrm_uf_field', 'civicrm_uf_join', 'civicrm_uf_group', 'civicrm_custom_field', 'civicrm_custom_group', 'civicrm_contact'));
365 }
366
367 /*
368 * check contact activity profile with success
369 */
370 function testContactActivitySetSuccess() {
371 list($params, $expected) = $this->_createContactWithActivity();
372
373 $updateParams = array(
374 'first_name' => 'abc2',
375 'last_name' => 'xyz2',
376 'email-Primary' => 'abc2.xyz2@yahoo.com',
377 'activity_subject' => 'Test Meeting',
378 'activity_details' => 'a test activity details',
379 'activity_duration' => '100',
380 'activity_date_time' => '03/08/2010',
381 'activity_status_id' => '2',
382 );
383 $profileParams = array_merge($params, $updateParams);
384 $profile = civicrm_api('profile', 'set', $profileParams);
385 $result = civicrm_api('profile', 'get', $params);
386 $this->assertEquals(0, $result['is_error'], "In line " . __LINE__ . " error message: " . CRM_Utils_Array::value('error_message', $result)
387 );
388
389 foreach ($updateParams as $profileField => $value) {
390 $this->assertEquals($value, CRM_Utils_Array::value($profileField, $result['values']), "In line " . __LINE__ . " error message: " . "missing/mismatching value for {$profileField}"
391 );
392 }
393
394 $this->quickCleanup(array(
395 'civicrm_uf_field',
396 'civicrm_uf_join',
397 'civicrm_uf_group',
398 'civicrm_custom_field',
399 'civicrm_custom_group',
400 'civicrm_contact',
401 ),
402 TRUE
403 );
404 }
405
406 /////////////// test civicrm_api3_profile_apply //////////////////
407
408 /**
409 * check Without ProfileId
410 */
411 function testProfileApplyWithoutProfileId() {
412 $params = array(
413 'contact_id' => 1,
414 'version' => 3,
415 );
416 $result = civicrm_api('profile', 'apply', $params);
417 $this->assertEquals($result['is_error'], 1);
418 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: profile_id');
419 }
420
421 /**
422 * check with no invalid profile Id
423 */
424 function testProfileApplyInvalidProfileId() {
425 $params = array(
426 'contact_id' => 1,
427 'profile_id' => 1000,
428 'version' => 3,
429 );
430 $result = civicrm_api('profile', 'apply', $params);
431 $this->assertEquals($result['is_error'], 1);
432 }
433
434 /**
435 * check with success
436 */
437 function testProfileApply() {
438 $pofileFieldValues = $this->_createIndividualContact();
439 current($pofileFieldValues);
440 $contactId = key($pofileFieldValues);
441
442 $params = array(
443 'profile_id' => 25,
444 'contact_id' => $contactId,
445 'version' => 3,
446 'first_name' => 'abc2',
447 'last_name' => 'xyz2',
448 'email-Primary' => 'abc2.xyz2@gmail.com',
449 'phone-1-1' => '022 321 826',
450 'country-1' => '1013',
451 'state_province-1' => '1000',
452 );
453
454 $result = civicrm_api('profile', 'apply', $params);
455 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
456
457 $this->assertEquals(0, $result['is_error'], "In line " . __LINE__ . " error message: " . CRM_Utils_Array::value('error_message', $result)
458 );
459
460 // Expected field values
461 $expected['contact'] = array(
462 'contact_id' => $contactId,
463 'contact_type' => 'Individual',
464 'first_name' => 'abc2',
465 'last_name' => 'xyz2',
466 );
467 $expected['email'] = array(
468 'location_type_id' => 1,
469 'is_primary' => 1,
470 'email' => 'abc2.xyz2@gmail.com',
471 );
472
473 $expected['phone'] = array(
474 'location_type_id' => 1,
475 'is_primary' => 1,
476 'phone_type_id' => 1,
477 'phone' => '022 321 826',
478 );
479 $expected['address'] = array(
480 'location_type_id' => 1,
481 'is_primary' => 1,
482 'country_id' => 1013,
483 'state_province_id' => 1000,
484 );
485
486 foreach ($expected['contact'] as $field => $value) {
487 $this->assertEquals($value, CRM_Utils_Array::value($field, $result['values']), "In line " . __LINE__ . " error message: " . "missing/mismatching value for {$field}"
488 );
489 }
490
491 foreach (array(
492 'email', 'phone', 'address') as $fieldType) {
493 $typeValues = array_pop($result['values'][$fieldType]);
494 foreach ($expected[$fieldType] as $field => $value) {
495 $this->assertEquals($value, CRM_Utils_Array::value($field, $typeValues), "In line " . __LINE__ . " error message: " . "missing/mismatching value for {$field} ({$fieldType})"
496 );
497 }
498 }
499 }
500
501 /*
502 * Helper function to create an Individual with address/email/phone info. Import UF Group and UF Fields
503 */
504 function _createIndividualContact() {
505 $contactParams = array(
506 'first_name' => 'abc1',
507 'last_name' => 'xyz1',
508 'contact_type' => 'Individual',
509 'email' => 'abc1.xyz1@yahoo.com',
510 'version' => '3',
511 'api.address.create' => array(
512 'location_type_id' => 1,
513 'is_primary' => 1,
514 'name' => 'Saint Helier St',
515 'county' => 'Marin',
516 'country' => 'United States',
517 'state_province' => 'Michigan',
518 'supplemental_address_1' => 'Hallmark Ct',
519 'supplemental_address_2' => 'Jersey Village',
520 ),
521 'api.phone.create' => array(
522 'location_type_id' => '1',
523 'phone' => '021 512 755',
524 'phone_type_id' => '1',
525 'is_primary' => '1',
526 ),
527 );
528
529 $contact = civicrm_api('contact', 'create', $contactParams);
530
531 $this->assertEquals(0, $contact['is_error'], "In line " . __LINE__ . " error message: " . CRM_Utils_Array::value('error_message', $contact)
532 );
533
534 $keys = array_keys($contact['values']);
535 $contactId = array_pop($keys);
536
537 $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'])
538 );
539 $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'])
540 );
541
542 // Create new profile having group_type: Contact,Individual
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_group_25.xml"
547 )
548 );
549 // Create Contact + Idividual fields for profile
550 $op = new PHPUnit_Extensions_Database_Operation_Insert();
551 $op->execute($this->_dbconn,
552 new PHPUnit_Extensions_Database_DataSet_XMLDataSet(
553 dirname(__FILE__) . "/dataset/uf_field_uf_group_25.xml"
554 )
555 );
556
557
558 // expected result of above created profile with contact Id $contactId
559 $profileData[$contactId] = array(
560 'first_name' => 'abc1',
561 'last_name' => 'xyz1',
562 'email-Primary' => 'abc1.xyz1@yahoo.com',
563 'phone-1-1' => '021 512 755',
564 'country-1' => '1228',
565 'state_province-1' => '1021',
566 );
567
568 return $profileData;
569 }
570
571 function _createContactWithActivity() {
572 // @TODO: Create profile with custom fields
573 $op = new PHPUnit_Extensions_Database_Operation_Insert();
574 $op->execute($this->_dbconn,
575 new PHPUnit_Extensions_Database_DataSet_FlatXMLDataSet(
576 dirname(__FILE__) . '/dataset/uf_group_contact_activity_26.xml'
577 )
578 );
579 // hack: xml data set do not accept \ 1 (CRM_Core_DAO::VALUE_SEPARATOR)
580 CRM_Core_DAO::setFieldValue('CRM_Core_DAO_UFGroup', '26', 'group_type', 'Individual,Contact,Activity' . CRM_Core_DAO::VALUE_SEPARATOR . 'ActivityType:1');
581
582 $sourceContactId = $this->individualCreate();
583 $contactParams = array(
584 'first_name' => 'abc1',
585 'last_name' => 'xyz1',
586 'contact_type' => 'Individual',
587 'email' => 'abc1.xyz1@yahoo.com',
588 'version' => '3',
589 'api.address.create' => array(
590 'location_type_id' => 1,
591 'is_primary' => 1,
592 'name' => 'Saint Helier St',
593 'county' => 'Marin',
594 'country' => 'United States',
595 'state_province' => 'Michigan',
596 'supplemental_address_1' => 'Hallmark Ct',
597 'supplemental_address_2' => 'Jersey Village',
598 ),
599 );
600
601 $contact = civicrm_api('contact', 'create', $contactParams);
602
603 $this->assertEquals(0, $contact['is_error'], "In line " . __LINE__ . " error message: " . CRM_Utils_Array::value('error_message', $contact)
604 );
605
606 $keys = array_keys($contact['values']);
607 $contactId = array_pop($keys);
608
609 $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'])
610 );
611
612 $activityParams = array(
613 'source_contact_id' => $sourceContactId,
614 'assignee_contact_id' => $contactId,
615 'activity_type_id' => '1',
616 'subject' => 'Make-it-Happen Meeting',
617 'activity_date_time' => '20110316',
618 'duration' => '120',
619 'location' => 'Pensulvania',
620 'details' => 'a test activity',
621 'status_id' => '1',
622 'version' => '3',
623 'priority_id' => '1',
624 );
625 $activity = civicrm_api('activity', 'create', $activityParams);
626 $this->assertEquals(0, $activity['is_error'], "In line " . __LINE__ . " error message: " . CRM_Utils_Array::value('error_message', $activity)
627 );
628
629 $activityValues = array_pop($activity['values']);
630
631 // valid parameters for above profile
632 $profileParams = array(
633 'profile_id' => 26,
634 'contact_id' => $contactId,
635 'activity_id' => $activityValues['id'],
636 'version' => 3,
637 );
638
639 // expected result of above created profile
640 $expected = array(
641 'first_name' => 'abc1',
642 'last_name' => 'xyz1',
643 'email-Primary' => 'abc1.xyz1@yahoo.com',
644 'activity_subject' => 'Make-it-Happen Meeting',
645 'activity_details' => 'a test activity',
646 'activity_duration' => '120',
647 'activity_date_time_time' => '12:00AM',
648 'activity_date_time' => '03/16/2011',
649 'activity_status_id' => '1',
650 );
651
652 return array($profileParams, $expected);
653 }
654}
655