fix year in headers
[civicrm-core.git] / tests / phpunit / api / v3 / ProfileTest.php
CommitLineData
6a488035 1<?php
b6708aeb 2/*
3 +--------------------------------------------------------------------+
81621fee 4| CiviCRM version 4.7 |
b6708aeb 5+--------------------------------------------------------------------+
fa938177 6| Copyright CiviCRM LLC (c) 2004-2016 |
b6708aeb 7+--------------------------------------------------------------------+
8| This file is a part of CiviCRM. |
9| |
10| CiviCRM is free software; you can copy, modify, and distribute it |
11| under the terms of the GNU Affero General Public License |
12| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13| |
14| CiviCRM is distributed in the hope that it will be useful, but |
15| WITHOUT ANY WARRANTY; without even the implied warranty of |
16| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17| See the GNU Affero General Public License for more details. |
18| |
19| You should have received a copy of the GNU Affero General Public |
20| License and the CiviCRM Licensing Exception along |
21| with this program; if not, contact CiviCRM LLC |
22| at info[AT]civicrm[DOT]org. If you have questions about the |
23| GNU Affero General Public License or the licensing of CiviCRM, |
24| see the CiviCRM license FAQ at http://civicrm.org/licensing |
25+--------------------------------------------------------------------+
e70a7fc0 26 */
6a488035 27
6a488035
TO
28/**
29 * Test APIv3 civicrm_profile_* functions
30 *
6c6e6187 31 * @package CiviCRM
acb109b7 32 * @group headless
6a488035
TO
33 */
34class api_v3_ProfileTest extends CiviUnitTestCase {
35 protected $_apiversion;
9da2e77c 36 protected $_profileID = 0;
174dbdd5
E
37 protected $_membershipTypeID;
38 protected $_contactID;
4cbe18b8 39
00be9182 40 public function setUp() {
6a488035
TO
41 $this->_apiversion = 3;
42 parent::setUp();
43 $config = CRM_Core_Config::singleton();
eefea86b
TO
44 $countryLimit = $config->countryLimit;
45 $countryLimit[1] = 1013;
46 $config->countryLimit = $countryLimit;
47
9da2e77c 48 $this->createLoggedInUser();
174dbdd5 49 $this->_membershipTypeID = $this->membershipTypeCreate();
6a488035
TO
50 }
51
00be9182 52 public function tearDown() {
7fbb4198 53
6a488035 54 $this->quickCleanup(array(
21170390 55 'civicrm_contact',
56 'civicrm_phone',
57 'civicrm_address',
174dbdd5
E
58 'civicrm_membership',
59 'civicrm_contribution',
225d474b 60 'civicrm_uf_match',
21170390 61 ), TRUE);
174dbdd5 62 $this->callAPISuccess('membership_type', 'delete', array('id' => $this->_membershipTypeID));
f01ce56b 63 // ok can't be bothered wring an api to do this & truncating is crazy
9da2e77c 64 CRM_Core_DAO::executeQuery(" DELETE FROM civicrm_uf_group WHERE id IN ($this->_profileID, 26)");
6a488035
TO
65 }
66
7fbb4198 67 ////////////// test $this->callAPISuccess3_profile_get //////////////////
6a488035
TO
68
69 /**
eceb18cc 70 * Check Without ProfileId.
6a488035 71 */
00be9182 72 public function testProfileGetWithoutProfileId() {
6a488035
TO
73 $params = array(
74 'contact_id' => 1,
6a488035 75 );
7fbb4198 76 $result = $this->callAPIFailure('profile', 'get', $params,
77 'Mandatory key(s) missing from params array: profile_id'
78 );
6a488035
TO
79 }
80
81 /**
eceb18cc 82 * Check with no invalid profile Id.
6a488035 83 */
00be9182 84 public function testProfileGetInvalidProfileId() {
6a488035
TO
85 $params = array(
86 'contact_id' => 1,
87 'profile_id' => 1000,
6a488035 88 );
d0e1eff2 89 $result = $this->callAPIFailure('profile', 'get', $params);
6a488035
TO
90 }
91
92 /**
eceb18cc 93 * Check with success.
6a488035 94 */
00be9182 95 public function testProfileGet() {
6a488035 96 $pofileFieldValues = $this->_createIndividualContact();
92915c55
TO
97 $expected = current($pofileFieldValues);
98 $contactId = key($pofileFieldValues);
99 $params = array(
9da2e77c 100 'profile_id' => $this->_profileID,
6a488035 101 'contact_id' => $contactId,
6a488035 102 );
f01ce56b 103 $result = $this->callAPISuccess('profile', 'get', $params);
6a488035 104 foreach ($expected as $profileField => $value) {
9da2e77c 105 $this->assertEquals($value, CRM_Utils_Array::value($profileField, $result['values']));
6a488035 106 }
6a488035
TO
107 }
108
00be9182 109 public function testProfileGetMultiple() {
f01ce56b 110 $pofileFieldValues = $this->_createIndividualContact();
92915c55
TO
111 $expected = current($pofileFieldValues);
112 $contactId = key($pofileFieldValues);
113 $params = array(
9da2e77c 114 'profile_id' => array($this->_profileID, 1, 'Billing'),
f01ce56b 115 'contact_id' => $contactId,
116 );
117
118 $result = $this->callAPIAndDocument('profile', 'get', $params, __FUNCTION__, __FILE__);
119 foreach ($expected as $profileField => $value) {
9da2e77c 120 $this->assertEquals($value, CRM_Utils_Array::value($profileField, $result['values'][$this->_profileID]), " error message: " . "missing/mismatching value for {$profileField}");
f01ce56b 121 }
122 $this->assertEquals('abc1', $result['values'][1]['first_name'], " error message: " . "missing/mismatching value for {$profileField}");
123 $this->assertFalse(array_key_exists('email-Primary', $result['values'][1]), 'profile 1 doesn not include email');
124 $this->assertEquals($result['values']['Billing'], array(
125 'billing_first_name' => 'abc1',
e0efd2d0 126 'billing_middle_name' => 'J.',
f01ce56b 127 'billing_last_name' => 'xyz1',
b29f74b6 128 'billing_street_address-5' => '5 Saint Helier St',
129 'billing_city-5' => 'Gotham City',
e0efd2d0 130 'billing_state_province_id-5' => '1021',
45c30250 131 'billing_country_id-5' => '1228',
e0efd2d0 132 'billing_postal_code-5' => '90210',
133 'billing-email-5' => 'abc1.xyz1@yahoo.com',
134 'email-5' => 'abc1.xyz1@yahoo.com',
f01ce56b 135 ));
136 }
137
00be9182 138 public function testProfileGetBillingUseIsBillingLocation() {
b29f74b6 139 $individual = $this->_createIndividualContact();
92915c55 140 $contactId = key($individual);
b29f74b6 141 $this->callAPISuccess('address', 'create', array(
142 'is_billing' => 1,
143 'street_address' => 'is billing st',
144 'location_type_id' => 2,
145 'contact_id' => $contactId,
21170390 146 ));
b29f74b6 147
148 $expected = current($individual);
149
150 $params = array(
9da2e77c 151 'profile_id' => array($this->_profileID, 1, 'Billing'),
b29f74b6 152 'contact_id' => $contactId,
153 );
154
155 $result = $this->callAPISuccess('profile', 'get', $params);
156 $this->assertEquals('abc1', $result['values'][1]['first_name']);
157 $this->assertEquals(array(
158 'billing_first_name' => 'abc1',
159 'billing_middle_name' => 'J.',
160 'billing_last_name' => 'xyz1',
161 'billing_street_address-5' => 'is billing st',
162 'billing_city-5' => '',
163 'billing_state_province_id-5' => '',
164 'billing_country_id-5' => '',
165 'billing-email-5' => 'abc1.xyz1@yahoo.com',
166 'email-5' => 'abc1.xyz1@yahoo.com',
167 'billing_postal_code-5' => '',
168 ), $result['values']['Billing']);
169 }
170
00be9182 171 public function testProfileGetMultipleHasBillingLocation() {
f01ce56b 172 $individual = $this->_createIndividualContact();
92915c55
TO
173 $contactId = key($individual);
174 $this->callAPISuccess('address', 'create', array(
175 'contact_id' => $contactId,
176 'street_address' => '25 Big Street',
177 'city' => 'big city',
a130e045 178 'location_type_id' => 5,
92915c55
TO
179 ));
180 $this->callAPISuccess('email', 'create', array(
181 'contact_id' => $contactId,
182 'email' => 'big@once.com',
183 'location_type_id' => 2,
a130e045 184 'is_billing' => 1,
92915c55 185 ));
f01ce56b 186
187 $expected = current($individual);
188
189 $params = array(
9da2e77c 190 'profile_id' => array($this->_profileID, 1, 'Billing'),
f01ce56b 191 'contact_id' => $contactId,
192 );
193
194 $result = $this->callAPISuccess('profile', 'get', $params, __FUNCTION__, __FILE__);
195 $this->assertEquals('abc1', $result['values'][1]['first_name']);
196 $this->assertEquals($result['values']['Billing'], array(
197 'billing_first_name' => 'abc1',
e0efd2d0 198 'billing_middle_name' => 'J.',
f01ce56b 199 'billing_last_name' => 'xyz1',
200 'billing_street_address-5' => '25 Big Street',
201 'billing_city-5' => 'big city',
202 'billing_state_province_id-5' => '',
203 'billing_country_id-5' => '',
204 'billing-email-5' => 'big@once.com',
e0efd2d0 205 'email-5' => 'big@once.com',
206 'billing_postal_code-5' => '',
f01ce56b 207 ));
208 }
b29f74b6 209
5a9e1452 210 /**
100fef9d 211 * Get Billing empty contact - this will return generic defaults
5a9e1452 212 */
00be9182 213 public function testProfileGetBillingEmptyContact() {
5a9e1452 214
215 $params = array(
216 'profile_id' => array('Billing'),
217 );
218
219 $result = $this->callAPISuccess('profile', 'get', $params);
220 $this->assertEquals(array(
221 'billing_first_name' => '',
222 'billing_middle_name' => '',
223 'billing_last_name' => '',
224 'billing_street_address-5' => '',
225 'billing_city-5' => '',
226 'billing_state_province_id-5' => '',
227 'billing_country_id-5' => '1228',
228 'billing_email-5' => '',
229 'email-5' => '',
230 'billing_postal_code-5' => '',
231 ), $result['values']['Billing']);
232 }
b29f74b6 233
7fbb4198 234 /**
eceb18cc 235 * Check contact activity profile without activity id.
7fbb4198 236 */
00be9182 237 public function testContactActivityGetWithoutActivityId() {
6a488035
TO
238 list($params, $expected) = $this->_createContactWithActivity();
239
240 unset($params['activity_id']);
7fbb4198 241 $result = $this->callAPIFailure('profile', 'get', $params,
242 'Mandatory key(s) missing from params array: activity_id');
6a488035
TO
243 }
244
7fbb4198 245 /**
eceb18cc 246 * Check contact activity profile wrong activity id.
7fbb4198 247 */
00be9182 248 public function testContactActivityGetWrongActivityId() {
6a488035
TO
249 list($params, $expected) = $this->_createContactWithActivity();
250
251 $params['activity_id'] = 100001;
7fbb4198 252 $result = $this->callAPIFailure('profile', 'get', $params,
21170390 253 'Invalid Activity Id (aid).');
6a488035
TO
254 }
255
c490a46a 256 /**
eceb18cc 257 * Check contact activity profile with wrong activity type.
c490a46a 258 */
00be9182 259 public function testContactActivityGetWrongActivityType() {
6a488035
TO
260 //flush cache by calling with reset
261 $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, TRUE, 'name', TRUE);
262
263 $sourceContactId = $this->householdCreate();
264
265 $activityparams = array(
266 'source_contact_id' => $sourceContactId,
267 'activity_type_id' => '2',
268 'subject' => 'Test activity',
269 'activity_date_time' => '20110316',
270 'duration' => '120',
271 'location' => 'Pensulvania',
272 'details' => 'a test activity',
273 'status_id' => '1',
6a488035
TO
274 'priority_id' => '1',
275 );
276
7fbb4198 277 $activity = $this->callAPISuccess('activity', 'create', $activityparams);
6a488035
TO
278
279 $activityValues = array_pop($activity['values']);
280
281 list($params, $expected) = $this->_createContactWithActivity();
282
283 $params['activity_id'] = $activityValues['id'];
7fbb4198 284 $result = $this->callAPIFailure('profile', 'get', $params,
285 'This activity cannot be edited or viewed via this profile.'
286 );
6a488035
TO
287 }
288
c490a46a 289 /**
eceb18cc 290 * Check contact activity profile with success.
c490a46a 291 */
00be9182 292 public function testContactActivityGetSuccess() {
6a488035
TO
293 list($params, $expected) = $this->_createContactWithActivity();
294
7fbb4198 295 $result = $this->callAPISuccess('profile', 'get', $params);
6a488035
TO
296
297 foreach ($expected as $profileField => $value) {
298 $this->assertEquals($value, CRM_Utils_Array::value($profileField, $result['values']), "In line " . __LINE__ . " error message: " . "missing/mismatching value for {$profileField}"
299 );
300 }
6a488035
TO
301 }
302
6a488035 303 /**
9dec4e61 304 * Check getfields works & gives us our fields
305 */
00be9182 306 public function testGetFields() {
9dec4e61 307 $this->_createIndividualProfile();
308 $this->_addCustomFieldToProfile($this->_profileID);
92915c55
TO
309 $result = $this->callAPIAndDocument('profile', 'getfields', array(
310 'action' => 'submit',
a130e045 311 'profile_id' => $this->_profileID,
92915c55 312 ), __FUNCTION__, __FILE__,
9dec4e61 313 'demonstrates retrieving profile fields passing in an id');
314 $this->assertArrayKeyExists('first_name', $result['values']);
315 $this->assertEquals('2', $result['values']['first_name']['type']);
9da2e77c 316 $this->assertEquals('Email', $result['values']['email-primary']['title']);
9dec4e61 317 $this->assertEquals('civicrm_state_province', $result['values']['state_province-1']['pseudoconstant']['table']);
318 $this->assertEquals('defaultValue', $result['values']['custom_1']['default_value']);
319 $this->assertFalse(array_key_exists('participant_status', $result['values']));
320 }
c490a46a 321
9dec4e61 322 /**
b0b44427 323 * Check getfields works & gives us our fields - partipant profile
6a488035 324 */
00be9182 325 public function testGetFieldsParticipantProfile() {
9dec4e61 326 $result = $this->callAPISuccess('profile', 'getfields', array(
92915c55
TO
327 'action' => 'submit',
328 'profile_id' => 'participant_status',
a130e045 329 'get_options' => 'all',
92915c55 330 )
9dec4e61 331 );
29fbb90a
E
332 $this->assertTrue(array_key_exists('participant_status_id', $result['values']));
333 $this->assertEquals('Attended', $result['values']['participant_status_id']['options'][2]);
334 $this->assertEquals(array('participant_status'), $result['values']['participant_status_id']['api.aliases']);
6a488035 335 }
b0b44427 336
337 /**
338 * Check getfields works & gives us our fields - membership_batch_entry
339 * (getting to the end with no e-notices is pretty good evidence it's working)
340 */
00be9182 341 public function testGetFieldsMembershipBatchProfile() {
b0b44427 342 $result = $this->callAPISuccess('profile', 'getfields', array(
92915c55
TO
343 'action' => 'submit',
344 'profile_id' => 'membership_batch_entry',
a130e045 345 'get_options' => 'all',
92915c55 346 )
b0b44427 347 );
348 $this->assertTrue(array_key_exists('total_amount', $result['values']));
7c3f2c03 349 $this->assertTrue(array_key_exists('financial_type_id', $result['values']));
92915c55
TO
350 $this->assertEquals(array(
351 'contribution_type_id',
352 'contribution_type',
a130e045 353 'financial_type',
92915c55 354 ), $result['values']['financial_type_id']['api.aliases']);
7c3f2c03 355 $this->assertTrue(!array_key_exists('financial_type', $result['values']));
b0b44427 356 $this->assertEquals(12, $result['values']['receive_date']['type']);
357 }
358
359 /**
360 * Check getfields works & gives us our fields - do them all
361 * (getting to the end with no e-notices is pretty good evidence it's working)
362 */
00be9182 363 public function testGetFieldsAllProfiles() {
b0b44427 364 $result = $this->callAPISuccess('uf_group', 'get', array('return' => 'id'));
365 $profileIDs = array_keys($result['values']);
366 foreach ($profileIDs as $profileID) {
21170390 367 $result = $this->callAPISuccess('profile', 'getfields', array(
92915c55
TO
368 'action' => 'submit',
369 'profile_id' => $profileID,
a130e045 370 'get_options' => 'all',
92915c55 371 )
21170390 372 );
b0b44427 373 }
374 }
9dec4e61 375 /////////////// test $this->callAPISuccess3_profile_set //////////////////
6a488035
TO
376
377 /**
eceb18cc 378 * Check Without ProfileId.
6a488035 379 */
00be9182 380 public function testProfileSubmitWithoutProfileId() {
6a488035
TO
381 $params = array(
382 'contact_id' => 1,
6a488035 383 );
9dec4e61 384 $result = $this->callAPIFailure('profile', 'submit', $params,
7fbb4198 385 'Mandatory key(s) missing from params array: profile_id'
386 );
6a488035
TO
387 }
388
389 /**
eceb18cc 390 * Check with no invalid profile Id.
6a488035 391 */
00be9182 392 public function testProfileSubmitInvalidProfileId() {
6a488035
TO
393 $params = array(
394 'contact_id' => 1,
395 'profile_id' => 1000,
6a488035 396 );
9dec4e61 397 $result = $this->callAPIFailure('profile', 'submit', $params);
6a488035
TO
398 }
399
400 /**
eceb18cc 401 * Check with missing required field in profile.
6a488035 402 */
00be9182 403 public function testProfileSubmitCheckProfileRequired() {
6a488035
TO
404 $pofileFieldValues = $this->_createIndividualContact();
405 current($pofileFieldValues);
406 $contactId = key($pofileFieldValues);
407 $updateParams = array(
408 'first_name' => 'abc2',
409 'last_name' => 'xyz2',
410 'phone-1-1' => '022 321 826',
411 'country-1' => '1013',
412 'state_province-1' => '1000',
413 );
414
9da2e77c 415 $params = array_merge(array('profile_id' => $this->_profileID, 'contact_id' => $contactId),
6a488035
TO
416 $updateParams
417 );
418
d235daf6
MM
419 $this->callAPIFailure('profile', 'submit', $params,
420 "Mandatory key(s) missing from params array: email-primary"
7fbb4198 421 );
6a488035
TO
422 }
423
424 /**
eceb18cc 425 * Check with success.
6a488035 426 */
00be9182 427 public function testProfileSubmit() {
6a488035
TO
428 $pofileFieldValues = $this->_createIndividualContact();
429 current($pofileFieldValues);
430 $contactId = key($pofileFieldValues);
431
432 $updateParams = array(
433 'first_name' => 'abc2',
434 'last_name' => 'xyz2',
5aa090ea 435 'email-primary' => 'abc2.xyz2@gmail.com',
6a488035
TO
436 'phone-1-1' => '022 321 826',
437 'country-1' => '1013',
438 'state_province-1' => '1000',
439 );
440
441 $params = array_merge(array(
9da2e77c 442 'profile_id' => $this->_profileID,
21170390 443 'contact_id' => $contactId,
444 ), $updateParams);
6a488035 445
9dec4e61 446 $result = $this->callAPIAndDocument('profile', 'submit', $params, __FUNCTION__, __FILE__);
6a488035
TO
447
448 $getParams = array(
9da2e77c 449 'profile_id' => $this->_profileID,
6a488035 450 'contact_id' => $contactId,
6a488035 451 );
7fbb4198 452 $profileDetails = $this->callAPISuccess('profile', 'get', $getParams);
6a488035
TO
453
454 foreach ($updateParams as $profileField => $value) {
f5bf5e4f 455 $this->assertEquals($value, CRM_Utils_Array::value($profileField, $profileDetails['values']), "missing/mismatching value for {$profileField}"
6a488035
TO
456 );
457 }
f5bf5e4f
E
458 unset($params['email-primary']);
459 $params['email-Primary'] = 'my@mail.com';
6c6e6187 460 $this->callAPISuccess('profile', 'submit', $params);
5aa090ea 461 $profileDetails = $this->callAPISuccess('profile', 'get', $getParams);
f5bf5e4f 462 $this->assertEquals('my@mail.com', $profileDetails['values']['email-Primary']);
6a488035
TO
463 }
464
7c3f2c03
E
465 /**
466 * Ensure caches are being cleared so we don't get into a debugging trap because of cached metadata
467 * First we delete & create to increment the version & then check for caching probs
468 */
00be9182 469 public function testProfileSubmitCheckCaching() {
7c3f2c03
E
470 $this->callAPISuccess('membership_type', 'delete', array('id' => $this->_membershipTypeID));
471 $this->_membershipTypeID = $this->membershipTypeCreate();
472
473 $membershipTypes = $this->callAPISuccess('membership_type', 'get', array());
92915c55
TO
474 $profileFields = $this->callAPISuccess('profile', 'getfields', array(
475 'get_options' => 'all',
476 'action' => 'submit',
a130e045 477 'profile_id' => 'membership_batch_entry',
92915c55
TO
478 ));
479 $getoptions = $this->callAPISuccess('membership', 'getoptions', array(
480 'field' => 'membership_type',
a130e045 481 'context' => 'validate',
92915c55 482 ));
7c3f2c03 483 $this->assertEquals(array_keys($membershipTypes['values']), array_keys($getoptions['values']));
f5c68f3c 484 $this->assertEquals(array_keys($membershipTypes['values']), array_keys($profileFields['values']['membership_type_id']['options']));
7c3f2c03 485
6c6e6187 486 }
f5c68f3c
E
487
488 /**
eceb18cc 489 * Test that the fields are returned in the right order despite the faffing around that goes on.
f5c68f3c 490 */
00be9182 491 public function testMembershipGetFieldsOrder() {
92915c55
TO
492 $result = $this->callAPISuccess('profile', 'getfields', array(
493 'action' => 'submit',
a130e045 494 'profile_id' => 'membership_batch_entry',
92915c55 495 ));
f5c68f3c 496 $weight = 1;
22e263ad
TO
497 foreach ($result['values'] as $fieldName => $field) {
498 if ($fieldName == 'profile_id') {
f5c68f3c
E
499 continue;
500 }
501 $this->assertEquals($field['weight'], $weight);
502 $weight++;
503 }
504 }
92915c55 505
7c3f2c03
E
506 /**
507 * Check we can submit membership batch profiles (create mode)
508 */
00be9182 509 public function testProfileSubmitMembershipBatch() {
7c3f2c03
E
510 $this->_contactID = $this->individualCreate();
511 $this->callAPISuccess('profile', 'submit', array(
92915c55
TO
512 'profile_id' => 'membership_batch_entry',
513 'financial_type_id' => 1,
514 'membership_type' => $this->_membershipTypeID,
515 'join_date' => 'now',
516 'total_amount' => 10,
517 'contribution_status_id' => 1,
518 'receive_date' => 'now',
519 'contact_id' => $this->_contactID,
7c3f2c03
E
520 ));
521 }
92915c55 522
158d3e03 523 /**
eceb18cc 524 * Set is deprecated but we need to ensure it still works.
158d3e03 525 */
00be9182 526 public function testLegacySet() {
158d3e03 527 $pofileFieldValues = $this->_createIndividualContact();
528 current($pofileFieldValues);
529 $contactId = key($pofileFieldValues);
530
531 $updateParams = array(
532 'first_name' => 'abc2',
533 'last_name' => 'xyz2',
534 'email-Primary' => 'abc2.xyz2@gmail.com',
535 'phone-1-1' => '022 321 826',
536 'country-1' => '1013',
537 'state_province-1' => '1000',
538 );
539
540 $params = array_merge(array(
9da2e77c 541 'profile_id' => $this->_profileID,
158d3e03 542 'contact_id' => $contactId,
543 ), $updateParams);
544
545 $result = $this->callAPISuccess('profile', 'set', $params);
546 $this->assertArrayKeyExists('values', $result);
547 $getParams = array(
9da2e77c 548 'profile_id' => $this->_profileID,
158d3e03 549 'contact_id' => $contactId,
550 );
551 $profileDetails = $this->callAPISuccess('profile', 'get', $getParams);
552
553 foreach ($updateParams as $profileField => $value) {
554 $this->assertEquals($value, CRM_Utils_Array::value($profileField, $profileDetails['values']), "In line " . __LINE__ . " error message: " . "missing/mismatching value for {$profileField}"
555 );
556 }
557 }
c490a46a
CW
558
559 /**
eceb18cc 560 * Check contact activity profile without activity id.
c490a46a 561 */
00be9182 562 public function testContactActivitySubmitWithoutActivityId() {
6a488035
TO
563 list($params, $expected) = $this->_createContactWithActivity();
564
565 $params = array_merge($params, $expected);
566 unset($params['activity_id']);
9dec4e61 567 $result = $this->callAPIFailure('profile', 'submit', $params);
6a488035 568 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: activity_id');
6a488035
TO
569 }
570
c490a46a 571 /**
eceb18cc 572 * Check contact activity profile wrong activity id.
c490a46a 573 */
00be9182 574 public function testContactActivitySubmitWrongActivityId() {
6a488035 575 list($params, $expected) = $this->_createContactWithActivity();
6a488035
TO
576 $params = array_merge($params, $expected);
577 $params['activity_id'] = 100001;
9dec4e61 578 $result = $this->callAPIFailure('profile', 'submit', $params);
6a488035 579 $this->assertEquals($result['error_message'], 'Invalid Activity Id (aid).');
6a488035
TO
580 }
581
c490a46a 582 /**
eceb18cc 583 * Check contact activity profile with wrong activity type.
c490a46a 584 */
00be9182 585 public function testContactActivitySubmitWrongActivityType() {
6a488035
TO
586 //flush cache by calling with reset
587 $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, TRUE, 'name', TRUE);
588
589 $sourceContactId = $this->householdCreate();
590
591 $activityparams = array(
592 'source_contact_id' => $sourceContactId,
593 'activity_type_id' => '2',
594 'subject' => 'Test activity',
595 'activity_date_time' => '20110316',
596 'duration' => '120',
597 'location' => 'Pensulvania',
598 'details' => 'a test activity',
599 'status_id' => '1',
6a488035
TO
600 'priority_id' => '1',
601 );
602
7fbb4198 603 $activity = $this->callAPISuccess('activity', 'create', $activityparams);
6a488035
TO
604
605 $activityValues = array_pop($activity['values']);
606
607 list($params, $expected) = $this->_createContactWithActivity();
608
609 $params = array_merge($params, $expected);
610 $params['activity_id'] = $activityValues['id'];
9dec4e61 611 $result = $this->callAPIFailure('profile', 'submit', $params,
7fbb4198 612 'This activity cannot be edited or viewed via this profile.');
6a488035
TO
613 }
614
c490a46a 615 /**
eceb18cc 616 * Check contact activity profile with success.
c490a46a 617 */
00be9182 618 public function testContactActivitySubmitSuccess() {
6a488035
TO
619 list($params, $expected) = $this->_createContactWithActivity();
620
621 $updateParams = array(
622 'first_name' => 'abc2',
623 'last_name' => 'xyz2',
624 'email-Primary' => 'abc2.xyz2@yahoo.com',
625 'activity_subject' => 'Test Meeting',
626 'activity_details' => 'a test activity details',
627 'activity_duration' => '100',
628 'activity_date_time' => '03/08/2010',
629 'activity_status_id' => '2',
630 );
631 $profileParams = array_merge($params, $updateParams);
c3d3e837
E
632 $this->callAPISuccess('profile', 'submit', $profileParams);
633 $result = $this->callAPISuccess('profile', 'get', $params);
6a488035
TO
634
635 foreach ($updateParams as $profileField => $value) {
636 $this->assertEquals($value, CRM_Utils_Array::value($profileField, $result['values']), "In line " . __LINE__ . " error message: " . "missing/mismatching value for {$profileField}"
637 );
638 }
6a488035
TO
639 }
640
6a488035 641 /**
eceb18cc 642 * Check profile apply Without ProfileId.
6a488035 643 */
00be9182 644 public function testProfileApplyWithoutProfileId() {
6a488035
TO
645 $params = array(
646 'contact_id' => 1,
6a488035 647 );
7fbb4198 648 $result = $this->callAPIFailure('profile', 'apply', $params,
649 'Mandatory key(s) missing from params array: profile_id');
6a488035
TO
650 }
651
652 /**
eceb18cc 653 * Check profile apply with no invalid profile Id.
6a488035 654 */
00be9182 655 public function testProfileApplyInvalidProfileId() {
6a488035
TO
656 $params = array(
657 'contact_id' => 1,
658 'profile_id' => 1000,
6a488035 659 );
d0e1eff2 660 $result = $this->callAPIFailure('profile', 'apply', $params);
6a488035
TO
661 }
662
663 /**
eceb18cc 664 * Check with success.
6a488035 665 */
00be9182 666 public function testProfileApply() {
6a488035
TO
667 $pofileFieldValues = $this->_createIndividualContact();
668 current($pofileFieldValues);
669 $contactId = key($pofileFieldValues);
670
671 $params = array(
9da2e77c 672 'profile_id' => $this->_profileID,
6a488035 673 'contact_id' => $contactId,
6a488035
TO
674 'first_name' => 'abc2',
675 'last_name' => 'xyz2',
676 'email-Primary' => 'abc2.xyz2@gmail.com',
677 'phone-1-1' => '022 321 826',
678 'country-1' => '1013',
679 'state_province-1' => '1000',
680 );
681
7fbb4198 682 $result = $this->callAPIAndDocument('profile', 'apply', $params, __FUNCTION__, __FILE__);
6a488035
TO
683
684 // Expected field values
685 $expected['contact'] = array(
686 'contact_id' => $contactId,
687 'contact_type' => 'Individual',
688 'first_name' => 'abc2',
689 'last_name' => 'xyz2',
690 );
691 $expected['email'] = array(
692 'location_type_id' => 1,
693 'is_primary' => 1,
694 'email' => 'abc2.xyz2@gmail.com',
695 );
696
697 $expected['phone'] = array(
698 'location_type_id' => 1,
699 'is_primary' => 1,
700 'phone_type_id' => 1,
701 'phone' => '022 321 826',
702 );
703 $expected['address'] = array(
704 'location_type_id' => 1,
705 'is_primary' => 1,
706 'country_id' => 1013,
707 'state_province_id' => 1000,
708 );
709
710 foreach ($expected['contact'] as $field => $value) {
711 $this->assertEquals($value, CRM_Utils_Array::value($field, $result['values']), "In line " . __LINE__ . " error message: " . "missing/mismatching value for {$field}"
712 );
713 }
714
715 foreach (array(
92915c55
TO
716 'email',
717 'phone',
a130e045 718 'address',
92915c55 719 ) as $fieldType) {
6a488035
TO
720 $typeValues = array_pop($result['values'][$fieldType]);
721 foreach ($expected[$fieldType] as $field => $value) {
722 $this->assertEquals($value, CRM_Utils_Array::value($field, $typeValues), "In line " . __LINE__ . " error message: " . "missing/mismatching value for {$field} ({$fieldType})"
723 );
724 }
725 }
726 }
727
4cbe18b8 728 /**
c490a46a 729 * Helper function to create an Individual with address/email/phone info. Import UF Group and UF Fields
4cbe18b8
EM
730 * @param array $params
731 *
732 * @return mixed
733 */
00be9182 734 public function _createIndividualContact($params = array()) {
e0efd2d0 735 $contactParams = array_merge(array(
92915c55
TO
736 'first_name' => 'abc1',
737 'last_name' => 'xyz1',
738 'email' => 'abc1.xyz1@yahoo.com',
739 'api.address.create' => array(
740 'location_type_id' => 1,
741 'is_primary' => 1,
742 'street_address' => '5 Saint Helier St',
743 'county' => 'Marin',
86797006 744 'country' => 'UNITED STATES',
92915c55
TO
745 'state_province' => 'Michigan',
746 'supplemental_address_1' => 'Hallmark Ct',
747 'supplemental_address_2' => 'Jersey Village',
748 'postal_code' => '90210',
749 'city' => 'Gotham City',
750 'is_billing' => 0,
751 ),
752 'api.phone.create' => array(
753 'location_type_id' => '1',
754 'phone' => '021 512 755',
755 'phone_type_id' => '1',
756 'is_primary' => '1',
757 ),
758 ), $params
6a488035
TO
759 );
760
174dbdd5 761 $this->_contactID = $this->individualCreate($contactParams);
9dec4e61 762 $this->_createIndividualProfile();
6a488035 763 // expected result of above created profile with contact Id $contactId
174dbdd5 764 $profileData[$this->_contactID] = array(
6a488035
TO
765 'first_name' => 'abc1',
766 'last_name' => 'xyz1',
9da2e77c 767 'email-primary' => 'abc1.xyz1@yahoo.com',
6a488035
TO
768 'phone-1-1' => '021 512 755',
769 'country-1' => '1228',
770 'state_province-1' => '1021',
771 );
772
773 return $profileData;
774 }
775
4cbe18b8
EM
776 /**
777 * @return array
778 */
00be9182 779 public function _createContactWithActivity() {
6a488035
TO
780 // @TODO: Create profile with custom fields
781 $op = new PHPUnit_Extensions_Database_Operation_Insert();
782 $op->execute($this->_dbconn,
bbfd46a5 783 $this->createFlatXMLDataSet(
6a488035
TO
784 dirname(__FILE__) . '/dataset/uf_group_contact_activity_26.xml'
785 )
786 );
787 // hack: xml data set do not accept \ 1 (CRM_Core_DAO::VALUE_SEPARATOR)
788 CRM_Core_DAO::setFieldValue('CRM_Core_DAO_UFGroup', '26', 'group_type', 'Individual,Contact,Activity' . CRM_Core_DAO::VALUE_SEPARATOR . 'ActivityType:1');
789
790 $sourceContactId = $this->individualCreate();
791 $contactParams = array(
792 'first_name' => 'abc1',
793 'last_name' => 'xyz1',
794 'contact_type' => 'Individual',
795 'email' => 'abc1.xyz1@yahoo.com',
6a488035
TO
796 'api.address.create' => array(
797 'location_type_id' => 1,
798 'is_primary' => 1,
799 'name' => 'Saint Helier St',
800 'county' => 'Marin',
86797006 801 'country' => 'UNITED STATES',
6a488035
TO
802 'state_province' => 'Michigan',
803 'supplemental_address_1' => 'Hallmark Ct',
804 'supplemental_address_2' => 'Jersey Village',
805 ),
806 );
807
7fbb4198 808 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
6a488035
TO
809
810 $keys = array_keys($contact['values']);
811 $contactId = array_pop($keys);
812
813 $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'])
814 );
815
816 $activityParams = array(
817 'source_contact_id' => $sourceContactId,
818 'assignee_contact_id' => $contactId,
819 'activity_type_id' => '1',
820 'subject' => 'Make-it-Happen Meeting',
821 'activity_date_time' => '20110316',
822 'duration' => '120',
823 'location' => 'Pensulvania',
824 'details' => 'a test activity',
825 'status_id' => '1',
6a488035
TO
826 'priority_id' => '1',
827 );
7fbb4198 828 $activity = $this->callAPISuccess('activity', 'create', $activityParams);
6a488035
TO
829
830 $activityValues = array_pop($activity['values']);
831
832 // valid parameters for above profile
833 $profileParams = array(
834 'profile_id' => 26,
835 'contact_id' => $contactId,
836 'activity_id' => $activityValues['id'],
21170390 837 );
6a488035
TO
838
839 // expected result of above created profile
840 $expected = array(
841 'first_name' => 'abc1',
842 'last_name' => 'xyz1',
843 'email-Primary' => 'abc1.xyz1@yahoo.com',
844 'activity_subject' => 'Make-it-Happen Meeting',
845 'activity_details' => 'a test activity',
846 'activity_duration' => '120',
847 'activity_date_time_time' => '12:00AM',
848 'activity_date_time' => '03/16/2011',
849 'activity_status_id' => '1',
850 );
851
852 return array($profileParams, $expected);
853 }
92915c55 854
9dec4e61 855 /**
eceb18cc 856 * Create a profile.
9dec4e61 857 */
00be9182 858 public function _createIndividualProfile() {
9da2e77c
E
859
860 // creating these via the api as we want to utilise & test the flushing of caches when fields created
861 // via the api
862
863 $ufGroupParams = array(
6c6e6187 864 'group_type' => 'Individual,Contact', // really we should remove this & test the ufField create sets it
9da2e77c
E
865 'name' => 'test_individual_contact_profile',
866 'title' => 'Flat Coffee',
867 'api.uf_field.create' => array(
868 array(
869 'field_name' => 'first_name',
870 'is_required' => 1,
871 'visibility' => 'Public Pages and Listings',
872 'field_type' => 'Individual',
91d8fc8a 873 'label' => 'First Name',
9da2e77c
E
874 ),
875 array(
876 'field_name' => 'last_name',
877 'is_required' => 1,
878 'visibility' => 'Public Pages and Listings',
879 'field_type' => 'Individual',
91d8fc8a 880 'label' => 'Last Name',
9da2e77c
E
881 ),
882 array(
883 'field_name' => 'email',
884 'is_required' => 1,
885 'visibility' => 'Public Pages and Listings',
886 'field_type' => 'Contact',
887 'label' => 'Email',
888 ),
889 array(
890 'field_name' => 'phone',
891 'is_required' => 1,
892 'visibility' => 'Public Pages and Listings',
893 'field_type' => 'Contact',
894 'location_type_id' => 1,
895 'phone_type_id' => 1,
21dfd5f5 896 'label' => 'Phone',
9da2e77c
E
897 ),
898 array(
899 'field_name' => 'country',
900 'is_required' => 1,
901 'visibility' => 'Public Pages and Listings',
902 'field_type' => 'Contact',
903 'location_type_id' => 1,
21dfd5f5 904 'label' => 'Country',
9da2e77c
E
905 ),
906 array(
907 'field_name' => 'state_province',
908 'is_required' => 1,
909 'visibility' => 'Public Pages and Listings',
910 'field_type' => 'Contact',
911 'location_type_id' => 1,
21dfd5f5 912 'label' => 'State Province',
9da2e77c
E
913 ),
914 array(
915 'field_name' => 'postal_code',
916 'is_required' => 0,
917 'field_type' => 'Contact',
918 'location_type_id' => 1,
21dfd5f5 919 'label' => 'State Province',
92915c55 920 ),
9da2e77c 921 ),
9dec4e61 922 );
9da2e77c
E
923 $profile = $this->callAPISuccess('uf_group', 'create', $ufGroupParams);
924 $this->_profileID = $profile['id'];
9dec4e61 925 }
926
4cbe18b8 927 /**
100fef9d 928 * @param int $profileID
4cbe18b8 929 */
00be9182 930 public function _addCustomFieldToProfile($profileID) {
9dec4e61 931 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, '');
92915c55
TO
932 $this->uFFieldCreate(array(
933 'uf_group_id' => $profileID,
934 'field_name' => 'custom_' . $ids['custom_field_id'],
a130e045 935 'contact_type' => 'Contact',
92915c55 936 ));
9dec4e61 937 }
96025800 938
6a488035 939}