Fix api profile.submit to work with tag & note fields
[civicrm-core.git] / tests / phpunit / api / v3 / ProfileTest.php
CommitLineData
6a488035 1<?php
b6708aeb 2/*
3 +--------------------------------------------------------------------+
2fe49090 4| CiviCRM version 5 |
b6708aeb 5+--------------------------------------------------------------------+
8c9251b3 6| Copyright CiviCRM LLC (c) 2004-2018 |
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();
9c473adf 88 $expected = reset($profileFieldValues);
363544d7 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();
9c473adf 102 $expected = reset($profileFieldValues);
363544d7 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() {
9c473adf
CW
385 $profileFieldValues = $this->_createIndividualContact();
386 $contactId = key($profileFieldValues);
6a488035
TO
387 $updateParams = array(
388 'first_name' => 'abc2',
389 'last_name' => 'xyz2',
390 'phone-1-1' => '022 321 826',
391 'country-1' => '1013',
392 'state_province-1' => '1000',
393 );
394
9da2e77c 395 $params = array_merge(array('profile_id' => $this->_profileID, 'contact_id' => $contactId),
6a488035
TO
396 $updateParams
397 );
398
d235daf6
MM
399 $this->callAPIFailure('profile', 'submit', $params,
400 "Mandatory key(s) missing from params array: email-primary"
7fbb4198 401 );
6a488035
TO
402 }
403
404 /**
eceb18cc 405 * Check with success.
6a488035 406 */
00be9182 407 public function testProfileSubmit() {
9c473adf
CW
408 $profileFieldValues = $this->_createIndividualContact();
409 $contactId = key($profileFieldValues);
6a488035
TO
410
411 $updateParams = array(
412 'first_name' => 'abc2',
413 'last_name' => 'xyz2',
5aa090ea 414 'email-primary' => 'abc2.xyz2@gmail.com',
6a488035
TO
415 'phone-1-1' => '022 321 826',
416 'country-1' => '1013',
417 'state_province-1' => '1000',
418 );
419
420 $params = array_merge(array(
9da2e77c 421 'profile_id' => $this->_profileID,
21170390 422 'contact_id' => $contactId,
423 ), $updateParams);
6a488035 424
363544d7 425 $this->callAPIAndDocument('profile', 'submit', $params, __FUNCTION__, __FILE__);
6a488035
TO
426
427 $getParams = array(
9da2e77c 428 'profile_id' => $this->_profileID,
6a488035 429 'contact_id' => $contactId,
6a488035 430 );
7fbb4198 431 $profileDetails = $this->callAPISuccess('profile', 'get', $getParams);
6a488035
TO
432
433 foreach ($updateParams as $profileField => $value) {
f5bf5e4f 434 $this->assertEquals($value, CRM_Utils_Array::value($profileField, $profileDetails['values']), "missing/mismatching value for {$profileField}"
6a488035
TO
435 );
436 }
f5bf5e4f
E
437 unset($params['email-primary']);
438 $params['email-Primary'] = 'my@mail.com';
6c6e6187 439 $this->callAPISuccess('profile', 'submit', $params);
5aa090ea 440 $profileDetails = $this->callAPISuccess('profile', 'get', $getParams);
f5bf5e4f 441 $this->assertEquals('my@mail.com', $profileDetails['values']['email-Primary']);
6a488035
TO
442 }
443
7c3f2c03
E
444 /**
445 * Ensure caches are being cleared so we don't get into a debugging trap because of cached metadata
446 * First we delete & create to increment the version & then check for caching probs
447 */
00be9182 448 public function testProfileSubmitCheckCaching() {
7c3f2c03
E
449 $this->callAPISuccess('membership_type', 'delete', array('id' => $this->_membershipTypeID));
450 $this->_membershipTypeID = $this->membershipTypeCreate();
451
452 $membershipTypes = $this->callAPISuccess('membership_type', 'get', array());
92915c55
TO
453 $profileFields = $this->callAPISuccess('profile', 'getfields', array(
454 'get_options' => 'all',
455 'action' => 'submit',
a130e045 456 'profile_id' => 'membership_batch_entry',
92915c55
TO
457 ));
458 $getoptions = $this->callAPISuccess('membership', 'getoptions', array(
459 'field' => 'membership_type',
a130e045 460 'context' => 'validate',
92915c55 461 ));
7c3f2c03 462 $this->assertEquals(array_keys($membershipTypes['values']), array_keys($getoptions['values']));
f5c68f3c 463 $this->assertEquals(array_keys($membershipTypes['values']), array_keys($profileFields['values']['membership_type_id']['options']));
7c3f2c03 464
6c6e6187 465 }
f5c68f3c
E
466
467 /**
eceb18cc 468 * Test that the fields are returned in the right order despite the faffing around that goes on.
f5c68f3c 469 */
00be9182 470 public function testMembershipGetFieldsOrder() {
92915c55
TO
471 $result = $this->callAPISuccess('profile', 'getfields', array(
472 'action' => 'submit',
a130e045 473 'profile_id' => 'membership_batch_entry',
92915c55 474 ));
f5c68f3c 475 $weight = 1;
22e263ad
TO
476 foreach ($result['values'] as $fieldName => $field) {
477 if ($fieldName == 'profile_id') {
f5c68f3c
E
478 continue;
479 }
480 $this->assertEquals($field['weight'], $weight);
481 $weight++;
482 }
483 }
92915c55 484
7c3f2c03
E
485 /**
486 * Check we can submit membership batch profiles (create mode)
487 */
00be9182 488 public function testProfileSubmitMembershipBatch() {
7c3f2c03
E
489 $this->_contactID = $this->individualCreate();
490 $this->callAPISuccess('profile', 'submit', array(
92915c55
TO
491 'profile_id' => 'membership_batch_entry',
492 'financial_type_id' => 1,
493 'membership_type' => $this->_membershipTypeID,
494 'join_date' => 'now',
495 'total_amount' => 10,
496 'contribution_status_id' => 1,
497 'receive_date' => 'now',
498 'contact_id' => $this->_contactID,
7c3f2c03
E
499 ));
500 }
92915c55 501
158d3e03 502 /**
eceb18cc 503 * Set is deprecated but we need to ensure it still works.
158d3e03 504 */
00be9182 505 public function testLegacySet() {
9c473adf
CW
506 $profileFieldValues = $this->_createIndividualContact();
507 $contactId = key($profileFieldValues);
158d3e03 508
509 $updateParams = array(
510 'first_name' => 'abc2',
511 'last_name' => 'xyz2',
512 'email-Primary' => 'abc2.xyz2@gmail.com',
513 'phone-1-1' => '022 321 826',
514 'country-1' => '1013',
515 'state_province-1' => '1000',
516 );
517
518 $params = array_merge(array(
9da2e77c 519 'profile_id' => $this->_profileID,
158d3e03 520 'contact_id' => $contactId,
521 ), $updateParams);
522
523 $result = $this->callAPISuccess('profile', 'set', $params);
524 $this->assertArrayKeyExists('values', $result);
525 $getParams = array(
9da2e77c 526 'profile_id' => $this->_profileID,
158d3e03 527 'contact_id' => $contactId,
528 );
529 $profileDetails = $this->callAPISuccess('profile', 'get', $getParams);
530
531 foreach ($updateParams as $profileField => $value) {
532 $this->assertEquals($value, CRM_Utils_Array::value($profileField, $profileDetails['values']), "In line " . __LINE__ . " error message: " . "missing/mismatching value for {$profileField}"
533 );
534 }
535 }
c490a46a
CW
536
537 /**
eceb18cc 538 * Check contact activity profile without activity id.
c490a46a 539 */
00be9182 540 public function testContactActivitySubmitWithoutActivityId() {
6a488035
TO
541 list($params, $expected) = $this->_createContactWithActivity();
542
543 $params = array_merge($params, $expected);
544 unset($params['activity_id']);
9dec4e61 545 $result = $this->callAPIFailure('profile', 'submit', $params);
6a488035 546 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: activity_id');
6a488035
TO
547 }
548
c490a46a 549 /**
eceb18cc 550 * Check contact activity profile wrong activity id.
c490a46a 551 */
00be9182 552 public function testContactActivitySubmitWrongActivityId() {
6a488035 553 list($params, $expected) = $this->_createContactWithActivity();
6a488035
TO
554 $params = array_merge($params, $expected);
555 $params['activity_id'] = 100001;
9dec4e61 556 $result = $this->callAPIFailure('profile', 'submit', $params);
6a488035 557 $this->assertEquals($result['error_message'], 'Invalid Activity Id (aid).');
6a488035
TO
558 }
559
c490a46a 560 /**
eceb18cc 561 * Check contact activity profile with wrong activity type.
c490a46a 562 */
00be9182 563 public function testContactActivitySubmitWrongActivityType() {
6a488035 564 //flush cache by calling with reset
363544d7 565 CRM_Core_PseudoConstant::activityType(TRUE, TRUE, TRUE, 'name', TRUE);
6a488035
TO
566
567 $sourceContactId = $this->householdCreate();
568
569 $activityparams = array(
570 'source_contact_id' => $sourceContactId,
571 'activity_type_id' => '2',
572 'subject' => 'Test activity',
573 'activity_date_time' => '20110316',
574 'duration' => '120',
575 'location' => 'Pensulvania',
576 'details' => 'a test activity',
577 'status_id' => '1',
6a488035
TO
578 'priority_id' => '1',
579 );
580
7fbb4198 581 $activity = $this->callAPISuccess('activity', 'create', $activityparams);
6a488035
TO
582
583 $activityValues = array_pop($activity['values']);
584
585 list($params, $expected) = $this->_createContactWithActivity();
586
587 $params = array_merge($params, $expected);
588 $params['activity_id'] = $activityValues['id'];
363544d7 589 $this->callAPIFailure('profile', 'submit', $params,
7fbb4198 590 'This activity cannot be edited or viewed via this profile.');
6a488035
TO
591 }
592
c490a46a 593 /**
eceb18cc 594 * Check contact activity profile with success.
c490a46a 595 */
00be9182 596 public function testContactActivitySubmitSuccess() {
363544d7 597 list($params) = $this->_createContactWithActivity();
6a488035
TO
598
599 $updateParams = array(
600 'first_name' => 'abc2',
601 'last_name' => 'xyz2',
602 'email-Primary' => 'abc2.xyz2@yahoo.com',
603 'activity_subject' => 'Test Meeting',
604 'activity_details' => 'a test activity details',
605 'activity_duration' => '100',
2a66c165 606 'activity_date_time' => '2010-03-08 00:00:00',
6a488035
TO
607 'activity_status_id' => '2',
608 );
609 $profileParams = array_merge($params, $updateParams);
c3d3e837
E
610 $this->callAPISuccess('profile', 'submit', $profileParams);
611 $result = $this->callAPISuccess('profile', 'get', $params);
6a488035
TO
612
613 foreach ($updateParams as $profileField => $value) {
363544d7 614 $this->assertEquals($value, CRM_Utils_Array::value($profileField, $result['values']), " error message: " . "missing/mismatching value for {$profileField}"
6a488035
TO
615 );
616 }
6a488035
TO
617 }
618
6a488035 619 /**
eceb18cc 620 * Check profile apply Without ProfileId.
6a488035 621 */
00be9182 622 public function testProfileApplyWithoutProfileId() {
6a488035
TO
623 $params = array(
624 'contact_id' => 1,
6a488035 625 );
363544d7 626 $this->callAPIFailure('profile', 'apply', $params,
7fbb4198 627 'Mandatory key(s) missing from params array: profile_id');
6a488035
TO
628 }
629
630 /**
eceb18cc 631 * Check profile apply with no invalid profile Id.
6a488035 632 */
00be9182 633 public function testProfileApplyInvalidProfileId() {
6a488035
TO
634 $params = array(
635 'contact_id' => 1,
636 'profile_id' => 1000,
6a488035 637 );
363544d7 638 $this->callAPIFailure('profile', 'apply', $params);
6a488035
TO
639 }
640
641 /**
eceb18cc 642 * Check with success.
6a488035 643 */
00be9182 644 public function testProfileApply() {
363544d7 645 $profileFieldValues = $this->_createIndividualContact();
646 current($profileFieldValues);
647 $contactId = key($profileFieldValues);
6a488035
TO
648
649 $params = array(
9da2e77c 650 'profile_id' => $this->_profileID,
6a488035 651 'contact_id' => $contactId,
6a488035
TO
652 'first_name' => 'abc2',
653 'last_name' => 'xyz2',
654 'email-Primary' => 'abc2.xyz2@gmail.com',
655 'phone-1-1' => '022 321 826',
656 'country-1' => '1013',
657 'state_province-1' => '1000',
658 );
659
7fbb4198 660 $result = $this->callAPIAndDocument('profile', 'apply', $params, __FUNCTION__, __FILE__);
6a488035
TO
661
662 // Expected field values
663 $expected['contact'] = array(
664 'contact_id' => $contactId,
665 'contact_type' => 'Individual',
666 'first_name' => 'abc2',
667 'last_name' => 'xyz2',
668 );
669 $expected['email'] = array(
670 'location_type_id' => 1,
671 'is_primary' => 1,
672 'email' => 'abc2.xyz2@gmail.com',
673 );
674
675 $expected['phone'] = array(
676 'location_type_id' => 1,
677 'is_primary' => 1,
678 'phone_type_id' => 1,
679 'phone' => '022 321 826',
680 );
681 $expected['address'] = array(
682 'location_type_id' => 1,
683 'is_primary' => 1,
684 'country_id' => 1013,
685 'state_province_id' => 1000,
686 );
687
688 foreach ($expected['contact'] as $field => $value) {
689 $this->assertEquals($value, CRM_Utils_Array::value($field, $result['values']), "In line " . __LINE__ . " error message: " . "missing/mismatching value for {$field}"
690 );
691 }
692
693 foreach (array(
92915c55
TO
694 'email',
695 'phone',
a130e045 696 'address',
92915c55 697 ) as $fieldType) {
6a488035
TO
698 $typeValues = array_pop($result['values'][$fieldType]);
699 foreach ($expected[$fieldType] as $field => $value) {
700 $this->assertEquals($value, CRM_Utils_Array::value($field, $typeValues), "In line " . __LINE__ . " error message: " . "missing/mismatching value for {$field} ({$fieldType})"
701 );
702 }
703 }
704 }
705
9c473adf
CW
706 /**
707 * Check success with tags.
708 */
709 public function testSubmitWithTags() {
710 $profileFieldValues = $this->_createIndividualContact();
711 $params = reset($profileFieldValues);
712 $contactId = key($profileFieldValues);
713 $params['profile_id'] = $this->_profileID;
714 $params['contact_id'] = $contactId;
715
716 $this->callAPISuccess('ufField', 'create', array(
717 'uf_group_id' => $this->_profileID,
718 'field_name' => 'tag',
719 'visibility' => 'Public Pages and Listings',
720 'field_type' => 'Contact',
721 'label' => 'Tags',
722 ));
723
724 $tag_1 = $this->callAPISuccess('tag', 'create', ['name' => 'abc'])['id'];
725 $tag_2 = $this->callAPISuccess('tag', 'create', ['name' => 'def'])['id'];
726
727 $params['tag'] = "$tag_1,$tag_2";
728 $result = $this->callAPISuccess('profile', 'submit', $params);
729
730 $tags = $this->callAPISuccess('entityTag', 'get', ['entity_id' => $contactId]);
731 $this->assertEquals(2, $tags['count']);
732
733 $params['tag'] = [$tag_1];
734 $result = $this->callAPISuccess('profile', 'submit', $params);
735
736 $tags = $this->callAPISuccess('entityTag', 'get', ['entity_id' => $contactId]);
737 $this->assertEquals(1, $tags['count']);
738 }
739
740 /**
741 * Check success with a note.
742 */
743 public function testSubmitWithNote() {
744 $profileFieldValues = $this->_createIndividualContact();
745 $params = reset($profileFieldValues);
746 $contactId = key($profileFieldValues);
747 $params['profile_id'] = $this->_profileID;
748 $params['contact_id'] = $contactId;
749
750 $this->callAPISuccess('ufField', 'create', array(
751 'uf_group_id' => $this->_profileID,
752 'field_name' => 'note',
753 'visibility' => 'Public Pages and Listings',
754 'field_type' => 'Contact',
755 'label' => 'Note',
756 ));
757
758 $params['note'] = "Hello 123";
759 $this->callAPISuccess('profile', 'submit', $params);
760
761 $note = $this->callAPISuccessGetSingle('note', ['entity_id' => $contactId]);
762 $this->assertEquals("Hello 123", $note['note']);
763 }
764
4cbe18b8 765 /**
c490a46a 766 * Helper function to create an Individual with address/email/phone info. Import UF Group and UF Fields
4cbe18b8
EM
767 * @param array $params
768 *
769 * @return mixed
770 */
00be9182 771 public function _createIndividualContact($params = array()) {
e0efd2d0 772 $contactParams = array_merge(array(
92915c55
TO
773 'first_name' => 'abc1',
774 'last_name' => 'xyz1',
775 'email' => 'abc1.xyz1@yahoo.com',
776 'api.address.create' => array(
777 'location_type_id' => 1,
778 'is_primary' => 1,
779 'street_address' => '5 Saint Helier St',
780 'county' => 'Marin',
86797006 781 'country' => 'UNITED STATES',
92915c55
TO
782 'state_province' => 'Michigan',
783 'supplemental_address_1' => 'Hallmark Ct',
784 'supplemental_address_2' => 'Jersey Village',
207f62c6 785 'supplemental_address_3' => 'My Town',
92915c55
TO
786 'postal_code' => '90210',
787 'city' => 'Gotham City',
788 'is_billing' => 0,
789 ),
790 'api.phone.create' => array(
791 'location_type_id' => '1',
792 'phone' => '021 512 755',
793 'phone_type_id' => '1',
794 'is_primary' => '1',
795 ),
796 ), $params
6a488035
TO
797 );
798
174dbdd5 799 $this->_contactID = $this->individualCreate($contactParams);
9dec4e61 800 $this->_createIndividualProfile();
6a488035 801 // expected result of above created profile with contact Id $contactId
174dbdd5 802 $profileData[$this->_contactID] = array(
6a488035
TO
803 'first_name' => 'abc1',
804 'last_name' => 'xyz1',
9da2e77c 805 'email-primary' => 'abc1.xyz1@yahoo.com',
6a488035
TO
806 'phone-1-1' => '021 512 755',
807 'country-1' => '1228',
808 'state_province-1' => '1021',
809 );
810
811 return $profileData;
812 }
813
4cbe18b8
EM
814 /**
815 * @return array
816 */
00be9182 817 public function _createContactWithActivity() {
6a488035
TO
818 // @TODO: Create profile with custom fields
819 $op = new PHPUnit_Extensions_Database_Operation_Insert();
820 $op->execute($this->_dbconn,
bbfd46a5 821 $this->createFlatXMLDataSet(
6a488035
TO
822 dirname(__FILE__) . '/dataset/uf_group_contact_activity_26.xml'
823 )
824 );
825 // hack: xml data set do not accept \ 1 (CRM_Core_DAO::VALUE_SEPARATOR)
826 CRM_Core_DAO::setFieldValue('CRM_Core_DAO_UFGroup', '26', 'group_type', 'Individual,Contact,Activity' . CRM_Core_DAO::VALUE_SEPARATOR . 'ActivityType:1');
827
828 $sourceContactId = $this->individualCreate();
829 $contactParams = array(
830 'first_name' => 'abc1',
831 'last_name' => 'xyz1',
832 'contact_type' => 'Individual',
833 'email' => 'abc1.xyz1@yahoo.com',
6a488035
TO
834 'api.address.create' => array(
835 'location_type_id' => 1,
836 'is_primary' => 1,
837 'name' => 'Saint Helier St',
838 'county' => 'Marin',
86797006 839 'country' => 'UNITED STATES',
6a488035
TO
840 'state_province' => 'Michigan',
841 'supplemental_address_1' => 'Hallmark Ct',
842 'supplemental_address_2' => 'Jersey Village',
207f62c6 843 'supplemental_address_3' => 'My Town',
6a488035
TO
844 ),
845 );
846
7fbb4198 847 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
6a488035
TO
848
849 $keys = array_keys($contact['values']);
850 $contactId = array_pop($keys);
851
363544d7 852 $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
853 );
854
855 $activityParams = array(
856 'source_contact_id' => $sourceContactId,
857 'assignee_contact_id' => $contactId,
858 'activity_type_id' => '1',
859 'subject' => 'Make-it-Happen Meeting',
2a66c165 860 'activity_date_time' => '2011-03-16 00:00:00',
6a488035
TO
861 'duration' => '120',
862 'location' => 'Pensulvania',
863 'details' => 'a test activity',
864 'status_id' => '1',
6a488035
TO
865 'priority_id' => '1',
866 );
7fbb4198 867 $activity = $this->callAPISuccess('activity', 'create', $activityParams);
6a488035
TO
868
869 $activityValues = array_pop($activity['values']);
870
871 // valid parameters for above profile
872 $profileParams = array(
873 'profile_id' => 26,
874 'contact_id' => $contactId,
875 'activity_id' => $activityValues['id'],
21170390 876 );
6a488035
TO
877
878 // expected result of above created profile
879 $expected = array(
880 'first_name' => 'abc1',
881 'last_name' => 'xyz1',
882 'email-Primary' => 'abc1.xyz1@yahoo.com',
883 'activity_subject' => 'Make-it-Happen Meeting',
884 'activity_details' => 'a test activity',
885 'activity_duration' => '120',
2a66c165 886 'activity_date_time' => '2011-03-16 00:00:00',
6a488035
TO
887 'activity_status_id' => '1',
888 );
889
890 return array($profileParams, $expected);
891 }
92915c55 892
9dec4e61 893 /**
eceb18cc 894 * Create a profile.
9dec4e61 895 */
00be9182 896 public function _createIndividualProfile() {
9da2e77c 897 $ufGroupParams = array(
363544d7 898 'group_type' => 'Individual,Contact',
899 // really we should remove this & test the ufField create sets it
9da2e77c
E
900 'name' => 'test_individual_contact_profile',
901 'title' => 'Flat Coffee',
902 'api.uf_field.create' => array(
903 array(
904 'field_name' => 'first_name',
905 'is_required' => 1,
906 'visibility' => 'Public Pages and Listings',
907 'field_type' => 'Individual',
91d8fc8a 908 'label' => 'First Name',
9da2e77c
E
909 ),
910 array(
911 'field_name' => 'last_name',
912 'is_required' => 1,
913 'visibility' => 'Public Pages and Listings',
914 'field_type' => 'Individual',
91d8fc8a 915 'label' => 'Last Name',
9da2e77c
E
916 ),
917 array(
918 'field_name' => 'email',
919 'is_required' => 1,
920 'visibility' => 'Public Pages and Listings',
921 'field_type' => 'Contact',
922 'label' => 'Email',
923 ),
924 array(
925 'field_name' => 'phone',
926 'is_required' => 1,
927 'visibility' => 'Public Pages and Listings',
928 'field_type' => 'Contact',
929 'location_type_id' => 1,
930 'phone_type_id' => 1,
21dfd5f5 931 'label' => 'Phone',
9da2e77c
E
932 ),
933 array(
934 'field_name' => 'country',
935 'is_required' => 1,
936 'visibility' => 'Public Pages and Listings',
937 'field_type' => 'Contact',
938 'location_type_id' => 1,
21dfd5f5 939 'label' => 'Country',
9da2e77c
E
940 ),
941 array(
942 'field_name' => 'state_province',
943 'is_required' => 1,
944 'visibility' => 'Public Pages and Listings',
945 'field_type' => 'Contact',
946 'location_type_id' => 1,
21dfd5f5 947 'label' => 'State Province',
9da2e77c
E
948 ),
949 array(
950 'field_name' => 'postal_code',
951 'is_required' => 0,
952 'field_type' => 'Contact',
953 'location_type_id' => 1,
21dfd5f5 954 'label' => 'State Province',
92915c55 955 ),
9da2e77c 956 ),
9dec4e61 957 );
9da2e77c
E
958 $profile = $this->callAPISuccess('uf_group', 'create', $ufGroupParams);
959 $this->_profileID = $profile['id'];
9dec4e61 960 }
961
4cbe18b8 962 /**
100fef9d 963 * @param int $profileID
4cbe18b8 964 */
00be9182 965 public function _addCustomFieldToProfile($profileID) {
9dec4e61 966 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, '');
92915c55
TO
967 $this->uFFieldCreate(array(
968 'uf_group_id' => $profileID,
969 'field_name' => 'custom_' . $ids['custom_field_id'],
a130e045 970 'contact_type' => 'Contact',
92915c55 971 ));
9dec4e61 972 }
96025800 973
6a488035 974}