Merge pull request #1105 from colemanw/prevNext
[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
59 $this->quickCleanup(array(
60 'civicrm_uf_field',
61 'civicrm_uf_join',
62 'civicrm_uf_group',
63 'civicrm_contact',
64 'civicrm_phone',
65 'civicrm_address',
66 ), TRUE);
67 }
68
69 ////////////// test $this->callAPISuccess3_profile_get //////////////////
70
71 /**
72 * check Without ProfileId
73 */
74 function testProfileGetWithoutProfileId() {
75 $params = array(
76 'contact_id' => 1,
77 );
78 $result = $this->callAPIFailure('profile', 'get', $params,
79 'Mandatory key(s) missing from params array: profile_id'
80 );
81 }
82
83 /**
84 * check with no invalid profile Id
85 */
86 function testProfileGetInvalidProfileId() {
87 $params = array(
88 'contact_id' => 1,
89 'profile_id' => 1000,
90 );
91 $result = $this->callAPIFailure('profile', 'get', $params);
92 }
93
94 /**
95 * check with success
96 */
97 function testProfileGet() {
98 $pofileFieldValues = $this->_createIndividualContact();
99 $expected = current($pofileFieldValues);
100 $contactId = key($pofileFieldValues);
101 $params = array(
102 'profile_id' => 25,
103 'contact_id' => $contactId,
104 );
105
106 $result = $this->callAPIAndDocument('profile', 'get', $params, __FUNCTION__, __FILE__);
107
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}"
110 );
111 }
112 }
113
114 /**
115 * check contact activity profile without activity id
116 */
117 function testContactActivityGetWithoutActivityId() {
118 list($params, $expected) = $this->_createContactWithActivity();
119
120 unset($params['activity_id']);
121 $result = $this->callAPIFailure('profile', 'get', $params,
122 'Mandatory key(s) missing from params array: activity_id');
123 }
124
125 /**
126 * check contact activity profile wrong activity id
127 */
128 function testContactActivityGetWrongActivityId() {
129 list($params, $expected) = $this->_createContactWithActivity();
130
131 $params['activity_id'] = 100001;
132 $result = $this->callAPIFailure('profile', 'get', $params,
133 'Invalid Activity Id (aid).');
134 }
135
136 /*
137 * check contact activity profile with wrong activity type
138 */
139 function testContactActivityGetWrongActivityType() {
140 //flush cache by calling with reset
141 $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, TRUE, 'name', TRUE);
142
143 $sourceContactId = $this->householdCreate();
144
145 $activityparams = array(
146 'source_contact_id' => $sourceContactId,
147 'activity_type_id' => '2',
148 'subject' => 'Test activity',
149 'activity_date_time' => '20110316',
150 'duration' => '120',
151 'location' => 'Pensulvania',
152 'details' => 'a test activity',
153 'status_id' => '1',
154 'priority_id' => '1',
155 );
156
157 $activity = $this->callAPISuccess('activity', 'create', $activityparams);
158
159 $activityValues = array_pop($activity['values']);
160
161 list($params, $expected) = $this->_createContactWithActivity();
162
163 $params['activity_id'] = $activityValues['id'];
164 $result = $this->callAPIFailure('profile', 'get', $params,
165 'This activity cannot be edited or viewed via this profile.'
166 );
167 }
168
169 /*
170 * check contact activity profile with success
171 */
172 function testContactActivityGetSuccess() {
173 list($params, $expected) = $this->_createContactWithActivity();
174
175 $result = $this->callAPISuccess('profile', 'get', $params);
176
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}"
179 );
180 }
181 }
182
183 /////////////// test $this->callAPISuccess3_profile_set //////////////////
184
185 /**
186 * check with no array
187 */
188 function testProfileSetNoArray() {
189 $params = NULL;
190 $result = $this->callAPIFailure('profile', 'set', $params);
191 $this->assertEquals($result['error_message'], 'Input variable `params` is not an array');
192 }
193
194 /**
195 * check Without ProfileId
196 */
197 function testProfileSetWithoutProfileId() {
198 $params = array(
199 'contact_id' => 1,
200 );
201 $result = $this->callAPIFailure('profile', 'set', $params,
202 'Mandatory key(s) missing from params array: profile_id'
203 );
204 }
205
206 /**
207 * check with no invalid profile Id
208 */
209 function testProfileSetInvalidProfileId() {
210 $params = array(
211 'contact_id' => 1,
212 'profile_id' => 1000,
213 );
214 $result = $this->callAPIFailure('profile', 'set', $params);
215 }
216
217 /**
218 * check with missing required field in profile
219 */
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',
230 );
231
232 $params = array_merge(array('profile_id' => 25, 'contact_id' => $contactId),
233 $updateParams
234 );
235
236 $result = $this->callAPIFailure('profile', 'set', $params,
237 'Missing required parameters for profile id 25: email-Primary'
238 );
239 }
240
241 /**
242 * check with success
243 */
244 function testProfileSet() {
245 $pofileFieldValues = $this->_createIndividualContact();
246 current($pofileFieldValues);
247 $contactId = key($pofileFieldValues);
248
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',
256 );
257
258 $params = array_merge(array(
259 'profile_id' => 25,
260 'contact_id' => $contactId,
261 ), $updateParams);
262
263 $result = $this->callAPIAndDocument('profile', 'set', $params, __FUNCTION__, __FILE__);
264
265 $getParams = array(
266 'profile_id' => 25,
267 'contact_id' => $contactId,
268 );
269 $profileDetails = $this->callAPISuccess('profile', 'get', $getParams);
270
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}"
273 );
274 }
275 }
276
277 /*
278 * check contact activity profile without activity id
279 */
280 function testContactActivitySetWithoutActivityId() {
281 list($params, $expected) = $this->_createContactWithActivity();
282
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');
287
288 $this->quickCleanup(array('civicrm_uf_field', 'civicrm_uf_join', 'civicrm_uf_group', 'civicrm_custom_field', 'civicrm_custom_group', 'civicrm_contact'));
289 }
290
291 /*
292 * check contact activity profile wrong activity id
293 */
294 function testContactActivitySetWrongActivityId() {
295 list($params, $expected) = $this->_createContactWithActivity();
296
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).');
301
302 $this->quickCleanup(array('civicrm_uf_field', 'civicrm_uf_join', 'civicrm_uf_group', 'civicrm_custom_field', 'civicrm_custom_group', 'civicrm_contact'));
303 }
304
305 /*
306 * check contact activity profile with wrong activity type
307 */
308 function testContactActivitySetWrongActivityType() {
309 //flush cache by calling with reset
310 $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, TRUE, 'name', TRUE);
311
312 $sourceContactId = $this->householdCreate();
313
314 $activityparams = array(
315 'source_contact_id' => $sourceContactId,
316 'activity_type_id' => '2',
317 'subject' => 'Test activity',
318 'activity_date_time' => '20110316',
319 'duration' => '120',
320 'location' => 'Pensulvania',
321 'details' => 'a test activity',
322 'status_id' => '1',
323 'priority_id' => '1',
324 );
325
326 $activity = $this->callAPISuccess('activity', 'create', $activityparams);
327
328 $activityValues = array_pop($activity['values']);
329
330 list($params, $expected) = $this->_createContactWithActivity();
331
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.');
336 }
337
338 /*
339 * check contact activity profile with success
340 */
341 function testContactActivitySetSuccess() {
342 list($params, $expected) = $this->_createContactWithActivity();
343
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',
353 );
354 $profileParams = array_merge($params, $updateParams);
355 $profile = $this->callAPISuccess('profile', 'set', $profileParams);
356 $result = $this->callAPISuccess('profile', 'get', $params);
357
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}"
360 );
361 }
362 }
363
364 /**
365 * check profile apply Without ProfileId
366 */
367 function testProfileApplyWithoutProfileId() {
368 $params = array(
369 'contact_id' => 1,
370 );
371 $result = $this->callAPIFailure('profile', 'apply', $params,
372 'Mandatory key(s) missing from params array: profile_id');
373 }
374
375 /**
376 * check profile apply with no invalid profile Id
377 */
378 function testProfileApplyInvalidProfileId() {
379 $params = array(
380 'contact_id' => 1,
381 'profile_id' => 1000,
382 );
383 $result = $this->callAPIFailure('profile', 'apply', $params);
384 }
385
386 /**
387 * check with success
388 */
389 function testProfileApply() {
390 $pofileFieldValues = $this->_createIndividualContact();
391 current($pofileFieldValues);
392 $contactId = key($pofileFieldValues);
393
394 $params = array(
395 'profile_id' => 25,
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',
403 );
404
405 $result = $this->callAPIAndDocument('profile', 'apply', $params, __FUNCTION__, __FILE__);
406
407 // Expected field values
408 $expected['contact'] = array(
409 'contact_id' => $contactId,
410 'contact_type' => 'Individual',
411 'first_name' => 'abc2',
412 'last_name' => 'xyz2',
413 );
414 $expected['email'] = array(
415 'location_type_id' => 1,
416 'is_primary' => 1,
417 'email' => 'abc2.xyz2@gmail.com',
418 );
419
420 $expected['phone'] = array(
421 'location_type_id' => 1,
422 'is_primary' => 1,
423 'phone_type_id' => 1,
424 'phone' => '022 321 826',
425 );
426 $expected['address'] = array(
427 'location_type_id' => 1,
428 'is_primary' => 1,
429 'country_id' => 1013,
430 'state_province_id' => 1000,
431 );
432
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}"
435 );
436 }
437
438 foreach (array(
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})"
443 );
444 }
445 }
446 }
447
448 /*
449 * Helper function to create an Individual with address/email/phone info. Import UF Group and UF Fields
450 */
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,
459 'is_primary' => 1,
460 'name' => 'Saint Helier St',
461 'county' => 'Marin',
462 'country' => 'United States',
463 'state_province' => 'Michigan',
464 'supplemental_address_1' => 'Hallmark Ct',
465 'supplemental_address_2' => 'Jersey Village',
466 ),
467 'api.phone.create' => array(
468 'location_type_id' => '1',
469 'phone' => '021 512 755',
470 'phone_type_id' => '1',
471 'is_primary' => '1',
472 ),
473 );
474
475 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
476
477 $keys = array_keys($contact['values']);
478 $contactId = array_pop($keys);
479
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'])
481 );
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'])
483 );
484
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"
490 )
491 );
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"
497 )
498 );
499
500
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',
509 );
510
511 return $profileData;
512 }
513
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'
520 )
521 );
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');
524
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,
533 'is_primary' => 1,
534 'name' => 'Saint Helier St',
535 'county' => 'Marin',
536 'country' => 'United States',
537 'state_province' => 'Michigan',
538 'supplemental_address_1' => 'Hallmark Ct',
539 'supplemental_address_2' => 'Jersey Village',
540 ),
541 );
542
543 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
544
545 $keys = array_keys($contact['values']);
546 $contactId = array_pop($keys);
547
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'])
549 );
550
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',
557 'duration' => '120',
558 'location' => 'Pensulvania',
559 'details' => 'a test activity',
560 'status_id' => '1',
561 'priority_id' => '1',
562 );
563 $activity = $this->callAPISuccess('activity', 'create', $activityParams);
564
565 $activityValues = array_pop($activity['values']);
566
567 // valid parameters for above profile
568 $profileParams = array(
569 'profile_id' => 26,
570 'contact_id' => $contactId,
571 'activity_id' => $activityValues['id'],
572 );
573
574 // expected result of above created profile
575 $expected = array(
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',
585 );
586
587 return array($profileParams, $expected);
588 }
589 }
590