Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /** | |
3 | * File for the TestActivity 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: ActivityTest.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_activity_* functions | |
39 | * | |
40 | * @package CiviCRM_APIv3 | |
41 | * @subpackage API_Activity | |
42 | */ | |
43 | ||
44 | class api_v3_ActivityTest extends CiviUnitTestCase { | |
45 | protected $_params; | |
46 | protected $_params2; | |
47 | protected $_entity = 'activity'; | |
48 | protected $_apiversion = 3; | |
49 | protected $test_activity_type_value; | |
50 | public $_eNoticeCompliant = TRUE; | |
51 | /** | |
52 | * Test setup for every test | |
53 | * | |
54 | * Connect to the database, truncate the tables that will be used | |
55 | * and redirect stdin to a temporary file | |
56 | */ | |
57 | public function setUp() { | |
58 | // Connect to the database | |
59 | parent::setUp(); | |
60 | $tablesToTruncate = array( | |
61 | 'civicrm_activity', | |
62 | 'civicrm_contact', | |
63 | 'civicrm_custom_group', | |
64 | 'civicrm_custom_field', | |
65 | ); | |
66 | ||
67 | $this->quickCleanup($tablesToTruncate); | |
68 | ||
69 | ||
70 | ||
71 | // Insert a row in civicrm_contact creating contact 17 | |
72 | $op = new PHPUnit_Extensions_Database_Operation_Insert(); | |
73 | $op->execute($this->_dbconn, | |
74 | new PHPUnit_Extensions_Database_DataSet_XMLDataSet( | |
75 | dirname(__FILE__) . '/dataset/contact_17.xml' | |
76 | ) | |
77 | ); | |
78 | ||
79 | //create activity types | |
80 | $activityTypes = civicrm_api('option_value', 'create', array( | |
81 | 'version' => API_LATEST_VERSION, 'option_group_id' => 2, | |
82 | 'name' => 'Test activity type', | |
83 | 'label' => 'Test activity type', | |
84 | 'sequential' => 1, | |
85 | )); | |
86 | $this->test_activity_type_value = $activityTypes['values'][0]['value']; | |
87 | $this->test_activity_type_id = $activityTypes['id']; | |
88 | $this->_params = array( | |
89 | 'source_contact_id' => 17, | |
90 | 'activity_type_id' => $this->test_activity_type_value, | |
91 | 'subject' => 'test activity type id', | |
92 | 'activity_date_time' => '2011-06-02 14:36:13', | |
93 | 'status_id' => 2, | |
94 | 'priority_id' => 1, | |
95 | 'duration' => 120, | |
96 | 'location' => 'Pensulvania', | |
97 | 'details' => 'a test activity', | |
98 | 'version' => $this->_apiversion, | |
99 | ); | |
100 | $this->_params2 = array( | |
101 | 'source_contact_id' => 17, | |
102 | 'subject' => 'Eat & drink', | |
103 | 'activity_date_time' => date('Ymd'), | |
104 | 'duration' => 120, | |
105 | 'location' => 'Napier', | |
106 | 'details' => 'discuss & eat', | |
107 | 'status_id' => 1, | |
108 | 'activity_type_id' => $this->test_activity_type_value, | |
109 | 'version' => $this->_apiversion, | |
110 | ); | |
111 | // create a logged in USER since the code references it for source_contact_id | |
112 | $this->createLoggedInUser(); | |
113 | } | |
114 | ||
115 | /** | |
116 | * Tears down the fixture, for example, closes a network connection. | |
117 | * This method is called after a test is executed. | |
118 | * | |
119 | * @access protected | |
120 | */ | |
121 | function tearDown() { | |
122 | $tablesToTruncate = array( | |
123 | 'civicrm_contact', | |
124 | 'civicrm_activity', | |
b319d00a | 125 | 'civicrm_activity_contact', |
6a488035 TO |
126 | ); |
127 | $this->quickCleanup($tablesToTruncate, TRUE); | |
128 | civicrm_api('option_value', 'delete', array('version' => 3, 'id' => $this->test_activity_type_id)); | |
129 | } | |
130 | ||
131 | /** | |
132 | * check with empty array | |
133 | */ | |
134 | function testActivityCreateEmpty() { | |
135 | $params = array('version' => $this->_apiversion); | |
136 | $result = civicrm_api('activity', 'create', $params); | |
137 | $this->assertEquals($result['is_error'], 1, | |
138 | "In line " . __LINE__ | |
139 | ); | |
140 | } | |
141 | ||
142 | /** | |
143 | * check if required fields are not passed | |
144 | */ | |
145 | function testActivityCreateWithoutRequired() { | |
146 | $params = array( | |
147 | 'subject' => 'this case should fail', | |
148 | 'scheduled_date_time' => date('Ymd'), | |
149 | 'version' => $this->_apiversion, | |
150 | ); | |
151 | ||
152 | $result = civicrm_api('activity', 'create', $params); | |
153 | $this->assertEquals($result['is_error'], 1, | |
154 | "In line " . __LINE__ | |
155 | ); | |
156 | } | |
157 | ||
158 | /** | |
159 | * Test civicrm_activity_create() with mismatched activity_type_id | |
160 | * and activity_name | |
161 | */ | |
162 | function testActivityCreateMismatchNameType() { | |
163 | $params = array( | |
164 | 'source_contact_id' => 17, | |
165 | 'subject' => 'Test activity', | |
166 | 'activity_date_time' => date('Ymd'), | |
167 | 'duration' => 120, | |
168 | 'location' => 'Pensulvania', | |
169 | 'details' => 'a test activity', | |
170 | 'status_id' => 1, | |
171 | 'activity_name' => 'Fubar activity type', | |
172 | 'activity_type_id' => 5, | |
173 | 'scheduled_date_time' => date('Ymd'), | |
174 | 'version' => $this->_apiversion, | |
175 | ); | |
176 | ||
177 | $result = civicrm_api('activity', 'create', $params); | |
178 | $this->assertEquals($result['is_error'], 1, | |
179 | "In line " . __LINE__ | |
180 | ); | |
181 | } | |
182 | ||
183 | /** | |
184 | * Test civicrm_activity_id() with missing source_contact_id is put with the current user. | |
185 | */ | |
186 | function testActivityCreateWithMissingContactId() { | |
187 | $params = array( | |
188 | 'subject' => 'Make-it-Happen Meeting', | |
189 | 'activity_date_time' => date('Ymd'), | |
190 | 'duration' => 120, | |
191 | 'location' => 'Pensulvania', | |
192 | 'details' => 'a test activity', | |
193 | 'status_id' => 1, | |
194 | 'activity_name' => 'Test activity type', | |
195 | 'version' => $this->_apiversion, | |
196 | ); | |
197 | ||
198 | $result = civicrm_api('activity', 'create', $params); | |
199 | ||
200 | // we should use the session contact ID, CRM-8180 | |
201 | $this->assertAPISuccess($result); | |
202 | } | |
203 | ||
204 | /** | |
205 | * Test civicrm_activity_id() with non-numeric source_contact_id | |
206 | */ | |
207 | function testActivityCreateWithNonNumericContactId() { | |
208 | $params = array( | |
209 | 'source_contact_id' => 'fubar', | |
210 | 'subject' => 'Make-it-Happen Meeting', | |
211 | 'activity_date_time' => date('Ymd'), | |
212 | 'duration' => 120, | |
213 | 'location' => 'Pensulvania', | |
214 | 'details' => 'a test activity', | |
215 | 'status_id' => 1, | |
216 | 'activity_name' => 'Test activity type', | |
217 | 'version' => $this->_apiversion, | |
218 | ); | |
219 | ||
220 | $result = civicrm_api('activity', 'create', $params); | |
221 | ||
222 | $this->assertEquals($result['is_error'], 1, | |
223 | "In line " . __LINE__ | |
224 | ); | |
225 | } | |
226 | ||
227 | /** | |
228 | * Test civicrm_activity_id() with non-numeric duration | |
229 | * @todo Come back to this in later stages | |
230 | */ | |
231 | /// we don't offer single parameter correctness checking at the moment | |
232 | //function testActivityCreateWithNonNumericDuration( ) | |
233 | //{ | |
234 | // $params = array( | |
235 | // 'source_contact_id' => 17, | |
236 | // 'subject' => 'Discussion on Apis for v3', | |
237 | // 'activity_date_time' => date('Ymd'), | |
238 | // 'duration' => 'fubar', | |
239 | // 'location' => 'Pensulvania', | |
240 | // 'details' => 'a test activity', | |
241 | // 'status_id' => 1, | |
242 | // 'activity_name' => 'Test activity type' | |
243 | // ); | |
244 | // | |
245 | // $result = civicrm_activity_create($params); | |
246 | // | |
247 | // $this->assertEquals( $result['is_error'], 1, | |
248 | // "In line " . __LINE__ ); | |
249 | //} | |
250 | ||
251 | /** | |
252 | * check with incorrect required fields | |
253 | */ | |
254 | function testActivityCreateWithNonNumericActivityTypeId() { | |
255 | $params = array( | |
256 | 'source_contact_id' => 17, | |
257 | 'subject' => 'Make-it-Happen Meeting', | |
258 | 'activity_date_time' => date('Ymd'), | |
259 | 'duration' => 120, | |
260 | 'location' => 'Pensulvania', | |
261 | 'details' => 'a test activity', | |
262 | 'status_id' => 1, | |
263 | 'activity_type_id' => 'Test activity type', | |
264 | 'version' => $this->_apiversion, | |
265 | ); | |
266 | ||
267 | $result = civicrm_api('activity', 'create', $params); | |
268 | ||
269 | $this->assertEquals($result['is_error'], 1, | |
270 | "In line " . __LINE__ | |
271 | ); | |
272 | } | |
273 | ||
274 | /** | |
275 | * check with incorrect required fields | |
276 | */ | |
277 | function testActivityCreateWithUnknownActivityTypeId() { | |
278 | $params = array( | |
279 | 'source_contact_id' => 17, | |
280 | 'subject' => 'Make-it-Happen Meeting', | |
281 | 'activity_date_time' => date('Ymd'), | |
282 | 'duration' => 120, | |
283 | 'location' => 'Pensulvania', | |
284 | 'details' => 'a test activity', | |
285 | 'status_id' => 1, | |
286 | 'activity_type_id' => 699, | |
287 | 'version' => $this->_apiversion, | |
288 | ); | |
289 | ||
290 | $result = civicrm_api('activity', 'create', $params); | |
291 | ||
292 | $this->assertEquals($result['is_error'], 1, | |
293 | "In line " . __LINE__ | |
294 | ); | |
295 | } | |
296 | ||
297 | function testActivityCreateWithInvalidPriority() { | |
298 | $params = array( | |
299 | 'source_contact_id' => 17, | |
300 | 'subject' => 'Make-it-Happen Meeting', | |
301 | 'activity_date_time' => date('Ymd'), | |
302 | 'duration' => 120, | |
303 | 'location' => 'Pensulvania', | |
304 | 'details' => 'a test activity', | |
305 | 'status_id' => 1, | |
306 | 'priority_id' => 44, | |
307 | 'activity_type_id' => 1, | |
308 | 'version' => $this->_apiversion, | |
309 | ); | |
310 | ||
311 | $result = civicrm_api('activity', 'create', $params); | |
312 | $this->assertEquals($result['is_error'], 1, "In line " . __LINE__); | |
313 | $this->assertEquals('priority_id is not valid', $result['error_message']); | |
314 | $this->assertEquals(2001,$result['error_code']); | |
315 | $this->assertEquals('priority_id',$result['error_field']); | |
316 | } | |
317 | ||
318 | function testActivityCreateWithValidStringPriority() { | |
319 | $params = array( | |
320 | 'source_contact_id' => 17, | |
321 | 'subject' => 'Make-it-Happen Meeting', | |
322 | 'activity_date_time' => date('Ymd'), | |
323 | 'duration' => 120, | |
324 | 'location' => 'Pensulvania', | |
325 | 'details' => 'a test activity', | |
326 | 'status_id' => 1, | |
327 | 'priority_id' => 'Urgent', | |
328 | 'activity_type_id' => 1, | |
329 | 'version' => $this->_apiversion, | |
330 | ); | |
331 | ||
332 | $result = civicrm_api('activity', 'create', $params); | |
333 | $this->assertEquals($result['is_error'], 0, "In line " . __LINE__); | |
334 | $this->assertEquals(1, $result['values'][$result['id']]['priority_id']); | |
335 | } | |
336 | ||
337 | function testActivityCreateWithInValidStringPriority() { | |
338 | $params = array( | |
339 | 'source_contact_id' => 17, | |
340 | 'subject' => 'Make-it-Happen Meeting', | |
341 | 'activity_date_time' => date('Ymd'), | |
342 | 'duration' => 120, | |
343 | 'location' => 'Pensulvania', | |
344 | 'details' => 'a test activity', | |
345 | 'status_id' => 1, | |
346 | 'priority_id' => 'ergUrgent', | |
347 | 'activity_type_id' => 1, | |
348 | 'version' => $this->_apiversion, | |
349 | ); | |
350 | ||
351 | $result = civicrm_api('activity', 'create', $params); | |
352 | $this->assertEquals($result['is_error'], 1, "In line " . __LINE__); | |
353 | $this->assertEquals('priority_id ergUrgent is not valid', $result['error_message']); | |
354 | } | |
355 | ||
356 | /** | |
357 | * Test civicrm_activity_create() with valid parameters | |
358 | */ | |
359 | function testActivityCreate() { | |
360 | ||
361 | $result = civicrm_api('activity', 'create', $this->_params); | |
362 | $this->assertAPISuccess($result, ' in line ' . __LINE__); | |
363 | $result = civicrm_api('activity', 'get', $this->_params); | |
364 | $this->assertAPISuccess($result, ' in line ' . __LINE__); | |
b02ddee7 | 365 | //$this->assertEquals($result['values'][$result['id']]['source_contact_id'], 17, 'in line ' . __LINE__); |
6a488035 TO |
366 | $this->assertEquals($result['values'][$result['id']]['duration'], 120, 'in line ' . __LINE__); |
367 | $this->assertEquals($result['values'][$result['id']]['subject'], 'test activity type id', 'in line ' . __LINE__); | |
368 | $this->assertEquals($result['values'][$result['id']]['activity_date_time'], '2011-06-02 14:36:13', 'in line ' . __LINE__); | |
369 | $this->assertEquals($result['values'][$result['id']]['location'], 'Pensulvania', 'in line ' . __LINE__); | |
370 | $this->assertEquals($result['values'][$result['id']]['details'], 'a test activity', 'in line ' . __LINE__); | |
371 | $this->assertEquals($result['values'][$result['id']]['status_id'], 2, 'in line ' . __LINE__); | |
372 | $this->assertEquals($result['values'][$result['id']]['id'], $result['id'], 'in line ' . __LINE__); | |
373 | } | |
374 | ||
375 | /** | |
376 | * Test civicrm_activity_create() with valid parameters - use type_id | |
377 | */ | |
378 | function testActivityCreateCampaignTypeID() { | |
379 | // force reload of config object | |
380 | $config = CRM_Core_Config::singleton(TRUE, TRUE); | |
381 | ||
382 | require_once 'CRM/Core/BAO/ConfigSetting.php'; | |
383 | CRM_Core_BAO_ConfigSetting::enableComponent('CiviCampaign'); | |
384 | //flush cache by calling with reset | |
385 | $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, TRUE, 'name', TRUE); | |
386 | ||
387 | $defaults = array(); | |
388 | ||
389 | $params = array( | |
390 | 'source_contact_id' => 17, | |
391 | 'subject' => 'Make-it-Happen Meeting', | |
392 | 'activity_date_time' => '20110316', | |
393 | 'duration' => 120, | |
394 | 'location' => 'Pensulvania', | |
395 | 'details' => 'a test activity', | |
396 | 'status_id' => 1, | |
397 | 'activity_type_id' => 29, | |
398 | 'version' => $this->_apiversion, | |
399 | 'priority_id' => 1, | |
400 | ); | |
401 | ||
402 | $result = civicrm_api('activity', 'create', $params); | |
403 | //todo test target & assignee are set | |
404 | $this->assertEquals($result['is_error'], 0, | |
405 | "Error message: " . CRM_Utils_Array::value('error_message', $result) . ' in line ' . __LINE__ | |
406 | ); | |
407 | ||
b02ddee7 | 408 | //$this->assertEquals($result['values'][$result['id']]['source_contact_id'], 17, 'in line ' . __LINE__); |
6a488035 TO |
409 | $result = civicrm_api('activity', 'get', array('id' => $result['id'], 'version' => $this->_apiversion)); |
410 | $this->assertEquals($result['values'][$result['id']]['source_contact_id'], 17, 'in line ' . __LINE__); | |
411 | ||
412 | $this->assertEquals($result['values'][$result['id']]['duration'], 120, 'in line ' . __LINE__); | |
413 | $this->assertEquals($result['values'][$result['id']]['subject'], 'Make-it-Happen Meeting', 'in line ' . __LINE__); | |
414 | $this->assertEquals($result['values'][$result['id']]['activity_date_time'], '2011-03-16 00:00:00', 'in line ' . __LINE__); | |
415 | $this->assertEquals($result['values'][$result['id']]['location'], 'Pensulvania', 'in line ' . __LINE__); | |
416 | $this->assertEquals($result['values'][$result['id']]['details'], 'a test activity', 'in line ' . __LINE__); | |
417 | $this->assertEquals($result['values'][$result['id']]['status_id'], 1, 'in line ' . __LINE__); | |
418 | } | |
419 | ||
420 | function testActivityReturnTargetAssignee() { | |
421 | ||
422 | $description = "Example demonstrates setting & retrieving the target & source"; | |
423 | $subfile = "GetTargetandAssignee"; | |
424 | $params = array( | |
425 | 'source_contact_id' => 17, | |
426 | 'subject' => 'Make-it-Happen Meeting', | |
427 | 'activity_date_time' => '20110316', | |
428 | 'duration' => 120, | |
429 | 'location' => 'Pensulvania', | |
430 | 'details' => 'a test activity', | |
431 | 'status_id' => 1, | |
432 | 'activity_type_id' => 1, | |
433 | 'version' => $this->_apiversion, | |
434 | 'priority_id' => 1, | |
435 | 'target_contact_id' => 17, | |
436 | 'assignee_contact_id' => 17, | |
437 | ); | |
438 | ||
439 | $result = civicrm_api('activity', 'create', $params); | |
440 | //todo test target & assignee are set | |
441 | $this->assertEquals($result['is_error'], 0, | |
442 | "Error message: " . CRM_Utils_Array::value('error_message', $result) . ' in line ' . __LINE__ | |
443 | ); | |
444 | ||
445 | $this->documentMe($params, $result, __FUNCTION__, __FILE__, $description, $subfile); | |
446 | $result = civicrm_api('activity', 'get', array('id' => $result['id'], 'version' => $this->_apiversion, 'return.assignee_contact_id' => 1, 'return.target_contact_id' => 1)); | |
447 | ||
448 | $this->assertEquals(17, $result['values'][$result['id']]['assignee_contact_id'][0], 'in line ' . __LINE__); | |
449 | $this->assertEquals(17, $result['values'][$result['id']]['target_contact_id'][0], 'in line ' . __LINE__); | |
450 | } | |
451 | ||
452 | function testActivityCreateExample() { | |
453 | ||
454 | /** | |
455 | * Test civicrm_activity_create() using example code | |
456 | */ | |
457 | require_once 'api/v3/examples/ActivityCreate.php'; | |
458 | $result = activity_create_example(); | |
459 | $expectedResult = activity_create_expectedresult(); | |
460 | $this->assertEquals($result, $expectedResult); | |
461 | } | |
462 | ||
463 | /** | |
464 | * Test civicrm_activity_create() with valid parameters for unique fields - | |
465 | * set up to see if unique fields work but activity_subject doesn't | |
466 | ||
467 | function testActivityCreateUniqueName( ) | |
468 | { | |
469 | $this->markTestSkipped('test to see if api will take unique names but it doesn\'t yet'); | |
470 | /*fields with unique names activity_id, | |
471 | * activity_subject,activity_duration | |
472 | * activity_location, activity_status_id | |
473 | * activity_is_test | |
474 | * activity_medium_id | |
475 | ||
476 | $params = array( | |
477 | 'source_contact_id' => 17, | |
478 | 'activity_subject' => 'Make-it-Happen Meeting', | |
479 | 'activity_date_time' => date('Ymd'), | |
480 | 'activity_duration' => 120, | |
481 | 'activity_location' => 'Pensulvania', | |
482 | 'details' => 'a test activity', | |
483 | 'activity_status_id' => 1, | |
484 | 'activity_name' => 'Test activity type', | |
485 | 'version' => $this->_apiversion, | |
486 | ); | |
487 | ||
488 | $result = civicrm_api('activity', 'create' , $params ); | |
489 | $this->assertEquals( $result['is_error'], 0, | |
490 | "Error message: " . CRM_Utils_Array::value( 'error_message', $result ) ); | |
491 | ||
492 | $this->assertEquals( $result['values'][$result['id']]['source_contact_id'], 17 ); | |
493 | $this->assertEquals( $result['values'][$result['id']]['duration'], 120 ); | |
494 | // This field gets lost | |
495 | $this->assertEquals( $result['values'][$result['id']]['subject'], 'Make-it-Happen Meeting' ); | |
496 | $this->assertEquals( $result['values'][$result['id']]['activity_date_time'], date('Ymd') . '000000' ); | |
497 | $this->assertEquals( $result['values'][$result['id']]['location'], 'Pensulvania' ); | |
498 | $this->assertEquals( $result['values'][$result['id']]['details'], 'a test activity' ); | |
499 | $this->assertEquals( $result['values'][$result['id']]['status_id'], 1 ); | |
500 | ||
501 | } | |
502 | */ | |
503 | ||
504 | /** | |
505 | * Test civicrm_activity_create() with valid parameters | |
506 | * and some custom data | |
507 | */ | |
508 | function testActivityCreateCustom() { | |
509 | $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__); | |
510 | $params = $this->_params; | |
511 | $params['custom_' . $ids['custom_field_id']] = "custom string"; | |
512 | $result = civicrm_api($this->_entity, 'create', $params); | |
513 | $this->assertAPISuccess($result, ' in line ' . __LINE__); | |
514 | $result = civicrm_api($this->_entity, 'get', array('return.custom_' . $ids['custom_field_id'] => 1, 'version' => 3, 'id' => $result['id'])); | |
515 | $this->assertEquals("custom string", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__); | |
516 | ||
517 | $this->customFieldDelete($ids['custom_field_id']); | |
518 | $this->customGroupDelete($ids['custom_group_id']); | |
519 | } | |
520 | ||
521 | /** | |
522 | * Test civicrm_activity_create() with valid parameters | |
523 | * and some custom data | |
524 | */ | |
525 | function testActivityCreateCustomContactRefField() { | |
526 | ||
527 | civicrm_api('contact', 'create', array('version' => 3, 'id' => 17, 'sort_name' => 'Contact, Test')); | |
528 | $subfile = 'ContactRefCustomField'; | |
529 | $description = "demonstrates create with Contact Reference Custom Field"; | |
530 | $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__); | |
531 | $params = array( | |
532 | 'custom_group_id' => $ids['custom_group_id'], | |
533 | 'name' => 'Worker_Lookup', | |
534 | 'label' => 'Worker Lookup', | |
535 | 'html_type' => 'Autocomplete-Select', | |
536 | 'data_type' => 'ContactReference', | |
537 | 'weight' => 4, | |
538 | 'is_searchable' => 1, | |
539 | 'is_active' => 1, | |
540 | 'version' => $this->_apiversion, | |
541 | ); | |
542 | ||
543 | $customField = civicrm_api('custom_field', 'create', $params); | |
544 | $params = $this->_params; | |
545 | $params['custom_' . $customField['id']] = "17"; | |
546 | ||
547 | $result = civicrm_api($this->_entity, 'create', $params); | |
548 | $this->documentMe($params, $result, __FUNCTION__, __FILE__, $description, $subfile); | |
549 | $this->assertAPISuccess($result, ' in line ' . __LINE__); | |
550 | $result = civicrm_api($this->_entity, 'get', array('return.custom_' . $customField => 1, 'version' => 3, 'id' => $result['id'])); | |
551 | $this->documentMe($params, $result, __FUNCTION__, __FILE__, 'Get with Contact Ref Custom Field', 'ContactRefCustomFieldGet'); | |
552 | ||
553 | $this->assertEquals('Contact, Test', $result['values'][$result['id']]['custom_' . $customField['id']], ' in line ' . __LINE__); | |
554 | $this->assertEquals(17, $result['values'][$result['id']]['custom_' . $customField['id'] . "_id"], ' in line ' . __LINE__); | |
555 | $this->assertEquals('Contact, Test', $result['values'][$result['id']]['custom_' . $customField['id'] . '_1'], ' in line ' . __LINE__); | |
556 | $this->assertEquals(17, $result['values'][$result['id']]['custom_' . $customField['id'] . "_1_id"], ' in line ' . __LINE__); | |
557 | $this->customFieldDelete($ids['custom_field_id']); | |
558 | $this->customGroupDelete($ids['custom_group_id']); | |
559 | } | |
560 | ||
561 | /** | |
562 | * Test civicrm_activity_create() with an invalid text status_id | |
563 | */ | |
564 | function testActivityCreateBadTextStatus() { | |
565 | ||
566 | $params = array( | |
567 | 'source_contact_id' => 17, | |
568 | 'subject' => 'Discussion on Apis for v3', | |
569 | 'activity_date_time' => date('Ymd'), | |
570 | 'duration' => 120, | |
571 | 'location' => 'Pensulvania', | |
572 | 'details' => 'a test activity', | |
573 | 'status_id' => 'Invalid', | |
574 | 'activity_name' => 'Test activity type', | |
575 | 'version' => $this->_apiversion, | |
576 | ); | |
577 | ||
578 | $result = civicrm_api('activity', 'create', $params); | |
579 | ||
580 | $this->assertEquals($result['is_error'], 1, | |
581 | "In line " . __LINE__ | |
582 | ); | |
583 | } | |
584 | ||
585 | /** | |
586 | * Test civicrm_activity_create() with valid parameters, | |
587 | * using a text status_id | |
588 | */ | |
589 | function testActivityCreateTextStatus() { | |
590 | ||
591 | ||
592 | $params = array( | |
593 | 'source_contact_id' => 17, | |
594 | 'subject' => 'Make-it-Happen Meeting', | |
595 | 'activity_date_time' => date('Ymd'), | |
596 | 'duration' => 120, | |
597 | 'location' => 'Pensulvania', | |
598 | 'details' => 'a test activity', | |
599 | 'status_id' => 'Scheduled', | |
600 | 'activity_name' => 'Test activity type', | |
601 | 'version' => $this->_apiversion, | |
602 | ); | |
603 | ||
604 | $result = civicrm_api('activity', 'create', $params); | |
605 | $this->assertEquals($result['is_error'], 0, | |
606 | "Error message: " . CRM_Utils_Array::value('error_message', $result) . ' in line ' . __LINE__ | |
607 | ); | |
b02ddee7 | 608 | //$this->assertEquals($result['values'][$result['id']]['source_contact_id'], 17, 'in line ' . __LINE__); |
6a488035 TO |
609 | $this->assertEquals($result['values'][$result['id']]['duration'], 120, 'in line ' . __LINE__); |
610 | $this->assertEquals($result['values'][$result['id']]['subject'], 'Make-it-Happen Meeting', 'in line ' . __LINE__); | |
611 | $this->assertEquals($result['values'][$result['id']]['activity_date_time'], date('Ymd') . '000000', 'in line ' . __LINE__); | |
612 | $this->assertEquals($result['values'][$result['id']]['location'], 'Pensulvania', 'in line ' . __LINE__); | |
613 | $this->assertEquals($result['values'][$result['id']]['details'], 'a test activity', 'in line ' . __LINE__); | |
614 | $this->assertEquals($result['values'][$result['id']]['status_id'], 'Scheduled', 'in line ' . __LINE__); | |
615 | } | |
616 | ||
617 | /** | |
618 | * Test civicrm_activity_get() with no params | |
619 | */ | |
620 | function testActivityGetEmpty() { | |
621 | $params = array('version' => $this->_apiversion); | |
622 | $result = civicrm_api('activity', 'get', $params); | |
623 | $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__); | |
624 | } | |
625 | ||
626 | /** | |
627 | * Test civicrm_activity_get() with a good activity ID | |
628 | */ | |
629 | function testActivityGetGoodID1() { | |
630 | // Insert rows in civicrm_activity creating activities 4 and | |
631 | // 13 | |
632 | $description = "Function demonstrates getting asignee_contact_id & using it to get the contact"; | |
633 | $subfile = 'ReturnAssigneeContact'; | |
634 | $activity = civicrm_api('activity', 'create', $this->_params); | |
635 | ||
636 | $contact = civicrm_api('Contact', 'Create', array( | |
637 | 'first_name' => "The Rock", | |
638 | 'last_name' =>'roccky', | |
639 | 'contact_type' => 'Individual', | |
640 | 'version' => 3, | |
641 | 'api.activity.create' => array( | |
642 | 'id' => $activity['id'], 'assignee_contact_id' => '$value.id', | |
643 | ), | |
644 | )); | |
645 | ||
646 | $params = array( | |
647 | 'activity_id' => $activity['id'], | |
648 | 'version' => $this->_apiversion, | |
649 | 'sequential' => 1, | |
650 | 'return.assignee_contact_id' => 1, | |
651 | 'api.contact.get' => array( | |
652 | 'id' => '$value.source_contact_id', | |
653 | ), | |
654 | ); | |
655 | ||
656 | $result = civicrm_api('Activity', 'Get', $params); | |
657 | $this->documentMe($params, $result, __FUNCTION__, __FILE__, $description, $subfile); | |
658 | ||
659 | $this->assertAPISuccess($result); | |
660 | ||
661 | $this->assertEquals($activity['id'], $result['id'], 'In line ' . __LINE__); | |
b02ddee7 | 662 | //$this->assertEquals(17, $result['values'][0]['source_contact_id'], 'In line ' . __LINE__); |
6a488035 TO |
663 | |
664 | $this->assertEquals($contact['id'], $result['values'][0]['assignee_contact_id'][0], 'In line ' . __LINE__); | |
665 | ||
666 | $this->assertEquals(17, $result['values'][0]['api.contact.get']['values'][0]['contact_id'], 'In line ' . __LINE__); | |
667 | $this->assertEquals($this->test_activity_type_value, $result['values'][0]['activity_type_id'], 'In line ' . __LINE__); | |
668 | $this->assertEquals("test activity type id", $result['values'][0]['subject'], 'In line ' . __LINE__); | |
669 | } | |
670 | ||
671 | /* | |
672 | * test that get functioning does filtering | |
673 | */ | |
674 | function testGetFilter() { | |
675 | $params = array( | |
676 | 'source_contact_id' => 17, | |
677 | 'subject' => 'Make-it-Happen Meeting', | |
678 | 'activity_date_time' => '20110316', | |
679 | 'duration' => 120, | |
680 | 'location' => 'Pensulvania', | |
681 | 'details' => 'a test activity', | |
682 | 'status_id' => 1, | |
683 | 'activity_name' => 'Test activity type', | |
684 | 'version' => $this->_apiversion, | |
685 | 'priority_id' => 1, | |
686 | ); | |
687 | civicrm_api('Activity', 'Create', $params); | |
688 | $result = civicrm_api('Activity', 'Get', array('version' => 3, 'subject' => 'Make-it-Happen Meeting')); | |
689 | $this->assertEquals(1, $result['count']); | |
690 | $this->assertEquals('Make-it-Happen Meeting', $result['values'][$result['id']]['subject']); | |
691 | civicrm_api('Activity', 'Delete', array('version' => 3, 'id' => $result['id'])); | |
692 | } | |
693 | /* | |
694 | * test that get functioning does filtering | |
695 | */ | |
696 | function testGetStatusID() { | |
697 | $params = array( | |
698 | 'source_contact_id' => 17, | |
699 | 'subject' => 'Make-it-Happen Meeting', | |
700 | 'activity_date_time' => '20110316', | |
701 | 'duration' => 120, | |
702 | 'location' => 'Pensulvania', | |
703 | 'details' => 'a test activity', | |
704 | 'status_id' => 1, | |
705 | 'activity_name' => 'Test activity type', | |
706 | 'version' => $this->_apiversion, | |
707 | 'priority_id' => 1, | |
708 | ); | |
709 | civicrm_api('Activity', 'Create', $params); | |
710 | $result = civicrm_api('Activity', 'Get', array( | |
711 | 'version' => $this->_apiversion, | |
712 | 'activity_status_id' => '1')); | |
713 | $this->assertEquals(1, $result['count'], 'one activity of status 1 should exist'); | |
714 | ||
715 | $result = civicrm_api('Activity', 'Get', array( | |
716 | 'version' => $this->_apiversion, | |
717 | 'status_id' => '1')); | |
718 | $this->assertEquals(1, $result['count'], 'status_id should also work'); | |
719 | ||
720 | $result = civicrm_api('Activity', 'Get', array( | |
721 | 'version' => $this->_apiversion, | |
722 | 'activity_status_id' => '2')); | |
723 | $this->assertEquals(0, $result['count'], 'No activities of status 1 should exist'); | |
724 | $result = civicrm_api('Activity', 'Get', array( | |
725 | 'version' => $this->_apiversion, | |
726 | 'status_id' => '2')); | |
727 | $this->assertEquals(0, $result['count'], 'No activities of status 1 should exist'); | |
728 | ||
729 | ||
730 | } | |
731 | ||
732 | /* | |
733 | * test that get functioning does filtering | |
734 | */ | |
735 | function testGetFilterMaxDate() { | |
736 | $params = array( | |
737 | 'source_contact_id' => 17, | |
738 | 'subject' => 'Make-it-Happen Meeting', | |
739 | 'activity_date_time' => '20110101', | |
740 | 'duration' => 120, | |
741 | 'location' => 'Pensulvania', | |
742 | 'details' => 'a test activity', | |
743 | 'status_id' => 1, | |
744 | 'activity_name' => 'Test activity type', | |
745 | 'version' => $this->_apiversion, | |
746 | 'priority_id' => 1, | |
747 | ); | |
748 | $activityOne = civicrm_api('Activity', 'Create', $params); | |
749 | $params['activity_date_time'] = 20120216; | |
750 | $activityTwo = civicrm_api('Activity', 'Create', $params); | |
751 | $result = civicrm_api('Activity', 'Get', array( | |
752 | 'version' => 3, | |
753 | )); | |
754 | $description = "demonstrates _low filter (at time of writing doesn't work if contact_id is set"; | |
755 | $subfile = "DateTimeLow"; | |
756 | $this->assertEquals(2, $result['count']); | |
757 | $params = array( | |
758 | 'version' => 3, | |
759 | 'filter.activity_date_time_low' => '20120101000000', | |
760 | 'sequential' => 1, | |
761 | ); | |
762 | $result = civicrm_api('Activity', 'Get', $params); | |
763 | $this->documentMe($params, $result, __FUNCTION__, __FILE__, $description, $subfile); | |
764 | $this->assertEquals(1, $result['count'], 'in line ' . __LINE__); | |
765 | $description = "demonstrates _high filter (at time of writing doesn't work if contact_id is set"; | |
766 | $subfile = "DateTimeHigh"; | |
767 | $this->assertEquals('2012-02-16 00:00:00', $result['values'][0]['activity_date_time'], 'in line ' . __LINE__); | |
768 | $params = array( | |
769 | 'source_contact_id' => 17, | |
770 | 'version' => 3, | |
771 | 'filter.activity_date_time_high' => '20120101000000', | |
772 | 'sequential' => 1, | |
773 | ); | |
774 | $result = civicrm_api('Activity', 'Get', $params); | |
775 | $this->documentMe($params, $result, __FUNCTION__, __FILE__, $description, $subfile); | |
776 | ||
777 | $this->assertEquals(1, $result['count']); | |
778 | $this->assertEquals('2011-01-01 00:00:00', $result['values'][0]['activity_date_time'], 'in line ' . __LINE__); | |
779 | ||
780 | civicrm_api('Activity', 'Delete', array('version' => 3, 'id' => $activityOne['id'])); | |
781 | civicrm_api('Activity', 'Delete', array('version' => 3, 'id' => $activityTwo['id'])); | |
782 | } | |
783 | ||
784 | /** | |
785 | * Test civicrm_activity_get() with a good activity ID which | |
786 | * has associated custom data | |
787 | */ | |
788 | function testActivityGetGoodIDCustom() { | |
789 | $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__); | |
790 | ||
791 | $params = $this->_params; | |
792 | $params['custom_' . $ids['custom_field_id']] = "custom string"; | |
793 | ||
794 | $result = civicrm_api($this->_entity, 'create', $params); | |
795 | $this->assertAPISuccess($result, ' in line ' . __LINE__); | |
796 | // Retrieve the test value | |
797 | $params = array( | |
798 | 'activity_type_id' => $this->test_activity_type_value, | |
799 | 'version' => 3, | |
800 | 'sequential' => 1, | |
801 | 'return.custom_' . $ids['custom_field_id'] => 1, | |
802 | ); | |
803 | $result = civicrm_api('activity', 'get', $params, TRUE); | |
804 | $this->documentMe($params, $result, __FUNCTION__, __FILE__); | |
805 | $this->assertAPISuccess($result, ' in line ' . __LINE__); | |
806 | $this->assertEquals("custom string", $result['values'][0]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__); | |
807 | ||
808 | ||
b02ddee7 | 809 | //$this->assertEquals(17, $result['values'][0]['source_contact_id'], 'In line ' . __LINE__); |
6a488035 TO |
810 | $this->assertEquals($this->test_activity_type_value, $result['values'][0]['activity_type_id'], 'In line ' . __LINE__); |
811 | $this->assertEquals('test activity type id', $result['values'][0]['subject'], 'In line ' . __LINE__); | |
812 | $this->customFieldDelete($ids['custom_field_id']); | |
813 | $this->customGroupDelete($ids['custom_group_id']); | |
814 | } | |
815 | ||
816 | /** | |
817 | * Test civicrm_activity_get() with a good activity ID which | |
818 | * has associated custom data | |
819 | */ | |
820 | function testActivityGetContact_idCustom() { | |
821 | $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__); | |
822 | ||
823 | $params = $this->_params; | |
824 | $params['custom_' . $ids['custom_field_id']] = "custom string"; | |
825 | ||
826 | $result = civicrm_api($this->_entity, 'create', $params); | |
827 | // Retrieve the test value | |
828 | $params = array( | |
829 | 'contact_id' => $this->_params['source_contact_id'], | |
830 | 'activity_type_id' => $this->test_activity_type_value, | |
831 | 'version' => 3, | |
832 | 'sequential' => 1, | |
833 | 'return.custom_' . $ids['custom_field_id'] => 1, | |
834 | ); | |
835 | $result = civicrm_api('activity', 'get', $params, TRUE); | |
836 | $this->assertAPISuccess($result, 'in line ' . __LINE__); | |
837 | $this->documentMe($params, $result, __FUNCTION__, __FILE__); | |
838 | $this->assertEquals(0, $result['is_error'], "Error message: " . CRM_Utils_Array::value('error_message', $result)); | |
839 | $this->assertEquals("custom string", $result['values'][0]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__); | |
840 | ||
b02ddee7 | 841 | //$this->assertEquals(17, $result['values'][0]['source_contact_id'], 'In line ' . __LINE__); |
6a488035 TO |
842 | $this->assertEquals($this->test_activity_type_value, $result['values'][0]['activity_type_id'], 'In line ' . __LINE__); |
843 | $this->assertEquals('test activity type id', $result['values'][0]['subject'], 'In line ' . __LINE__); | |
844 | $this->assertEquals($result['values'][0]['id'], $result['id'], 'in line ' . __LINE__); | |
845 | } | |
846 | ||
847 | /** | |
848 | * check activity deletion with empty params | |
849 | */ | |
850 | function testDeleteActivityForEmptyParams() { | |
851 | $params = array('version' => $this->_apiversion); | |
852 | $result = civicrm_api('activity', 'delete', $params); | |
853 | $this->assertEquals($result['is_error'], 1, | |
854 | "In line " . __LINE__ | |
855 | ); | |
856 | } | |
857 | ||
858 | /** | |
859 | * check activity deletion without activity id | |
860 | */ | |
861 | function testDeleteActivityWithoutId() { | |
862 | $params = array( | |
863 | 'activity_name' => 'Meeting', | |
864 | 'version' => $this->_apiversion, | |
865 | ); | |
866 | $result = civicrm_api('activity', 'delete', $params); | |
867 | $this->assertEquals($result['is_error'], 1, | |
868 | "In line " . __LINE__ | |
869 | ); | |
870 | } | |
871 | ||
872 | /** | |
873 | * check activity deletion without activity type | |
874 | */ | |
875 | function testDeleteActivityWithoutActivityType() { | |
876 | $params = array('id' => 1); | |
877 | $result = civicrm_api('activity', 'delete', $params); | |
878 | $this->assertEquals($result['is_error'], 1, | |
879 | "In line " . __LINE__ | |
880 | ); | |
881 | } | |
882 | ||
883 | /** | |
884 | * check activity deletion with incorrect data | |
885 | */ | |
886 | function testDeleteActivityWithIncorrectActivityType() { | |
887 | $params = array( | |
888 | 'id' => 1, | |
889 | 'activity_name' => 'Test Activity', | |
890 | ); | |
891 | ||
892 | $result = civicrm_api('activity', 'delete', $params); | |
893 | $this->assertEquals($result['is_error'], 1, | |
894 | "In line " . __LINE__ | |
895 | ); | |
896 | } | |
897 | ||
898 | /** | |
899 | * check activity deletion with correct data | |
900 | */ | |
901 | function testDeleteActivity() { | |
902 | $result = civicrm_api('activity', 'create', $this->_params); | |
903 | $params = array( | |
904 | 'id' => $result['id'], | |
905 | 'version' => $this->_apiversion, | |
906 | ); | |
907 | ||
908 | $result = civicrm_api('activity', 'delete', $params); | |
909 | $this->documentMe($params, $result, __FUNCTION__, __FILE__); | |
910 | $this->assertAPISuccess($result); | |
911 | } | |
912 | ||
913 | /** | |
914 | * check with empty array | |
915 | */ | |
916 | function testActivityUpdateEmpty() { | |
917 | $params = array(); | |
918 | $result = civicrm_api('activity', 'create', $params); | |
919 | $this->assertEquals($result['is_error'], 1, | |
920 | "In line " . __LINE__ | |
921 | ); | |
922 | } | |
923 | ||
924 | /** | |
925 | * check if required fields are not passed | |
926 | */ | |
927 | function testActivityUpdateWithoutRequired() { | |
928 | $params = array( | |
929 | 'subject' => 'this case should fail', | |
930 | 'scheduled_date_time' => date('Ymd'), | |
931 | ); | |
932 | ||
933 | $result = civicrm_api('activity', 'create', $params); | |
934 | $this->assertEquals($result['is_error'], 1, | |
935 | "In line " . __LINE__ | |
936 | ); | |
937 | } | |
938 | ||
939 | /** | |
940 | * check with incorrect required fields | |
941 | */ | |
942 | function testActivityUpdateWithIncorrectData() { | |
943 | $params = array( | |
944 | 'activity_name' => 'Meeting', | |
945 | 'subject' => 'this case should fail', | |
946 | 'scheduled_date_time' => date('Ymd'), | |
947 | ); | |
948 | ||
949 | $result = civicrm_api('activity', 'create', $params); | |
950 | $this->assertEquals($result['is_error'], 1, | |
951 | "In line " . __LINE__ | |
952 | ); | |
953 | } | |
954 | ||
955 | /** | |
956 | * Test civicrm_activity_update() with non-numeric id | |
957 | */ | |
958 | function testActivityUpdateWithNonNumericId() { | |
959 | $params = array( | |
960 | 'id' => 'lets break it', | |
961 | 'activity_name' => 'Meeting', | |
962 | 'subject' => 'this case should fail', | |
963 | 'scheduled_date_time' => date('Ymd'), | |
964 | ); | |
965 | ||
966 | $result = civicrm_api('activity', 'create', $params); | |
967 | $this->assertEquals($result['is_error'], 1, | |
968 | "In line " . __LINE__ | |
969 | ); | |
970 | } | |
971 | ||
972 | /** | |
973 | * check with incorrect required fields | |
974 | */ | |
975 | function testActivityUpdateWithIncorrectContactActivityType() { | |
976 | $params = array( | |
977 | 'id' => 1, | |
978 | 'activity_name' => 'Test Activity', | |
979 | 'subject' => 'this case should fail', | |
980 | 'scheduled_date_time' => date('Ymd'), | |
981 | 'version' => $this->_apiversion, | |
982 | 'source_contact_id' => 17, | |
983 | ); | |
984 | ||
985 | $result = civicrm_api('activity', 'create', $params); | |
986 | $this->assertEquals($result['is_error'], 1, "In line " . __LINE__); | |
987 | $this->assertEquals($result['error_message'], 'Invalid Activity Id', "In line " . __LINE__); | |
988 | } | |
989 | ||
990 | /** | |
991 | * Test civicrm_activity_update() to update an existing activity | |
992 | */ | |
993 | function testActivityUpdate() { | |
994 | $result = civicrm_api('activity', 'create', $this->_params); | |
995 | ||
996 | $params = array( | |
997 | 'id' => $result['id'], | |
998 | 'subject' => 'Make-it-Happen Meeting', | |
999 | 'activity_date_time' => '20091011123456', | |
1000 | 'duration' => 120, | |
1001 | 'location' => '21, Park Avenue', | |
1002 | 'details' => 'Lets update Meeting', | |
1003 | 'status_id' => 1, | |
1004 | 'source_contact_id' => 17, | |
1005 | 'priority_id' => 1, | |
1006 | 'version' => $this->_apiversion, | |
1007 | ); | |
1008 | ||
1009 | $result = civicrm_api('activity', 'create', $params); | |
1010 | $this->assertAPISuccess($result, 'in line ' . __LINE__); | |
1011 | //hack on date comparison - really we should make getAndCheck smarter to handle dates | |
1012 | $params['activity_date_time'] = '2009-10-11 12:34:56'; | |
1013 | $this->getAndCheck($params, $result['id'], 'activity'); | |
1014 | } | |
1015 | ||
1016 | /** | |
1017 | * Test civicrm_activity_update() with valid parameters | |
1018 | * and some custom data | |
1019 | */ | |
1020 | function testActivityUpdateCustom() { | |
1021 | $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__); | |
1022 | ||
1023 | $params = $this->_params; | |
1024 | ||
1025 | // Create an activity with custom data | |
1026 | //this has been updated from the previous 'old format' function - need to make it work | |
1027 | $params = array( | |
1028 | 'source_contact_id' => 17, | |
1029 | 'subject' => 'Make-it-Happen Meeting', | |
1030 | 'activity_date_time' => '2009-10-18', | |
1031 | 'duration' => 120, | |
1032 | 'location' => 'Pensulvania', | |
1033 | 'details' => 'a test activity to check the update api', | |
1034 | 'status_id' => 1, | |
1035 | 'activity_name' => 'Test activity type', | |
1036 | 'version' => $this->_apiversion, | |
1037 | 'custom_' . $ids['custom_field_id'] => 'custom string', | |
1038 | ); | |
1039 | $result = civicrm_api('activity', 'create', $params); | |
1040 | ||
1041 | $activityId = $result['id']; | |
1042 | $this->assertEquals(0, $result['is_error'], | |
1043 | "Error message: " . CRM_Utils_Array::value('error_message', $result) | |
1044 | ); | |
1045 | $result = civicrm_api($this->_entity, 'get', array('return.custom_' . $ids['custom_field_id'] => 1, 'version' => 3, 'id' => $result['id'])); | |
1046 | $this->assertEquals("custom string", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__); | |
1047 | $this->assertEquals("2009-10-18 00:00:00", $result['values'][$result['id']]['activity_date_time'], ' in line ' . __LINE__); | |
1048 | $fields = civicrm_api('activity', 'getfields', array('version' => $this->_apiversion)); | |
1049 | $this->assertTrue(is_array($fields['values']['custom_' . $ids['custom_field_id']])); | |
1050 | ||
1051 | // Update the activity with custom data | |
1052 | $params = array( | |
1053 | 'id' => $activityId, | |
1054 | 'source_contact_id' => 17, | |
1055 | 'subject' => 'Make-it-Happen Meeting', | |
1056 | 'status_id' => 1, | |
1057 | 'activity_name' => 'Test activity type', | |
1058 | // add this since dates are messed up | |
1059 | 'activity_date_time' => date('Ymd'), | |
1060 | 'custom_' . $ids['custom_field_id'] => 'Updated my test data', | |
1061 | 'version' => $this->_apiversion, | |
1062 | ); | |
1063 | $result = civicrm_api('Activity', 'Create', $params); | |
1064 | $this->assertEquals(0, $result['is_error'], | |
1065 | "Error message: " . CRM_Utils_Array::value('error_message', $result) | |
1066 | ); | |
1067 | ||
1068 | $result = civicrm_api($this->_entity, 'get', array('return.custom_' . $ids['custom_field_id'] => 1, 'version' => 3, 'id' => $result['id'])); | |
1069 | $this->assertEquals("Updated my test data", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__); | |
1070 | } | |
1071 | ||
1072 | /** | |
1073 | * Test civicrm_activity_update() for core activity fields | |
1074 | * and some custom data | |
1075 | */ | |
1076 | function testActivityUpdateCheckCoreFields() { | |
1077 | $params = $this->_params; | |
1078 | $contact1Params = array( | |
1079 | 'first_name' => 'John', | |
1080 | 'middle_name' => 'J.', | |
1081 | 'last_name' => 'Anderson', | |
1082 | 'prefix_id' => 3, | |
1083 | 'suffix_id' => 3, | |
1084 | 'email' => 'john_anderson@civicrm.org', | |
1085 | 'contact_type' => 'Individual', | |
1086 | ); | |
1087 | ||
1088 | $contact1 = $this->individualCreate($contact1Params); | |
1089 | $contact2Params = array( | |
1090 | 'first_name' => 'Michal', | |
1091 | 'middle_name' => 'J.', | |
1092 | 'last_name' => 'Anderson', | |
1093 | 'prefix_id' => 3, | |
1094 | 'suffix_id' => 3, | |
1095 | 'email' => 'michal_anderson@civicrm.org', | |
1096 | 'contact_type' => 'Individual', | |
1097 | ); | |
1098 | ||
1099 | $contact2 = $this->individualCreate($contact2Params); | |
1100 | ||
1101 | $params['assignee_contact_id'] = array($contact1, $contact2); | |
1102 | $params['target_contact_id'] = array($contact2 => $contact2); | |
1103 | $result = civicrm_api('Activity', 'Create', $params); | |
1104 | ||
1105 | $result = civicrm_api('activity', 'create', $params); | |
1106 | $activityId = $result['id']; | |
1107 | $this->assertAPISuccess($result); | |
1108 | $getParams = array( | |
1109 | 'return.assignee_contact_id' => 1, | |
1110 | 'return.target_contact_id' => 1, | |
1111 | 'version' => $this->_apiversion, | |
1112 | 'id' => $activityId, | |
1113 | ); | |
1114 | $result = civicrm_api($this->_entity, 'get',$getParams ); | |
1115 | $assignee = $result['values'][$result['id']]['assignee_contact_id']; | |
1116 | $target = $result['values'][$result['id']]['target_contact_id']; | |
1117 | $this->assertEquals(2, count($assignee), ' in line ' . __LINE__); | |
1118 | $this->assertEquals(1, count($target), ' in line ' . __LINE__); | |
1119 | $this->assertEquals(TRUE, in_array($contact1, $assignee), ' in line ' . __LINE__); | |
1120 | $this->assertEquals(TRUE, in_array($contact2, $target), ' in line ' . __LINE__); | |
1121 | ||
1122 | $contact3Params = array( | |
1123 | 'first_name' => 'Jijo', | |
1124 | 'middle_name' => 'J.', | |
1125 | 'last_name' => 'Anderson', | |
1126 | 'prefix_id' => 3, | |
1127 | 'suffix_id' => 3, | |
1128 | 'email' => 'jijo_anderson@civicrm.org', | |
1129 | 'contact_type' => 'Individual', | |
1130 | ); | |
1131 | ||
1132 | $contact4Params = array( | |
1133 | 'first_name' => 'Grant', | |
1134 | 'middle_name' => 'J.', | |
1135 | 'last_name' => 'Anderson', | |
1136 | 'prefix_id' => 3, | |
1137 | 'suffix_id' => 3, | |
1138 | 'email' => 'grant_anderson@civicrm.org', | |
1139 | 'contact_type' => 'Individual', | |
1140 | ); | |
1141 | ||
1142 | $contact3 = $this->individualCreate($contact3Params); | |
1143 | $contact4 = $this->individualCreate($contact4Params); | |
1144 | ||
1145 | $params = array(); | |
1146 | $params['id'] = $activityId; | |
1147 | $params['version'] = $this->_apiversion; | |
1148 | $params['assignee_contact_id'] = array($contact3 => $contact3); | |
1149 | $params['target_contact_id'] = array($contact4 => $contact4); | |
1150 | ||
1151 | $result = civicrm_api('activity', 'create', $params); | |
1152 | $this->assertEquals(0, $result['is_error'], | |
1153 | "Error message: " . CRM_Utils_Array::value('error_message', $result) | |
1154 | ); | |
1155 | ||
1156 | $this->assertEquals($activityId, $result['id'], ' in line ' . __LINE__); | |
1157 | ||
1158 | $result = civicrm_api($this->_entity, 'get', array('return.assignee_contact_id' => 1, 'return.target_contact_id' => 1, 'version' => 3, 'id' => $result['id'])); | |
1159 | ||
1160 | $assignee = $result['values'][$result['id']]['assignee_contact_id']; | |
1161 | $target = $result['values'][$result['id']]['target_contact_id']; | |
1162 | ||
1163 | $this->assertEquals(1, count($assignee), ' in line ' . __LINE__); | |
1164 | $this->assertEquals(1, count($target), ' in line ' . __LINE__); | |
1165 | $this->assertEquals(TRUE, in_array($contact3, $assignee), ' in line ' . __LINE__); | |
1166 | $this->assertEquals(TRUE, in_array($contact4, $target), ' in line ' . __LINE__); | |
1167 | ||
1168 | foreach ($this->_params as $fld => $val) { | |
1169 | if ($fld == 'version') { | |
1170 | continue; | |
1171 | } | |
1172 | $this->assertEquals($val, $result['values'][$result['id']][$fld], ' in line ' . __LINE__); | |
1173 | } | |
1174 | } | |
1175 | ||
1176 | /** | |
1177 | * Test civicrm_activity_update() where the DB has a date_time | |
1178 | * value and there is none in the update params. | |
1179 | */ | |
1180 | function testActivityUpdateNotDate() { | |
1181 | $result = civicrm_api('activity', 'create', $this->_params); | |
1182 | ||
1183 | $params = array( | |
1184 | 'id' => $result['id'], | |
1185 | 'subject' => 'Make-it-Happen Meeting', | |
1186 | 'duration' => 120, | |
1187 | 'location' => '21, Park Avenue', | |
1188 | 'details' => 'Lets update Meeting', | |
1189 | 'status_id' => 1, | |
1190 | 'source_contact_id' => 17, | |
1191 | 'priority_id' => 1, | |
1192 | 'version' => $this->_apiversion, | |
1193 | ); | |
1194 | ||
1195 | $result = civicrm_api('activity', 'create', $params); | |
1196 | $this->assertAPISuccess($result, 'in line ' . __LINE__); | |
1197 | //hack on date comparison - really we should make getAndCheck smarter to handle dates | |
1198 | $params['activity_date_time'] = $this->_params['activity_date_time']; | |
1199 | $this->getAndCheck($params, $result['id'], 'activity'); | |
1200 | } | |
1201 | ||
1202 | /** | |
1203 | * check activity update with status | |
1204 | */ | |
1205 | function testActivityUpdateWithStatus() { | |
1206 | $activity = civicrm_api('activity', 'create', $this->_params); | |
1207 | $this->assertAPISuccess($activity, "In line " . __LINE__); | |
1208 | $params = array( | |
1209 | 'id' => $activity['id'], | |
1210 | 'source_contact_id' => 17, | |
1211 | 'subject' => 'Hurry update works', | |
1212 | 'status_id' => 1, | |
1213 | 'activity_name' => 'Test activity type', | |
1214 | 'version' => $this->_apiversion, | |
1215 | ); | |
1216 | ||
1217 | $result = civicrm_api('activity', 'create', $params); | |
1218 | $this->assertAPISuccess($result, "In line " . __LINE__); | |
1219 | $this->assertEquals($result['id'], $activity['id'], "In line " . __LINE__); | |
b02ddee7 | 1220 | /*$this->assertEquals($result['values'][$activity['id']]['source_contact_id'], 17, |
6a488035 | 1221 | "In line " . __LINE__ |
b02ddee7 | 1222 | );*/ |
6a488035 TO |
1223 | $this->assertEquals($result['values'][$activity['id']]['subject'], 'Hurry update works', |
1224 | "In line " . __LINE__ | |
1225 | ); | |
1226 | $this->assertEquals($result['values'][$activity['id']]['status_id'], 1, | |
1227 | "In line " . __LINE__ | |
1228 | ); | |
1229 | } | |
1230 | ||
1231 | /** | |
1232 | * Test civicrm_activity_update() where the source_contact_id | |
1233 | * is not in the update params. | |
1234 | */ | |
1235 | function testActivityUpdateKeepSource() { | |
1236 | $activity = civicrm_api('activity', 'create', $this->_params); | |
1237 | // Updating the activity but not providing anything for the source contact | |
1238 | // (It was set as 17 earlier.) | |
1239 | $params = array( | |
1240 | 'id' => $activity['id'], | |
1241 | 'subject' => 'Updated Make-it-Happen Meeting', | |
1242 | 'duration' => 120, | |
1243 | 'location' => '21, Park Avenue', | |
1244 | 'details' => 'Lets update Meeting', | |
1245 | 'status_id' => 1, | |
1246 | 'activity_name' => 'Test activity type', | |
1247 | 'priority_id' => 1, | |
1248 | 'version' => $this->_apiversion, | |
1249 | ); | |
1250 | ||
1251 | $result = civicrm_api('activity', 'create', $params); | |
1252 | $this->assertAPISuccess($result, 'in line ' . __LINE__); | |
1253 | $findactivity = civicrm_api('Activity', 'Get', array('id' => $activity['id'], 'version' => 3)); | |
1254 | ||
1255 | $this->assertAPISuccess($findactivity); | |
b02ddee7 | 1256 | //$this->assertEquals(17, $findactivity['values'][$findactivity['id']]['source_contact_id'], 'In line ' . __LINE__); |
6a488035 TO |
1257 | } |
1258 | ||
1259 | /** | |
1260 | * Test civicrm_activities_contact_get() | |
1261 | */ | |
1262 | function testActivitiesContactGet() { | |
1263 | $activity = civicrm_api('activity', 'create', $this->_params); | |
1264 | $activity2 = civicrm_api('activity', 'create', $this->_params2); | |
1265 | // Get activities associated with contact 17 | |
1266 | $params = array( | |
1267 | 'contact_id' => 17, | |
1268 | 'version' => $this->_apiversion, | |
1269 | ); | |
1270 | $result = civicrm_api('activity', 'get', $params); | |
1271 | ||
1272 | $this->assertAPISuccess($result, 'in line ' . __LINE__); | |
1273 | $this->assertEquals(2, $result['count'], 'In line ' . __LINE__); | |
1274 | $this->assertEquals($this->test_activity_type_value, $result['values'][$activity['id']]['activity_type_id'], 'In line ' . __LINE__); | |
1275 | $this->assertEquals('Test activity type', $result['values'][$activity['id']]['activity_name'], 'In line ' . __LINE__); | |
1276 | $this->assertEquals('Test activity type', $result['values'][$activity2['id']]['activity_name'], 'In line ' . __LINE__); | |
1277 | } | |
1278 | /* | |
1279 | * test chained Activity format | |
1280 | */ | |
1281 | function testchainedActivityGet() { | |
1282 | ||
1283 | $activity = civicrm_api('Contact', 'Create', array( | |
1284 | 'version' => $this->_apiversion, | |
1285 | 'display_name' => "bob brown", | |
1286 | 'contact_type' => 'Individual', | |
1287 | 'api.activity_type.create' => array( | |
1288 | 'weight' => '2', | |
1289 | 'label' => 'send out letters', | |
1290 | 'filter' => 0, | |
1291 | 'is_active' => 1, | |
1292 | 'is_optgroup' => 1, | |
1293 | 'is_default' => 0, | |
1294 | ), 'api.activity.create' => array('subject' => 'send letter', 'activity_type_id' => '$value.api.activity_type.create.values.0.value'), | |
1295 | )); | |
1296 | ||
1297 | $this->assertAPISuccess($activity, 'in line ' . __LINE__); | |
1298 | $result = civicrm_api('Activity', 'Get', array( | |
1299 | 'version' => 3, | |
1300 | 'id' => $activity['id'], | |
1301 | 'return.assignee_contact_id' => 1, | |
1302 | 'api.contact.get' => array('api.pledge.get' => 1), | |
1303 | )); | |
1304 | } | |
1305 | ||
1306 | /** | |
1307 | * check civicrm_activities_contact_get() with empty array | |
1308 | */ | |
1309 | function testActivityContactGetEmpty() { | |
1310 | $params = array(); | |
1311 | $result = civicrm_api('activity', 'get', $params); | |
1312 | $this->assertEquals($result['is_error'], 1, "In line " . __LINE__); | |
1313 | } | |
1314 | ||
1315 | /** | |
1316 | * Test civicrm_activity_contact_get() with missing source_contact_id | |
1317 | */ | |
1318 | function testActivitiesContactGetWithInvalidParameter() { | |
1319 | $params = NULL; | |
1320 | $result = civicrm_api('activity', 'get', $params); | |
1321 | $this->assertEquals($result['is_error'], 1, | |
1322 | "In line " . __LINE__ | |
1323 | ); | |
1324 | } | |
1325 | ||
1326 | /** | |
1327 | * Test civicrm_activity_contact_get() with invalid Contact Id | |
1328 | */ | |
1329 | function testActivitiesContactGetWithInvalidContactId() { | |
1330 | $params = array('contact_id' => NULL); | |
1331 | $result = civicrm_api('activity', 'get', $params); | |
1332 | $this->assertEquals($result['is_error'], 1, | |
1333 | "In line " . __LINE__ | |
1334 | ); | |
1335 | ||
1336 | $params = array('contact_id' => 'contact'); | |
1337 | $result = civicrm_api('activity', 'get', $params); | |
1338 | $this->assertEquals($result['is_error'], 1, | |
1339 | "In line " . __LINE__ | |
1340 | ); | |
1341 | ||
1342 | $params = array('contact_id' => 2.4); | |
1343 | $result = civicrm_api('activity', 'get', $params); | |
1344 | $this->assertEquals($result['is_error'], 1, | |
1345 | "In line " . __LINE__ | |
1346 | ); | |
1347 | } | |
1348 | ||
1349 | /** | |
1350 | * Test civicrm_activity_contact_get() with contact having no Activity | |
1351 | */ | |
1352 | function testActivitiesContactGetHavingNoActivity() { | |
1353 | $params = array( | |
1354 | 'first_name' => 'dan', | |
1355 | 'last_name' => 'conberg', | |
1356 | 'email' => 'dan.conberg@w.co.in', | |
1357 | 'contact_type' => 'Individual', | |
1358 | 'version' => $this->_apiversion, | |
1359 | ); | |
1360 | ||
1361 | $contact = civicrm_api('contact', 'create', $params); | |
1362 | $params = array( | |
1363 | 'contact_id' => $contact['id'], | |
1364 | 'version' => $this->_apiversion, | |
1365 | ); | |
1366 | $result = civicrm_api('activity', 'get', $params); | |
1367 | $this->assertEquals($result['is_error'], 0, 'in line ' . __LINE__); | |
1368 | $this->assertEquals($result['count'], 0, 'in line ' . __LINE__); | |
1369 | } | |
1370 | ||
1371 | function testGetFields() { | |
1372 | $params = array('version' => 3, 'action' => 'create'); | |
1373 | $result = civicrm_api('activity', 'getfields', $params); | |
1374 | $this->documentMe($params, $result, __FUNCTION__, __FILE__, NULL, NULL, 'getfields'); | |
1375 | $this->assertTrue(is_array($result['values']), 'get fields doesnt return values array in line ' . __LINE__); | |
1376 | // $this->assertTrue(is_array($result['values']['priority_id']['options'])); | |
1377 | foreach ($result['values'] as $key => $value) { | |
1378 | $this->assertTrue(is_array($value), $key . " is not an array in line " . __LINE__); | |
1379 | } | |
1380 | } | |
1381 | } |