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