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