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