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