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