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