4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
30 require_once 'CiviTest/CiviUnitTestCase.php';
31 class api_v3_EventTest
extends CiviUnitTestCase
{
33 protected $_apiversion;
38 'name' => 'Event Create',
39 'description' => 'Test all Event Create API methods.',
40 'group' => 'CiviCRM API Tests',
46 $this->_apiversion
= 3;
47 $this->_entity
= 'event';
48 $this->_params
= array(
50 'title' => 'Annual CiviCRM meet',
51 'summary' => 'If you have any CiviCRM realted issues or want to track where CiviCRM is heading, Sign up now',
52 'description' => 'This event is intended to give brief idea about progess of CiviCRM and giving solutions to common user issues',
55 'start_date' => 20081021,
56 'end_date' => 20081023,
57 'is_online_registration' => 1,
58 'registration_start_date' => 20080601,
59 'registration_end_date' => '2008-10-15',
60 'max_participants' => 100,
61 'event_full_text' => 'Sorry! We are already full',
64 'is_show_location' => 0,
67 'title' => 'Annual CiviCRM meet 2',
68 'summary' => 'If you have any CiviCRM realted issues or want to track where CiviCRM is heading, Sign up now',
69 'description' => 'This event is intended to give brief idea about progess of CiviCRM and giving solutions to common user issues',
72 'start_date' => 20101021,
73 'end_date' => 20101023,
74 'is_online_registration' => 1,
75 'registration_start_date' => 20100601,
76 'registration_end_date' => '2010-10-15',
77 'max_participants' => 100,
78 'event_full_text' => 'Sorry! We are already full',
81 'is_show_location' => 0,
87 'title' => 'Annual CiviCRM meet',
89 'start_date' => 20081021, ),
91 'title' => 'Annual CiviCRM meet 2',
93 'start_date' => 20101021, ),
96 $this->events
= array();
97 $this->eventIds
= array();
98 foreach ($params as $event) {
99 $result = $this->callAPISuccess('Event', 'Create', $event);
100 $this->_events
[] = $result;
101 $this->_eventIds
[] = $result['id'];
105 function tearDown() {
106 foreach ($this->eventIds
as $eventId) {
107 $this->eventDelete($eventId);
109 $tablesToTruncate = array(
110 'civicrm_participant',
113 $this->quickCleanup($tablesToTruncate, TRUE);
116 ///////////////// civicrm_event_get methods
118 function testGetEventById() {
120 'id' => $this->_events
[1]['id'],);
121 $result = $this->callAPISuccess('event', 'get', $params);
122 $this->assertEquals($result['values'][$this->_eventIds
[1]]['event_title'], 'Annual CiviCRM meet 2');
125 function testGetEventByEventTitle() {
128 'event_title' => 'Annual CiviCRM meet',
129 'sequential' => TRUE,
132 $result = $this->callAPIAndDocument('event', 'get', $params, __FUNCTION__
, __FILE__
);
133 $this->assertEquals(1, $result['count']);
134 $this->assertEquals($result['values'][0]['id'], $this->_eventIds
[0]);
137 function testGetEventByWrongTitle() {
139 'title' => 'No event with that title',);
140 $result = $this->callAPISuccess('Event', 'Get', $params);
141 $this->assertEquals(0, $result['count']);
143 function testGetEventByIdSort() {
145 'return.sort' => 'id ASC',
146 'return.max_results' => 1, );
147 $result = $this->callAPISuccess('Event', 'Get', $params);
148 $this->assertEquals(1, $result['id'], ' in line ' . __LINE__
);
155 $result = $this->callAPISuccess('Event', 'Get', $params);
156 $this->assertAPISuccess($result, ' in line ' . __LINE__
);
157 $this->assertEquals(2, $result['id'], ' in line ' . __LINE__
);
163 $result = $this->callAPISuccess('Event', 'Get', $params);
164 $this->assertEquals(1, $result['id'], ' in line ' . __LINE__
);
169 * Getting the id back of an event.
170 * Does not work yet, bug in API
174 function testGetIdOfEventByEventTitle() {
175 $params = array( 'title' => 'Annual CiviCRM meet',
179 $result = $this->callAPISuccess('Event', 'Get', $params);
185 * Test 'is.Current' option. Existing event is 'old' so only current should be returned
187 function testGetIsCurrent() {
191 $currentEventParams = array('start_date' => date('Y-m-d', strtotime('+ 1 day')),
192 'end_date' => date('Y-m-d', strtotime('+ 1 week')),
194 $currentEventParams = array_merge($this->_params
[1], $currentEventParams);
195 $currentEvent = $this->callAPISuccess('Event', 'Create', $currentEventParams);
196 $description = "demonstrates use of is.Current option";
197 $subfile = "IsCurrentOption";
198 $result = $this->callAPIAndDocument('Event', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
199 $allEvents = $this->callAPISuccess('Event', 'Get', array());
200 $this->callAPISuccess('Event', 'Delete', array('id' => $currentEvent['id']));
201 $this->assertEquals(1, $result['count'], 'confirm only one event found in line ' . __LINE__
);
202 $this->assertEquals(3, $allEvents['count'], 'confirm three events exist (ie. two not found) ' . __LINE__
);
203 $this->assertEquals($currentEvent['id'], $result['id'], '');
206 * There has been a schema change & the api needs to buffer developers from it
208 function testGetPaymentProcessorId() {
209 $params = $this->_params
[0];
210 $params['payment_processor_id'] = 1;
211 $params['sequential'] =1;
212 $result = $this->callAPISuccess('event', 'create', $params);
213 $this->assertEquals( 1,$result['values'][0]['payment_processor'][0], "handing of payment processor compatibility");
214 $result = $this->callAPISuccess('event', 'get', $params);
215 $this->assertEquals($result['values'][0]['payment_processor_id'], 1,"handing get payment processor compatibility");
218 function testInvalidData() {
219 $params = $this->_params
[0];
220 $params['sequential'] =1;
221 $params['loc_block_id'] =100;
222 $result = $this->callAPIFailure('event', 'create', $params);
227 * Test 'is.Current' option. Existing event is 'old' so only current should be returned
229 function testGetSingleReturnIsFull() {
230 $contactID = $this->individualCreate();
232 'id' => $this->_eventIds
[0], 'max_participants' => 1,
234 $result = $this->callAPISuccess('Event', 'Create', $params);
236 $getEventParams = array(
237 'id' => $this->_eventIds
[0], 'return.is_full' => 1,
240 $currentEvent = $this->callAPISuccess('Event', 'getsingle', $getEventParams);
241 $description = "demonstrates use of return is_full ";
242 $subfile = "IsFullOption";
243 $this->assertEquals(0, $currentEvent['is_full'], ' is full is set in line ' . __LINE__
);
244 $this->assertEquals(1, $currentEvent['available_places'], 'available places is set in line ' . __LINE__
);
245 $participant = $this->callAPISuccess('Participant', 'create', array('participant_status' => 1, 'role_id' => 1, 'contact_id' => $contactID, 'event_id' => $this->_eventIds
[0]));
246 $currentEvent = $this->callAPIAndDocument('Event', 'getsingle', $getEventParams, __FUNCTION__
, __FILE__
, $description, $subfile, 'getsingle');
247 $this->assertEquals(1, $currentEvent['is_full'], ' is full is set in line ' . __LINE__
);
248 $this->assertEquals(0, $currentEvent['available_places'], 'available places is set in line ' . __LINE__
);
250 $this->contactDelete($contactID);
253 * Legacy support for Contribution Type ID. We need to ensure this is supported
254 * as an alias for financial_type_id
256 function testCreateGetEventLegacyContributionTypeID() {
257 $contributionTypeArray = array('contribution_type_id' => 3);
258 if(isset($this->_params
[0]['financial_type_id'])){
259 //in case someone edits $this->_params & invalidates this test :-)
260 unset($this->_params
[0]['financial_type_id']);
262 $result = $this->callAPISuccess('event', 'create', $this->_params
[0] +
$contributionTypeArray);
263 $getresult = $this->callAPISuccess('event', 'get', array() +
$contributionTypeArray);
264 $this->assertEquals($getresult['values'][$getresult['id']]['contribution_type_id'], 3);
265 $this->assertEquals($result['id'], $getresult['id']);
266 $this->callAPISuccess('event', 'delete', array('id' => $result['id']));
268 ///////////////// civicrm_event_create methods
271 * check with complete array + custom field
272 * Note that the test is written on purpose without any
273 * variables specific to participant so it can be replicated into other entities
274 * and / or moved to the automated test suite
276 function testCreateWithCustom() {
277 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
279 $params = $this->_params
[0];
280 $params['custom_' . $ids['custom_field_id']] = "custom string";
282 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
);
284 $check = $this->callAPISuccess($this->_entity
, 'get', array('return.custom_' . $ids['custom_field_id'] => 1, 'id' => $result['id']));
285 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
287 $this->customFieldDelete($ids['custom_field_id']);
288 $this->customGroupDelete($ids['custom_group_id']);
289 $this->callAPISuccess($this->_entity
, 'Delete', array('id' => $result['id']));
292 function testCreateEventParamsNotArray() {
294 $result = $this->callAPIFailure('event', 'create', $params);
297 function testCreateEventEmptyParams() {
299 $result = $this->callAPIFailure('event', 'create', $params);
302 function testCreateEventParamsWithoutTitle() {
303 unset($this->_params
['title']);
304 $result = $this->callAPIFailure('event', 'create', $this->_params
);
305 $this->assertAPIFailure($result);
308 function testCreateEventParamsWithoutEventTypeId() {
309 unset($this->_params
['event_type_id']);
310 $result = $this->callAPIFailure('event', 'create', $this->_params
);
313 function testCreateEventParamsWithoutStartDate() {
314 unset($this->_params
['start_date']);
315 $result = $this->callAPIFailure('event', 'create', $this->_params
);
318 function testCreateEventSuccess() {
319 $result = $this->callAPIAndDocument('Event', 'Create', $this->_params
[0], __FUNCTION__
, __FILE__
);
320 $this->assertArrayHasKey('id', $result['values'][$result['id']], 'In line ' . __LINE__
);
321 $result = $this->callAPISuccess($this->_entity
, 'Get', array('id' => $result['id']));
322 $this->callAPISuccess($this->_entity
, 'Delete', array('id' => $result['id']));
323 $this->assertEquals('2008-10-21 00:00:00', $result['values'][$result['id']]['start_date'], 'start date is not set in line ' . __LINE__
);
324 $this->assertEquals('2008-10-23 00:00:00', $result['values'][$result['id']]['end_date'], 'end date is not set in line ' . __LINE__
);
325 $this->assertEquals('2008-06-01 00:00:00', $result['values'][$result['id']]['registration_start_date'], 'start date is not set in line ' . __LINE__
);
326 $this->assertEquals('2008-10-15 00:00:00', $result['values'][$result['id']]['registration_end_date'], 'end date is not set in line ' . __LINE__
);
329 * Test that passing in Unique field names works
331 function testCreateEventSuccessUniqueFieldNames() {
332 $this->_params
[0]['event_start_date'] = $this->_params
[0]['start_date'];
333 unset($this->_params
[1]['start_date']);
334 $this->_params
[0]['event_title'] = $this->_params
[0]['title'];
335 unset($this->_params
[0]['title']);
336 $result = $this->callAPISuccess('Event', 'Create', $this->_params
[0]);
337 $this->assertAPISuccess($result, 'In line ' . __LINE__
);
338 $this->assertArrayHasKey('id', $result['values'][$result['id']], 'In line ' . __LINE__
);
339 $result = $this->callAPISuccess($this->_entity
, 'Get', array('id' => $result['id']));
340 $this->callAPISuccess($this->_entity
, 'Delete', array('id' => $result['id']));
342 $this->assertEquals('2008-10-21 00:00:00', $result['values'][$result['id']]['start_date'], 'start date is not set in line ' . __LINE__
);
343 $this->assertEquals('2008-10-23 00:00:00', $result['values'][$result['id']]['end_date'], 'end date is not set in line ' . __LINE__
);
344 $this->assertEquals('2008-06-01 00:00:00', $result['values'][$result['id']]['registration_start_date'], 'start date is not set in line ' . __LINE__
);
345 $this->assertEquals('2008-10-15 00:00:00', $result['values'][$result['id']]['registration_end_date'], 'end date is not set in line ' . __LINE__
);
346 $this->assertEquals($this->_params
[0]['event_title'], $result['values'][$result['id']]['title'], 'end date is not set in line ' . __LINE__
);
349 function testUpdateEvent() {
350 $result = $this->callAPISuccess('event', 'create', $this->_params
[1]);
353 'id' => $result['id'], 'max_participants' => 150,
355 $this->callAPISuccess('Event', 'Create', $params);
356 $updated = $this->callAPISuccess('Event', 'Get', $params, __FUNCTION__
, __FILE__
);
357 $this->assertEquals(150, $updated['values'][$result['id']]['max_participants']);
358 $this->assertEquals('Annual CiviCRM meet 2', $updated['values'][$result['id']]['title']);
359 $this->callAPISuccess($this->_entity
, 'Delete', array('id' => $result['id']));
363 function testDeleteEmptyParams() {
364 $result = $this->callAPIFailure('Event', 'Delete', array());
367 function testDelete() {
369 'id' => $this->_eventIds
[0],
371 $result = $this->callAPIAndDocument('Event', 'Delete', $params, __FUNCTION__
, __FILE__
);
375 * check event_id still supported for delete
377 function testDeleteWithEventId() {
379 'event_id' => $this->_eventIds
[0], );
380 $result = $this->callAPISuccess('Event', 'Delete', $params);
381 $this->assertAPISuccess($result, 'in line ' . __LINE__
);
384 * Trying to delete an event with participants should return error
386 function testDeleteWithExistingParticipant() {
387 $contactID = $this->individualCreate();
388 $participantID = $this->participantCreate(
390 'contactID' => $contactID,
391 'eventID' => $this->_eventIds
[0],
394 $result = $this->callAPISuccess('Event', 'Delete', array('id' => $this->_eventIds
[0]));
397 function testDeleteWithWrongEventId() {
398 $params = array('event_id' => $this->_eventIds
[0]);
399 $result = $this->callAPISuccess('Event', 'Delete', $params);
400 // try to delete again - there's no such event anymore
401 $params = array('event_id' => $this->_eventIds
[0]);
402 $result = $this->callAPIFailure('Event', 'Delete', $params);
405 ///////////////// civicrm_event_search methods
408 * Test civicrm_event_search with wrong params type
410 function testSearchWrongParamsType() {
411 $params = 'a string';
412 $result = $this->callAPIFailure('event', 'get', $params);
416 * Test civicrm_event_search with empty params
418 function testSearchEmptyParams() {
419 $event = $this->callAPISuccess('event', 'create', $this->_params
[1]);
421 $getparams = array( 'sequential' => 1,
423 $result = $this->callAPISuccess('event', 'get', $getparams);
424 $this->assertEquals($result['count'], 3, 'In line ' . __LINE__
);
425 $res = $result['values'][0];
426 $this->assertArrayKeyExists('title', $res, 'In line ' . __LINE__
);
427 $this->assertEquals($res['event_type_id'], $this->_params
[1]['event_type_id'], 'In line ' . __LINE__
);
431 * Test civicrm_event_search. Success expected.
433 function testSearch() {
435 'event_type_id' => 1,
438 'return.start_date' => 1, );
439 $result = $this->callAPISuccess('event', 'get', $params);
441 $this->assertEquals($result['values'][$this->_eventIds
[0]]['id'], $this->_eventIds
[0], 'In line ' . __LINE__
);
442 $this->assertEquals($result['values'][$this->_eventIds
[0]]['title'], 'Annual CiviCRM meet', 'In line ' . __LINE__
);
446 * Test civicrm_event_search. Success expected.
447 * return.offset and return.max_results test (CRM-5266)
449 function testSearchWithOffsetAndMaxResults() {
452 while ($maxEvents > 0) {
453 $params = array( 'title' => 'Test Event' . $maxEvents,
454 'event_type_id' => 2,
455 'start_date' => 20081021,
458 $events[$maxEvents] = $this->callAPISuccess('event', 'create', $params);
461 $params = array( 'event_type_id' => 2,
464 'return.offset' => 2,
465 'return.max_results' => 2,
467 $result = $this->callAPISuccess('event', 'get', $params);
468 $this->assertAPISuccess($result);
469 $this->assertEquals(2, $result['count'], ' 2 results returned In line ' . __LINE__
);
472 function testEventCreationPermissions() {
474 'event_type_id' => 1, 'start_date' => '2010-10-03', 'title' => 'le cake is a tie', 'check_permissions' => TRUE, );
475 $config = &CRM_Core_Config
::singleton();
476 $config->userPermissionClass
->permissions
= array('access CiviCRM');
477 $result = $this->callAPIFailure('event', 'create', $params);
478 $this->assertEquals('API permission check failed for event/create call; insufficient permission: require access CiviCRM and access CiviEvent and edit all events', $result['error_message'], 'lacking permissions should not be enough to create an event');
480 $config->userPermissionClass
->permissions
= array('access CiviEvent', 'edit all events', 'access CiviCRM');
481 $result = $this->callAPISuccess('event', 'create', $params);
484 function testgetfields() {
485 $description = "demonstrate use of getfields to interogate api";
486 $params = array('action' => 'create');
487 $result = $this->callAPISuccess('event', 'getfields', $params);
488 $this->assertEquals(1, $result['values']['title']['api.required'], 'in line ' . __LINE__
);
491 * test api_action param also works
493 function testgetfieldsRest() {
494 $description = "demonstrate use of getfields to interogate api";
495 $params = array('api_action' => 'create');
496 $result = $this->callAPISuccess('event', 'getfields', $params);
497 $this->assertEquals(1, $result['values']['title']['api.required'], 'in line ' . __LINE__
);
499 function testgetfieldsGet() {
500 $description = "demonstrate use of getfields to interogate api";
501 $params = array('action' => 'get');
502 $result = $this->callAPISuccess('event', 'getfields', $params);
503 $this->assertEquals('title', $result['values']['event_title']['name'], 'in line ' . __LINE__
);
505 function testgetfieldsDelete() {
506 $description = "demonstrate use of getfields to interogate api";
507 $params = array('action' => 'delete');
508 $result = $this->callAPISuccess('event', 'getfields', $params);
509 $this->assertEquals(1, $result['values']['id']['api.required']);