Merge pull request #5180 from monishdeb/master
[civicrm-core.git] / tests / phpunit / api / v3 / ContactTest.php
1 <?php
2 /**
3 * @file
4 * File for the TestContact class.
5 *
6 * (PHP 5)
7 *
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
11 * GNU Affero General Public License version 3
12 * @version $Id: ContactTest.php 31254 2010-12-15 10:09:29Z eileen $
13 * @package CiviCRM
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 */
35 require_once 'CiviTest/CiviUnitTestCase.php';
36
37
38 /**
39 * Test APIv3 civicrm_contact* functions
40 *
41 * @package CiviCRM_APIv3
42 * @subpackage API_Contact
43 */
44 class api_v3_ContactTest extends CiviUnitTestCase {
45 public $DBResetRequired = FALSE;
46 protected $_apiversion;
47 protected $_entity;
48 protected $_params;
49
50 protected $_contactID;
51 protected $_financialTypeId = 1;
52
53 /**
54 * Test setup for every test.
55 *
56 * Connect to the database, truncate the tables that will be used
57 * and redirect stdin to a temporary file
58 */
59 public function setUp() {
60 // Connect to the database.
61 parent::setUp();
62 $this->_apiversion = 3;
63 $this->_entity = 'contact';
64 $this->_params = array(
65 'first_name' => 'abc1',
66 'contact_type' => 'Individual',
67 'last_name' => 'xyz1',
68 );
69 }
70
71 public function tearDown() {
72 // truncate a few tables
73 $tablesToTruncate = array(
74 'civicrm_contact',
75 'civicrm_email',
76 'civicrm_contribution',
77 'civicrm_line_item',
78 'civicrm_website',
79 'civicrm_relationship',
80 'civicrm_uf_match',
81 'civicrm_phone',
82 );
83
84 $this->quickCleanup($tablesToTruncate, TRUE);
85 }
86
87 /**
88 * Test civicrm_contact_create.
89 *
90 * Verify that attempt to create individual contact with only
91 * first and last names succeeds
92 */
93 public function testAddCreateIndividual() {
94 $oldCount = CRM_Core_DAO::singleValueQuery('select count(*) from civicrm_contact');
95 $params = array(
96 'first_name' => 'abc1',
97 'contact_type' => 'Individual',
98 'last_name' => 'xyz1',
99 );
100
101 $contact = $this->callAPISuccess('contact', 'create', $params);
102 $this->assertTrue(is_numeric($contact['id']));
103 $this->assertTrue($contact['id'] > 0);
104 $newCount = CRM_Core_DAO::singleValueQuery('select count(*) from civicrm_contact');
105 $this->assertEquals($oldCount + 1, $newCount);
106
107 $this->assertDBState('CRM_Contact_DAO_Contact',
108 $contact['id'],
109 $params
110 );
111 }
112
113 /**
114 * Test civicrm_contact_create with sub-types.
115 *
116 * Verify that sub-types are created successfully and not deleted by subsequent updates.
117 */
118 public function testIndividualSubType() {
119 $params = array(
120 'first_name' => 'test abc',
121 'contact_type' => 'Individual',
122 'last_name' => 'test xyz',
123 'contact_sub_type' => array('Student', 'Staff'),
124 );
125 $contact = $this->callAPISuccess('contact', 'create', $params);
126 $cid = $contact['id'];
127
128 $params = array(
129 'id' => $cid,
130 'middle_name' => 'foo',
131 );
132 $this->callAPISuccess('contact', 'create', $params);
133 unset($params['middle_name']);
134
135 $contact = $this->callAPISuccess('contact', 'get', $params);
136
137 $this->assertEquals(array('Student', 'Staff'), $contact['values'][$cid]['contact_sub_type']);
138 }
139
140 /**
141 * Verify that attempt to create contact with empty params fails.
142 */
143 public function testCreateEmptyContact() {
144 $this->callAPIFailure('contact', 'create', array());
145 }
146
147 /**
148 * Verify that attempt to create contact with bad contact type fails.
149 */
150 public function testCreateBadTypeContact() {
151 $params = array(
152 'email' => 'man1@yahoo.com',
153 'contact_type' => 'Does not Exist',
154 );
155 $this->callAPIFailure('contact', 'create', $params, "'Does not Exist' is not a valid option for field contact_type");
156 }
157
158 /**
159 * Verify that attempt to create individual contact without required fields fails.
160 */
161 public function testCreateBadRequiredFieldsIndividual() {
162 $params = array(
163 'middle_name' => 'This field is not required',
164 'contact_type' => 'Individual',
165 );
166 $this->callAPIFailure('contact', 'create', $params);
167 }
168
169 /**
170 * Verify that attempt to create household contact without required fields fails.
171 */
172 public function testCreateBadRequiredFieldsHousehold() {
173 $params = array(
174 'middle_name' => 'This field is not required',
175 'contact_type' => 'Household',
176 );
177 $this->callAPIFailure('contact', 'create', $params);
178 }
179
180 /**
181 * Test required field check.
182 *
183 * Verify that attempt to create organization contact without required fields fails.
184 */
185 public function testCreateBadRequiredFieldsOrganization() {
186 $params = array(
187 'middle_name' => 'This field is not required',
188 'contact_type' => 'Organization',
189 );
190
191 $this->callAPIFailure('contact', 'create', $params);
192 }
193
194 /**
195 * Verify that attempt to create individual contact with only an email succeeds.
196 */
197 public function testCreateEmailIndividual() {
198
199 $params = array(
200 'email' => 'man3@yahoo.com',
201 'contact_type' => 'Individual',
202 'location_type_id' => 1,
203 );
204
205 $contact = $this->callAPISuccess('contact', 'create', $params);
206
207 $this->assertEquals(1, $contact['id']);
208 $email = $this->callAPISuccess('email', 'get', array('contact_id' => $contact['id']));
209 $this->assertEquals(1, $email['count']);
210 $this->assertEquals('man3@yahoo.com', $email['values'][$email['id']]['email']);
211
212 $this->callAPISuccess('contact', 'delete', $contact);
213 }
214
215 /**
216 * Test creating individual by name.
217 *
218 * Verify create individual contact with only first and last names succeeds.
219 */
220 public function testCreateNameIndividual() {
221 $params = array(
222 'first_name' => 'abc1',
223 'contact_type' => 'Individual',
224 'last_name' => 'xyz1',
225 );
226
227 $contact = $this->callAPISuccess('contact', 'create', $params);
228 $this->assertEquals(1, $contact['id']);
229 }
230
231 /**
232 * Test old keys still work.
233 *
234 * Verify that attempt to create individual contact with
235 * first and last names and old key values works
236 */
237 public function testCreateNameIndividualOldKeys() {
238 $params = array(
239 'individual_prefix' => 'Dr.',
240 'first_name' => 'abc1',
241 'contact_type' => 'Individual',
242 'last_name' => 'xyz1',
243 'individual_suffix' => 'Jr.',
244 );
245
246 $contact = $this->callAPISuccess('contact', 'create', $params);
247 $result = $this->callAPISuccess('contact', 'getsingle', array('id' => $contact['id']));
248
249 $this->assertArrayKeyExists('prefix_id', $result);
250 $this->assertArrayKeyExists('suffix_id', $result);
251 $this->assertArrayKeyExists('gender_id', $result);
252 $this->assertEquals(4, $result['prefix_id']);
253 $this->assertEquals(1, $result['suffix_id']);
254 }
255
256 /**
257 * Test preferred keys work.
258 *
259 * Verify that attempt to create individual contact with
260 * first and last names and old key values works
261 */
262 public function testCreateNameIndividualRecommendedKeys2() {
263 $params = array(
264 'prefix_id' => 'Dr.',
265 'first_name' => 'abc1',
266 'contact_type' => 'Individual',
267 'last_name' => 'xyz1',
268 'suffix_id' => 'Jr.',
269 'gender_id' => 'Male',
270 );
271
272 $contact = $this->callAPISuccess('contact', 'create', $params);
273 $result = $this->callAPISuccess('contact', 'getsingle', array('id' => $contact['id']));
274
275 $this->assertArrayKeyExists('prefix_id', $result);
276 $this->assertArrayKeyExists('suffix_id', $result);
277 $this->assertArrayKeyExists('gender_id', $result);
278 $this->assertEquals(4, $result['prefix_id']);
279 $this->assertEquals(1, $result['suffix_id']);
280 }
281
282 /**
283 * Test household name is sufficient for create.
284 *
285 * Verify that attempt to create household contact with only
286 * household name succeeds
287 */
288 public function testCreateNameHousehold() {
289 $params = array(
290 'household_name' => 'The abc Household',
291 'contact_type' => 'Household',
292 );
293 $contact = $this->callAPISuccess('contact', 'create', $params);
294 $this->assertEquals(1, $contact['id']);
295 }
296
297 /**
298 * Test organization name is sufficient for create.
299 *
300 * Verify that attempt to create organization contact with only
301 * organization name succeeds.
302 */
303 public function testCreateNameOrganization() {
304 $params = array(
305 'organization_name' => 'The abc Organization',
306 'contact_type' => 'Organization',
307 );
308 $contact = $this->callAPISuccess('contact', 'create', $params);
309 $this->assertEquals(1, $contact['id']);
310 }
311
312 /**
313 * Verify that attempt to create organization contact without organization name fails.
314 */
315 public function testCreateNoNameOrganization() {
316 $params = array(
317 'first_name' => 'The abc Organization',
318 'contact_type' => 'Organization',
319 );
320 $this->callAPIFailure('contact', 'create', $params);
321 }
322
323 /**
324 * Check with complete array + custom field.
325 *
326 * Note that the test is written on purpose without any
327 * variables specific to participant so it can be replicated into other entities
328 * and / or moved to the automated test suite
329 */
330 public function testCreateWithCustom() {
331 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
332
333 $params = $this->_params;
334 $params['custom_' . $ids['custom_field_id']] = "custom string";
335 $description = "This demonstrates setting a custom field through the API.";
336 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__, $description);
337
338 $check = $this->callAPISuccess($this->_entity, 'get', array(
339 'return.custom_' . $ids['custom_field_id'] => 1,
340 'id' => $result['id'],
341 ));
342 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']]);
343
344 $this->customFieldDelete($ids['custom_field_id']);
345 $this->customGroupDelete($ids['custom_group_id']);
346 }
347
348 /**
349 * CRM-12773 - expectation is that civicrm quietly ignores fields without values.
350 */
351 public function testCreateWithNULLCustomCRM12773() {
352 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
353 $params = $this->_params;
354 $params['custom_' . $ids['custom_field_id']] = NULL;
355 $this->callAPISuccess('contact', 'create', $params);
356 $this->customFieldDelete($ids['custom_field_id']);
357 $this->customGroupDelete($ids['custom_group_id']);
358 }
359
360
361 /**
362 * Test creating a current employer through API.
363 */
364 public function testContactCreateCurrentEmployer() {
365 // Here we will just do the get for set-up purposes.
366 $count = $this->callAPISuccess('contact', 'getcount', array(
367 'organization_name' => 'new employer org',
368 'contact_type' => 'Organization',
369 ));
370 $this->assertEquals(0, $count);
371 $employerResult = $this->callAPISuccess('contact', 'create', array_merge($this->_params, array(
372 'current_employer' => 'new employer org',
373 )
374 ));
375 // do it again as an update to check it doesn't cause an error
376 $employerResult = $this->callAPISuccess('contact', 'create', array_merge($this->_params, array(
377 'current_employer' => 'new employer org',
378 'id' => $employerResult['id'],
379 )
380 ));
381 $expectedCount = 1;
382 $this->callAPISuccess('contact', 'getcount', array(
383 'organization_name' => 'new employer org',
384 'contact_type' => 'Organization',
385 ),
386 $expectedCount);
387
388 $result = $this->callAPISuccess('contact', 'getsingle', array(
389 'id' => $employerResult['id'],
390 ));
391
392 $this->assertEquals('new employer org', $result['current_employer']);
393
394 }
395
396 /**
397 * Test creating a current employer through API.
398 *
399 * Check it will re-activate a de-activated employer
400 */
401 public function testContactCreateDuplicateCurrentEmployerEnables() {
402 // Set up - create employer relationship.
403 $employerResult = $this->callAPISuccess('contact', 'create', array_merge($this->_params, array(
404 'current_employer' => 'new employer org',
405 )
406 ));
407 $relationship = $this->callAPISuccess('relationship', 'get', array(
408 'contact_id_a' => $employerResult['id'],
409 ));
410
411 //disable & check it is disabled
412 $this->callAPISuccess('relationship', 'create', array('id' => $relationship['id'], 'is_active' => 0));
413 $this->callAPISuccess('relationship', 'getvalue', array(
414 'id' => $relationship['id'],
415 'return' => 'is_active',
416 ), 0);
417
418 // Re-set the current employer - thus enabling the relationship.
419 $this->callAPISuccess('contact', 'create', array_merge($this->_params, array(
420 'current_employer' => 'new employer org',
421 'id' => $employerResult['id'],
422 )
423 ));
424 //check is_active is now 1
425 $relationship = $this->callAPISuccess('relationship', 'getsingle', array(
426 'return' => 'is_active',
427 ));
428 $this->assertEquals(1, $relationship['is_active']);
429 }
430
431 /**
432 * Check deceased contacts are not retrieved.
433 *
434 * Note at time of writing the default is to return default. This should possibly be changed & test added.
435 */
436 public function testGetDeceasedRetrieved() {
437 $this->callAPISuccess($this->_entity, 'create', $this->_params);
438 $c2 = $this->callAPISuccess($this->_entity, 'create', array(
439 'first_name' => 'bb',
440 'last_name' => 'ccc',
441 'contact_type' => 'Individual',
442 'is_deceased' => 1,
443 ));
444 $result = $this->callAPISuccess($this->_entity, 'get', array('is_deceased' => 0));
445 $this->assertFalse(array_key_exists($c2['id'], $result['values']));
446 }
447
448 /**
449 * Test that sort works - old syntax.
450 */
451 public function testGetSort() {
452 $c1 = $this->callAPISuccess($this->_entity, 'create', $this->_params);
453 $c2 = $this->callAPISuccess($this->_entity, 'create', array(
454 'first_name' => 'bb',
455 'last_name' => 'ccc',
456 'contact_type' => 'Individual',
457 ));
458 $result = $this->callAPISuccess($this->_entity, 'get', array(
459 'sort' => 'first_name ASC',
460 'return.first_name' => 1,
461 'sequential' => 1,
462 'rowCount' => 1,
463 ));
464
465 $this->assertEquals('abc1', $result['values'][0]['first_name']);
466 $result = $this->callAPISuccess($this->_entity, 'get', array(
467 'sort' => 'first_name DESC',
468 'return.first_name' => 1,
469 'sequential' => 1,
470 'rowCount' => 1,
471 ));
472 $this->assertEquals('bb', $result['values'][0]['first_name']);
473
474 $this->callAPISuccess($this->_entity, 'delete', array('id' => $c1['id']));
475 $this->callAPISuccess($this->_entity, 'delete', array('id' => $c2['id']));
476 }
477
478 /**
479 * Test that we can retrieve contacts using array syntax.
480 *
481 * I.e 'id' => array('IN' => array('3,4')).
482 */
483 public function testGetINIDArray() {
484 $c1 = $this->callAPISuccess($this->_entity, 'create', $this->_params);
485 $c2 = $this->callAPISuccess($this->_entity, 'create', array(
486 'first_name' => 'bb',
487 'last_name' => 'ccc',
488 'contact_type' => 'Individual',
489 ));
490 $c3 = $this->callAPISuccess($this->_entity, 'create', array(
491 'first_name' => 'hh',
492 'last_name' => 'll',
493 'contact_type' => 'Individual',
494 ));
495 $result = $this->callAPISuccess($this->_entity, 'get', array('id' => array('IN' => array($c1['id'], $c3['id']))));
496 $this->assertEquals(2, $result['count']);
497 $this->assertEquals(array($c1['id'], $c3['id']), array_keys($result['values']));
498 $this->callAPISuccess($this->_entity, 'delete', array('id' => $c1['id']));
499 $this->callAPISuccess($this->_entity, 'delete', array('id' => $c2['id']));
500 $this->callAPISuccess($this->_entity, 'delete', array('id' => $c3['id']));
501 }
502
503 /**
504 * Test variants on deleted behaviour.
505 */
506 public function testGetDeleted() {
507 $params = $this->_params;
508 $contact1 = $this->callAPISuccess('contact', 'create', $params);
509 $params['is_deleted'] = 1;
510 $params['last_name'] = 'bcd';
511 $contact2 = $this->callAPISuccess('contact', 'create', $params);
512 $countActive = $this->callAPISuccess('contact', 'getcount', array('showAll' => 'active'));
513 $countAll = $this->callAPISuccess('contact', 'getcount', array('showAll' => 'all'));
514 $countTrash = $this->callAPISuccess('contact', 'getcount', array('showAll' => 'trash'));
515 $countDefault = $this->callAPISuccess('contact', 'getcount', array());
516 $countDeleted = $this->callAPISuccess('contact', 'getcount', array(
517 'contact_is_deleted' => 1,
518 ));
519 $countNotDeleted = $this->callAPISuccess('contact', 'getcount', array(
520 'contact_is_deleted' => 0,
521 ));
522 $this->callAPISuccess('contact', 'delete', array('id' => $contact1['id']));
523 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
524 $this->assertEquals(1, $countNotDeleted, 'contact_is_deleted => 0 is respected in line ' . __LINE__);
525 $this->assertEquals(1, $countActive);
526 $this->assertEquals(1, $countTrash);
527 $this->assertEquals(2, $countAll);
528 $this->assertEquals(1, $countDeleted);
529 $this->assertEquals(1, $countDefault, 'Only active by default in line ' . __LINE__);
530 }
531
532 /**
533 * Test that sort works - new syntax.
534 */
535 public function testGetSortNewSyntax() {
536 $c1 = $this->callAPISuccess($this->_entity, 'create', $this->_params);
537 $c2 = $this->callAPISuccess($this->_entity, 'create', array(
538 'first_name' => 'bb',
539 'last_name' => 'ccc',
540 'contact_type' => 'Individual',
541 ));
542 $result = $this->callAPISuccess($this->_entity, 'getvalue', array(
543 'return' => 'first_name',
544 'options' => array(
545 'limit' => 1,
546 'sort' => 'first_name',
547 ),
548 ));
549 $this->assertEquals('abc1', $result, 'in line' . __LINE__);
550
551 $result = $this->callAPISuccess($this->_entity, 'getvalue', array(
552 'return' => 'first_name',
553 'options' => array(
554 'limit' => 1,
555 'sort' => 'first_name DESC',
556 ),
557 ));
558 $this->assertEquals('bb', $result);
559
560 $this->callAPISuccess($this->_entity, 'delete', array('id' => $c1['id']));
561 $this->callAPISuccess($this->_entity, 'delete', array('id' => $c2['id']));
562 }
563
564 /**
565 * Test apostrophe works in get & create.
566 */
567 public function testGetApostropheCRM10857() {
568 $params = array_merge($this->_params, array('last_name' => "O'Connor"));
569 $this->callAPISuccess($this->_entity, 'create', $params);
570 $result = $this->callAPISuccess($this->_entity, 'getsingle', array(
571 'last_name' => "O'Connor",
572 'sequential' => 1,
573 ));
574 $this->assertEquals("O'Connor", $result['last_name'], 'in line' . __LINE__);
575 }
576
577 /**
578 * Check with complete array + custom field.
579 *
580 * Note that the test is written on purpose without any
581 * variables specific to participant so it can be replicated into other entities
582 * and / or moved to the automated test suite
583 */
584 public function testGetWithCustom() {
585 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
586
587 $params = $this->_params;
588 $params['custom_' . $ids['custom_field_id']] = "custom string";
589 $description = "This demonstrates setting a custom field through the API.";
590 $subfile = "CustomFieldGet";
591 $result = $this->callAPISuccess($this->_entity, 'create', $params);
592
593 $check = $this->callAPIAndDocument($this->_entity, 'get', array(
594 'return.custom_' . $ids['custom_field_id'] => 1,
595 'id' => $result['id'],
596 ), __FUNCTION__, __FILE__, $description, $subfile);
597
598 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']]);
599 $fields = ($this->callAPISuccess('contact', 'getfields', $params));
600 $this->assertTrue(is_array($fields['values']['custom_' . $ids['custom_field_id']]));
601 $this->customFieldDelete($ids['custom_field_id']);
602 $this->customGroupDelete($ids['custom_group_id']);
603 }
604
605 /**
606 * Check with complete array + custom field.
607 *
608 * Note that the test is written on purpose without any
609 * variables specific to participant so it can be replicated into other entities
610 * and / or moved to the automated test suite
611 */
612 public function testGetWithCustomReturnSyntax() {
613 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
614
615 $params = $this->_params;
616 $params['custom_' . $ids['custom_field_id']] = "custom string";
617 $description = "This demonstrates setting a custom field through the API.";
618 $subfile = "CustomFieldGetReturnSyntaxVariation";
619 $result = $this->callAPISuccess($this->_entity, 'create', $params);
620 $params = array('return' => 'custom_' . $ids['custom_field_id'], 'id' => $result['id']);
621 $check = $this->callAPIAndDocument($this->_entity, 'get', $params, __FUNCTION__, __FILE__, $description, $subfile);
622
623 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']]);
624 $this->customFieldDelete($ids['custom_field_id']);
625 $this->customGroupDelete($ids['custom_group_id']);
626 }
627
628 /**
629 * Check that address name is returned if required.
630 */
631 public function testGetReturnAddressName() {
632 $contactID = $this->individualCreate();
633 $this->callAPISuccess('address', 'create', array(
634 'contact_id' => $contactID,
635 'address_name' => 'My house',
636 'location_type_id' => 'Home',
637 'street_address' => '1 my road',
638 ));
639 $result = $this->callAPISuccessGetSingle('contact', array(
640 'return' => 'address_name, street_address',
641 'id' => $contactID,
642 ));
643 $this->assertEquals('1 my road', $result['street_address']);
644 $this->assertEquals('My house', $result['address_name']);
645
646 }
647
648 public function testGetGroupIDFromContact() {
649 $groupId = $this->groupCreate();
650 $description = "Get all from group and display contacts.";
651 $subFile = "GroupFilterUsingContactAPI";
652 $params = array(
653 'email' => 'man2@yahoo.com',
654 'contact_type' => 'Individual',
655 'location_type_id' => 1,
656 'api.group_contact.create' => array('group_id' => $groupId),
657 );
658
659 $this->callAPISuccess('contact', 'create', $params);
660 // testing as integer
661 $params = array(
662 'filter.group_id' => $groupId,
663 'contact_type' => 'Individual',
664 );
665 $result = $this->callAPIAndDocument('contact', 'get', $params, __FUNCTION__, __FILE__, $description, $subFile);
666 $this->assertEquals(1, $result['count']);
667 // group 26 doesn't exist, but we can still search contacts in it.
668 $params = array(
669 'filter.group_id' => 26,
670 'contact_type' => 'Individual',
671 );
672 $this->callAPISuccess('contact', 'get', $params);
673 // testing as string
674 $params = array(
675 'filter.group_id' => "$groupId, 26",
676 'contact_type' => 'Individual',
677 );
678 $result = $this->callAPIAndDocument('contact', 'get', $params, __FUNCTION__, __FILE__, $description, $subFile);
679 $this->assertEquals(1, $result['count']);
680 $params = array(
681 'filter.group_id' => "26,27",
682 'contact_type' => 'Individual',
683 );
684 $this->callAPISuccess('contact', 'get', $params);
685
686 // testing as string
687 $params = array(
688 'filter.group_id' => array($groupId, 26),
689 'contact_type' => 'Individual',
690 );
691 $result = $this->callAPIAndDocument('contact', 'get', $params, __FUNCTION__, __FILE__, $description, $subFile);
692 $this->assertEquals(1, $result['count']);
693
694 //test in conjunction with other criteria
695 $params = array(
696 'filter.group_id' => array($groupId, 26),
697 'contact_type' => 'Organization',
698 );
699 $this->callAPISuccess('contact', 'get', $params);
700 $params = array(
701 'filter.group_id' => array(26, 27),
702 'contact_type' => 'Individual',
703 );
704 $result = $this->callAPISuccess('contact', 'get', $params);
705 $this->assertEquals(0, $result['count']);
706 }
707
708 /**
709 * Verify that attempt to create individual contact with two chained websites succeeds.
710 */
711 public function testCreateIndividualWithContributionDottedSyntax() {
712 $description = "This demonstrates the syntax to create 2 chained entities.";
713 $subFile = "ChainTwoWebsites";
714 $params = array(
715 'first_name' => 'abc3',
716 'last_name' => 'xyz3',
717 'contact_type' => 'Individual',
718 'email' => 'man3@yahoo.com',
719 'api.contribution.create' => array(
720 'receive_date' => '2010-01-01',
721 'total_amount' => 100.00,
722 'financial_type_id' => $this->_financialTypeId,
723 'payment_instrument_id' => 1,
724 'non_deductible_amount' => 10.00,
725 'fee_amount' => 50.00,
726 'net_amount' => 90.00,
727 'trxn_id' => 15345,
728 'invoice_id' => 67990,
729 'source' => 'SSF',
730 'contribution_status_id' => 1,
731 ),
732 'api.website.create' => array(
733 'url' => "http://civicrm.org",
734 ),
735 'api.website.create.2' => array(
736 'url' => "http://chained.org",
737 ),
738 );
739
740 $result = $this->callAPIAndDocument('Contact', 'create', $params, __FUNCTION__, __FILE__, $description, $subFile);
741
742 $this->assertEquals(1, $result['id']);
743 // checking child function result not covered in callAPIAndDocument
744 $this->assertAPISuccess($result['values'][$result['id']]['api.website.create']);
745 $this->assertEquals("http://chained.org", $result['values'][$result['id']]['api.website.create.2']['values'][0]['url']);
746 $this->assertEquals("http://civicrm.org", $result['values'][$result['id']]['api.website.create']['values'][0]['url']);
747
748 // delete the contact
749 $this->callAPISuccess('contact', 'delete', $result);
750 }
751
752 /**
753 * Verify that attempt to create individual contact with chained contribution and website succeeds.
754 */
755 public function testCreateIndividualWithContributionChainedArrays() {
756 $params = array(
757 'first_name' => 'abc3',
758 'last_name' => 'xyz3',
759 'contact_type' => 'Individual',
760 'email' => 'man3@yahoo.com',
761 'api.contribution.create' => array(
762 'receive_date' => '2010-01-01',
763 'total_amount' => 100.00,
764 'financial_type_id' => $this->_financialTypeId,
765 'payment_instrument_id' => 1,
766 'non_deductible_amount' => 10.00,
767 'fee_amount' => 50.00,
768 'net_amount' => 90.00,
769 'trxn_id' => 12345,
770 'invoice_id' => 67890,
771 'source' => 'SSF',
772 'contribution_status_id' => 1,
773 ),
774 'api.website.create' => array(
775 array(
776 'url' => "http://civicrm.org",
777 ),
778 array(
779 'url' => "http://chained.org",
780 'website_type_id' => 2,
781 ),
782 ),
783 );
784
785 $description = "Demonstrates creating two websites as an array.";
786 $subfile = "ChainTwoWebsitesSyntax2";
787 $result = $this->callAPIAndDocument('Contact', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
788
789 $this->assertEquals(1, $result['id']);
790 // the callAndDocument doesn't check the chained call
791 $this->assertEquals(0, $result['values'][$result['id']]['api.website.create'][0]['is_error']);
792 $this->assertEquals("http://chained.org", $result['values'][$result['id']]['api.website.create'][1]['values'][0]['url']);
793 $this->assertEquals("http://civicrm.org", $result['values'][$result['id']]['api.website.create'][0]['values'][0]['url']);
794
795 $this->callAPISuccess('contact', 'delete', $result);
796 }
797
798 /**
799 * Verify that attempt to create individual contact with first, and last names and email succeeds.
800 */
801 public function testCreateIndividualWithNameEmail() {
802 $params = array(
803 'first_name' => 'abc3',
804 'last_name' => 'xyz3',
805 'contact_type' => 'Individual',
806 'email' => 'man3@yahoo.com',
807 );
808
809 $contact = $this->callAPISuccess('contact', 'create', $params);
810 $this->assertEquals(1, $contact['id']);
811
812 $this->callAPISuccess('contact', 'delete', $contact);
813 }
814
815 /**
816 * Verify that attempt to create individual contact with no data fails.
817 */
818 public function testCreateIndividualWithOutNameEmail() {
819 $params = array(
820 'contact_type' => 'Individual',
821 );
822 $this->callAPIFailure('contact', 'create', $params);
823 }
824
825 /**
826 * Test create individual contact with first &last names, email and location type succeeds.
827 */
828 public function testCreateIndividualWithNameEmailLocationType() {
829 $params = array(
830 'first_name' => 'abc4',
831 'last_name' => 'xyz4',
832 'email' => 'man4@yahoo.com',
833 'contact_type' => 'Individual',
834 'location_type_id' => 1,
835 );
836 $result = $this->callAPISuccess('contact', 'create', $params);
837
838 $this->assertEquals(1, $result['id']);
839
840 $this->callAPISuccess('contact', 'delete', array('id' => $result['id']));
841 }
842
843 /**
844 * Verify that when changing employers the old employer relationship becomes inactive.
845 */
846 public function testCreateIndividualWithEmployer() {
847 $employer = $this->organizationCreate();
848 $employer2 = $this->organizationCreate();
849
850 $params = array(
851 'email' => 'man4@yahoo.com',
852 'contact_type' => 'Individual',
853 'employer_id' => $employer,
854 );
855
856 $result = $this->callAPISuccess('contact', 'create', $params);
857 $relationships = $this->callAPISuccess('relationship', 'get', array(
858 'contact_id_a' => $result['id'],
859 'sequential' => 1,
860 ));
861
862 $this->assertEquals($employer, $relationships['values'][0]['contact_id_b']);
863
864 // Add more random relationships to make the test more realistic
865 foreach (array('Employee of', 'Volunteer for') as $relationshipType) {
866 $relTypeId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', $relationshipType, 'id', 'name_a_b');
867 $this->callAPISuccess('relationship', 'create', array(
868 'contact_id_a' => $result['id'],
869 'contact_id_b' => $this->organizationCreate(),
870 'is_active' => 1,
871 'relationship_type_id' => $relTypeId,
872 ));
873 }
874
875 // Add second employer
876 $params['employer_id'] = $employer2;
877 $params['id'] = $result['id'];
878 $result = $this->callAPISuccess('contact', 'create', $params);
879
880 $relationships = $this->callAPISuccess('relationship', 'get', array(
881 'contact_id_a' => $result['id'],
882 'sequential' => 1,
883 'is_active' => 0,
884 ));
885
886 $this->assertEquals($employer, $relationships['values'][0]['contact_id_b']);
887 }
888
889 /**
890 * Verify that attempt to create household contact with details succeeds.
891 */
892 public function testCreateHouseholdDetails() {
893 $params = array(
894 'household_name' => 'abc8\'s House',
895 'nick_name' => 'x House',
896 'email' => 'man8@yahoo.com',
897 'contact_type' => 'Household',
898 );
899
900 $contact = $this->callAPISuccess('contact', 'create', $params);
901
902 $this->assertEquals(1, $contact['id']);
903
904 $this->callAPISuccess('contact', 'delete', $contact);
905 }
906
907 /**
908 * Verify that attempt to create household contact with inadequate details fails.
909 */
910 public function testCreateHouseholdInadequateDetails() {
911 $params = array(
912 'nick_name' => 'x House',
913 'email' => 'man8@yahoo.com',
914 'contact_type' => 'Household',
915 );
916 $this->callAPIFailure('contact', 'create', $params);
917 }
918
919 /**
920 * Verify successful update of individual contact.
921 */
922 public function testUpdateIndividualWithAll() {
923 // Insert a row in civicrm_contact creating individual contact.
924 $op = new PHPUnit_Extensions_Database_Operation_Insert();
925 $op->execute($this->_dbconn,
926 $this->createXMLDataSet(
927 dirname(__FILE__) . '/dataset/contact_ind.xml'
928 )
929 );
930
931 $params = array(
932 'id' => 23,
933 'first_name' => 'abcd',
934 'contact_type' => 'Individual',
935 'nick_name' => 'This is nickname first',
936 'do_not_email' => '1',
937 'do_not_phone' => '1',
938 'do_not_mail' => '1',
939 'do_not_trade' => '1',
940 'legal_identifier' => 'ABC23853ZZ2235',
941 'external_identifier' => '1928837465',
942 'image_URL' => 'http://some.url.com/image.jpg',
943 'home_url' => 'http://www.example.org',
944
945 );
946
947 $this->callAPISuccess('Contact', 'Update', $params);
948 $getResult = $this->callAPISuccess('Contact', 'Get', $params);
949 unset($params['contact_id']);
950 //Todo - neither API v2 or V3 are testing for home_url - not sure if it is being set.
951 //reducing this test partially back to api v2 level to get it through
952 unset($params['home_url']);
953 foreach ($params as $key => $value) {
954 $this->assertEquals($value, $getResult['values'][23][$key]);
955 }
956 // Check updated civicrm_contact against expected.
957 $expected = $this->createXMLDataSet(
958 dirname(__FILE__) . '/dataset/contact_ind_upd.xml'
959 );
960 $actual = new PHPUnit_Extensions_Database_DataSet_QueryDataSet(
961 $this->_dbconn
962 );
963 $actual->addTable('civicrm_contact');
964 $expected->matches($actual);
965 }
966
967 /**
968 * Verify successful update of organization contact.
969 */
970 public function testUpdateOrganizationWithAll() {
971 // Insert a row in civicrm_contact creating organization contact
972 $op = new PHPUnit_Extensions_Database_Operation_Insert();
973 $op->execute($this->_dbconn,
974 $this->createXMLDataSet(
975 dirname(__FILE__) . '/dataset/contact_org.xml'
976 )
977 );
978
979 $params = array(
980 'id' => 24,
981 'organization_name' => 'WebAccess India Pvt Ltd',
982 'legal_name' => 'WebAccess',
983 'sic_code' => 'ABC12DEF',
984 'contact_type' => 'Organization',
985 );
986
987 $this->callAPISuccess('Contact', 'Update', $params);
988
989 // Check updated civicrm_contact against expected.
990 $expected = $this->createXMLDataSet(
991 dirname(__FILE__) . '/dataset/contact_org_upd.xml'
992 );
993 $actual = new PHPUnit_Extensions_Database_DataSet_QueryDataSet(
994 $this->_dbconn
995 );
996 $actual->addTable('civicrm_contact');
997 $expected->matches($actual);
998 }
999
1000 /**
1001 * Verify successful update of household contact.
1002 */
1003 public function testUpdateHouseholdWithAll() {
1004 // Insert a row in civicrm_contact creating household contact
1005 $op = new PHPUnit_Extensions_Database_Operation_Insert();
1006 $op->execute($this->_dbconn,
1007 $this->createXMLDataSet(
1008 dirname(__FILE__) . '/dataset/contact_hld.xml'
1009 )
1010 );
1011
1012 $params = array(
1013 'id' => 25,
1014 'household_name' => 'ABC household',
1015 'nick_name' => 'ABC House',
1016 'contact_type' => 'Household',
1017 );
1018
1019 $result = $this->callAPISuccess('Contact', 'Update', $params);
1020
1021 $expected = array(
1022 'contact_type' => 'Household',
1023 'is_opt_out' => 0,
1024 'sort_name' => 'ABC household',
1025 'display_name' => 'ABC household',
1026 'nick_name' => 'ABC House',
1027 );
1028 $this->getAndCheck($expected, $result['id'], 'contact');
1029 }
1030
1031 /**
1032 * Test civicrm_update() without contact type.
1033 *
1034 * Deliberately exclude contact_type as it should still cope using civicrm_api.
1035 *
1036 * CRM-7645.
1037 */
1038 public function testUpdateCreateWithID() {
1039 // Insert a row in civicrm_contact creating individual contact.
1040 $op = new PHPUnit_Extensions_Database_Operation_Insert();
1041 $op->execute($this->_dbconn,
1042 $this->createXMLDataSet(
1043 dirname(__FILE__) . '/dataset/contact_ind.xml'
1044 )
1045 );
1046
1047 $params = array(
1048 'id' => 23,
1049 'first_name' => 'abcd',
1050 'last_name' => 'wxyz',
1051 );
1052 $this->callAPISuccess('Contact', 'Update', $params);
1053 }
1054
1055 /**
1056 * Test civicrm_contact_delete() with no contact ID.
1057 */
1058 public function testContactDeleteNoID() {
1059 $params = array(
1060 'foo' => 'bar',
1061 );
1062 $this->callAPIFailure('contact', 'delete', $params);
1063 }
1064
1065 /**
1066 * Test civicrm_contact_delete() with error.
1067 */
1068 public function testContactDeleteError() {
1069 $params = array('contact_id' => 999);
1070 $this->callAPIFailure('contact', 'delete', $params);
1071 }
1072
1073 /**
1074 * Test civicrm_contact_delete().
1075 */
1076 public function testContactDelete() {
1077 $contactID = $this->individualCreate();
1078 $params = array(
1079 'id' => $contactID,
1080 );
1081 $this->callAPIAndDocument('contact', 'delete', $params, __FUNCTION__, __FILE__);
1082 }
1083
1084 /**
1085 * Test civicrm_contact_get() return only first name.
1086 */
1087 public function testContactGetRetFirst() {
1088 $contact = $this->callAPISuccess('contact', 'create', $this->_params);
1089 $params = array(
1090 'contact_id' => $contact['id'],
1091 'return_first_name' => TRUE,
1092 'sort' => 'first_name',
1093 );
1094 $result = $this->callAPISuccess('contact', 'get', $params);
1095 $this->assertEquals(1, $result['count']);
1096 $this->assertEquals($contact['id'], $result['id']);
1097 $this->assertEquals('abc1', $result['values'][$contact['id']]['first_name']);
1098 }
1099
1100 /**
1101 * Test civicrm_contact_get() return only first name & last name.
1102 *
1103 * Use comma separated string return with a space.
1104 */
1105 public function testContactGetReturnFirstLast() {
1106 $contact = $this->callAPISuccess('contact', 'create', $this->_params);
1107 $params = array(
1108 'contact_id' => $contact['id'],
1109 'return' => 'first_name, last_name',
1110 );
1111 $result = $this->callAPISuccess('contact', 'getsingle', $params);
1112 $this->assertEquals('abc1', $result['first_name']);
1113 $this->assertEquals('xyz1', $result['last_name']);
1114 //check that other defaults not returns
1115 $this->assertArrayNotHasKey('sort_name', $result);
1116 $params = array(
1117 'contact_id' => $contact['id'],
1118 'return' => 'first_name,last_name',
1119 );
1120 $result = $this->callAPISuccess('contact', 'getsingle', $params);
1121 $this->assertEquals('abc1', $result['first_name']);
1122 $this->assertEquals('xyz1', $result['last_name']);
1123 //check that other defaults not returns
1124 $this->assertArrayNotHasKey('sort_name', $result);
1125 }
1126
1127 /**
1128 * Test civicrm_contact_get() return only first name & last name.
1129 *
1130 * Use comma separated string return without a space
1131 */
1132 public function testContactGetReturnFirstLastNoComma() {
1133 $contact = $this->callAPISuccess('contact', 'create', $this->_params);
1134 $params = array(
1135 'contact_id' => $contact['id'],
1136 'return' => 'first_name,last_name',
1137 );
1138 $result = $this->callAPISuccess('contact', 'getsingle', $params);
1139 $this->assertEquals('abc1', $result['first_name']);
1140 $this->assertEquals('xyz1', $result['last_name']);
1141 //check that other defaults not returns
1142 $this->assertArrayNotHasKey('sort_name', $result);
1143 }
1144
1145 /**
1146 * Test civicrm_contact_get() with default return properties.
1147 */
1148 public function testContactGetRetDefault() {
1149 $contactID = $this->individualCreate();
1150 $params = array(
1151 'contact_id' => $contactID,
1152 'sort' => 'first_name',
1153 );
1154 $result = $this->callAPISuccess('contact', 'get', $params);
1155 $this->assertEquals($contactID, $result['values'][$contactID]['contact_id']);
1156 $this->assertEquals('Anthony', $result['values'][$contactID]['first_name']);
1157 }
1158
1159 /**
1160 * Test civicrm_contact_getquick() with empty name param.
1161 */
1162 public function testContactGetQuick() {
1163 // Insert a row in civicrm_contact creating individual contact.
1164 $op = new PHPUnit_Extensions_Database_Operation_Insert();
1165 $op->execute($this->_dbconn,
1166 $this->createXMLDataSet(
1167 dirname(__FILE__) . '/dataset/contact_17.xml'
1168 )
1169 );
1170 $op->execute($this->_dbconn,
1171 $this->createXMLDataSet(
1172 dirname(__FILE__) . '/dataset/email_contact_17.xml'
1173 )
1174 );
1175 $params = array(
1176 'name' => "T",
1177 );
1178
1179 $result = $this->callAPISuccess('contact', 'getquick', $params);
1180 $this->assertEquals(17, $result['values'][0]['id']);
1181 }
1182
1183 /**
1184 * Test civicrm_contact_get) with empty params.
1185 */
1186 public function testContactGetEmptyParams() {
1187 $this->callAPISuccess('contact', 'get', array());
1188 }
1189
1190 /**
1191 * Test civicrm_contact_get(,true) with no matches.
1192 */
1193 public function testContactGetOldParamsNoMatches() {
1194 // Insert a row in civicrm_contact creating contact 17.
1195 $op = new PHPUnit_Extensions_Database_Operation_Insert();
1196 $op->execute($this->_dbconn,
1197 $this->createXMLDataSet(
1198 dirname(__FILE__) . '/dataset/contact_17.xml'
1199 )
1200 );
1201
1202 $params = array(
1203 'first_name' => 'Fred',
1204 );
1205 $result = $this->callAPISuccess('contact', 'get', $params);
1206 $this->assertEquals(0, $result['count']);
1207 }
1208
1209 /**
1210 * Test civicrm_contact_get(,true) with one match.
1211 */
1212 public function testContactGetOldParamsOneMatch() {
1213 // Insert a row in civicrm_contact creating contact 17
1214 $op = new PHPUnit_Extensions_Database_Operation_Insert();
1215 $op->execute($this->_dbconn,
1216 $this->createXMLDataSet(dirname(__FILE__) . '/dataset/contact_17.xml'
1217 )
1218 );
1219
1220 $params = array(
1221 'first_name' => 'Test',
1222 );
1223 $result = $this->callAPISuccess('contact', 'get', $params);
1224 $this->assertEquals(17, $result['values'][17]['contact_id']);
1225 $this->assertEquals(17, $result['id']);
1226 }
1227
1228 /**
1229 * Test civicrm_contact_search_count().
1230 */
1231 public function testContactGetEmail() {
1232 $params = array(
1233 'email' => 'man2@yahoo.com',
1234 'contact_type' => 'Individual',
1235 'location_type_id' => 1,
1236 );
1237
1238 $contact = $this->callAPISuccess('contact', 'create', $params);
1239
1240 $this->assertEquals(1, $contact['id']);
1241
1242 $params = array(
1243 'email' => 'man2@yahoo.com',
1244 );
1245 $result = $this->callAPIAndDocument('contact', 'get', $params, __FUNCTION__, __FILE__);
1246 $this->assertEquals(1, $result['values'][1]['contact_id']);
1247 $this->assertEquals('man2@yahoo.com', $result['values'][1]['email']);
1248
1249 // delete the contact
1250 $this->callAPISuccess('contact', 'delete', $contact);
1251 }
1252
1253 /**
1254 * Test birth date parameters.
1255 *
1256 * These include value, array & birth_date_high, birth_date_low
1257 * && deceased.
1258 */
1259 public function testContactGetBirthDate() {
1260 $contact1 = $this->callAPISuccess('contact', 'create', array_merge($this->_params, array('birth_date' => 'first day of next month - 2 years')));
1261 $contact2 = $this->callAPISuccess('contact', 'create', array_merge($this->_params, array('birth_date' => 'first day of next month - 5 years')));
1262 $contact3 = $this->callAPISuccess('contact', 'create', array_merge($this->_params, array('birth_date' => 'first day of next month -20 years')));
1263
1264 $result = $this->callAPISuccess('contact', 'get', array());
1265 $this->assertEquals(date('Y-m-d', strtotime('first day of next month -2 years')), $result['values'][$contact1['id']]['birth_date']);
1266 $result = $this->callAPISuccess('contact', 'get', array('birth_date' => 'first day of next month -5 years'));
1267 $this->assertEquals(1, $result['count']);
1268 $this->assertEquals(date('Y-m-d', strtotime('first day of next month -5 years')), $result['values'][$contact2['id']]['birth_date']);
1269 $result = $this->callAPISuccess('contact', 'get', array('birth_date_high' => date('Y-m-d', strtotime('-6 years'))));
1270 $this->assertEquals(1, $result['count']);
1271 $this->assertEquals(date('Y-m-d', strtotime('first day of next month -20 years')), $result['values'][$contact3['id']]['birth_date']);
1272 $result = $this->callAPISuccess('contact', 'get', array(
1273 'birth_date_low' => date('Y-m-d', strtotime('-6 years')),
1274 'birth_date_high' => date('Y-m-d', strtotime('- 3 years')),
1275 ));
1276 $this->assertEquals(1, $result['count']);
1277 $this->assertEquals(date('Y-m-d', strtotime('first day of next month -5 years')), $result['values'][$contact2['id']]['birth_date']);
1278 $result = $this->callAPISuccess('contact', 'get', array(
1279 'birth_date_low' => '-6 years',
1280 'birth_date_high' => '- 3 years',
1281 ));
1282 $this->assertEquals(1, $result['count']);
1283 $this->assertEquals(date('Y-m-d', strtotime('first day of next month -5 years')), $result['values'][$contact2['id']]['birth_date']);
1284 }
1285
1286 /**
1287 * Test Deceased date parameters.
1288 *
1289 * These include value, array & Deceased_date_high, Deceased date_low
1290 * && deceased.
1291 */
1292 public function testContactGetDeceasedDate() {
1293 $contact1 = $this->callAPISuccess('contact', 'create', array_merge($this->_params, array('deceased_date' => 'first day of next month - 2 years')));
1294 $contact2 = $this->callAPISuccess('contact', 'create', array_merge($this->_params, array('deceased_date' => 'first day of next month - 5 years')));
1295 $contact3 = $this->callAPISuccess('contact', 'create', array_merge($this->_params, array('deceased_date' => 'first day of next month -20 years')));
1296
1297 $result = $this->callAPISuccess('contact', 'get', array());
1298 $this->assertEquals(date('Y-m-d', strtotime('first day of next month -2 years')), $result['values'][$contact1['id']]['deceased_date']);
1299 $result = $this->callAPISuccess('contact', 'get', array('deceased_date' => 'first day of next month -5 years'));
1300 $this->assertEquals(1, $result['count']);
1301 $this->assertEquals(date('Y-m-d', strtotime('first day of next month -5 years')), $result['values'][$contact2['id']]['deceased_date']);
1302 $result = $this->callAPISuccess('contact', 'get', array('deceased_date_high' => date('Y-m-d', strtotime('-6 years'))));
1303 $this->assertEquals(1, $result['count']);
1304 $this->assertEquals(date('Y-m-d', strtotime('first day of next month -20 years')), $result['values'][$contact3['id']]['deceased_date']);
1305 $result = $this->callAPISuccess('contact', 'get', array(
1306 'deceased_date_low' => '-6 years',
1307 'deceased_date_high' => date('Y-m-d', strtotime('- 3 years')),
1308 ));
1309 $this->assertEquals(1, $result['count']);
1310 $this->assertEquals(date('Y-m-d', strtotime('first day of next month -5 years')), $result['values'][$contact2['id']]['deceased_date']);
1311 }
1312
1313 /**
1314 * Test for Contact.get id=@user:username.
1315 */
1316 public function testContactGetByUsername() {
1317 // Setup - create contact with a uf-match.
1318 $cid = $this->individualCreate(array(
1319 'contact_type' => 'Individual',
1320 'first_name' => 'testGetByUsername',
1321 'last_name' => 'testGetByUsername',
1322 ));
1323
1324 $ufMatchParams = array(
1325 'domain_id' => CRM_Core_Config::domainID(),
1326 'uf_id' => 99,
1327 'uf_name' => 'the-email-matching-key-is-not-really-the-username',
1328 'contact_id' => $cid,
1329 );
1330 $ufMatch = CRM_Core_BAO_UFMatch::create($ufMatchParams);
1331 $this->assertTrue(is_numeric($ufMatch->id));
1332
1333 // setup - mock the calls to CRM_Utils_System_*::getUfId
1334 $userSystem = $this->getMock('CRM_Utils_System_UnitTests', array('getUfId'));
1335 $userSystem->expects($this->once())
1336 ->method('getUfId')
1337 ->with($this->equalTo('exampleUser'))
1338 ->will($this->returnValue(99));
1339 CRM_Core_Config::singleton()->userSystem = $userSystem;
1340
1341 // perform a lookup
1342 $result = $this->callAPISuccess('Contact', 'get', array(
1343 'id' => '@user:exampleUser',
1344 ));
1345 $this->assertEquals('testGetByUsername', $result['values'][$cid]['first_name']);
1346 }
1347
1348 /**
1349 * Test to check return works OK.
1350 */
1351 public function testContactGetReturnValues() {
1352 $extraParams = array('nick_name' => 'Bob', 'phone' => '456', 'email' => 'e@mail.com');
1353 $contactID = $this->individualCreate($extraParams);
1354 //actually it turns out the above doesn't create a phone
1355 $this->callAPISuccess('phone', 'create', array('contact_id' => $contactID, 'phone' => '456'));
1356 $result = $this->callAPISuccess('contact', 'getsingle', array('id' => $contactID));
1357 foreach ($extraParams as $key => $value) {
1358 $this->assertEquals($result[$key], $value);
1359 }
1360 //now we check they are still returned with 'return' key
1361 $result = $this->callAPISuccess('contact', 'getsingle', array(
1362 'id' => $contactID,
1363 'return' => array_keys($extraParams),
1364 ));
1365 foreach ($extraParams as $key => $value) {
1366 $this->assertEquals($result[$key], $value);
1367 }
1368 }
1369
1370 public function testCRM13252MultipleChainedPhones() {
1371 $contactID = $this->householdCreate();
1372 $this->callAPISuccessGetCount('phone', array('contact_id' => $contactID), 0);
1373 $params = array(
1374 'contact_id' => $contactID,
1375 'household_name' => 'Household 1',
1376 'contact_type' => 'Household',
1377 'api.phone.create' => array(
1378 0 => array(
1379 'phone' => '111-111-1111',
1380 'location_type_id' => 1,
1381 'phone_type_id' => 1,
1382 ),
1383 1 => array(
1384 'phone' => '222-222-2222',
1385 'location_type_id' => 1,
1386 'phone_type_id' => 2,
1387 ),
1388 ),
1389 );
1390 $this->callAPISuccess('contact', 'create', $params);
1391 $this->callAPISuccessGetCount('phone', array('contact_id' => $contactID), 2);
1392
1393 }
1394
1395 /**
1396 * Test for Contact.get id=@user:username (with an invalid username).
1397 */
1398 public function testContactGetByUnknownUsername() {
1399 // setup - mock the calls to CRM_Utils_System_*::getUfId
1400 $userSystem = $this->getMock('CRM_Utils_System_UnitTests', array('getUfId'));
1401 $userSystem->expects($this->once())
1402 ->method('getUfId')
1403 ->with($this->equalTo('exampleUser'))
1404 ->will($this->returnValue(NULL));
1405 CRM_Core_Config::singleton()->userSystem = $userSystem;
1406
1407 // perform a lookup
1408 $result = $this->callAPIFailure('Contact', 'get', array(
1409 'id' => '@user:exampleUser',
1410 ));
1411 $this->assertRegExp('/cannot be resolved to a contact ID/', $result['error_message']);
1412 }
1413
1414 /**
1415 * Verify attempt to create individual with chained arrays.
1416 */
1417 public function testGetIndividualWithChainedArrays() {
1418 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
1419 $params['custom_' . $ids['custom_field_id']] = "custom string";
1420
1421 $moreIDs = $this->CustomGroupMultipleCreateWithFields();
1422 $description = "This demonstrates the usage of chained api functions.\nIn this case no notes or custom fields have been created.";
1423 $subfile = "APIChainedArray";
1424 $params = array(
1425 'first_name' => 'abc3',
1426 'last_name' => 'xyz3',
1427 'contact_type' => 'Individual',
1428 'email' => 'man3@yahoo.com',
1429 'api.contribution.create' => array(
1430 'receive_date' => '2010-01-01',
1431 'total_amount' => 100.00,
1432 'financial_type_id' => 1,
1433 'payment_instrument_id' => 1,
1434 'non_deductible_amount' => 10.00,
1435 'fee_amount' => 50.00,
1436 'net_amount' => 90.00,
1437 'trxn_id' => 12345,
1438 'invoice_id' => 67890,
1439 'source' => 'SSF',
1440 'contribution_status_id' => 1,
1441 ),
1442 'api.contribution.create.1' => array(
1443 'receive_date' => '2011-01-01',
1444 'total_amount' => 120.00,
1445 'financial_type_id' => $this->_financialTypeId = 1,
1446 'payment_instrument_id' => 1,
1447 'non_deductible_amount' => 10.00,
1448 'fee_amount' => 50.00,
1449 'net_amount' => 90.00,
1450 'trxn_id' => 12335,
1451 'invoice_id' => 67830,
1452 'source' => 'SSF',
1453 'contribution_status_id' => 1,
1454 ),
1455 'api.website.create' => array(
1456 array(
1457 'url' => "http://civicrm.org",
1458 ),
1459 ),
1460 );
1461
1462 $result = $this->callAPISuccess('Contact', 'create', $params);
1463 $params = array(
1464 'id' => $result['id'],
1465 'api.website.get' => array(),
1466 'api.Contribution.get' => array(
1467 'total_amount' => '120.00',
1468 ),
1469 'api.CustomValue.get' => 1,
1470 'api.Note.get' => 1,
1471 );
1472 $result = $this->callAPIAndDocument('Contact', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile);
1473 // delete the contact
1474 $this->callAPISuccess('contact', 'delete', $result);
1475 $this->customGroupDelete($ids['custom_group_id']);
1476 $this->customGroupDelete($moreIDs['custom_group_id']);
1477 $this->assertEquals(1, $result['id']);
1478 $this->assertEquals(0, $result['values'][$result['id']]['api.website.get']['is_error']);
1479 $this->assertEquals("http://civicrm.org", $result['values'][$result['id']]['api.website.get']['values'][0]['url']);
1480 }
1481
1482 public function testGetIndividualWithChainedArraysFormats() {
1483 $description = "This demonstrates the usage of chained api functions.\nIn this case no notes or custom fields have been created.";
1484 $subfile = "APIChainedArrayFormats";
1485 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
1486 $params['custom_' . $ids['custom_field_id']] = "custom string";
1487
1488 $moreIDs = $this->CustomGroupMultipleCreateWithFields();
1489 $params = array(
1490 'first_name' => 'abc3',
1491 'last_name' => 'xyz3',
1492 'contact_type' => 'Individual',
1493 'email' => 'man3@yahoo.com',
1494 'api.contribution.create' => array(
1495 'receive_date' => '2010-01-01',
1496 'total_amount' => 100.00,
1497 'financial_type_id' => $this->_financialTypeId,
1498 'payment_instrument_id' => 1,
1499 'non_deductible_amount' => 10.00,
1500 'fee_amount' => 50.00,
1501 'net_amount' => 90.00,
1502 'source' => 'SSF',
1503 'contribution_status_id' => 1,
1504 ),
1505 'api.contribution.create.1' => array(
1506 'receive_date' => '2011-01-01',
1507 'total_amount' => 120.00,
1508 'financial_type_id' => $this->_financialTypeId,
1509 'payment_instrument_id' => 1,
1510 'non_deductible_amount' => 10.00,
1511 'fee_amount' => 50.00,
1512 'net_amount' => 90.00,
1513 'source' => 'SSF',
1514 'contribution_status_id' => 1,
1515 ),
1516 'api.website.create' => array(
1517 array(
1518 'url' => "http://civicrm.org",
1519 ),
1520 ),
1521 );
1522
1523 $result = $this->callAPISuccess('Contact', 'create', $params);
1524 $params = array(
1525 'id' => $result['id'],
1526 'api.website.getValue' => array('return' => 'url'),
1527 'api.Contribution.getCount' => array(),
1528 'api.CustomValue.get' => 1,
1529 'api.Note.get' => 1,
1530 'api.Membership.getCount' => array(),
1531 );
1532 $result = $this->callAPIAndDocument('Contact', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile);
1533 $this->assertEquals(1, $result['id']);
1534 $this->assertEquals(2, $result['values'][$result['id']]['api.Contribution.getCount']);
1535 $this->assertEquals(0, $result['values'][$result['id']]['api.Note.get']['is_error']);
1536 $this->assertEquals("http://civicrm.org", $result['values'][$result['id']]['api.website.getValue']);
1537
1538 $this->callAPISuccess('contact', 'delete', $result);
1539 $this->customGroupDelete($ids['custom_group_id']);
1540 $this->customGroupDelete($moreIDs['custom_group_id']);
1541 }
1542
1543 /**
1544 * Test complex chaining.
1545 */
1546 public function testGetIndividualWithChainedArraysAndMultipleCustom() {
1547 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
1548 $params['custom_' . $ids['custom_field_id']] = "custom string";
1549 $moreIDs = $this->CustomGroupMultipleCreateWithFields();
1550 $andMoreIDs = $this->CustomGroupMultipleCreateWithFields(array(
1551 'title' => "another group",
1552 'name' => 'another name',
1553 ));
1554 $description = "This demonstrates the usage of chained api functions with multiple custom fields.";
1555 $subfile = "APIChainedArrayMultipleCustom";
1556 $params = array(
1557 'first_name' => 'abc3',
1558 'last_name' => 'xyz3',
1559 'contact_type' => 'Individual',
1560 'email' => 'man3@yahoo.com',
1561 'api.contribution.create' => array(
1562 'receive_date' => '2010-01-01',
1563 'total_amount' => 100.00,
1564 'financial_type_id' => 1,
1565 'payment_instrument_id' => 1,
1566 'non_deductible_amount' => 10.00,
1567 'fee_amount' => 50.00,
1568 'net_amount' => 90.00,
1569 'trxn_id' => 12345,
1570 'invoice_id' => 67890,
1571 'source' => 'SSF',
1572 'contribution_status_id' => 1,
1573 ),
1574 'api.contribution.create.1' => array(
1575 'receive_date' => '2011-01-01',
1576 'total_amount' => 120.00,
1577 'financial_type_id' => 1,
1578 'payment_instrument_id' => 1,
1579 'non_deductible_amount' => 10.00,
1580 'fee_amount' => 50.00,
1581 'net_amount' => 90.00,
1582 'trxn_id' => 12335,
1583 'invoice_id' => 67830,
1584 'source' => 'SSF',
1585 'contribution_status_id' => 1,
1586 ),
1587 'api.website.create' => array(
1588 array(
1589 'url' => "http://civicrm.org",
1590 ),
1591 ),
1592 'custom_' . $ids['custom_field_id'] => "value 1",
1593 'custom_' . $moreIDs['custom_field_id'][0] => "value 2",
1594 'custom_' . $moreIDs['custom_field_id'][1] => "warm beer",
1595 'custom_' . $andMoreIDs['custom_field_id'][1] => "vegemite",
1596 );
1597
1598 $result = $this->callAPISuccess('Contact', 'create', $params);
1599 $result = $this->callAPISuccess('Contact', 'create', array(
1600 'contact_type' => 'Individual',
1601 'id' => $result['id'],
1602 'custom_' .
1603 $moreIDs['custom_field_id'][0] => "value 3",
1604 'custom_' .
1605 $ids['custom_field_id'] => "value 4",
1606 ));
1607
1608 $params = array(
1609 'id' => $result['id'],
1610 'api.website.getValue' => array('return' => 'url'),
1611 'api.Contribution.getCount' => array(),
1612 'api.CustomValue.get' => 1,
1613 );
1614 $result = $this->callAPIAndDocument('Contact', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile);
1615
1616 $this->customGroupDelete($ids['custom_group_id']);
1617 $this->customGroupDelete($moreIDs['custom_group_id']);
1618 $this->customGroupDelete($andMoreIDs['custom_group_id']);
1619 $this->assertEquals(1, $result['id']);
1620 $this->assertEquals(0, $result['values'][$result['id']]['api.CustomValue.get']['is_error']);
1621 $this->assertEquals('http://civicrm.org', $result['values'][$result['id']]['api.website.getValue']);
1622 }
1623
1624 /**
1625 * Test checks usage of $values to pick & choose inputs.
1626 */
1627 public function testChainingValuesCreate() {
1628 $description = "This demonstrates the usage of chained api functions. Specifically it has one 'parent function' &
1629 2 child functions - one receives values from the parent (Contact) and the other child (Tag).";
1630 $subfile = "APIChainedArrayValuesFromSiblingFunction";
1631 $params = array(
1632 'display_name' => 'batman',
1633 'contact_type' => 'Individual',
1634 'api.tag.create' => array('name' => '$value.id', 'description' => '$value.display_name', 'format.only_id' => 1),
1635 'api.entity_tag.create' => array('tag_id' => '$value.api.tag.create'),
1636 );
1637 $result = $this->callAPIAndDocument('Contact', 'Create', $params, __FUNCTION__, __FILE__, $description, $subfile);
1638 $this->assertEquals(0, $result['values'][$result['id']]['api.entity_tag.create']['is_error']);
1639
1640 $tablesToTruncate = array(
1641 'civicrm_contact',
1642 'civicrm_activity',
1643 'civicrm_entity_tag',
1644 'civicrm_tag',
1645 );
1646 $this->quickCleanup($tablesToTruncate, TRUE);
1647 }
1648
1649 /**
1650 * Test TrueFalse format - I couldn't come up with an easy way to get an error on Get.
1651 */
1652 public function testContactGetFormatIsSuccessTrue() {
1653 $this->createContactFromXML();
1654 $description = "This demonstrates use of the 'format.is_success' param.
1655 This param causes only the success or otherwise of the function to be returned as BOOLEAN";
1656 $subfile = "FormatIsSuccess_True";
1657 $params = array('id' => 17, 'format.is_success' => 1);
1658 $result = $this->callAPIAndDocument('Contact', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile);
1659 $this->assertEquals(1, $result);
1660 $this->callAPISuccess('Contact', 'Delete', $params);
1661 }
1662
1663 /**
1664 * Test TrueFalse format.
1665 */
1666 public function testContactCreateFormatIsSuccessFalse() {
1667
1668 $description = "This demonstrates use of the 'format.is_success' param.
1669 This param causes only the success or otherwise of the function to be returned as BOOLEAN";
1670 $subfile = "FormatIsSuccess_Fail";
1671 $params = array('id' => 500, 'format.is_success' => 1);
1672 $result = $this->callAPIAndDocument('Contact', 'Create', $params, __FUNCTION__, __FILE__, $description, $subfile);
1673 $this->assertEquals(0, $result);
1674 }
1675
1676 /**
1677 * Test Single Entity format.
1678 */
1679 public function testContactGetSingleEntityArray() {
1680 $this->createContactFromXML();
1681 $description = "This demonstrates use of the 'format.single_entity_array' param.
1682 This param causes the only contact to be returned as an array without the other levels.
1683 It will be ignored if there is not exactly 1 result";
1684 $subfile = "GetSingleContact";
1685 $params = array('id' => 17);
1686 $result = $this->callAPIAndDocument('Contact', 'GetSingle', $params, __FUNCTION__, __FILE__, $description, $subfile);
1687 $this->assertEquals('Test Contact', $result['display_name']);
1688 $this->callAPISuccess('Contact', 'Delete', $params);
1689 }
1690
1691 /**
1692 * Test Single Entity format.
1693 */
1694 public function testContactGetFormatCountOnly() {
1695 $this->createContactFromXML();
1696 $description = "This demonstrates use of the 'getCount' action.
1697 This param causes the count of the only function to be returned as an integer.";
1698 $subfile = "GetCountContact";
1699 $params = array('id' => 17);
1700 $result = $this->callAPIAndDocument('Contact', 'GetCount', $params, __FUNCTION__, __FILE__, $description,
1701 $subfile, 'getcount');
1702 $this->assertEquals('1', $result);
1703 $this->callAPISuccess('Contact', 'Delete', $params);
1704 }
1705
1706 /**
1707 * Test id only format.
1708 */
1709 public function testContactGetFormatIDOnly() {
1710 $this->createContactFromXML();
1711 $description = "This demonstrates use of the 'format.id_only' param.
1712 This param causes the id of the only entity to be returned as an integer.
1713 It will be ignored if there is not exactly 1 result";
1714 $subfile = "FormatOnlyID";
1715 $params = array('id' => 17, 'format.only_id' => 1);
1716 $result = $this->callAPIAndDocument('Contact', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile);
1717 $this->assertEquals('17', $result);
1718 $this->callAPISuccess('Contact', 'Delete', $params);
1719 }
1720
1721 /**
1722 * Test id only format.
1723 */
1724 public function testContactGetFormatSingleValue() {
1725 $this->createContactFromXML();
1726 $description = "This demonstrates use of the 'format.single_value' param.
1727 This param causes only a single value of the only entity to be returned as an string.
1728 It will be ignored if there is not exactly 1 result";
1729 $subFile = "FormatSingleValue";
1730 $params = array('id' => 17, 'return' => 'display_name');
1731 $result = $this->callAPIAndDocument('Contact', 'getvalue', $params, __FUNCTION__, __FILE__, $description, $subFile, 'getvalue');
1732 $this->assertEquals('Test Contact', $result);
1733 $this->callAPISuccess('Contact', 'Delete', $params);
1734 }
1735
1736 /**
1737 * Test that permissions are respected when creating contacts.
1738 */
1739 public function testContactCreationPermissions() {
1740 $params = array(
1741 'contact_type' => 'Individual',
1742 'first_name' => 'Foo',
1743 'last_name' => 'Bear',
1744 'check_permissions' => TRUE,
1745 );
1746 $config = CRM_Core_Config::singleton();
1747 $config->userPermissionClass->permissions = array('access CiviCRM');
1748 $result = $this->callAPIFailure('contact', 'create', $params);
1749 $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');
1750
1751 $config->userPermissionClass->permissions = array('access CiviCRM', 'add contacts', 'import contacts');
1752 $this->callAPISuccess('contact', 'create', $params, NULL, 'overfluous permissions should be enough to create a contact');
1753 }
1754
1755 /**
1756 * Test update with check permissions set.
1757 */
1758 public function testContactUpdatePermissions() {
1759 $params = array(
1760 'contact_type' => 'Individual',
1761 'first_name' => 'Foo',
1762 'last_name' => 'Bear',
1763 'check_permissions' => TRUE,
1764 );
1765 $result = $this->callAPISuccess('contact', 'create', $params);
1766 $config = CRM_Core_Config::singleton();
1767 $params = array(
1768 'id' => $result['id'],
1769 'contact_type' => 'Individual',
1770 'last_name' => 'Bar',
1771 'check_permissions' => TRUE,
1772 );
1773
1774 $config->userPermissionClass->permissions = array('access CiviCRM');
1775 $result = $this->callAPIFailure('contact', 'update', $params);
1776 $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');
1777
1778 $config->userPermissionClass->permissions = array(
1779 'access CiviCRM',
1780 'add contacts',
1781 'view all contacts',
1782 'edit all contacts',
1783 'import contacts',
1784 );
1785 $this->callAPISuccess('contact', 'update', $params, NULL, 'overfluous permissions should be enough to update a contact');
1786 }
1787
1788 /**
1789 * Set up helper to create a contact.
1790 */
1791 public function createContactFromXML() {
1792 // Insert a row in civicrm_contact creating contact 17.
1793 $op = new PHPUnit_Extensions_Database_Operation_Insert();
1794 $op->execute($this->_dbconn,
1795 $this->createXMLDataSet(
1796 dirname(__FILE__) . '/dataset/contact_17.xml'
1797 )
1798 );
1799 }
1800
1801 /**
1802 * Test contact proximity api.
1803 */
1804 public function testContactProximity() {
1805 // first create a contact with a SF location with a specific
1806 // geocode
1807 $contactID = $this->organizationCreate();
1808
1809 // now create the address
1810 $params = array(
1811 'street_address' => '123 Main Street',
1812 'city' => 'San Francisco',
1813 'is_primary' => 1,
1814 'country_id' => 1228,
1815 'state_province_id' => 1004,
1816 'geo_code_1' => '37.79',
1817 'geo_code_2' => '-122.40',
1818 'location_type_id' => 1,
1819 'contact_id' => $contactID,
1820 );
1821
1822 $result = $this->callAPISuccess('address', 'create', $params);
1823 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
1824
1825 // now do a proximity search with a close enough geocode and hope to match
1826 // that specific contact only!
1827 $proxParams = array(
1828 'latitude' => 37.7,
1829 'longitude' => -122.3,
1830 'unit' => 'mile',
1831 'distance' => 10,
1832 );
1833 $result = $this->callAPISuccess('contact', 'proximity', $proxParams);
1834 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
1835 }
1836
1837 /**
1838 * Test that Ajax API permission is sufficient to access getquick api.
1839 *
1840 * (note that getquick api is required for autocomplete & has ACL permissions applied)
1841 */
1842 public function testGetquickPermissionCRM13744() {
1843 CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access CiviEvent');
1844 $this->callAPIFailure('contact', 'getquick', array('name' => 'b', 'check_permissions' => TRUE));
1845 CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access CiviCRM');
1846 $this->callAPISuccess('contact', 'getquick', array('name' => 'b', 'check_permissions' => TRUE));
1847 CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access AJAX API');
1848 $this->callAPISuccess('contact', 'getquick', array('name' => 'b', 'check_permissions' => TRUE));
1849 }
1850
1851 /**
1852 * Test get ref api - gets a list of references to an entity.
1853 */
1854 public function testGetReferenceCounts() {
1855 $result = $this->callAPISuccess('Contact', 'create', array(
1856 'first_name' => 'Testily',
1857 'last_name' => 'McHaste',
1858 'contact_type' => 'Individual',
1859 'api.Address.replace' => array(
1860 'values' => array(),
1861 ),
1862 'api.Email.replace' => array(
1863 'values' => array(
1864 array(
1865 'email' => 'spam@dev.null',
1866 'is_primary' => 0,
1867 'location_type_id' => 1,
1868 ),
1869 ),
1870 ),
1871 'api.Phone.replace' => array(
1872 'values' => array(
1873 array(
1874 'phone' => '234-567-0001',
1875 'is_primary' => 1,
1876 'location_type_id' => 1,
1877 ),
1878 array(
1879 'phone' => '234-567-0002',
1880 'is_primary' => 0,
1881 'location_type_id' => 1,
1882 ),
1883 ),
1884 ),
1885 ));
1886
1887 //$dao = new CRM_Contact_BAO_Contact();
1888 //$dao->id = $result['id'];
1889 //$this->assertTrue((bool) $dao->find(TRUE));
1890 //
1891 //$refCounts = $dao->getReferenceCounts();
1892 //$this->assertTrue(is_array($refCounts));
1893 //$refCountsIdx = CRM_Utils_Array::index(array('name'), $refCounts);
1894
1895 $refCounts = $this->callAPISuccess('Contact', 'getrefcount', array(
1896 'id' => $result['id'],
1897 ));
1898 $refCountsIdx = CRM_Utils_Array::index(array('name'), $refCounts['values']);
1899
1900 $this->assertEquals(1, $refCountsIdx['sql:civicrm_email:contact_id']['count']);
1901 $this->assertEquals('civicrm_email', $refCountsIdx['sql:civicrm_email:contact_id']['table']);
1902 $this->assertEquals(2, $refCountsIdx['sql:civicrm_phone:contact_id']['count']);
1903 $this->assertEquals('civicrm_phone', $refCountsIdx['sql:civicrm_phone:contact_id']['table']);
1904 $this->assertTrue(!isset($refCountsIdx['sql:civicrm_address:contact_id']));
1905 }
1906
1907 public function testSQLOperatorsOnContactAPI() {
1908 $this->individualCreate();
1909 $this->organizationCreate();
1910 $this->householdCreate();
1911 $contacts = $this->callAPISuccess('contact', 'get', array('legal_name' => array('IS NOT NULL' => TRUE)));
1912 $this->assertEquals($contacts['count'], CRM_Core_DAO::singleValueQuery('select count(*) FROM civicrm_contact WHERE legal_name IS NOT NULL'));
1913 $contacts = $this->callAPISuccess('contact', 'get', array('legal_name' => array('IS NULL' => TRUE)));
1914 $this->assertEquals($contacts['count'], CRM_Core_DAO::singleValueQuery('select count(*) FROM civicrm_contact WHERE legal_name IS NULL'));
1915 }
1916
1917 /**
1918 * CRM-14743 - test api respects search operators.
1919 */
1920 public function testGetModifiedDateByOperators() {
1921 $preExistingContactCount = CRM_Core_DAO::singleValueQuery('select count(*) FROM civicrm_contact');
1922 $contact1 = $this->individualCreate();
1923 $sql = "UPDATE civicrm_contact SET created_date = '2012-01-01', modified_date = '2013-01-01' WHERE id = " . $contact1;
1924 CRM_Core_DAO::executeQuery($sql);
1925 $contact2 = $this->individualCreate();
1926 $sql = "UPDATE civicrm_contact SET created_date = '2012-02-01', modified_date = '2013-02-01' WHERE id = " . $contact2;
1927 CRM_Core_DAO::executeQuery($sql);
1928 $contact3 = $this->householdCreate();
1929 $sql = "UPDATE civicrm_contact SET created_date = '2012-03-01', modified_date = '2013-03-01' WHERE id = " . $contact3;
1930 CRM_Core_DAO::executeQuery($sql);
1931 $contacts = $this->callAPISuccess('contact', 'get', array('modified_date' => array('<' => '2014-01-01')));
1932 $this->assertEquals($contacts['count'], 3);
1933 $contacts = $this->callAPISuccess('contact', 'get', array('modified_date' => array('>' => '2014-01-01')));
1934 $this->assertEquals($contacts['count'], $preExistingContactCount);
1935 }
1936
1937 /**
1938 * CRM-14743 - test api respects search operators.
1939 */
1940 public function testGetCreatedDateByOperators() {
1941 $preExistingContactCount = CRM_Core_DAO::singleValueQuery('select count(*) FROM civicrm_contact');
1942 $contact1 = $this->individualCreate();
1943 $sql = "UPDATE civicrm_contact SET created_date = '2012-01-01' WHERE id = " . $contact1;
1944 CRM_Core_DAO::executeQuery($sql);
1945 $contact2 = $this->individualCreate();
1946 $sql = "UPDATE civicrm_contact SET created_date = '2012-02-01' WHERE id = " . $contact2;
1947 CRM_Core_DAO::executeQuery($sql);
1948 $contact3 = $this->householdCreate();
1949 $sql = "UPDATE civicrm_contact SET created_date = '2012-03-01' WHERE id = " . $contact3;
1950 CRM_Core_DAO::executeQuery($sql);
1951 $contacts = $this->callAPISuccess('contact', 'get', array('created_date' => array('<' => '2014-01-01')));
1952 $this->assertEquals($contacts['count'], 3);
1953 $contacts = $this->callAPISuccess('contact', 'get', array('created_date' => array('>' => '2014-01-01')));
1954 $this->assertEquals($contacts['count'], $preExistingContactCount);
1955 }
1956
1957 /**
1958 * CRM-14263 check that API is not affected by search profile related bug.
1959 */
1960 public function testReturnCityProfile() {
1961 $contactID = $this->individualCreate();
1962 CRM_Core_Config::singleton()->defaultSearchProfileID = 1;
1963 $this->callAPISuccess('address', 'create', array(
1964 'contact_id' => $contactID,
1965 'city' => 'Cool City',
1966 'location_type_id' => 1,
1967 ));
1968 $result = $this->callAPISuccess('contact', 'get', array('city' => 'Cool City', 'return' => 'contact_type'));
1969 $this->assertEquals(1, $result['count']);
1970 }
1971
1972 /**
1973 * CRM-15443 - ensure getlist api does not return deleted contacts
1974 */
1975 public function testGetlistExcludeConditions() {
1976 $name = md5(time());
1977 $contact = $this->individualCreate(array('last_name' => $name));
1978 $this->individualCreate(array('last_name' => $name, 'is_deceased' => 1));
1979 $this->individualCreate(array('last_name' => $name, 'is_deleted' => 1));
1980 // We should get all but the deleted contact.
1981 $result = $this->callAPISuccess('contact', 'getlist', array('input' => $name));
1982 $this->assertEquals(2, $result['count'], 'In line ' . __LINE__);
1983 // Force-exclude the deceased contact.
1984 $result = $this->callAPISuccess('contact', 'getlist', array(
1985 'input' => $name,
1986 'params' => array('is_deceased' => 0),
1987 ));
1988 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
1989 $this->assertEquals($contact, $result['values'][0]['id'], 'In line ' . __LINE__);
1990 }
1991
1992 /**
1993 * Test contact getactions.
1994 */
1995 public function testGetActions() {
1996 $description = "Getting the available actions for an entity.";
1997 $result = $this->callAPIAndDocument($this->_entity, 'getactions', array(), __FUNCTION__, __FILE__, $description);
1998 $expected = array(
1999 'create',
2000 'delete',
2001 'get',
2002 'getactions',
2003 'getcount',
2004 'getfields',
2005 'getlist',
2006 'getoptions',
2007 'getquick',
2008 'getrefcount',
2009 'getsingle',
2010 'getvalue',
2011 'merge',
2012 'proximity',
2013 'replace',
2014 'setvalue',
2015 'update',
2016 );
2017 $deprecated = array(
2018 'update',
2019 'getquick',
2020 );
2021 foreach ($expected as $action) {
2022 $this->assertTrue(in_array($action, $result['values']), "Expected action $action");
2023 }
2024 foreach ($deprecated as $action) {
2025 $this->assertArrayKeyExists($action, $result['deprecated']);
2026 }
2027 }
2028
2029 }