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