CRM-17361 -- Extend Contact getoptions to include Address fields
[civicrm-core.git] / tests / phpunit / api / v3 / ContactTest.php
CommitLineData
6a488035
TO
1<?php
2/**
381fa321 3 * @file
4 * File for the TestContact class.
6a488035
TO
5 *
6 * (PHP 5)
7 *
6c6e6187
TO
8 * @author Walt Haas <walt@dharmatech.org> (801) 534-1262
9 * @copyright Copyright CiviCRM LLC (C) 2009
10 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html
6a488035 11 * GNU Affero General Public License version 3
6c6e6187
TO
12 * @version $Id: ContactTest.php 31254 2010-12-15 10:09:29Z eileen $
13 * @package CiviCRM
6a488035
TO
14 *
15 * This file is part of CiviCRM
16 *
17 * CiviCRM is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU Affero General Public License
19 * as published by the Free Software Foundation; either version 3 of
20 * the License, or (at your option) any later version.
21 *
22 * CiviCRM is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU Affero General Public License for more details.
26 *
27 * You should have received a copy of the GNU Affero General Public
28 * License along with this program. If not, see
29 * <http://www.gnu.org/licenses/>.
30 */
31
32/**
33 * Include class definitions
34 */
35require_once 'CiviTest/CiviUnitTestCase.php';
36
37
38/**
39 * Test APIv3 civicrm_contact* functions
40 *
6c6e6187
TO
41 * @package CiviCRM_APIv3
42 * @subpackage API_Contact
6a488035 43 */
6a488035
TO
44class api_v3_ContactTest extends CiviUnitTestCase {
45 public $DBResetRequired = FALSE;
46 protected $_apiversion;
47 protected $_entity;
48 protected $_params;
b7c9bc4c 49
f6722559 50 protected $_contactID;
6c6e6187 51 protected $_financialTypeId = 1;
6a488035 52
6a488035 53 /**
381fa321 54 * Test setup for every test.
6a488035 55 *
d177a2a6
EM
56 * Connect to the database, truncate the tables that will be used
57 * and redirect stdin to a temporary file
6a488035
TO
58 */
59 public function setUp() {
381fa321 60 // Connect to the database.
6a488035
TO
61 parent::setUp();
62 $this->_apiversion = 3;
f6722559 63 $this->_entity = 'contact';
64 $this->_params = array(
6a488035
TO
65 'first_name' => 'abc1',
66 'contact_type' => 'Individual',
67 'last_name' => 'xyz1',
6a488035 68 );
6a488035
TO
69 }
70
701a69da 71 /**
72 * Restore the DB for the next test.
73 *
74 * @throws \Exception
75 */
00be9182 76 public function tearDown() {
6a488035
TO
77 // truncate a few tables
78 $tablesToTruncate = array(
79 'civicrm_contact',
80 'civicrm_email',
81 'civicrm_contribution',
82 'civicrm_line_item',
83 'civicrm_website',
e635f9d4
TO
84 'civicrm_relationship',
85 'civicrm_uf_match',
405f289b 86 'civicrm_phone',
e9e27a80 87 'civicrm_address',
6a488035
TO
88 );
89
f6722559 90 $this->quickCleanup($tablesToTruncate, TRUE);
6a488035
TO
91 }
92
93 /**
381fa321 94 * Test civicrm_contact_create.
6a488035 95 *
381fa321 96 * Verify that attempt to create individual contact with only
97 * first and last names succeeds
6a488035 98 */
00be9182 99 public function testAddCreateIndividual() {
6a488035
TO
100 $oldCount = CRM_Core_DAO::singleValueQuery('select count(*) from civicrm_contact');
101 $params = array(
102 'first_name' => 'abc1',
103 'contact_type' => 'Individual',
104 'last_name' => 'xyz1',
6a488035
TO
105 );
106
f6722559 107 $contact = $this->callAPISuccess('contact', 'create', $params);
fe482240
EM
108 $this->assertTrue(is_numeric($contact['id']));
109 $this->assertTrue($contact['id'] > 0);
6a488035 110 $newCount = CRM_Core_DAO::singleValueQuery('select count(*) from civicrm_contact');
6c6e6187 111 $this->assertEquals($oldCount + 1, $newCount);
6a488035 112
6a488035
TO
113 $this->assertDBState('CRM_Contact_DAO_Contact',
114 $contact['id'],
115 $params
116 );
117 }
118
b99f9616 119 /**
120 * Test civicrm_contact_create.
121 *
122 * Verify that preferred language can be set.
123 */
124 public function testAddCreateIndividualWithPreferredLanguage() {
125 $params = array(
126 'first_name' => 'abc1',
127 'contact_type' => 'Individual',
128 'last_name' => 'xyz1',
129 'preferred_language' => 'es_ES',
130 );
131
132 $contact = $this->callAPISuccess('contact', 'create', $params);
133 $this->getAndCheck($params, $contact['id'], 'Contact');
134 }
135
6a488035 136 /**
381fa321 137 * Test civicrm_contact_create with sub-types.
6a488035 138 *
381fa321 139 * Verify that sub-types are created successfully and not deleted by subsequent updates.
6a488035 140 */
00be9182 141 public function testIndividualSubType() {
6a488035
TO
142 $params = array(
143 'first_name' => 'test abc',
144 'contact_type' => 'Individual',
145 'last_name' => 'test xyz',
146 'contact_sub_type' => array('Student', 'Staff'),
5896d037 147 );
f6722559 148 $contact = $this->callAPISuccess('contact', 'create', $params);
6a488035
TO
149 $cid = $contact['id'];
150
151 $params = array(
152 'id' => $cid,
153 'middle_name' => 'foo',
6a488035 154 );
f6722559 155 $this->callAPISuccess('contact', 'create', $params);
6a488035
TO
156 unset($params['middle_name']);
157
f6722559 158 $contact = $this->callAPISuccess('contact', 'get', $params);
6a488035 159
fe482240 160 $this->assertEquals(array('Student', 'Staff'), $contact['values'][$cid]['contact_sub_type']);
6a488035
TO
161 }
162
163 /**
381fa321 164 * Verify that attempt to create contact with empty params fails.
6a488035 165 */
00be9182 166 public function testCreateEmptyContact() {
f6722559 167 $this->callAPIFailure('contact', 'create', array());
6a488035
TO
168 }
169
170 /**
381fa321 171 * Verify that attempt to create contact with bad contact type fails.
6a488035 172 */
00be9182 173 public function testCreateBadTypeContact() {
6a488035
TO
174 $params = array(
175 'email' => 'man1@yahoo.com',
176 'contact_type' => 'Does not Exist',
6a488035 177 );
6c6e6187 178 $this->callAPIFailure('contact', 'create', $params, "'Does not Exist' is not a valid option for field contact_type");
6a488035
TO
179 }
180
181 /**
381fa321 182 * Verify that attempt to create individual contact without required fields fails.
6a488035 183 */
00be9182 184 public function testCreateBadRequiredFieldsIndividual() {
6a488035
TO
185 $params = array(
186 'middle_name' => 'This field is not required',
187 'contact_type' => 'Individual',
188 );
4b58ed3b 189 $this->callAPIFailure('contact', 'create', $params);
6a488035
TO
190 }
191
192 /**
381fa321 193 * Verify that attempt to create household contact without required fields fails.
6a488035 194 */
00be9182 195 public function testCreateBadRequiredFieldsHousehold() {
6a488035
TO
196 $params = array(
197 'middle_name' => 'This field is not required',
198 'contact_type' => 'Household',
199 );
4b58ed3b 200 $this->callAPIFailure('contact', 'create', $params);
6a488035
TO
201 }
202
203 /**
381fa321 204 * Test required field check.
205 *
206 * Verify that attempt to create organization contact without required fields fails.
6a488035 207 */
00be9182 208 public function testCreateBadRequiredFieldsOrganization() {
6a488035
TO
209 $params = array(
210 'middle_name' => 'This field is not required',
211 'contact_type' => 'Organization',
212 );
213
4b58ed3b 214 $this->callAPIFailure('contact', 'create', $params);
6a488035
TO
215 }
216
217 /**
381fa321 218 * Verify that attempt to create individual contact with only an email succeeds.
6a488035 219 */
00be9182 220 public function testCreateEmailIndividual() {
6a488035
TO
221
222 $params = array(
223 'email' => 'man3@yahoo.com',
224 'contact_type' => 'Individual',
225 'location_type_id' => 1,
6a488035
TO
226 );
227
f6722559 228 $contact = $this->callAPISuccess('contact', 'create', $params);
229
fe482240 230 $this->assertEquals(1, $contact['id']);
f6722559 231 $email = $this->callAPISuccess('email', 'get', array('contact_id' => $contact['id']));
232 $this->assertEquals(1, $email['count']);
233 $this->assertEquals('man3@yahoo.com', $email['values'][$email['id']]['email']);
6a488035 234
f6722559 235 $this->callAPISuccess('contact', 'delete', $contact);
6a488035
TO
236 }
237
238 /**
381fa321 239 * Test creating individual by name.
240 *
241 * Verify create individual contact with only first and last names succeeds.
6a488035 242 */
00be9182 243 public function testCreateNameIndividual() {
6a488035
TO
244 $params = array(
245 'first_name' => 'abc1',
246 'contact_type' => 'Individual',
247 'last_name' => 'xyz1',
6a488035 248 );
6a488035 249
f6722559 250 $contact = $this->callAPISuccess('contact', 'create', $params);
251 $this->assertEquals(1, $contact['id']);
6a488035
TO
252 }
253
c10e7177 254 /**
255 * Test creating individual by display_name.
256 *
257 * Display name & sort name should be set.
258 */
259 public function testCreateDisplayNameIndividual() {
260 $params = array(
261 'display_name' => 'abc1',
262 'contact_type' => 'Individual',
263 );
264
265 $contact = $this->callAPISuccess('contact', 'create', $params);
266 $params['sort_name'] = 'abc1';
267 $this->getAndCheck($params, $contact['id'], 'contact');
268 }
269
6a488035 270 /**
381fa321 271 * Test old keys still work.
272 *
273 * Verify that attempt to create individual contact with
d177a2a6 274 * first and last names and old key values works
6a488035 275 */
00be9182 276 public function testCreateNameIndividualOldKeys() {
6a488035
TO
277 $params = array(
278 'individual_prefix' => 'Dr.',
279 'first_name' => 'abc1',
280 'contact_type' => 'Individual',
281 'last_name' => 'xyz1',
282 'individual_suffix' => 'Jr.',
6a488035
TO
283 );
284
f6722559 285 $contact = $this->callAPISuccess('contact', 'create', $params);
6ecbca5b 286 $result = $this->callAPISuccess('contact', 'getsingle', array('id' => $contact['id']));
fd651abc 287
a1255c80
CW
288 $this->assertArrayKeyExists('prefix_id', $result);
289 $this->assertArrayKeyExists('suffix_id', $result);
290 $this->assertArrayKeyExists('gender_id', $result);
291 $this->assertEquals(4, $result['prefix_id']);
292 $this->assertEquals(1, $result['suffix_id']);
6a488035
TO
293 }
294
295 /**
381fa321 296 * Test preferred keys work.
297 *
298 * Verify that attempt to create individual contact with
d177a2a6 299 * first and last names and old key values works
6a488035 300 */
381fa321 301 public function testCreateNameIndividualRecommendedKeys2() {
6a488035
TO
302 $params = array(
303 'prefix_id' => 'Dr.',
304 'first_name' => 'abc1',
305 'contact_type' => 'Individual',
306 'last_name' => 'xyz1',
307 'suffix_id' => 'Jr.',
308 'gender_id' => 'Male',
6a488035
TO
309 );
310
f6722559 311 $contact = $this->callAPISuccess('contact', 'create', $params);
6ecbca5b 312 $result = $this->callAPISuccess('contact', 'getsingle', array('id' => $contact['id']));
fd651abc 313
a1255c80
CW
314 $this->assertArrayKeyExists('prefix_id', $result);
315 $this->assertArrayKeyExists('suffix_id', $result);
316 $this->assertArrayKeyExists('gender_id', $result);
317 $this->assertEquals(4, $result['prefix_id']);
318 $this->assertEquals(1, $result['suffix_id']);
6a488035
TO
319 }
320
321 /**
381fa321 322 * Test household name is sufficient for create.
323 *
324 * Verify that attempt to create household contact with only
d177a2a6 325 * household name succeeds
6a488035 326 */
00be9182 327 public function testCreateNameHousehold() {
6a488035
TO
328 $params = array(
329 'household_name' => 'The abc Household',
330 'contact_type' => 'Household',
6a488035 331 );
f6722559 332 $contact = $this->callAPISuccess('contact', 'create', $params);
fe482240 333 $this->assertEquals(1, $contact['id']);
6a488035
TO
334 }
335
336 /**
381fa321 337 * Test organization name is sufficient for create.
338 *
339 * Verify that attempt to create organization contact with only
d177a2a6 340 * organization name succeeds.
6a488035 341 */
00be9182 342 public function testCreateNameOrganization() {
6a488035
TO
343 $params = array(
344 'organization_name' => 'The abc Organization',
345 'contact_type' => 'Organization',
6a488035 346 );
f6722559 347 $contact = $this->callAPISuccess('contact', 'create', $params);
348 $this->assertEquals(1, $contact['id']);
6a488035 349 }
5896d037 350
6a488035 351 /**
381fa321 352 * Verify that attempt to create organization contact without organization name fails.
6a488035 353 */
00be9182 354 public function testCreateNoNameOrganization() {
6a488035
TO
355 $params = array(
356 'first_name' => 'The abc Organization',
357 'contact_type' => 'Organization',
6a488035 358 );
4b58ed3b 359 $this->callAPIFailure('contact', 'create', $params);
6a488035 360 }
5896d037 361
6a488035 362 /**
381fa321 363 * Check with complete array + custom field.
364 *
6a488035
TO
365 * Note that the test is written on purpose without any
366 * variables specific to participant so it can be replicated into other entities
367 * and / or moved to the automated test suite
368 */
00be9182 369 public function testCreateWithCustom() {
6a488035
TO
370 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
371
372 $params = $this->_params;
373 $params['custom_' . $ids['custom_field_id']] = "custom string";
5c49fee0 374 $description = "This demonstrates setting a custom field through the API.";
fb32de45 375 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__, $description);
6a488035 376
5896d037 377 $check = $this->callAPISuccess($this->_entity, 'get', array(
92915c55
TO
378 'return.custom_' . $ids['custom_field_id'] => 1,
379 'id' => $result['id'],
380 ));
4b58ed3b 381 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']]);
6a488035
TO
382
383 $this->customFieldDelete($ids['custom_field_id']);
384 $this->customGroupDelete($ids['custom_group_id']);
385 }
386
fe6daa04 387 /**
381fa321 388 * CRM-12773 - expectation is that civicrm quietly ignores fields without values.
fe6daa04 389 */
00be9182 390 public function testCreateWithNULLCustomCRM12773() {
fe6daa04 391 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
392 $params = $this->_params;
393 $params['custom_' . $ids['custom_field_id']] = NULL;
4b58ed3b 394 $this->callAPISuccess('contact', 'create', $params);
fe6daa04 395 $this->customFieldDelete($ids['custom_field_id']);
396 $this->customGroupDelete($ids['custom_group_id']);
397 }
398
9747df8a 399 /**
400 * CRM-14232 test preferred language set to site default if not passed.
401 */
402 public function testCreatePreferredLanguageUnset() {
403 $this->callAPISuccess('Contact', 'create', array(
404 'first_name' => 'Snoop',
405 'last_name' => 'Dog',
406 'contact_type' => 'Individual')
407 );
408 $result = $this->callAPISuccessGetSingle('Contact', array('last_name' => 'Dog'));
409 $this->assertEquals('en_US', $result['preferred_language']);
410 }
411
412 /**
413 * CRM-14232 test preferred language returns setting if not passed.
414 */
415 public function testCreatePreferredLanguageSet() {
416 $this->callAPISuccess('Setting', 'create', array('contact_default_language' => 'fr_FR'));
417 $this->callAPISuccess('Contact', 'create', array(
418 'first_name' => 'Snoop',
419 'last_name' => 'Dog',
420 'contact_type' => 'Individual',
421 ));
422 $result = $this->callAPISuccessGetSingle('Contact', array('last_name' => 'Dog'));
423 $this->assertEquals('fr_FR', $result['preferred_language']);
424 }
425
426 /**
427 * CRM-14232 test preferred language returns setting if not passed where setting is NULL.
428 */
429 public function testCreatePreferredLanguageNull() {
430 $this->callAPISuccess('Setting', 'create', array('contact_default_language' => 'null'));
431 $this->callAPISuccess('Contact', 'create', array(
432 'first_name' => 'Snoop',
433 'last_name' => 'Dog',
434 'contact_type' => 'Individual',
435 )
436 );
437 $result = $this->callAPISuccessGetSingle('Contact', array('last_name' => 'Dog'));
438 $this->assertEquals(NULL, $result['preferred_language']);
439 }
440
441 /**
442 * CRM-14232 test preferred language returns setting if not passed where setting is NULL.
443 */
444 public function testCreatePreferredLanguagePassed() {
445 $this->callAPISuccess('Setting', 'create', array('contact_default_language' => 'null'));
446 $this->callAPISuccess('Contact', 'create', array(
447 'first_name' => 'Snoop',
448 'last_name' => 'Dog',
449 'contact_type' => 'Individual',
450 'preferred_language' => 'en_AU',
451 ));
452 $result = $this->callAPISuccessGetSingle('Contact', array('last_name' => 'Dog'));
453 $this->assertEquals('en_AU', $result['preferred_language']);
454 }
455
2930d67a 456 /**
457 * CRM-15792 - create/update datetime field for contact.
458 */
459 public function testCreateContactCustomFldDateTime() {
460 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'datetime_test_group'));
461 $dateTime = CRM_Utils_Date::currentDBDate();
462 //check date custom field is saved along with time when time_format is set
463 $params = array(
464 'first_name' => 'abc3',
465 'last_name' => 'xyz3',
466 'contact_type' => 'Individual',
467 'email' => 'man3@yahoo.com',
468 'api.CustomField.create' => array(
469 'custom_group_id' => $customGroup['id'],
470 'name' => 'test_datetime',
471 'label' => 'Demo Date',
472 'html_type' => 'Select Date',
473 'data_type' => 'Date',
474 'time_format' => 2,
475 'weight' => 4,
476 'is_required' => 1,
477 'is_searchable' => 0,
478 'is_active' => 1,
479 ),
480 );
481
2930d67a 482 $result = $this->callAPIAndDocument('Contact', 'create', $params, __FUNCTION__, __FILE__);
483 $customFldId = $result['values'][$result['id']]['api.CustomField.create']['id'];
ba4a1892 484 $this->assertNotNull($result['id']);
a15773db 485 $this->assertNotNull($customFldId);
2930d67a 486
487 $params = array(
488 'id' => $result['id'],
489 "custom_{$customFldId}" => $dateTime,
490 'api.CustomValue.get' => 1,
491 );
492
493 $result = $this->callAPIAndDocument('Contact', 'create', $params, __FUNCTION__, __FILE__);
ba4a1892 494 $this->assertNotNull($result['id']);
2930d67a 495 $customFldDate = date("YmdHis", strtotime($result['values'][$result['id']]['api.CustomValue.get']['values'][0]['latest']));
a15773db 496 $this->assertNotNull($customFldDate);
2930d67a 497 $this->assertEquals($dateTime, $customFldDate);
498 $customValueId = $result['values'][$result['id']]['api.CustomValue.get']['values'][0]['id'];
499 $dateTime = date('Ymd');
500 //date custom field should not contain time part when time_format is null
501 $params = array(
502 'id' => $result['id'],
503 'api.CustomField.create' => array(
41755648 504 'id' => $customFldId,
2930d67a 505 'html_type' => 'Select Date',
506 'data_type' => 'Date',
507 'time_format' => '',
508 ),
509 'api.CustomValue.create' => array(
510 'id' => $customValueId,
511 'entity_id' => $result['id'],
512 "custom_{$customFldId}" => $dateTime,
513 ),
514 'api.CustomValue.get' => 1,
515 );
516 $result = $this->callAPIAndDocument('Contact', 'create', $params, __FUNCTION__, __FILE__);
ba4a1892 517 $this->assertNotNull($result['id']);
2930d67a 518 $customFldDate = date("Ymd", strtotime($result['values'][$result['id']]['api.CustomValue.get']['values'][0]['latest']));
519 $customFldTime = date("His", strtotime($result['values'][$result['id']]['api.CustomValue.get']['values'][0]['latest']));
a15773db 520 $this->assertNotNull($customFldDate);
2930d67a 521 $this->assertEquals($dateTime, $customFldDate);
522 $this->assertEquals(000000, $customFldTime);
84b51197 523 $this->callAPIAndDocument('Contact', 'create', $params, __FUNCTION__, __FILE__);
2930d67a 524 }
525
fe6daa04 526
d424ffde 527 /**
381fa321 528 * Test creating a current employer through API.
6a488035 529 */
5896d037 530 public function testContactCreateCurrentEmployer() {
381fa321 531 // Here we will just do the get for set-up purposes.
f6722559 532 $count = $this->callAPISuccess('contact', 'getcount', array(
6a488035 533 'organization_name' => 'new employer org',
21dfd5f5 534 'contact_type' => 'Organization',
6a488035
TO
535 ));
536 $this->assertEquals(0, $count);
f6722559 537 $employerResult = $this->callAPISuccess('contact', 'create', array_merge($this->_params, array(
5896d037
TO
538 'current_employer' => 'new employer org',
539 )
6a488035 540 ));
bb043d6f
E
541 // do it again as an update to check it doesn't cause an error
542 $employerResult = $this->callAPISuccess('contact', 'create', array_merge($this->_params, array(
6c6e6187 543 'current_employer' => 'new employer org',
21dfd5f5 544 'id' => $employerResult['id'],
5896d037 545 )
bb043d6f 546 ));
f6722559 547 $expectedCount = 1;
4b58ed3b 548 $this->callAPISuccess('contact', 'getcount', array(
5896d037 549 'organization_name' => 'new employer org',
21dfd5f5 550 'contact_type' => 'Organization',
5896d037
TO
551 ),
552 $expectedCount);
6a488035 553
f6722559 554 $result = $this->callAPISuccess('contact', 'getsingle', array(
6a488035
TO
555 'id' => $employerResult['id'],
556 ));
557
558 $this->assertEquals('new employer org', $result['current_employer']);
559
560 }
5896d037 561
d424ffde 562 /**
381fa321 563 * Test creating a current employer through API.
564 *
565 * Check it will re-activate a de-activated employer
d424ffde 566 */
5896d037 567 public function testContactCreateDuplicateCurrentEmployerEnables() {
381fa321 568 // Set up - create employer relationship.
bb043d6f 569 $employerResult = $this->callAPISuccess('contact', 'create', array_merge($this->_params, array(
5896d037
TO
570 'current_employer' => 'new employer org',
571 )
bb043d6f 572 ));
6c6e6187 573 $relationship = $this->callAPISuccess('relationship', 'get', array(
bb043d6f 574 'contact_id_a' => $employerResult['id'],
6c6e6187 575 ));
bb043d6f
E
576
577 //disable & check it is disabled
578 $this->callAPISuccess('relationship', 'create', array('id' => $relationship['id'], 'is_active' => 0));
6c6e6187 579 $this->callAPISuccess('relationship', 'getvalue', array(
bb043d6f 580 'id' => $relationship['id'],
21dfd5f5 581 'return' => 'is_active',
bb043d6f
E
582 ), 0);
583
381fa321 584 // Re-set the current employer - thus enabling the relationship.
4b58ed3b 585 $this->callAPISuccess('contact', 'create', array_merge($this->_params, array(
6c6e6187 586 'current_employer' => 'new employer org',
21dfd5f5 587 'id' => $employerResult['id'],
5896d037 588 )
bb043d6f
E
589 ));
590 //check is_active is now 1
6c6e6187 591 $relationship = $this->callAPISuccess('relationship', 'getsingle', array(
5896d037
TO
592 'return' => 'is_active',
593 ));
6c6e6187 594 $this->assertEquals(1, $relationship['is_active']);
bb043d6f
E
595 }
596
8f32b005 597 /**
381fa321 598 * Check deceased contacts are not retrieved.
599 *
600 * Note at time of writing the default is to return default. This should possibly be changed & test added.
8f32b005 601 */
00be9182 602 public function testGetDeceasedRetrieved() {
4b58ed3b 603 $this->callAPISuccess($this->_entity, 'create', $this->_params);
5896d037 604 $c2 = $this->callAPISuccess($this->_entity, 'create', array(
92915c55
TO
605 'first_name' => 'bb',
606 'last_name' => 'ccc',
607 'contact_type' => 'Individual',
608 'is_deceased' => 1,
609 ));
8f32b005 610 $result = $this->callAPISuccess($this->_entity, 'get', array('is_deceased' => 0));
611 $this->assertFalse(array_key_exists($c2['id'], $result['values']));
612 }
6a488035 613
c490a46a 614 /**
381fa321 615 * Test that sort works - old syntax.
c490a46a 616 */
00be9182 617 public function testGetSort() {
f6722559 618 $c1 = $this->callAPISuccess($this->_entity, 'create', $this->_params);
5896d037 619 $c2 = $this->callAPISuccess($this->_entity, 'create', array(
92915c55
TO
620 'first_name' => 'bb',
621 'last_name' => 'ccc',
622 'contact_type' => 'Individual',
623 ));
5896d037
TO
624 $result = $this->callAPISuccess($this->_entity, 'get', array(
625 'sort' => 'first_name ASC',
626 'return.first_name' => 1,
627 'sequential' => 1,
628 'rowCount' => 1,
629 ));
6a488035
TO
630
631 $this->assertEquals('abc1', $result['values'][0]['first_name']);
f6722559 632 $result = $this->callAPISuccess($this->_entity, 'get', array(
633 'sort' => 'first_name DESC',
634 'return.first_name' => 1,
635 'sequential' => 1,
636 'rowCount' => 1,
637 ));
6a488035
TO
638 $this->assertEquals('bb', $result['values'][0]['first_name']);
639
f6722559 640 $this->callAPISuccess($this->_entity, 'delete', array('id' => $c1['id']));
641 $this->callAPISuccess($this->_entity, 'delete', array('id' => $c2['id']));
6a488035 642 }
5896d037 643
d424ffde 644 /**
381fa321 645 * Test that we can retrieve contacts using array syntax.
646 *
647 * I.e 'id' => array('IN' => array('3,4')).
d424ffde 648 */
00be9182 649 public function testGetINIDArray() {
78c0bfc0 650 $c1 = $this->callAPISuccess($this->_entity, 'create', $this->_params);
5896d037 651 $c2 = $this->callAPISuccess($this->_entity, 'create', array(
92915c55
TO
652 'first_name' => 'bb',
653 'last_name' => 'ccc',
654 'contact_type' => 'Individual',
655 ));
5896d037 656 $c3 = $this->callAPISuccess($this->_entity, 'create', array(
92915c55
TO
657 'first_name' => 'hh',
658 'last_name' => 'll',
659 'contact_type' => 'Individual',
660 ));
78c0bfc0 661 $result = $this->callAPISuccess($this->_entity, 'get', array('id' => array('IN' => array($c1['id'], $c3['id']))));
662 $this->assertEquals(2, $result['count']);
663 $this->assertEquals(array($c1['id'], $c3['id']), array_keys($result['values']));
664 $this->callAPISuccess($this->_entity, 'delete', array('id' => $c1['id']));
665 $this->callAPISuccess($this->_entity, 'delete', array('id' => $c2['id']));
666 $this->callAPISuccess($this->_entity, 'delete', array('id' => $c3['id']));
667 }
5896d037 668
d424ffde 669 /**
381fa321 670 * Test variants on deleted behaviour.
6a488035 671 */
00be9182 672 public function testGetDeleted() {
6a488035 673 $params = $this->_params;
f6722559 674 $contact1 = $this->callAPISuccess('contact', 'create', $params);
6a488035
TO
675 $params['is_deleted'] = 1;
676 $params['last_name'] = 'bcd';
f6722559 677 $contact2 = $this->callAPISuccess('contact', 'create', $params);
678 $countActive = $this->callAPISuccess('contact', 'getcount', array('showAll' => 'active'));
679 $countAll = $this->callAPISuccess('contact', 'getcount', array('showAll' => 'all'));
680 $countTrash = $this->callAPISuccess('contact', 'getcount', array('showAll' => 'trash'));
681 $countDefault = $this->callAPISuccess('contact', 'getcount', array());
682 $countDeleted = $this->callAPISuccess('contact', 'getcount', array(
683 'contact_is_deleted' => 1,
5896d037 684 ));
f6722559 685 $countNotDeleted = $this->callAPISuccess('contact', 'getcount', array(
686 'contact_is_deleted' => 0,
5896d037 687 ));
f6722559 688 $this->callAPISuccess('contact', 'delete', array('id' => $contact1['id']));
689 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
6a488035 690 $this->assertEquals(1, $countNotDeleted, 'contact_is_deleted => 0 is respected in line ' . __LINE__);
fe482240
EM
691 $this->assertEquals(1, $countActive);
692 $this->assertEquals(1, $countTrash);
693 $this->assertEquals(2, $countAll);
694 $this->assertEquals(1, $countDeleted);
6a488035
TO
695 $this->assertEquals(1, $countDefault, 'Only active by default in line ' . __LINE__);
696 }
c490a46a
CW
697
698 /**
381fa321 699 * Test that sort works - new syntax.
c490a46a 700 */
381fa321 701 public function testGetSortNewSyntax() {
5896d037
TO
702 $c1 = $this->callAPISuccess($this->_entity, 'create', $this->_params);
703 $c2 = $this->callAPISuccess($this->_entity, 'create', array(
92915c55
TO
704 'first_name' => 'bb',
705 'last_name' => 'ccc',
706 'contact_type' => 'Individual',
707 ));
f6722559 708 $result = $this->callAPISuccess($this->_entity, 'getvalue', array(
6a488035 709 'return' => 'first_name',
5896d037
TO
710 'options' => array(
711 'limit' => 1,
712 'sort' => 'first_name',
713 ),
714 ));
6a488035
TO
715 $this->assertEquals('abc1', $result, 'in line' . __LINE__);
716
f6722559 717 $result = $this->callAPISuccess($this->_entity, 'getvalue', array(
5896d037
TO
718 'return' => 'first_name',
719 'options' => array(
720 'limit' => 1,
721 'sort' => 'first_name DESC',
722 ),
723 ));
6a488035
TO
724 $this->assertEquals('bb', $result);
725
f6722559 726 $this->callAPISuccess($this->_entity, 'delete', array('id' => $c1['id']));
727 $this->callAPISuccess($this->_entity, 'delete', array('id' => $c2['id']));
6a488035 728 }
c490a46a 729
c4bc3d5a
JV
730 /**
731 * Test sort and limit for chained relationship get.
732 *
733 * https://issues.civicrm.org/jira/browse/CRM-15983
734 */
735 public function testSortLimitChainedRelationshipGetCRM15983() {
736 // Some contact
737 $create_result_1 = $this->callAPISuccess('contact', 'create', array(
738 'first_name' => 'Jules',
739 'last_name' => 'Smos',
740 'contact_type' => 'Individual',
741 ));
742
743 // Create another contact with two relationships.
744 $create_params = array(
745 'first_name' => 'Jos',
746 'last_name' => 'Smos',
747 'contact_type' => 'Individual',
748 'api.relationship.create' => array(
749 array(
750 'contact_id_a' => '$value.id',
751 'contact_id_b' => $create_result_1['id'],
752 // spouse of:
753 'relationship_type_id' => 2,
754 'start_date' => '2005-01-12',
755 'end_date' => '2006-01-11',
756 'description' => 'old',
757 ),
758 array(
759 'contact_id_a' => '$value.id',
760 'contact_id_b' => $create_result_1['id'],
761 // spouse of (was married twice :))
762 'relationship_type_id' => 2,
763 'start_date' => '2006-07-01',
764 'end_date' => '2010-07-01',
765 'description' => 'new',
766 ),
767 ),
768 );
769 $create_result = $this->callAPISuccess('contact', 'create', $create_params);
770
771 // Try to retrieve the contact and the most recent relationship.
772 $get_params = array(
773 'sequential' => 1,
774 'id' => $create_result['id'],
775 'api.relationship.get' => array(
776 'contact_id_a' => '$value.id',
777 'options' => array(
778 'limit' => '1',
779 'sort' => 'start_date DESC',
780 )),
781 );
782 $get_result = $this->callAPISuccess('contact', 'getsingle', $get_params);
783
784 // Clean up.
785 $this->callAPISuccess('contact', 'delete', array(
786 'id' => $create_result['id'],
787 ));
788
789 // Assert.
790 $this->assertEquals(1, $get_result['api.relationship.get']['count']);
791 $this->assertEquals('new', $get_result['api.relationship.get']['values'][0]['description']);
792 }
793
c490a46a 794 /**
381fa321 795 * Test apostrophe works in get & create.
6a488035 796 */
00be9182 797 public function testGetApostropheCRM10857() {
6a488035 798 $params = array_merge($this->_params, array('last_name' => "O'Connor"));
4b58ed3b 799 $this->callAPISuccess($this->_entity, 'create', $params);
f6722559 800 $result = $this->callAPISuccess($this->_entity, 'getsingle', array(
6a488035
TO
801 'last_name' => "O'Connor",
802 'sequential' => 1,
803 ));
804 $this->assertEquals("O'Connor", $result['last_name'], 'in line' . __LINE__);
805 }
806
807 /**
381fa321 808 * Check with complete array + custom field.
809 *
6a488035
TO
810 * Note that the test is written on purpose without any
811 * variables specific to participant so it can be replicated into other entities
812 * and / or moved to the automated test suite
813 */
00be9182 814 public function testGetWithCustom() {
6a488035
TO
815 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
816
817 $params = $this->_params;
818 $params['custom_' . $ids['custom_field_id']] = "custom string";
5c49fee0 819 $description = "This demonstrates setting a custom field through the API.";
6a488035 820 $subfile = "CustomFieldGet";
f6722559 821 $result = $this->callAPISuccess($this->_entity, 'create', $params);
6a488035 822
5896d037 823 $check = $this->callAPIAndDocument($this->_entity, 'get', array(
92915c55
TO
824 'return.custom_' . $ids['custom_field_id'] => 1,
825 'id' => $result['id'],
826 ), __FUNCTION__, __FILE__, $description, $subfile);
6a488035 827
4b58ed3b 828 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']]);
f6722559 829 $fields = ($this->callAPISuccess('contact', 'getfields', $params));
6a488035
TO
830 $this->assertTrue(is_array($fields['values']['custom_' . $ids['custom_field_id']]));
831 $this->customFieldDelete($ids['custom_field_id']);
832 $this->customGroupDelete($ids['custom_group_id']);
833 }
c490a46a
CW
834
835 /**
381fa321 836 * Check with complete array + custom field.
837 *
c490a46a
CW
838 * Note that the test is written on purpose without any
839 * variables specific to participant so it can be replicated into other entities
840 * and / or moved to the automated test suite
841 */
00be9182 842 public function testGetWithCustomReturnSyntax() {
6a488035
TO
843 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
844
845 $params = $this->_params;
846 $params['custom_' . $ids['custom_field_id']] = "custom string";
5c49fee0 847 $description = "This demonstrates setting a custom field through the API.";
6a488035 848 $subfile = "CustomFieldGetReturnSyntaxVariation";
f6722559 849 $result = $this->callAPISuccess($this->_entity, 'create', $params);
850 $params = array('return' => 'custom_' . $ids['custom_field_id'], 'id' => $result['id']);
851 $check = $this->callAPIAndDocument($this->_entity, 'get', $params, __FUNCTION__, __FILE__, $description, $subfile);
6a488035 852
43ef1263 853 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']]);
6a488035
TO
854 $this->customFieldDelete($ids['custom_field_id']);
855 $this->customGroupDelete($ids['custom_group_id']);
856 }
857
43ef1263 858 /**
381fa321 859 * Check that address name is returned if required.
43ef1263 860 */
5896d037 861 public function testGetReturnAddressName() {
43ef1263 862 $contactID = $this->individualCreate();
5896d037 863 $this->callAPISuccess('address', 'create', array(
92915c55
TO
864 'contact_id' => $contactID,
865 'address_name' => 'My house',
866 'location_type_id' => 'Home',
867 'street_address' => '1 my road',
868 ));
5896d037 869 $result = $this->callAPISuccessGetSingle('contact', array(
92915c55
TO
870 'return' => 'address_name, street_address',
871 'id' => $contactID,
872 ));
43ef1263
EM
873 $this->assertEquals('1 my road', $result['street_address']);
874 $this->assertEquals('My house', $result['address_name']);
875
876 }
877
701a69da 878 /**
879 * Test group filter syntaxes.
880 */
00be9182 881 public function testGetGroupIDFromContact() {
5896d037 882 $groupId = $this->groupCreate();
5c49fee0 883 $description = "Get all from group and display contacts.";
5896d037
TO
884 $subFile = "GroupFilterUsingContactAPI";
885 $params = array(
6a488035
TO
886 'email' => 'man2@yahoo.com',
887 'contact_type' => 'Individual',
888 'location_type_id' => 1,
6a488035
TO
889 'api.group_contact.create' => array('group_id' => $groupId),
890 );
891
4b58ed3b 892 $this->callAPISuccess('contact', 'create', $params);
6a488035
TO
893 // testing as integer
894 $params = array(
895 'filter.group_id' => $groupId,
6a488035
TO
896 'contact_type' => 'Individual',
897 );
4b58ed3b 898 $result = $this->callAPIAndDocument('contact', 'get', $params, __FUNCTION__, __FILE__, $description, $subFile);
6a488035
TO
899 $this->assertEquals(1, $result['count']);
900 // group 26 doesn't exist, but we can still search contacts in it.
901 $params = array(
902 'filter.group_id' => 26,
6a488035
TO
903 'contact_type' => 'Individual',
904 );
4b58ed3b 905 $this->callAPISuccess('contact', 'get', $params);
6a488035
TO
906 // testing as string
907 $params = array(
f6722559 908 'filter.group_id' => "$groupId, 26",
6a488035
TO
909 'contact_type' => 'Individual',
910 );
4b58ed3b 911 $result = $this->callAPIAndDocument('contact', 'get', $params, __FUNCTION__, __FILE__, $description, $subFile);
6a488035
TO
912 $this->assertEquals(1, $result['count']);
913 $params = array(
914 'filter.group_id' => "26,27",
6a488035
TO
915 'contact_type' => 'Individual',
916 );
4b58ed3b 917 $this->callAPISuccess('contact', 'get', $params);
6a488035
TO
918
919 // testing as string
6c6e6187 920 $params = array(
5896d037 921 'filter.group_id' => array($groupId, 26),
6a488035
TO
922 'contact_type' => 'Individual',
923 );
4b58ed3b 924 $result = $this->callAPIAndDocument('contact', 'get', $params, __FUNCTION__, __FILE__, $description, $subFile);
6a488035
TO
925 $this->assertEquals(1, $result['count']);
926
927 //test in conjunction with other criteria
6c6e6187 928 $params = array(
5896d037 929 'filter.group_id' => array($groupId, 26),
6a488035
TO
930 'contact_type' => 'Organization',
931 );
6c6e6187
TO
932 $this->callAPISuccess('contact', 'get', $params);
933 $params = array(
5896d037 934 'filter.group_id' => array(26, 27),
6a488035
TO
935 'contact_type' => 'Individual',
936 );
f6722559 937 $result = $this->callAPISuccess('contact', 'get', $params);
4b58ed3b 938 $this->assertEquals(0, $result['count']);
6a488035
TO
939 }
940
941 /**
381fa321 942 * Verify that attempt to create individual contact with two chained websites succeeds.
6a488035 943 */
00be9182 944 public function testCreateIndividualWithContributionDottedSyntax() {
5c49fee0 945 $description = "This demonstrates the syntax to create 2 chained entities.";
5896d037
TO
946 $subFile = "ChainTwoWebsites";
947 $params = array(
6a488035
TO
948 'first_name' => 'abc3',
949 'last_name' => 'xyz3',
950 'contact_type' => 'Individual',
951 'email' => 'man3@yahoo.com',
6a488035
TO
952 'api.contribution.create' => array(
953 'receive_date' => '2010-01-01',
954 'total_amount' => 100.00,
5896d037 955 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
956 'payment_instrument_id' => 1,
957 'non_deductible_amount' => 10.00,
958 'fee_amount' => 50.00,
959 'net_amount' => 90.00,
960 'trxn_id' => 15345,
961 'invoice_id' => 67990,
962 'source' => 'SSF',
963 'contribution_status_id' => 1,
964 ),
965 'api.website.create' => array(
966 'url' => "http://civicrm.org",
967 ),
968 'api.website.create.2' => array(
969 'url' => "http://chained.org",
970 ),
971 );
972
4b58ed3b 973 $result = $this->callAPIAndDocument('Contact', 'create', $params, __FUNCTION__, __FILE__, $description, $subFile);
6a488035 974
381fa321 975 $this->assertEquals(1, $result['id']);
f6722559 976 // checking child function result not covered in callAPIAndDocument
977 $this->assertAPISuccess($result['values'][$result['id']]['api.website.create']);
fe482240
EM
978 $this->assertEquals("http://chained.org", $result['values'][$result['id']]['api.website.create.2']['values'][0]['url']);
979 $this->assertEquals("http://civicrm.org", $result['values'][$result['id']]['api.website.create']['values'][0]['url']);
6a488035
TO
980
981 // delete the contact
f6722559 982 $this->callAPISuccess('contact', 'delete', $result);
6a488035
TO
983 }
984
985 /**
381fa321 986 * Verify that attempt to create individual contact with chained contribution and website succeeds.
6a488035 987 */
00be9182 988 public function testCreateIndividualWithContributionChainedArrays() {
6a488035
TO
989 $params = array(
990 'first_name' => 'abc3',
991 'last_name' => 'xyz3',
992 'contact_type' => 'Individual',
993 'email' => 'man3@yahoo.com',
6a488035
TO
994 'api.contribution.create' => array(
995 'receive_date' => '2010-01-01',
996 'total_amount' => 100.00,
5896d037 997 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
998 'payment_instrument_id' => 1,
999 'non_deductible_amount' => 10.00,
1000 'fee_amount' => 50.00,
1001 'net_amount' => 90.00,
1002 'trxn_id' => 12345,
1003 'invoice_id' => 67890,
1004 'source' => 'SSF',
1005 'contribution_status_id' => 1,
1006 ),
1007 'api.website.create' => array(
1008 array(
1009 'url' => "http://civicrm.org",
1010 ),
1011 array(
1012 'url' => "http://chained.org",
1013 'website_type_id' => 2,
1014 ),
1015 ),
1016 );
1017
5c49fee0 1018 $description = "Demonstrates creating two websites as an array.";
5896d037
TO
1019 $subfile = "ChainTwoWebsitesSyntax2";
1020 $result = $this->callAPIAndDocument('Contact', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
f6722559 1021
1022 $this->assertEquals(1, $result['id']);
1023 // the callAndDocument doesn't check the chained call
fe482240
EM
1024 $this->assertEquals(0, $result['values'][$result['id']]['api.website.create'][0]['is_error']);
1025 $this->assertEquals("http://chained.org", $result['values'][$result['id']]['api.website.create'][1]['values'][0]['url']);
1026 $this->assertEquals("http://civicrm.org", $result['values'][$result['id']]['api.website.create'][0]['values'][0]['url']);
6a488035 1027
f6722559 1028 $this->callAPISuccess('contact', 'delete', $result);
6a488035
TO
1029 }
1030
92d270fd
JV
1031 /**
1032 * Test for direction when chaining relationships.
1033 *
1034 * https://issues.civicrm.org/jira/browse/CRM-16084
1035 */
1036 public function testDirectionChainingRelationshipsCRM16084() {
1037 // Some contact, called Jules.
1038 $create_result_1 = $this->callAPISuccess('contact', 'create', array(
1039 'first_name' => 'Jules',
1040 'last_name' => 'Smos',
1041 'contact_type' => 'Individual',
1042 ));
1043
1044 // Another contact: Jos, child of Jules.
1045 $create_params = array(
1046 'first_name' => 'Jos',
1047 'last_name' => 'Smos',
1048 'contact_type' => 'Individual',
1049 'api.relationship.create' => array(
1050 array(
1051 'contact_id_a' => '$value.id',
1052 'contact_id_b' => $create_result_1['id'],
1053 // child of
1054 'relationship_type_id' => 1,
1055 ),
1056 ),
1057 );
1058 $create_result_2 = $this->callAPISuccess('contact', 'create', $create_params);
1059
1060 // Mia is the child of Jos.
1061 $create_params = array(
1062 'first_name' => 'Mia',
1063 'last_name' => 'Smos',
1064 'contact_type' => 'Individual',
1065 'api.relationship.create' => array(
1066 array(
1067 'contact_id_a' => '$value.id',
1068 'contact_id_b' => $create_result_2['id'],
1069 // child of
1070 'relationship_type_id' => 1,
1071 ),
1072 ),
1073 );
1074 $create_result_3 = $this->callAPISuccess('contact', 'create', $create_params);
1075
1076 // Get Jos and his children.
1077 $get_params = array(
1078 'sequential' => 1,
1079 'id' => $create_result_2['id'],
1080 'api.relationship.get' => array(
1081 'contact_id_b' => '$value.id',
1082 'relationship_type_id' => 1,
1083 ),
1084 );
1085 $get_result = $this->callAPISuccess('contact', 'getsingle', $get_params);
1086
1087 // Clean up first.
1088 $this->callAPISuccess('contact', 'delete', array(
1089 'id' => $create_result_1['id'],
1090 ));
1091 $this->callAPISuccess('contact', 'delete', array(
1092 'id' => $create_result_2['id'],
1093 ));
1094 $this->callAPISuccess('contact', 'delete', array(
1095 'id' => $create_result_2['id'],
1096 ));
1097
1098 // Assert.
1099 $this->assertEquals(1, $get_result['api.relationship.get']['count']);
1100 $this->assertEquals($create_result_3['id'], $get_result['api.relationship.get']['values'][0]['contact_id_a']);
1101 }
1102
6a488035 1103 /**
fe482240 1104 * Verify that attempt to create individual contact with first, and last names and email succeeds.
6a488035 1105 */
00be9182 1106 public function testCreateIndividualWithNameEmail() {
6a488035
TO
1107 $params = array(
1108 'first_name' => 'abc3',
1109 'last_name' => 'xyz3',
1110 'contact_type' => 'Individual',
1111 'email' => 'man3@yahoo.com',
6a488035
TO
1112 );
1113
f6722559 1114 $contact = $this->callAPISuccess('contact', 'create', $params);
fe482240 1115 $this->assertEquals(1, $contact['id']);
6a488035 1116
f6722559 1117 $this->callAPISuccess('contact', 'delete', $contact);
6a488035 1118 }
5896d037 1119
6a488035 1120 /**
eceb18cc 1121 * Verify that attempt to create individual contact with no data fails.
6a488035 1122 */
00be9182 1123 public function testCreateIndividualWithOutNameEmail() {
6a488035
TO
1124 $params = array(
1125 'contact_type' => 'Individual',
6a488035 1126 );
4b58ed3b 1127 $this->callAPIFailure('contact', 'create', $params);
6a488035 1128 }
5896d037 1129
6a488035 1130 /**
fe482240 1131 * Test create individual contact with first &last names, email and location type succeeds.
6a488035 1132 */
00be9182 1133 public function testCreateIndividualWithNameEmailLocationType() {
6a488035
TO
1134 $params = array(
1135 'first_name' => 'abc4',
1136 'last_name' => 'xyz4',
1137 'email' => 'man4@yahoo.com',
1138 'contact_type' => 'Individual',
1139 'location_type_id' => 1,
6a488035 1140 );
f6722559 1141 $result = $this->callAPISuccess('contact', 'create', $params);
6a488035 1142
fe482240 1143 $this->assertEquals(1, $result['id']);
6a488035 1144
f6722559 1145 $this->callAPISuccess('contact', 'delete', array('id' => $result['id']));
6a488035
TO
1146 }
1147
1148 /**
fe482240 1149 * Verify that when changing employers the old employer relationship becomes inactive.
6a488035 1150 */
00be9182 1151 public function testCreateIndividualWithEmployer() {
6a488035
TO
1152 $employer = $this->organizationCreate();
1153 $employer2 = $this->organizationCreate();
1154
1155 $params = array(
5896d037
TO
1156 'email' => 'man4@yahoo.com',
1157 'contact_type' => 'Individual',
1158 'employer_id' => $employer,
6a488035
TO
1159 );
1160
f6722559 1161 $result = $this->callAPISuccess('contact', 'create', $params);
1162 $relationships = $this->callAPISuccess('relationship', 'get', array(
6a488035
TO
1163 'contact_id_a' => $result['id'],
1164 'sequential' => 1,
1165 ));
1166
1167 $this->assertEquals($employer, $relationships['values'][0]['contact_id_b']);
1168
1169 // Add more random relationships to make the test more realistic
4b58ed3b
EM
1170 foreach (array('Employee of', 'Volunteer for') as $relationshipType) {
1171 $relTypeId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', $relationshipType, 'id', 'name_a_b');
1172 $this->callAPISuccess('relationship', 'create', array(
6a488035
TO
1173 'contact_id_a' => $result['id'],
1174 'contact_id_b' => $this->organizationCreate(),
1175 'is_active' => 1,
1176 'relationship_type_id' => $relTypeId,
1177 ));
6c6e6187 1178 }
6a488035
TO
1179
1180 // Add second employer
1181 $params['employer_id'] = $employer2;
1182 $params['id'] = $result['id'];
f6722559 1183 $result = $this->callAPISuccess('contact', 'create', $params);
6a488035 1184
f6722559 1185 $relationships = $this->callAPISuccess('relationship', 'get', array(
6a488035
TO
1186 'contact_id_a' => $result['id'],
1187 'sequential' => 1,
1188 'is_active' => 0,
1189 ));
1190
1191 $this->assertEquals($employer, $relationships['values'][0]['contact_id_b']);
1192 }
1193
1194 /**
fe482240 1195 * Verify that attempt to create household contact with details succeeds.
6a488035 1196 */
00be9182 1197 public function testCreateHouseholdDetails() {
6a488035
TO
1198 $params = array(
1199 'household_name' => 'abc8\'s House',
1200 'nick_name' => 'x House',
1201 'email' => 'man8@yahoo.com',
1202 'contact_type' => 'Household',
6a488035
TO
1203 );
1204
f6722559 1205 $contact = $this->callAPISuccess('contact', 'create', $params);
1206
fe482240 1207 $this->assertEquals(1, $contact['id']);
6a488035 1208
f6722559 1209 $this->callAPISuccess('contact', 'delete', $contact);
6a488035 1210 }
5896d037 1211
6a488035 1212 /**
381fa321 1213 * Verify that attempt to create household contact with inadequate details fails.
6a488035 1214 */
00be9182 1215 public function testCreateHouseholdInadequateDetails() {
6a488035
TO
1216 $params = array(
1217 'nick_name' => 'x House',
1218 'email' => 'man8@yahoo.com',
1219 'contact_type' => 'Household',
6a488035 1220 );
4b58ed3b 1221 $this->callAPIFailure('contact', 'create', $params);
6a488035
TO
1222 }
1223
6a488035 1224 /**
381fa321 1225 * Verify successful update of individual contact.
6a488035 1226 */
00be9182 1227 public function testUpdateIndividualWithAll() {
381fa321 1228 // Insert a row in civicrm_contact creating individual contact.
6a488035
TO
1229 $op = new PHPUnit_Extensions_Database_Operation_Insert();
1230 $op->execute($this->_dbconn,
bbfd46a5 1231 $this->createXMLDataSet(
6a488035
TO
1232 dirname(__FILE__) . '/dataset/contact_ind.xml'
1233 )
1234 );
1235
1236 $params = array(
1237 'id' => 23,
1238 'first_name' => 'abcd',
1239 'contact_type' => 'Individual',
1240 'nick_name' => 'This is nickname first',
1241 'do_not_email' => '1',
1242 'do_not_phone' => '1',
1243 'do_not_mail' => '1',
1244 'do_not_trade' => '1',
1245 'legal_identifier' => 'ABC23853ZZ2235',
1246 'external_identifier' => '1928837465',
1247 'image_URL' => 'http://some.url.com/image.jpg',
1248 'home_url' => 'http://www.example.org',
f6722559 1249
1250 );
43ef1263 1251
4b58ed3b 1252 $this->callAPISuccess('Contact', 'Update', $params);
f6722559 1253 $getResult = $this->callAPISuccess('Contact', 'Get', $params);
6a488035
TO
1254 unset($params['contact_id']);
1255 //Todo - neither API v2 or V3 are testing for home_url - not sure if it is being set.
4b58ed3b 1256 //reducing this test partially back to api v2 level to get it through
6a488035
TO
1257 unset($params['home_url']);
1258 foreach ($params as $key => $value) {
4b58ed3b 1259 $this->assertEquals($value, $getResult['values'][23][$key]);
6a488035 1260 }
381fa321 1261 // Check updated civicrm_contact against expected.
bbfd46a5 1262 $expected = $this->createXMLDataSet(
6a488035
TO
1263 dirname(__FILE__) . '/dataset/contact_ind_upd.xml'
1264 );
bbfd46a5 1265 $actual = new PHPUnit_Extensions_Database_DataSet_QueryDataSet(
6a488035
TO
1266 $this->_dbconn
1267 );
1268 $actual->addTable('civicrm_contact');
1269 $expected->matches($actual);
1270 }
1271
1272 /**
eceb18cc 1273 * Verify successful update of organization contact.
6a488035 1274 */
00be9182 1275 public function testUpdateOrganizationWithAll() {
381fa321 1276 // Insert a row in civicrm_contact creating organization contact
6a488035
TO
1277 $op = new PHPUnit_Extensions_Database_Operation_Insert();
1278 $op->execute($this->_dbconn,
bbfd46a5 1279 $this->createXMLDataSet(
6a488035
TO
1280 dirname(__FILE__) . '/dataset/contact_org.xml'
1281 )
1282 );
1283
1284 $params = array(
1285 'id' => 24,
1286 'organization_name' => 'WebAccess India Pvt Ltd',
1287 'legal_name' => 'WebAccess',
1288 'sic_code' => 'ABC12DEF',
1289 'contact_type' => 'Organization',
6a488035
TO
1290 );
1291
4b58ed3b 1292 $this->callAPISuccess('Contact', 'Update', $params);
6a488035 1293
381fa321 1294 // Check updated civicrm_contact against expected.
bbfd46a5 1295 $expected = $this->createXMLDataSet(
6a488035
TO
1296 dirname(__FILE__) . '/dataset/contact_org_upd.xml'
1297 );
bbfd46a5 1298 $actual = new PHPUnit_Extensions_Database_DataSet_QueryDataSet(
6a488035
TO
1299 $this->_dbconn
1300 );
1301 $actual->addTable('civicrm_contact');
1302 $expected->matches($actual);
1303 }
1304
1305 /**
381fa321 1306 * Verify successful update of household contact.
6a488035 1307 */
381fa321 1308 public function testUpdateHouseholdWithAll() {
1309 // Insert a row in civicrm_contact creating household contact
6a488035
TO
1310 $op = new PHPUnit_Extensions_Database_Operation_Insert();
1311 $op->execute($this->_dbconn,
bbfd46a5 1312 $this->createXMLDataSet(
6a488035
TO
1313 dirname(__FILE__) . '/dataset/contact_hld.xml'
1314 )
1315 );
1316
1317 $params = array(
1318 'id' => 25,
1319 'household_name' => 'ABC household',
1320 'nick_name' => 'ABC House',
1321 'contact_type' => 'Household',
6a488035
TO
1322 );
1323
f6722559 1324 $result = $this->callAPISuccess('Contact', 'Update', $params);
6a488035 1325
43ef1263
EM
1326 $expected = array(
1327 'contact_type' => 'Household',
1328 'is_opt_out' => 0,
1329 'sort_name' => 'ABC household',
1330 'display_name' => 'ABC household',
1331 'nick_name' => 'ABC House',
6a488035 1332 );
43ef1263 1333 $this->getAndCheck($expected, $result['id'], 'contact');
6a488035
TO
1334 }
1335
1336 /**
381fa321 1337 * Test civicrm_update() without contact type.
1338 *
1339 * Deliberately exclude contact_type as it should still cope using civicrm_api.
1340 *
1341 * CRM-7645.
6a488035 1342 */
6a488035 1343 public function testUpdateCreateWithID() {
381fa321 1344 // Insert a row in civicrm_contact creating individual contact.
6a488035
TO
1345 $op = new PHPUnit_Extensions_Database_Operation_Insert();
1346 $op->execute($this->_dbconn,
bbfd46a5 1347 $this->createXMLDataSet(
6a488035
TO
1348 dirname(__FILE__) . '/dataset/contact_ind.xml'
1349 )
1350 );
1351
6a488035
TO
1352 $params = array(
1353 'id' => 23,
1354 'first_name' => 'abcd',
1355 'last_name' => 'wxyz',
6a488035 1356 );
4b58ed3b 1357 $this->callAPISuccess('Contact', 'Update', $params);
6a488035
TO
1358 }
1359
1360 /**
381fa321 1361 * Test civicrm_contact_delete() with no contact ID.
6a488035 1362 */
00be9182 1363 public function testContactDeleteNoID() {
6a488035
TO
1364 $params = array(
1365 'foo' => 'bar',
6a488035 1366 );
4b58ed3b 1367 $this->callAPIFailure('contact', 'delete', $params);
6a488035
TO
1368 }
1369
1370 /**
381fa321 1371 * Test civicrm_contact_delete() with error.
6a488035 1372 */
00be9182 1373 public function testContactDeleteError() {
f6722559 1374 $params = array('contact_id' => 999);
4b58ed3b 1375 $this->callAPIFailure('contact', 'delete', $params);
6a488035
TO
1376 }
1377
1378 /**
381fa321 1379 * Test civicrm_contact_delete().
6a488035 1380 */
00be9182 1381 public function testContactDelete() {
f6722559 1382 $contactID = $this->individualCreate();
6a488035 1383 $params = array(
5896d037 1384 'id' => $contactID,
6a488035 1385 );
4b58ed3b 1386 $this->callAPIAndDocument('contact', 'delete', $params, __FUNCTION__, __FILE__);
6a488035
TO
1387 }
1388
1389 /**
381fa321 1390 * Test civicrm_contact_get() return only first name.
6a488035
TO
1391 */
1392 public function testContactGetRetFirst() {
f6722559 1393 $contact = $this->callAPISuccess('contact', 'create', $this->_params);
6a488035
TO
1394 $params = array(
1395 'contact_id' => $contact['id'],
1396 'return_first_name' => TRUE,
1397 'sort' => 'first_name',
6a488035 1398 );
f6722559 1399 $result = $this->callAPISuccess('contact', 'get', $params);
4b58ed3b
EM
1400 $this->assertEquals(1, $result['count']);
1401 $this->assertEquals($contact['id'], $result['id']);
1402 $this->assertEquals('abc1', $result['values'][$contact['id']]['first_name']);
6a488035
TO
1403 }
1404
1405 /**
381fa321 1406 * Test civicrm_contact_get() return only first name & last name.
1407 *
1408 * Use comma separated string return with a space.
6a488035 1409 */
381fa321 1410 public function testContactGetReturnFirstLast() {
f6722559 1411 $contact = $this->callAPISuccess('contact', 'create', $this->_params);
6a488035
TO
1412 $params = array(
1413 'contact_id' => $contact['id'],
1414 'return' => 'first_name, last_name',
6a488035 1415 );
f6722559 1416 $result = $this->callAPISuccess('contact', 'getsingle', $params);
4b58ed3b
EM
1417 $this->assertEquals('abc1', $result['first_name']);
1418 $this->assertEquals('xyz1', $result['last_name']);
6a488035
TO
1419 //check that other defaults not returns
1420 $this->assertArrayNotHasKey('sort_name', $result);
1421 $params = array(
1422 'contact_id' => $contact['id'],
1423 'return' => 'first_name,last_name',
6a488035 1424 );
f6722559 1425 $result = $this->callAPISuccess('contact', 'getsingle', $params);
4b58ed3b
EM
1426 $this->assertEquals('abc1', $result['first_name']);
1427 $this->assertEquals('xyz1', $result['last_name']);
6a488035
TO
1428 //check that other defaults not returns
1429 $this->assertArrayNotHasKey('sort_name', $result);
1430 }
1431
1432 /**
381fa321 1433 * Test civicrm_contact_get() return only first name & last name.
1434 *
d177a2a6 1435 * Use comma separated string return without a space
6a488035 1436 */
381fa321 1437 public function testContactGetReturnFirstLastNoComma() {
f6722559 1438 $contact = $this->callAPISuccess('contact', 'create', $this->_params);
6a488035
TO
1439 $params = array(
1440 'contact_id' => $contact['id'],
1441 'return' => 'first_name,last_name',
6a488035 1442 );
f6722559 1443 $result = $this->callAPISuccess('contact', 'getsingle', $params);
4b58ed3b
EM
1444 $this->assertEquals('abc1', $result['first_name']);
1445 $this->assertEquals('xyz1', $result['last_name']);
6a488035
TO
1446 //check that other defaults not returns
1447 $this->assertArrayNotHasKey('sort_name', $result);
1448 }
1449
1450 /**
381fa321 1451 * Test civicrm_contact_get() with default return properties.
6a488035
TO
1452 */
1453 public function testContactGetRetDefault() {
43ef1263 1454 $contactID = $this->individualCreate();
6a488035 1455 $params = array(
43ef1263 1456 'contact_id' => $contactID,
6a488035 1457 'sort' => 'first_name',
6a488035 1458 );
f6722559 1459 $result = $this->callAPISuccess('contact', 'get', $params);
43ef1263
EM
1460 $this->assertEquals($contactID, $result['values'][$contactID]['contact_id']);
1461 $this->assertEquals('Anthony', $result['values'][$contactID]['first_name']);
6a488035
TO
1462 }
1463
1464 /**
381fa321 1465 * Test civicrm_contact_getquick() with empty name param.
6a488035
TO
1466 */
1467 public function testContactGetQuick() {
fe482240 1468 // Insert a row in civicrm_contact creating individual contact.
6a488035
TO
1469 $op = new PHPUnit_Extensions_Database_Operation_Insert();
1470 $op->execute($this->_dbconn,
bbfd46a5 1471 $this->createXMLDataSet(
6a488035
TO
1472 dirname(__FILE__) . '/dataset/contact_17.xml'
1473 )
1474 );
1475 $op->execute($this->_dbconn,
bbfd46a5 1476 $this->createXMLDataSet(
6a488035
TO
1477 dirname(__FILE__) . '/dataset/email_contact_17.xml'
1478 )
1479 );
1480 $params = array(
1481 'name' => "T",
6a488035
TO
1482 );
1483
1d6020f1 1484 $result = $this->callAPISuccess('contact', 'getquick', $params);
fe482240 1485 $this->assertEquals(17, $result['values'][0]['id']);
6a488035
TO
1486 }
1487
1488 /**
fe482240 1489 * Test civicrm_contact_get) with empty params.
6a488035
TO
1490 */
1491 public function testContactGetEmptyParams() {
4b58ed3b 1492 $this->callAPISuccess('contact', 'get', array());
6a488035
TO
1493 }
1494
1495 /**
fe482240 1496 * Test civicrm_contact_get(,true) with no matches.
6a488035
TO
1497 */
1498 public function testContactGetOldParamsNoMatches() {
fe482240 1499 // Insert a row in civicrm_contact creating contact 17.
6a488035
TO
1500 $op = new PHPUnit_Extensions_Database_Operation_Insert();
1501 $op->execute($this->_dbconn,
bbfd46a5 1502 $this->createXMLDataSet(
6a488035
TO
1503 dirname(__FILE__) . '/dataset/contact_17.xml'
1504 )
1505 );
1506
1507 $params = array(
1508 'first_name' => 'Fred',
6a488035 1509 );
f6722559 1510 $result = $this->callAPISuccess('contact', 'get', $params);
fe482240 1511 $this->assertEquals(0, $result['count']);
6a488035
TO
1512 }
1513
1514 /**
fe482240 1515 * Test civicrm_contact_get(,true) with one match.
6a488035
TO
1516 */
1517 public function testContactGetOldParamsOneMatch() {
fe482240 1518 // Insert a row in civicrm_contact creating contact 17
6a488035
TO
1519 $op = new PHPUnit_Extensions_Database_Operation_Insert();
1520 $op->execute($this->_dbconn,
bbfd46a5 1521 $this->createXMLDataSet(dirname(__FILE__) . '/dataset/contact_17.xml'
6a488035
TO
1522 )
1523 );
1524
1525 $params = array(
1526 'first_name' => 'Test',
6a488035 1527 );
f6722559 1528 $result = $this->callAPISuccess('contact', 'get', $params);
fe482240
EM
1529 $this->assertEquals(17, $result['values'][17]['contact_id']);
1530 $this->assertEquals(17, $result['id']);
6a488035 1531 }
6a488035
TO
1532
1533 /**
fe482240 1534 * Test civicrm_contact_search_count().
6a488035
TO
1535 */
1536 public function testContactGetEmail() {
1537 $params = array(
1538 'email' => 'man2@yahoo.com',
1539 'contact_type' => 'Individual',
1540 'location_type_id' => 1,
6a488035
TO
1541 );
1542
f6722559 1543 $contact = $this->callAPISuccess('contact', 'create', $params);
1544
4b58ed3b 1545 $this->assertEquals(1, $contact['id']);
6a488035
TO
1546
1547 $params = array(
1548 'email' => 'man2@yahoo.com',
6a488035 1549 );
f6722559 1550 $result = $this->callAPIAndDocument('contact', 'get', $params, __FUNCTION__, __FILE__);
4b58ed3b
EM
1551 $this->assertEquals(1, $result['values'][1]['contact_id']);
1552 $this->assertEquals('man2@yahoo.com', $result['values'][1]['email']);
6a488035
TO
1553
1554 // delete the contact
f6722559 1555 $this->callAPISuccess('contact', 'delete', $contact);
6a488035
TO
1556 }
1557
b1f09bea 1558 /**
fe482240
EM
1559 * Test birth date parameters.
1560 *
1561 * These include value, array & birth_date_high, birth_date_low
1562 * && deceased.
b1f09bea 1563 */
4b58ed3b 1564 public function testContactGetBirthDate() {
b1f09bea
E
1565 $contact1 = $this->callAPISuccess('contact', 'create', array_merge($this->_params, array('birth_date' => 'first day of next month - 2 years')));
1566 $contact2 = $this->callAPISuccess('contact', 'create', array_merge($this->_params, array('birth_date' => 'first day of next month - 5 years')));
1567 $contact3 = $this->callAPISuccess('contact', 'create', array_merge($this->_params, array('birth_date' => 'first day of next month -20 years')));
1568
1569 $result = $this->callAPISuccess('contact', 'get', array());
1570 $this->assertEquals(date('Y-m-d', strtotime('first day of next month -2 years')), $result['values'][$contact1['id']]['birth_date']);
1571 $result = $this->callAPISuccess('contact', 'get', array('birth_date' => 'first day of next month -5 years'));
1572 $this->assertEquals(1, $result['count']);
1573 $this->assertEquals(date('Y-m-d', strtotime('first day of next month -5 years')), $result['values'][$contact2['id']]['birth_date']);
1574 $result = $this->callAPISuccess('contact', 'get', array('birth_date_high' => date('Y-m-d', strtotime('-6 years'))));
1575 $this->assertEquals(1, $result['count']);
1576 $this->assertEquals(date('Y-m-d', strtotime('first day of next month -20 years')), $result['values'][$contact3['id']]['birth_date']);
5896d037 1577 $result = $this->callAPISuccess('contact', 'get', array(
92915c55
TO
1578 'birth_date_low' => date('Y-m-d', strtotime('-6 years')),
1579 'birth_date_high' => date('Y-m-d', strtotime('- 3 years')),
1580 ));
b1f09bea
E
1581 $this->assertEquals(1, $result['count']);
1582 $this->assertEquals(date('Y-m-d', strtotime('first day of next month -5 years')), $result['values'][$contact2['id']]['birth_date']);
5896d037 1583 $result = $this->callAPISuccess('contact', 'get', array(
92915c55
TO
1584 'birth_date_low' => '-6 years',
1585 'birth_date_high' => '- 3 years',
1586 ));
9f60788a
EM
1587 $this->assertEquals(1, $result['count']);
1588 $this->assertEquals(date('Y-m-d', strtotime('first day of next month -5 years')), $result['values'][$contact2['id']]['birth_date']);
b1f09bea
E
1589 }
1590
1591 /**
fe482240
EM
1592 * Test Deceased date parameters.
1593 *
1594 * These include value, array & Deceased_date_high, Deceased date_low
d177a2a6 1595 * && deceased.
b1f09bea 1596 */
4b58ed3b 1597 public function testContactGetDeceasedDate() {
b1f09bea
E
1598 $contact1 = $this->callAPISuccess('contact', 'create', array_merge($this->_params, array('deceased_date' => 'first day of next month - 2 years')));
1599 $contact2 = $this->callAPISuccess('contact', 'create', array_merge($this->_params, array('deceased_date' => 'first day of next month - 5 years')));
1600 $contact3 = $this->callAPISuccess('contact', 'create', array_merge($this->_params, array('deceased_date' => 'first day of next month -20 years')));
1601
1602 $result = $this->callAPISuccess('contact', 'get', array());
1603 $this->assertEquals(date('Y-m-d', strtotime('first day of next month -2 years')), $result['values'][$contact1['id']]['deceased_date']);
1604 $result = $this->callAPISuccess('contact', 'get', array('deceased_date' => 'first day of next month -5 years'));
1605 $this->assertEquals(1, $result['count']);
1606 $this->assertEquals(date('Y-m-d', strtotime('first day of next month -5 years')), $result['values'][$contact2['id']]['deceased_date']);
1607 $result = $this->callAPISuccess('contact', 'get', array('deceased_date_high' => date('Y-m-d', strtotime('-6 years'))));
1608 $this->assertEquals(1, $result['count']);
1609 $this->assertEquals(date('Y-m-d', strtotime('first day of next month -20 years')), $result['values'][$contact3['id']]['deceased_date']);
5896d037 1610 $result = $this->callAPISuccess('contact', 'get', array(
92915c55
TO
1611 'deceased_date_low' => '-6 years',
1612 'deceased_date_high' => date('Y-m-d', strtotime('- 3 years')),
1613 ));
b1f09bea
E
1614 $this->assertEquals(1, $result['count']);
1615 $this->assertEquals(date('Y-m-d', strtotime('first day of next month -5 years')), $result['values'][$contact2['id']]['deceased_date']);
1616 }
1617
e635f9d4 1618 /**
fe482240 1619 * Test for Contact.get id=@user:username.
e635f9d4 1620 */
00be9182 1621 public function testContactGetByUsername() {
fe482240 1622 // Setup - create contact with a uf-match.
e635f9d4
TO
1623 $cid = $this->individualCreate(array(
1624 'contact_type' => 'Individual',
1625 'first_name' => 'testGetByUsername',
1626 'last_name' => 'testGetByUsername',
1627 ));
1628
1629 $ufMatchParams = array(
1630 'domain_id' => CRM_Core_Config::domainID(),
1631 'uf_id' => 99,
1632 'uf_name' => 'the-email-matching-key-is-not-really-the-username',
1633 'contact_id' => $cid,
1634 );
1635 $ufMatch = CRM_Core_BAO_UFMatch::create($ufMatchParams);
1636 $this->assertTrue(is_numeric($ufMatch->id));
1637
1638 // setup - mock the calls to CRM_Utils_System_*::getUfId
1639 $userSystem = $this->getMock('CRM_Utils_System_UnitTests', array('getUfId'));
1640 $userSystem->expects($this->once())
1641 ->method('getUfId')
1642 ->with($this->equalTo('exampleUser'))
1643 ->will($this->returnValue(99));
1644 CRM_Core_Config::singleton()->userSystem = $userSystem;
1645
1646 // perform a lookup
1647 $result = $this->callAPISuccess('Contact', 'get', array(
e635f9d4
TO
1648 'id' => '@user:exampleUser',
1649 ));
1650 $this->assertEquals('testGetByUsername', $result['values'][$cid]['first_name']);
1651 }
1652
265cc07d 1653 /**
eceb18cc 1654 * Test to check return works OK.
265cc07d 1655 */
00be9182 1656 public function testContactGetReturnValues() {
701a69da 1657 $extraParams = array(
1658 'nick_name' => 'Bob',
1659 'phone' => '456',
1660 'email' => 'e@mail.com',
1661 );
265cc07d 1662 $contactID = $this->individualCreate($extraParams);
1663 //actually it turns out the above doesn't create a phone
6c6e6187 1664 $this->callAPISuccess('phone', 'create', array('contact_id' => $contactID, 'phone' => '456'));
265cc07d 1665 $result = $this->callAPISuccess('contact', 'getsingle', array('id' => $contactID));
1666 foreach ($extraParams as $key => $value) {
1667 $this->assertEquals($result[$key], $value);
1668 }
1669 //now we check they are still returned with 'return' key
5896d037 1670 $result = $this->callAPISuccess('contact', 'getsingle', array(
92915c55
TO
1671 'id' => $contactID,
1672 'return' => array_keys($extraParams),
1673 ));
265cc07d 1674 foreach ($extraParams as $key => $value) {
1675 $this->assertEquals($result[$key], $value);
1676 }
1677 }
1678
701a69da 1679 /**
1680 * Test creating multiple phones using chaining.
1681 *
1682 * @throws \Exception
1683 */
00be9182 1684 public function testCRM13252MultipleChainedPhones() {
265cc07d 1685 $contactID = $this->householdCreate();
1686 $this->callAPISuccessGetCount('phone', array('contact_id' => $contactID), 0);
1687 $params = array(
5896d037
TO
1688 'contact_id' => $contactID,
1689 'household_name' => 'Household 1',
1690 'contact_type' => 'Household',
1691 'api.phone.create' => array(
265cc07d 1692 0 => array(
1693 'phone' => '111-111-1111',
1694 'location_type_id' => 1,
1695 'phone_type_id' => 1,
1696 ),
1697 1 => array(
1698 'phone' => '222-222-2222',
1699 'location_type_id' => 1,
1700 'phone_type_id' => 2,
21dfd5f5
TO
1701 ),
1702 ),
265cc07d 1703 );
4b58ed3b 1704 $this->callAPISuccess('contact', 'create', $params);
265cc07d 1705 $this->callAPISuccessGetCount('phone', array('contact_id' => $contactID), 2);
1706
1707 }
5896d037 1708
e635f9d4 1709 /**
fe482240 1710 * Test for Contact.get id=@user:username (with an invalid username).
e635f9d4 1711 */
00be9182 1712 public function testContactGetByUnknownUsername() {
e635f9d4
TO
1713 // setup - mock the calls to CRM_Utils_System_*::getUfId
1714 $userSystem = $this->getMock('CRM_Utils_System_UnitTests', array('getUfId'));
1715 $userSystem->expects($this->once())
1716 ->method('getUfId')
1717 ->with($this->equalTo('exampleUser'))
1718 ->will($this->returnValue(NULL));
1719 CRM_Core_Config::singleton()->userSystem = $userSystem;
1720
1721 // perform a lookup
1722 $result = $this->callAPIFailure('Contact', 'get', array(
e635f9d4
TO
1723 'id' => '@user:exampleUser',
1724 ));
1725 $this->assertRegExp('/cannot be resolved to a contact ID/', $result['error_message']);
1726 }
1727
6a488035 1728 /**
701a69da 1729 * Verify attempt to create individual with chained arrays and sequential.
48bb2598
JV
1730 */
1731 public function testGetIndividualWithChainedArraysAndSequential() {
1732 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
1733 $params['custom_' . $ids['custom_field_id']] = "custom string";
1734
84b51197 1735 $moreIDs = $this->CustomGroupMultipleCreateWithFields();
48bb2598
JV
1736 $params = array(
1737 'sequential' => 1,
1738 'first_name' => 'abc3',
1739 'last_name' => 'xyz3',
1740 'contact_type' => 'Individual',
1741 'email' => 'man3@yahoo.com',
1742 'api.website.create' => array(
1743 array(
1744 'url' => "http://civicrm.org",
1745 ),
1746 array(
1747 'url' => "https://civicrm.org",
1748 ),
1749 ),
1750 );
1751
1752 $result = $this->callAPISuccess('Contact', 'create', $params);
1753
48bb2598
JV
1754 // delete the contact and custom groups
1755 $this->callAPISuccess('contact', 'delete', array('id' => $result['id']));
1756 $this->customGroupDelete($ids['custom_group_id']);
84b51197 1757 $this->customGroupDelete($moreIDs['custom_group_id']);
48bb2598
JV
1758
1759 $this->assertEquals($result['id'], $result['values'][0]['id']);
1760 $this->assertArrayKeyExists('api.website.create', $result['values'][0]);
1761 }
1762
1763 /**
701a69da 1764 * Verify attempt to create individual with chained arrays.
6a488035 1765 */
00be9182 1766 public function testGetIndividualWithChainedArrays() {
6a488035
TO
1767 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
1768 $params['custom_' . $ids['custom_field_id']] = "custom string";
1769
381fa321 1770 $moreIDs = $this->CustomGroupMultipleCreateWithFields();
5c49fee0 1771 $description = "This demonstrates the usage of chained api functions.\nIn this case no notes or custom fields have been created.";
5896d037
TO
1772 $subfile = "APIChainedArray";
1773 $params = array(
6a488035
TO
1774 'first_name' => 'abc3',
1775 'last_name' => 'xyz3',
1776 'contact_type' => 'Individual',
1777 'email' => 'man3@yahoo.com',
6a488035
TO
1778 'api.contribution.create' => array(
1779 'receive_date' => '2010-01-01',
1780 'total_amount' => 100.00,
1781 'financial_type_id' => 1,
1782 'payment_instrument_id' => 1,
1783 'non_deductible_amount' => 10.00,
1784 'fee_amount' => 50.00,
1785 'net_amount' => 90.00,
1786 'trxn_id' => 12345,
1787 'invoice_id' => 67890,
1788 'source' => 'SSF',
1789 'contribution_status_id' => 1,
1790 ),
1791 'api.contribution.create.1' => array(
1792 'receive_date' => '2011-01-01',
1793 'total_amount' => 120.00,
6c6e6187 1794 'financial_type_id' => $this->_financialTypeId = 1,
6a488035
TO
1795 'payment_instrument_id' => 1,
1796 'non_deductible_amount' => 10.00,
1797 'fee_amount' => 50.00,
1798 'net_amount' => 90.00,
1799 'trxn_id' => 12335,
1800 'invoice_id' => 67830,
1801 'source' => 'SSF',
1802 'contribution_status_id' => 1,
1803 ),
1804 'api.website.create' => array(
1805 array(
1806 'url' => "http://civicrm.org",
1807 ),
1808 ),
1809 );
1810
f6722559 1811 $result = $this->callAPISuccess('Contact', 'create', $params);
6a488035 1812 $params = array(
f6722559 1813 'id' => $result['id'],
6a488035
TO
1814 'api.website.get' => array(),
1815 'api.Contribution.get' => array(
1816 'total_amount' => '120.00',
5896d037
TO
1817 ),
1818 'api.CustomValue.get' => 1,
6a488035
TO
1819 'api.Note.get' => 1,
1820 );
f6722559 1821 $result = $this->callAPIAndDocument('Contact', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile);
6a488035 1822 // delete the contact
f6722559 1823 $this->callAPISuccess('contact', 'delete', $result);
6a488035 1824 $this->customGroupDelete($ids['custom_group_id']);
381fa321 1825 $this->customGroupDelete($moreIDs['custom_group_id']);
4b58ed3b
EM
1826 $this->assertEquals(1, $result['id']);
1827 $this->assertEquals(0, $result['values'][$result['id']]['api.website.get']['is_error']);
1828 $this->assertEquals("http://civicrm.org", $result['values'][$result['id']]['api.website.get']['values'][0]['url']);
6a488035
TO
1829 }
1830
a77b0380 1831 /**
701a69da 1832 * Verify attempt to create individual with chained arrays and sequential.
a77b0380
JV
1833 *
1834 * See https://issues.civicrm.org/jira/browse/CRM-15815
1835 */
1836 public function testCreateIndividualWithChainedArrayAndSequential() {
1837 $params = array(
1838 'sequential' => 1,
1839 'first_name' => 'abc5',
1840 'last_name' => 'xyz5',
1841 'contact_type' => 'Individual',
1842 'email' => 'woman5@yahoo.com',
1843 'api.phone.create' => array(
1844 array('phone' => '03-231 07 95'),
1845 array('phone' => '03-232 51 62'),
1846 ),
1847 'api.website.create' => array(
1848 'url' => 'http://civicrm.org',
1849 ),
1850 );
1851 $result = $this->callAPISuccess('Contact', 'create', $params);
1852
1853 // I could try to parse the result to see whether the two phone numbers
1854 // and the website are there, but I am not sure about the correct format.
1855 // So I will just fetch it again before checking.
1856 // See also http://forum.civicrm.org/index.php/topic,35393.0.html
1857 $params = array(
1858 'sequential' => 1,
1859 'id' => $result['id'],
1860 'api.website.get' => array(),
1861 'api.phone.get' => array(),
1862 );
1863 $result = $this->callAPISuccess('Contact', 'get', $params);
1864
1865 // delete the contact
1866 $this->callAPISuccess('contact', 'delete', $result);
1867
1868 $this->assertEquals(2, $result['values'][0]['api.phone.get']['count']);
1869 $this->assertEquals(1, $result['values'][0]['api.website.get']['count']);
1870 }
1871
701a69da 1872 /**
1873 * Test retrieving an individual with chained array syntax.
1874 */
00be9182 1875 public function testGetIndividualWithChainedArraysFormats() {
5c49fee0 1876 $description = "This demonstrates the usage of chained api functions.\nIn this case no notes or custom fields have been created.";
6a488035
TO
1877 $subfile = "APIChainedArrayFormats";
1878 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
1879 $params['custom_' . $ids['custom_field_id']] = "custom string";
1880
381fa321 1881 $moreIDs = $this->CustomGroupMultipleCreateWithFields();
6a488035
TO
1882 $params = array(
1883 'first_name' => 'abc3',
1884 'last_name' => 'xyz3',
1885 'contact_type' => 'Individual',
1886 'email' => 'man3@yahoo.com',
6a488035
TO
1887 'api.contribution.create' => array(
1888 'receive_date' => '2010-01-01',
1889 'total_amount' => 100.00,
f6722559 1890 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1891 'payment_instrument_id' => 1,
1892 'non_deductible_amount' => 10.00,
1893 'fee_amount' => 50.00,
1894 'net_amount' => 90.00,
1895 'source' => 'SSF',
1896 'contribution_status_id' => 1,
1897 ),
1898 'api.contribution.create.1' => array(
1899 'receive_date' => '2011-01-01',
1900 'total_amount' => 120.00,
f6722559 1901 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1902 'payment_instrument_id' => 1,
1903 'non_deductible_amount' => 10.00,
1904 'fee_amount' => 50.00,
1905 'net_amount' => 90.00,
1906 'source' => 'SSF',
1907 'contribution_status_id' => 1,
1908 ),
1909 'api.website.create' => array(
1910 array(
1911 'url' => "http://civicrm.org",
1912 ),
1913 ),
1914 );
1915
f6722559 1916 $result = $this->callAPISuccess('Contact', 'create', $params);
6a488035 1917 $params = array(
f6722559 1918 'id' => $result['id'],
6a488035
TO
1919 'api.website.getValue' => array('return' => 'url'),
1920 'api.Contribution.getCount' => array(),
1921 'api.CustomValue.get' => 1,
1922 'api.Note.get' => 1,
1923 'api.Membership.getCount' => array(),
1924 );
f6722559 1925 $result = $this->callAPIAndDocument('Contact', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile);
4b58ed3b
EM
1926 $this->assertEquals(1, $result['id']);
1927 $this->assertEquals(2, $result['values'][$result['id']]['api.Contribution.getCount']);
1928 $this->assertEquals(0, $result['values'][$result['id']]['api.Note.get']['is_error']);
1929 $this->assertEquals("http://civicrm.org", $result['values'][$result['id']]['api.website.getValue']);
6a488035 1930
f6722559 1931 $this->callAPISuccess('contact', 'delete', $result);
6a488035 1932 $this->customGroupDelete($ids['custom_group_id']);
381fa321 1933 $this->customGroupDelete($moreIDs['custom_group_id']);
6a488035
TO
1934 }
1935
381fa321 1936 /**
1937 * Test complex chaining.
1938 */
00be9182 1939 public function testGetIndividualWithChainedArraysAndMultipleCustom() {
6a488035
TO
1940 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
1941 $params['custom_' . $ids['custom_field_id']] = "custom string";
381fa321 1942 $moreIDs = $this->CustomGroupMultipleCreateWithFields();
1943 $andMoreIDs = $this->CustomGroupMultipleCreateWithFields(array(
92915c55
TO
1944 'title' => "another group",
1945 'name' => 'another name',
1946 ));
5c49fee0 1947 $description = "This demonstrates the usage of chained api functions with multiple custom fields.";
6a488035
TO
1948 $subfile = "APIChainedArrayMultipleCustom";
1949 $params = array(
1950 'first_name' => 'abc3',
1951 'last_name' => 'xyz3',
1952 'contact_type' => 'Individual',
1953 'email' => 'man3@yahoo.com',
6a488035
TO
1954 'api.contribution.create' => array(
1955 'receive_date' => '2010-01-01',
1956 'total_amount' => 100.00,
5896d037 1957 'financial_type_id' => 1,
6a488035
TO
1958 'payment_instrument_id' => 1,
1959 'non_deductible_amount' => 10.00,
1960 'fee_amount' => 50.00,
1961 'net_amount' => 90.00,
1962 'trxn_id' => 12345,
1963 'invoice_id' => 67890,
1964 'source' => 'SSF',
1965 'contribution_status_id' => 1,
1966 ),
1967 'api.contribution.create.1' => array(
1968 'receive_date' => '2011-01-01',
1969 'total_amount' => 120.00,
5896d037 1970 'financial_type_id' => 1,
6a488035
TO
1971 'payment_instrument_id' => 1,
1972 'non_deductible_amount' => 10.00,
1973 'fee_amount' => 50.00,
1974 'net_amount' => 90.00,
1975 'trxn_id' => 12335,
1976 'invoice_id' => 67830,
1977 'source' => 'SSF',
1978 'contribution_status_id' => 1,
1979 ),
1980 'api.website.create' => array(
1981 array(
1982 'url' => "http://civicrm.org",
1983 ),
1984 ),
1985 'custom_' . $ids['custom_field_id'] => "value 1",
381fa321 1986 'custom_' . $moreIDs['custom_field_id'][0] => "value 2",
1987 'custom_' . $moreIDs['custom_field_id'][1] => "warm beer",
1988 'custom_' . $andMoreIDs['custom_field_id'][1] => "vegemite",
6a488035
TO
1989 );
1990
f6722559 1991 $result = $this->callAPISuccess('Contact', 'create', $params);
1992 $result = $this->callAPISuccess('Contact', 'create', array(
6c6e6187 1993 'contact_type' => 'Individual',
5896d037
TO
1994 'id' => $result['id'],
1995 'custom_' .
381fa321 1996 $moreIDs['custom_field_id'][0] => "value 3",
5896d037
TO
1997 'custom_' .
1998 $ids['custom_field_id'] => "value 4",
1999 ));
6a488035
TO
2000
2001 $params = array(
f6722559 2002 'id' => $result['id'],
6a488035
TO
2003 'api.website.getValue' => array('return' => 'url'),
2004 'api.Contribution.getCount' => array(),
2005 'api.CustomValue.get' => 1,
2006 );
f6722559 2007 $result = $this->callAPIAndDocument('Contact', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile);
2008
6a488035 2009 $this->customGroupDelete($ids['custom_group_id']);
381fa321 2010 $this->customGroupDelete($moreIDs['custom_group_id']);
2011 $this->customGroupDelete($andMoreIDs['custom_group_id']);
4b58ed3b
EM
2012 $this->assertEquals(1, $result['id']);
2013 $this->assertEquals(0, $result['values'][$result['id']]['api.CustomValue.get']['is_error']);
2014 $this->assertEquals('http://civicrm.org', $result['values'][$result['id']]['api.website.getValue']);
6a488035 2015 }
5896d037 2016
d424ffde 2017 /**
381fa321 2018 * Test checks usage of $values to pick & choose inputs.
6a488035 2019 */
00be9182 2020 public function testChainingValuesCreate() {
5c49fee0
CW
2021 $description = "This demonstrates the usage of chained api functions. Specifically it has one 'parent function' &
2022 2 child functions - one receives values from the parent (Contact) and the other child (Tag).";
6a488035
TO
2023 $subfile = "APIChainedArrayValuesFromSiblingFunction";
2024 $params = array(
6c6e6187 2025 'display_name' => 'batman',
5896d037 2026 'contact_type' => 'Individual',
701a69da 2027 'api.tag.create' => array(
2028 'name' => '$value.id',
2029 'description' => '$value.display_name',
2030 'format.only_id' => 1,
2031 ),
6a488035
TO
2032 'api.entity_tag.create' => array('tag_id' => '$value.api.tag.create'),
2033 );
f6722559 2034 $result = $this->callAPIAndDocument('Contact', 'Create', $params, __FUNCTION__, __FILE__, $description, $subfile);
6a488035 2035 $this->assertEquals(0, $result['values'][$result['id']]['api.entity_tag.create']['is_error']);
f6722559 2036
6a488035
TO
2037 $tablesToTruncate = array(
2038 'civicrm_contact',
2039 'civicrm_activity',
2040 'civicrm_entity_tag',
2041 'civicrm_tag',
2042 );
2043 $this->quickCleanup($tablesToTruncate, TRUE);
2044 }
2045
d424ffde 2046 /**
fe482240 2047 * Test TrueFalse format - I couldn't come up with an easy way to get an error on Get.
6a488035 2048 */
00be9182 2049 public function testContactGetFormatIsSuccessTrue() {
6a488035
TO
2050 $this->createContactFromXML();
2051 $description = "This demonstrates use of the 'format.is_success' param.
2052 This param causes only the success or otherwise of the function to be returned as BOOLEAN";
2053 $subfile = "FormatIsSuccess_True";
5896d037
TO
2054 $params = array('id' => 17, 'format.is_success' => 1);
2055 $result = $this->callAPIAndDocument('Contact', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile);
6a488035 2056 $this->assertEquals(1, $result);
f6722559 2057 $this->callAPISuccess('Contact', 'Delete', $params);
6a488035 2058 }
5896d037 2059
d424ffde 2060 /**
fe482240 2061 * Test TrueFalse format.
6a488035 2062 */
00be9182 2063 public function testContactCreateFormatIsSuccessFalse() {
6a488035
TO
2064
2065 $description = "This demonstrates use of the 'format.is_success' param.
2066 This param causes only the success or otherwise of the function to be returned as BOOLEAN";
2067 $subfile = "FormatIsSuccess_Fail";
5896d037
TO
2068 $params = array('id' => 500, 'format.is_success' => 1);
2069 $result = $this->callAPIAndDocument('Contact', 'Create', $params, __FUNCTION__, __FILE__, $description, $subfile);
6a488035
TO
2070 $this->assertEquals(0, $result);
2071 }
5896d037 2072
d424ffde 2073 /**
fe482240 2074 * Test Single Entity format.
6a488035 2075 */
fe482240 2076 public function testContactGetSingleEntityArray() {
6a488035
TO
2077 $this->createContactFromXML();
2078 $description = "This demonstrates use of the 'format.single_entity_array' param.
5c49fee0
CW
2079 This param causes the only contact to be returned as an array without the other levels.
2080 It will be ignored if there is not exactly 1 result";
6a488035 2081 $subfile = "GetSingleContact";
5896d037
TO
2082 $params = array('id' => 17);
2083 $result = $this->callAPIAndDocument('Contact', 'GetSingle', $params, __FUNCTION__, __FILE__, $description, $subfile);
4b58ed3b 2084 $this->assertEquals('Test Contact', $result['display_name']);
f6722559 2085 $this->callAPISuccess('Contact', 'Delete', $params);
6a488035
TO
2086 }
2087
d424ffde 2088 /**
381fa321 2089 * Test Single Entity format.
6a488035 2090 */
fe482240 2091 public function testContactGetFormatCountOnly() {
6a488035 2092 $this->createContactFromXML();
5c49fee0
CW
2093 $description = "This demonstrates use of the 'getCount' action.
2094 This param causes the count of the only function to be returned as an integer.";
5896d037 2095 $params = array('id' => 17);
381fa321 2096 $result = $this->callAPIAndDocument('Contact', 'GetCount', $params, __FUNCTION__, __FILE__, $description,
84b51197 2097 'GetCountContact');
4b58ed3b 2098 $this->assertEquals('1', $result);
f6722559 2099 $this->callAPISuccess('Contact', 'Delete', $params);
6a488035 2100 }
5896d037 2101
d424ffde 2102 /**
381fa321 2103 * Test id only format.
408b79bf 2104 */
fe482240 2105 public function testContactGetFormatIDOnly() {
6a488035 2106 $this->createContactFromXML();
5c49fee0
CW
2107 $description = "This demonstrates use of the 'format.id_only' param.
2108 This param causes the id of the only entity to be returned as an integer.
2109 It will be ignored if there is not exactly 1 result";
6a488035 2110 $subfile = "FormatOnlyID";
5896d037
TO
2111 $params = array('id' => 17, 'format.only_id' => 1);
2112 $result = $this->callAPIAndDocument('Contact', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile);
4b58ed3b 2113 $this->assertEquals('17', $result);
f6722559 2114 $this->callAPISuccess('Contact', 'Delete', $params);
6a488035
TO
2115 }
2116
d424ffde 2117 /**
381fa321 2118 * Test id only format.
408b79bf 2119 */
00be9182 2120 public function testContactGetFormatSingleValue() {
6a488035
TO
2121 $this->createContactFromXML();
2122 $description = "This demonstrates use of the 'format.single_value' param.
5c49fee0
CW
2123 This param causes only a single value of the only entity to be returned as an string.
2124 It will be ignored if there is not exactly 1 result";
4b58ed3b 2125 $subFile = "FormatSingleValue";
5896d037 2126 $params = array('id' => 17, 'return' => 'display_name');
a828d7b8 2127 $result = $this->callAPIAndDocument('Contact', 'getvalue', $params, __FUNCTION__, __FILE__, $description, $subFile);
4b58ed3b 2128 $this->assertEquals('Test Contact', $result);
f6722559 2129 $this->callAPISuccess('Contact', 'Delete', $params);
6a488035
TO
2130 }
2131
b2130237 2132 /**
381fa321 2133 * Test that permissions are respected when creating contacts.
b2130237 2134 */
00be9182 2135 public function testContactCreationPermissions() {
6a488035 2136 $params = array(
6c6e6187 2137 'contact_type' => 'Individual',
5896d037 2138 'first_name' => 'Foo',
6a488035
TO
2139 'last_name' => 'Bear',
2140 'check_permissions' => TRUE,
6a488035
TO
2141 );
2142 $config = CRM_Core_Config::singleton();
2143 $config->userPermissionClass->permissions = array('access CiviCRM');
d0e1eff2 2144 $result = $this->callAPIFailure('contact', 'create', $params);
1644b908 2145 $this->assertEquals('API permission check failed for Contact/create call; insufficient permission: require access CiviCRM and add contacts', $result['error_message'], 'lacking permissions should not be enough to create a contact');
6a488035
TO
2146
2147 $config->userPermissionClass->permissions = array('access CiviCRM', 'add contacts', 'import contacts');
701a69da 2148 $this->callAPISuccess('contact', 'create', $params);
6a488035
TO
2149 }
2150
381fa321 2151 /**
2152 * Test update with check permissions set.
2153 */
00be9182 2154 public function testContactUpdatePermissions() {
5896d037
TO
2155 $params = array(
2156 'contact_type' => 'Individual',
2157 'first_name' => 'Foo',
2158 'last_name' => 'Bear',
21dfd5f5 2159 'check_permissions' => TRUE,
5896d037 2160 );
f6722559 2161 $result = $this->callAPISuccess('contact', 'create', $params);
6a488035 2162 $config = CRM_Core_Config::singleton();
5896d037
TO
2163 $params = array(
2164 'id' => $result['id'],
2165 'contact_type' => 'Individual',
2166 'last_name' => 'Bar',
21dfd5f5 2167 'check_permissions' => TRUE,
5896d037 2168 );
6a488035
TO
2169
2170 $config->userPermissionClass->permissions = array('access CiviCRM');
d0e1eff2 2171 $result = $this->callAPIFailure('contact', 'update', $params);
1644b908 2172 $this->assertEquals('API permission check failed for Contact/update call; insufficient permission: require access CiviCRM and edit all contacts', $result['error_message'], 'lacking permissions should not be enough to update a contact');
6a488035 2173
5896d037
TO
2174 $config->userPermissionClass->permissions = array(
2175 'access CiviCRM',
2176 'add contacts',
2177 'view all contacts',
2178 'edit all contacts',
21dfd5f5 2179 'import contacts',
5896d037 2180 );
701a69da 2181 $this->callAPISuccess('contact', 'update', $params);
6a488035
TO
2182 }
2183
381fa321 2184 /**
2185 * Set up helper to create a contact.
2186 */
00be9182 2187 public function createContactFromXML() {
381fa321 2188 // Insert a row in civicrm_contact creating contact 17.
6a488035
TO
2189 $op = new PHPUnit_Extensions_Database_Operation_Insert();
2190 $op->execute($this->_dbconn,
bbfd46a5 2191 $this->createXMLDataSet(
6a488035
TO
2192 dirname(__FILE__) . '/dataset/contact_17.xml'
2193 )
2194 );
2195 }
2196
381fa321 2197 /**
2198 * Test contact proximity api.
2199 */
00be9182 2200 public function testContactProximity() {
6a488035
TO
2201 // first create a contact with a SF location with a specific
2202 // geocode
2203 $contactID = $this->organizationCreate();
2204
2205 // now create the address
2206 $params = array(
2207 'street_address' => '123 Main Street',
2208 'city' => 'San Francisco',
2209 'is_primary' => 1,
2210 'country_id' => 1228,
2211 'state_province_id' => 1004,
2212 'geo_code_1' => '37.79',
2213 'geo_code_2' => '-122.40',
2214 'location_type_id' => 1,
2215 'contact_id' => $contactID,
6a488035
TO
2216 );
2217
f6722559 2218 $result = $this->callAPISuccess('address', 'create', $params);
ba4a1892 2219 $this->assertEquals(1, $result['count']);
6a488035
TO
2220
2221 // now do a proximity search with a close enough geocode and hope to match
2222 // that specific contact only!
2223 $proxParams = array(
2224 'latitude' => 37.7,
2225 'longitude' => -122.3,
2226 'unit' => 'mile',
2227 'distance' => 10,
6a488035 2228 );
f6722559 2229 $result = $this->callAPISuccess('contact', 'proximity', $proxParams);
ba4a1892 2230 $this->assertEquals(1, $result['count']);
6a488035 2231 }
60ec9f43
E
2232
2233 /**
381fa321 2234 * Test that Ajax API permission is sufficient to access getquick api.
2235 *
1d6020f1 2236 * (note that getquick api is required for autocomplete & has ACL permissions applied)
60ec9f43 2237 */
fe482240 2238 public function testGetquickPermissionCRM13744() {
60ec9f43 2239 CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access CiviEvent');
4b58ed3b 2240 $this->callAPIFailure('contact', 'getquick', array('name' => 'b', 'check_permissions' => TRUE));
60ec9f43 2241 CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access CiviCRM');
4b58ed3b 2242 $this->callAPISuccess('contact', 'getquick', array('name' => 'b', 'check_permissions' => TRUE));
60ec9f43 2243 CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access AJAX API');
4b58ed3b 2244 $this->callAPISuccess('contact', 'getquick', array('name' => 'b', 'check_permissions' => TRUE));
60ec9f43 2245 }
9c8096cb 2246
08f4ab8d 2247 /**
2248 * Test that getquick returns contacts with an exact first name match first.
2baa1e00 2249 *
2250 * The search string 'b' & 'bob' both return ordered by sort_name if includeOrderByClause
2251 * is true (default) but if it is false then matches are returned in ID order.
08f4ab8d 2252 */
2253 public function testGetQuickExactFirst() {
2254 $this->getQuickSearchSampleData();
2255 $result = $this->callAPISuccess('contact', 'getquick', array('name' => 'b'));
2256 $this->assertEquals('A Bobby, Bobby', $result['values'][0]['sort_name']);
2baa1e00 2257 $this->assertEquals('B Bobby, Bobby', $result['values'][1]['sort_name']);
08f4ab8d 2258 $result = $this->callAPISuccess('contact', 'getquick', array('name' => 'bob'));
2259 $this->assertEquals('A Bobby, Bobby', $result['values'][0]['sort_name']);
2baa1e00 2260 $this->assertEquals('B Bobby, Bobby', $result['values'][1]['sort_name']);
2261 $this->callAPISuccess('Setting', 'create', array('includeOrderByClause' => FALSE));
2262 $result = $this->callAPISuccess('contact', 'getquick', array('name' => 'bob'));
2263 $this->assertEquals('Bob, Bob', $result['values'][0]['sort_name']);
2264 $this->assertEquals('A Bobby, Bobby', $result['values'][1]['sort_name']);
2265 }
2266
e9e27a80 2267 /**
2268 * Test that getquick returns contacts with an exact first name match first.
2269 */
2270 public function testGetQuickEmail() {
2271 $this->getQuickSearchSampleData();
c37a2d66 2272 $userID = $this->createLoggedInUser();
e9e27a80 2273 $result = $this->callAPISuccess('contact', 'getquick', array(
2274 'name' => 'c',
2275 ));
2276 $expectedData = array(
2277 'Bob, Bob :: bob@bob.com',
2278 'C Bobby, Bobby',
2279 'E Bobby, Bobby :: bob@bobby.com',
2280 'H Bobby, Bobby :: bob@h.com',
2281 );
c37a2d66 2282 $this->assertEquals(5, $result['count']);
e9e27a80 2283 foreach ($expectedData as $index => $value) {
2284 $this->assertEquals($value, $result['values'][$index]['data']);
2285 }
7f6f3df1 2286 $result = $this->callAPISuccess('contact', 'getquick', array(
2287 'name' => 'h.',
2288 ));
2289 $expectedData = array(
2290 'H Bobby, Bobby :: bob@h.com',
2291 );
2292 foreach ($expectedData as $index => $value) {
2293 $this->assertEquals($value, $result['values'][$index]['data']);
2294 }
2295 $this->callAPISuccess('Setting', 'create', array('includeWildCardInName' => FALSE));
2296 $result = $this->callAPISuccess('contact', 'getquick', array(
2297 'name' => 'h.',
2298 ));
2299 $this->callAPISuccess('Setting', 'create', array('includeWildCardInName' => TRUE));
2300 $this->assertEquals(0, $result['count']);
e9e27a80 2301 }
2302
c37a2d66 2303 /**
2304 * Test that getquick returns contacts with an exact first name match first.
2305 */
2306 public function testGetQuickEmailACL() {
2307 $this->getQuickSearchSampleData();
2308 $userID = $this->createLoggedInUser();
2309 CRM_Core_Config::singleton()->userPermissionClass->permissions = array();
2310 $result = $this->callAPISuccess('contact', 'getquick', array(
2311 'name' => 'c',
2312 ));
2313 $this->assertEquals(0, $result['count']);
2314
2315 $this->hookClass->setHook('civicrm_aclWhereClause', array($this, 'aclWhereNoBobH'));
2316 CRM_Contact_BAO_Contact_Permission::cache($userID, CRM_Core_Permission::VIEW, TRUE);
2317 $result = $this->callAPISuccess('contact', 'getquick', array(
2318 'name' => 'c',
2319 ));
2320 // Without the acl it would be 5 like the previous email getquick test.
2321 $this->assertEquals(4, $result['count']);
2322 $expectedData = array(
2323 'Bob, Bob :: bob@bob.com',
2324 'C Bobby, Bobby',
2325 'E Bobby, Bobby :: bob@bobby.com',
2326 );
2327 foreach ($expectedData as $index => $value) {
2328 $this->assertEquals($value, $result['values'][$index]['data']);
2329 }
2330 }
2331
2baa1e00 2332 /**
2333 * Test that getquick returns contacts with an exact first name match first.
2334 */
2335 public function testGetQuickExternalID() {
2336 $this->getQuickSearchSampleData();
2337 $result = $this->callAPISuccess('contact', 'getquick', array(
2338 'name' => 'b',
2339 'field_name' => 'external_identifier',
2340 'table_name' => 'cc',
2341 ));
2342 $this->assertEquals(0, $result['count']);
2343 $result = $this->callAPISuccess('contact', 'getquick', array(
2344 'name' => 'abc',
2345 'field_name' => 'external_identifier',
2346 'table_name' => 'cc',
2347 ));
2348 $this->assertEquals(1, $result['count']);
2349 $this->assertEquals('Bob, Bob', $result['values'][0]['sort_name']);
2350 }
2351
2352 /**
2353 * Test that getquick returns contacts with an exact first name match first.
2354 */
2355 public function testGetQuickID() {
2356 $this->getQuickSearchSampleData();
2357 $result = $this->callAPISuccess('contact', 'getquick', array(
2358 'name' => 2,
2359 'field_name' => 'id',
2360 'table_name' => 'cc',
2361 ));
2362 $this->assertEquals(1, $result['count']);
2363 $this->assertEquals('A Bobby, Bobby', $result['values'][0]['sort_name']);
2364 $result = $this->callAPISuccess('contact', 'getquick', array(
2365 'name' => 2,
2366 'field_name' => 'contact_id',
2367 'table_name' => 'cc',
2368 ));
2369 $this->assertEquals(1, $result['count']);
2370 $this->assertEquals('A Bobby, Bobby', $result['values'][0]['sort_name']);
2371 }
2372
2373 /**
2374 * Test that getquick returns contacts with an exact first name match first.
ef3fd279 2375 *
2376 * Depending on the setting the sort name sort might click in next or not - test!
2baa1e00 2377 */
2378 public function testGetQuickFirstName() {
2379 $this->getQuickSearchSampleData();
ef3fd279 2380 $this->callAPISuccess('Setting', 'create', array('includeOrderByClause' => TRUE));
2baa1e00 2381 $result = $this->callAPISuccess('contact', 'getquick', array(
2382 'name' => 'Bob',
2383 'field_name' => 'first_name',
2384 'table_name' => 'cc',
2385 ));
e9e27a80 2386 $expected = array(
2387 'Bob, Bob',
2388 'K Bobby, Bob',
2389 'A Bobby, Bobby',
2390 );
2391
2392 foreach ($expected as $index => $value) {
2393 $this->assertEquals($value, $result['values'][$index]['sort_name']);
2394 }
08f4ab8d 2395 $this->callAPISuccess('Setting', 'create', array('includeOrderByClause' => FALSE));
2396 $result = $this->callAPISuccess('contact', 'getquick', array('name' => 'bob'));
2397 $this->assertEquals('Bob, Bob', $result['values'][0]['sort_name']);
ef3fd279 2398 $this->assertEquals('A Bobby, Bobby', $result['values'][1]['sort_name']);
08f4ab8d 2399 }
2400
1b9c2802 2401 /**
2402 * Test that getquick applies ACLs.
2403 */
2404 public function testGetQuickFirstNameACLs() {
2405 $this->getQuickSearchSampleData();
2406 $userID = $this->createLoggedInUser();
c37a2d66 2407 $this->callAPISuccess('Setting', 'create', array('includeOrderByClause' => TRUE));
1b9c2802 2408 CRM_Core_Config::singleton()->userPermissionClass->permissions = array();
2409 $result = $this->callAPISuccess('contact', 'getquick', array(
2410 'name' => 'Bob',
2411 'field_name' => 'first_name',
2412 'table_name' => 'cc',
2413 ));
2414 $this->assertEquals(0, $result['count']);
2415
2416 $this->hookClass->setHook('civicrm_aclWhereClause', array($this, 'aclWhereNoBobH'));
2417 CRM_Contact_BAO_Contact_Permission::cache($userID, CRM_Core_Permission::VIEW, TRUE);
2418 $result = $this->callAPISuccess('contact', 'getquick', array(
2419 'name' => 'Bob',
2420 'field_name' => 'first_name',
2421 'table_name' => 'cc',
2422 ));
2423 $this->assertEquals('K Bobby, Bob', $result['values'][1]['sort_name']);
2424 // Without the ACL 9 would be bob@h.com.
2425 $this->assertEquals('I Bobby, Bobby', $result['values'][9]['sort_name']);
1b9c2802 2426 }
2427
2428 /**
2429 * Full results returned.
2430 * @implements CRM_Utils_Hook::aclWhereClause
2431 *
2432 * @param string $type
2433 * @param array $tables
2434 * @param array $whereTables
2435 * @param int $contactID
2436 * @param string $where
2437 */
2438 public function aclWhereNoBobH($type, &$tables, &$whereTables, &$contactID, &$where) {
c37a2d66 2439 $where = " (email <> 'bob@h.com' OR email IS NULL) ";
1b9c2802 2440 $whereTables['civicrm_email'] = "LEFT JOIN civicrm_email e ON contact_a.id = e.contact_id";
2441 }
2442
e9e27a80 2443 /**
2444 * Test that getquick returns contacts with an exact last name match first.
2445 */
2446 public function testGetQuickLastName() {
2447 $this->getQuickSearchSampleData();
2448 $this->callAPISuccess('Setting', 'create', array('includeOrderByClause' => TRUE));
2449 $result = $this->callAPISuccess('contact', 'getquick', array(
2450 'name' => 'Bob',
2451 'field_name' => 'last_name',
2452 'table_name' => 'cc',
2453 ));
2454 $expected = array(
2455 'Bob, Bob',
2456 'A Bobby, Bobby',
36575b09 2457 'B Bobby, Bobby',
e9e27a80 2458 );
2459
2460 foreach ($expected as $index => $value) {
2461 $this->assertEquals($value, $result['values'][$index]['sort_name']);
2462 }
2463 $this->callAPISuccess('Setting', 'create', array('includeOrderByClause' => FALSE));
2464 $result = $this->callAPISuccess('contact', 'getquick', array('name' => 'bob'));
2465 $this->assertEquals('Bob, Bob :: bob@bob.com', $result['values'][0]['data']);
2466 }
2467
2468 /**
2469 * Test that getquick returns contacts by city.
2470 */
2471 public function testGetQuickCity() {
2472 $this->getQuickSearchSampleData();
2473 $result = $this->callAPISuccess('contact', 'getquick', array(
2474 'name' => 'o',
2475 'field_name' => 'city',
2476 'table_name' => 'sts',
2477 ));
2478 $this->assertEquals('B Bobby, Bobby :: Toronto', $result['values'][0]['data']);
2479 $result = $this->callAPISuccess('contact', 'getquick', array(
2480 'name' => 'n',
2481 'field_name' => 'city',
2482 'table_name' => 'sts',
2483 ));
2484 $this->assertEquals('B Bobby, Bobby :: Toronto', $result['values'][0]['data']);
2485 $this->assertEquals('C Bobby, Bobby :: Whanganui', $result['values'][1]['data']);
2486 }
2487
08f4ab8d 2488 /**
2489 * Set up some sample data for testing quicksearch.
2490 */
2491 public function getQuickSearchSampleData() {
2492 $contacts = array(
e9e27a80 2493 array('first_name' => 'Bob', 'last_name' => 'Bob', 'external_identifier' => 'abc', 'email' => 'bob@bob.com'),
2baa1e00 2494 array('first_name' => 'Bobby', 'last_name' => 'A Bobby', 'external_identifier' => 'abcd'),
36575b09 2495 array(
2496 'first_name' => 'Bobby',
2497 'last_name' => 'B Bobby',
2498 'external_identifier' => 'bcd',
2499 'api.address.create' => array(
2500 'street_address' => 'Sesame Street',
2501 'city' => 'Toronto',
2502 'location_type_id' => 1,
2503 ),
2504 ),
2505 array(
2506 'first_name' => 'Bobby',
2507 'last_name' => 'C Bobby',
2508 'external_identifier' => 'bcde',
2509 'api.address.create' => array(
2510 'street_address' => 'Te huarahi',
2511 'city' => 'Whanganui',
2512 'location_type_id' => 1,
2513 ),
2514 ),
2baa1e00 2515 array('first_name' => 'Bobby', 'last_name' => 'D Bobby', 'external_identifier' => 'efg'),
e9e27a80 2516 array('first_name' => 'Bobby', 'last_name' => 'E Bobby', 'external_identifier' => 'hij', 'email' => 'bob@bobby.com'),
2baa1e00 2517 array('first_name' => 'Bobby', 'last_name' => 'F Bobby', 'external_identifier' => 'klm'),
2518 array('first_name' => 'Bobby', 'last_name' => 'G Bobby', 'external_identifier' => 'nop'),
e9e27a80 2519 array('first_name' => 'Bobby', 'last_name' => 'H Bobby', 'external_identifier' => 'qrs', 'email' => 'bob@h.com'),
2baa1e00 2520 array('first_name' => 'Bobby', 'last_name' => 'I Bobby'),
2521 array('first_name' => 'Bobby', 'last_name' => 'J Bobby'),
2522 array('first_name' => 'Bob', 'last_name' => 'K Bobby', 'external_identifier' => 'bcdef'),
08f4ab8d 2523 );
2524 foreach ($contacts as $type => $contact) {
2525 $contact['contact_type'] = 'Individual';
2526 $this->callAPISuccess('Contact', 'create', $contact);
2527 }
2528 }
2529
b2130237 2530 /**
381fa321 2531 * Test get ref api - gets a list of references to an entity.
b2130237 2532 */
00be9182 2533 public function testGetReferenceCounts() {
9c8096cb
TO
2534 $result = $this->callAPISuccess('Contact', 'create', array(
2535 'first_name' => 'Testily',
2536 'last_name' => 'McHaste',
2537 'contact_type' => 'Individual',
2538 'api.Address.replace' => array(
2539 'values' => array(),
2540 ),
2541 'api.Email.replace' => array(
2542 'values' => array(
2543 array(
2544 'email' => 'spam@dev.null',
2545 'is_primary' => 0,
2546 'location_type_id' => 1,
21dfd5f5 2547 ),
9c8096cb
TO
2548 ),
2549 ),
2550 'api.Phone.replace' => array(
2551 'values' => array(
2552 array(
2553 'phone' => '234-567-0001',
2554 'is_primary' => 1,
2555 'location_type_id' => 1,
2556 ),
2557 array(
2558 'phone' => '234-567-0002',
2559 'is_primary' => 0,
2560 'location_type_id' => 1,
2561 ),
2562 ),
2563 ),
2564 ));
2565
2566 //$dao = new CRM_Contact_BAO_Contact();
2567 //$dao->id = $result['id'];
2568 //$this->assertTrue((bool) $dao->find(TRUE));
2569 //
2570 //$refCounts = $dao->getReferenceCounts();
2571 //$this->assertTrue(is_array($refCounts));
2572 //$refCountsIdx = CRM_Utils_Array::index(array('name'), $refCounts);
2573
2574 $refCounts = $this->callAPISuccess('Contact', 'getrefcount', array(
21dfd5f5 2575 'id' => $result['id'],
9c8096cb
TO
2576 ));
2577 $refCountsIdx = CRM_Utils_Array::index(array('name'), $refCounts['values']);
2578
2579 $this->assertEquals(1, $refCountsIdx['sql:civicrm_email:contact_id']['count']);
2580 $this->assertEquals('civicrm_email', $refCountsIdx['sql:civicrm_email:contact_id']['table']);
2581 $this->assertEquals(2, $refCountsIdx['sql:civicrm_phone:contact_id']['count']);
2582 $this->assertEquals('civicrm_phone', $refCountsIdx['sql:civicrm_phone:contact_id']['table']);
2583 $this->assertTrue(!isset($refCountsIdx['sql:civicrm_address:contact_id']));
2584 }
2585
701a69da 2586 /**
2587 * Test the use of sql operators.
2588 */
00be9182 2589 public function testSQLOperatorsOnContactAPI() {
a75c13cc
EM
2590 $this->individualCreate();
2591 $this->organizationCreate();
2592 $this->householdCreate();
2593 $contacts = $this->callAPISuccess('contact', 'get', array('legal_name' => array('IS NOT NULL' => TRUE)));
2594 $this->assertEquals($contacts['count'], CRM_Core_DAO::singleValueQuery('select count(*) FROM civicrm_contact WHERE legal_name IS NOT NULL'));
2595 $contacts = $this->callAPISuccess('contact', 'get', array('legal_name' => array('IS NULL' => TRUE)));
2596 $this->assertEquals($contacts['count'], CRM_Core_DAO::singleValueQuery('select count(*) FROM civicrm_contact WHERE legal_name IS NULL'));
2597 }
9bee5ea2 2598
9bee5ea2 2599 /**
fe482240 2600 * CRM-14743 - test api respects search operators.
9bee5ea2 2601 */
00be9182 2602 public function testGetModifiedDateByOperators() {
9bee5ea2
EM
2603 $preExistingContactCount = CRM_Core_DAO::singleValueQuery('select count(*) FROM civicrm_contact');
2604 $contact1 = $this->individualCreate();
2605 $sql = "UPDATE civicrm_contact SET created_date = '2012-01-01', modified_date = '2013-01-01' WHERE id = " . $contact1;
2606 CRM_Core_DAO::executeQuery($sql);
2607 $contact2 = $this->individualCreate();
2608 $sql = "UPDATE civicrm_contact SET created_date = '2012-02-01', modified_date = '2013-02-01' WHERE id = " . $contact2;
2609 CRM_Core_DAO::executeQuery($sql);
2610 $contact3 = $this->householdCreate();
2611 $sql = "UPDATE civicrm_contact SET created_date = '2012-03-01', modified_date = '2013-03-01' WHERE id = " . $contact3;
2612 CRM_Core_DAO::executeQuery($sql);
2613 $contacts = $this->callAPISuccess('contact', 'get', array('modified_date' => array('<' => '2014-01-01')));
2614 $this->assertEquals($contacts['count'], 3);
2615 $contacts = $this->callAPISuccess('contact', 'get', array('modified_date' => array('>' => '2014-01-01')));
2616 $this->assertEquals($contacts['count'], $preExistingContactCount);
2617 }
2134e310 2618
2134e310 2619 /**
fe482240 2620 * CRM-14743 - test api respects search operators.
2134e310 2621 */
00be9182 2622 public function testGetCreatedDateByOperators() {
2134e310
EM
2623 $preExistingContactCount = CRM_Core_DAO::singleValueQuery('select count(*) FROM civicrm_contact');
2624 $contact1 = $this->individualCreate();
2625 $sql = "UPDATE civicrm_contact SET created_date = '2012-01-01' WHERE id = " . $contact1;
2626 CRM_Core_DAO::executeQuery($sql);
2627 $contact2 = $this->individualCreate();
2628 $sql = "UPDATE civicrm_contact SET created_date = '2012-02-01' WHERE id = " . $contact2;
2629 CRM_Core_DAO::executeQuery($sql);
2630 $contact3 = $this->householdCreate();
2631 $sql = "UPDATE civicrm_contact SET created_date = '2012-03-01' WHERE id = " . $contact3;
2632 CRM_Core_DAO::executeQuery($sql);
2633 $contacts = $this->callAPISuccess('contact', 'get', array('created_date' => array('<' => '2014-01-01')));
2634 $this->assertEquals($contacts['count'], 3);
2635 $contacts = $this->callAPISuccess('contact', 'get', array('created_date' => array('>' => '2014-01-01')));
2636 $this->assertEquals($contacts['count'], $preExistingContactCount);
2637 }
e5fccefb
EM
2638
2639 /**
fe482240 2640 * CRM-14263 check that API is not affected by search profile related bug.
e5fccefb 2641 */
5896d037 2642 public function testReturnCityProfile() {
e5fccefb
EM
2643 $contactID = $this->individualCreate();
2644 CRM_Core_Config::singleton()->defaultSearchProfileID = 1;
5896d037 2645 $this->callAPISuccess('address', 'create', array(
92915c55
TO
2646 'contact_id' => $contactID,
2647 'city' => 'Cool City',
2648 'location_type_id' => 1,
2649 ));
e5fccefb
EM
2650 $result = $this->callAPISuccess('contact', 'get', array('city' => 'Cool City', 'return' => 'contact_type'));
2651 $this->assertEquals(1, $result['count']);
2652 }
eaa112a5 2653
eaa112a5 2654 /**
701a69da 2655 * CRM-15443 - ensure getlist api does not return deleted contacts.
eaa112a5 2656 */
00be9182 2657 public function testGetlistExcludeConditions() {
eaa112a5 2658 $name = md5(time());
45fcf8b7 2659 $contact = $this->individualCreate(array('last_name' => $name));
381fa321 2660 $this->individualCreate(array('last_name' => $name, 'is_deceased' => 1));
2661 $this->individualCreate(array('last_name' => $name, 'is_deleted' => 1));
2662 // We should get all but the deleted contact.
eaa112a5 2663 $result = $this->callAPISuccess('contact', 'getlist', array('input' => $name));
ba4a1892 2664 $this->assertEquals(2, $result['count']);
381fa321 2665 // Force-exclude the deceased contact.
5896d037 2666 $result = $this->callAPISuccess('contact', 'getlist', array(
92915c55
TO
2667 'input' => $name,
2668 'params' => array('is_deceased' => 0),
2669 ));
ba4a1892
TM
2670 $this->assertEquals(1, $result['count']);
2671 $this->assertEquals($contact, $result['values'][0]['id']);
eaa112a5 2672 }
92776611
CW
2673
2674 /**
fe482240 2675 * Test contact getactions.
92776611 2676 */
00be9182 2677 public function testGetActions() {
92776611
CW
2678 $description = "Getting the available actions for an entity.";
2679 $result = $this->callAPIAndDocument($this->_entity, 'getactions', array(), __FUNCTION__, __FILE__, $description);
2680 $expected = array(
2681 'create',
2682 'delete',
2683 'get',
2684 'getactions',
2685 'getcount',
2686 'getfields',
2687 'getlist',
2688 'getoptions',
2689 'getquick',
2690 'getrefcount',
2691 'getsingle',
2692 'getvalue',
2693 'merge',
2694 'proximity',
2695 'replace',
2696 'setvalue',
2697 'update',
2698 );
2699 $deprecated = array(
2700 'update',
2701 'getquick',
2702 );
2703 foreach ($expected as $action) {
2704 $this->assertTrue(in_array($action, $result['values']), "Expected action $action");
2705 }
2706 foreach ($deprecated as $action) {
2707 $this->assertArrayKeyExists($action, $result['deprecated']);
2708 }
2709 }
96025800 2710
ffe87781 2711 /**
2712 * Test the duplicate check function.
2713 */
2714 public function testDuplicateCheck() {
2715 $this->callAPISuccess('Contact', 'create', array(
2716 'first_name' => 'Harry',
2717 'last_name' => 'Potter',
2718 'email' => 'harry@hogwarts.edu',
2719 'contact_type' => 'Individual',
2720 ));
2721 $result = $this->callAPISuccess('Contact', 'duplicatecheck', array(
2722 'match' => array(
2723 'first_name' => 'Harry',
2724 'last_name' => 'Potter',
2725 'email' => 'harry@hogwarts.edu',
2726 'contact_type' => 'Individual',
2727 ),
2728 ));
2729
2730 $this->assertEquals(1, $result['count']);
2731 $result = $this->callAPISuccess('Contact', 'duplicatecheck', array(
2732 'match' => array(
2733 'first_name' => 'Harry',
2734 'last_name' => 'Potter',
2735 'email' => 'no5@privet.drive',
2736 'contact_type' => 'Individual',
2737 ),
2738 ));
2739 $this->assertEquals(0, $result['count']);
2740 }
2741
46b3417a
CW
2742 public function testGetByContactType() {
2743 $individual = $this->callAPISuccess('Contact', 'create', array(
2744 'email' => 'individual@test.com',
2745 'contact_type' => 'Individual',
2746 ));
2747 $household = $this->callAPISuccess('Contact', 'create', array(
2748 'household_name' => 'household@test.com',
2749 'contact_type' => 'Household',
2750 ));
2751 $organization = $this->callAPISuccess('Contact', 'create', array(
2752 'organization_name' => 'organization@test.com',
2753 'contact_type' => 'Organization',
2754 ));
2755 // Test with id - getsingle will throw an exception if not found
2756 $this->callAPISuccess('Contact', 'getsingle', array(
2757 'id' => $individual['id'],
2758 'contact_type' => 'Individual',
2759 ));
2760 $this->callAPISuccess('Contact', 'getsingle', array(
2761 'id' => $individual['id'],
2762 'contact_type' => array('IN' => array('Individual')),
2763 'return' => 'id',
2764 ));
2765 $this->callAPISuccess('Contact', 'getsingle', array(
2766 'id' => $organization['id'],
2767 'contact_type' => array('IN' => array('Individual', 'Organization')),
2768 ));
2769 // Test as array
2770 $result = $this->callAPISuccess('Contact', 'get', array(
2771 'contact_type' => array('IN' => array('Individual', 'Organization')),
2772 'options' => array('limit' => 0),
2773 'return' => 'id',
2774 ));
2775 $this->assertContains($organization['id'], array_keys($result['values']));
2776 $this->assertContains($individual['id'], array_keys($result['values']));
2777 $this->assertNotContains($household['id'], array_keys($result['values']));
2778 // Test as string
2779 $result = $this->callAPISuccess('Contact', 'get', array(
2780 'contact_type' => 'Household',
2781 'options' => array('limit' => 0),
2782 'return' => 'id',
2783 ));
2784 $this->assertNotContains($organization['id'], array_keys($result['values']));
2785 $this->assertNotContains($individual['id'], array_keys($result['values']));
2786 $this->assertContains($household['id'], array_keys($result['values']));
2787 }
2788
6a488035 2789}