4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
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,
65 'version' => $this->_apiversion
,
68 'title' => 'Annual CiviCRM meet 2',
69 'summary' => 'If you have any CiviCRM realted issues or want to track where CiviCRM is heading, Sign up now',
70 'description' => 'This event is intended to give brief idea about progess of CiviCRM and giving solutions to common user issues',
73 'start_date' => 20101021,
74 'end_date' => 20101023,
75 'is_online_registration' => 1,
76 'registration_start_date' => 20100601,
77 'registration_end_date' => '2010-10-15',
78 'max_participants' => 100,
79 'event_full_text' => 'Sorry! We are already full',
82 'is_show_location' => 0,
83 'version' => $this->_apiversion
,
89 'title' => 'Annual CiviCRM meet',
91 'start_date' => 20081021,
92 'version' => $this->_apiversion
,
95 'title' => 'Annual CiviCRM meet 2',
97 'start_date' => 20101021,
98 'version' => $this->_apiversion
,
102 $this->events
= array();
103 $this->eventIds
= array();
104 foreach ($params as $event) {
105 $result = civicrm_api('Event', 'Create', $event);
106 $this->_events
[] = $result;
107 $this->_eventIds
[] = $result['id'];
111 function tearDown() {
112 foreach ($this->eventIds
as $eventId) {
113 $this->eventDelete($eventId);
117 if ($this->_eventId) {
118 $this->eventDelete($this->_eventId);
120 $this->eventDelete($this->_event['id']);
124 $tablesToTruncate = array(
125 'civicrm_participant',
128 $this->quickCleanup($tablesToTruncate, TRUE);
131 ///////////////// civicrm_event_get methods
133 function testGetEventById() {
135 'id' => $this->_events
[1]['id'],
136 'version' => $this->_apiversion
,
138 $result = civicrm_api('event', 'get', $params);
139 $this->assertEquals($result['values'][$this->_eventIds
[1]]['event_title'], 'Annual CiviCRM meet 2');
142 function testGetEventByEventTitle() {
145 'event_title' => 'Annual CiviCRM meet',
146 'version' => $this->_apiversion
,
149 $result = civicrm_api('event', 'get', $params);
150 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
151 $this->assertEquals(1, $result['count']);
152 $this->assertEquals($result['values'][0]['id'], $this->_eventIds
[0]['id']);
155 function testGetEventByWrongTitle() {
157 'title' => 'No event with that title',
158 'version' => $this->_apiversion
,
160 $result = civicrm_api('Event', 'Get', $params);
161 $this->assertEquals(0, $result['count']);
163 function testGetEventByIdSort() {
165 'return.sort' => 'id ASC',
166 'return.max_results' => 1,
167 'version' => $this->_apiversion
,
169 $result = civicrm_api('Event', 'Get', $params);
170 $this->assertEquals(1, $result['id'], ' in line ' . __LINE__
);
176 'version' => $this->_apiversion
,
179 $result = civicrm_api('Event', 'Get', $params);
180 $this->assertAPISuccess($result, ' in line ' . __LINE__
);
181 $this->assertEquals(2, $result['id'], ' in line ' . __LINE__
);
187 'version' => $this->_apiversion
,
189 $result = civicrm_api('Event', 'Get', $params);
190 $this->assertEquals(1, $result['id'], ' in line ' . __LINE__
);
195 * Getting the id back of an event.
196 * Does not work yet, bug in API
200 function testGetIdOfEventByEventTitle() {
202 'version' => $this->_apiversion,
203 'title' => 'Annual CiviCRM meet',
207 $result = civicrm_api('Event', 'Get', $params);
213 * Test 'is.Current' option. Existing event is 'old' so only current should be returned
215 function testGetIsCurrent() {
217 'version' => $this->_apiversion
,
220 $currentEventParams = array('start_date' => date('Y-m-d', strtotime('+ 1 day')),
221 'end_date' => date('Y-m-d', strtotime('+ 1 week')),
223 $currentEventParams = array_merge($this->_params
[1], $currentEventParams);
224 $currentEvent = civicrm_api('Event', 'Create', $currentEventParams);
225 $description = "demonstrates use of is.Current option";
226 $subfile = "IsCurrentOption";
227 $result = civicrm_api('Event', 'Get', $params);
229 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
, $description, $subfile);
230 $allEvents = civicrm_api('Event', 'Get', array('version' => 3));
231 civicrm_api('Event', 'Delete', array('version' => 3, 'id' => $currentEventParams['id']));
232 $this->assertEquals(1, $result['count'], 'confirm only one event found in line ' . __LINE__
);
233 $this->assertEquals(3, $allEvents['count'], 'confirm three events exist (ie. two not found) ' . __LINE__
);
234 $this->assertEquals($currentEvent['id'], $result['id'], '');
237 * There has been a schema change & the api needs to buffer developers from it
239 function testGetPaymentProcessorId() {
240 $params = $this->_params
[0];
241 $params['payment_processor_id'] = 1;
242 $params['sequential'] =1;
243 $result = civicrm_api('event', 'create', $params);
244 $this->assertEquals( 1,$result['values'][0]['payment_processor'][0], "handing of payment processor compatibility");
245 $result = civicrm_api('event', 'get', $params);
246 $this->assertEquals($result['values'][0]['payment_processor_id'], 1,"handing get payment processor compatibility");
249 function testInvalidData() {
250 $params = $this->_params
[0];
251 $params['sequential'] =1;
252 $params['loc_block_id'] =100;
253 $result = $this->callAPIFailure('event', 'create', $params);
258 * Test 'is.Current' option. Existing event is 'old' so only current should be returned
260 function testGetSingleReturnIsFull() {
261 $contactID = $this->individualCreate();
263 'id' => $this->_eventIds
[0],
264 'version' => $this->_apiversion
,
265 'max_participants' => 1,
267 $result = civicrm_api('Event', 'Create', $params);
269 $getEventParams = array(
270 'id' => $this->_eventIds
[0],
271 'version' => $this->_apiversion
,
272 'return.is_full' => 1,
275 $currentEvent = civicrm_api('Event', 'getsingle', $getEventParams);
276 $description = "demonstrates use of return is_full ";
277 $subfile = "IsFullOption";
278 $this->assertEquals(0, $currentEvent['is_full'], ' is full is set in line ' . __LINE__
);
279 $this->assertEquals(1, $currentEvent['available_places'], 'available places is set in line ' . __LINE__
);
280 $participant = civicrm_api('Participant', 'create', array('version' => 3, 'participant_status' => 1, 'role_id' => 1, 'contact_id' => $contactID, 'event_id' => $this->_eventIds
[0]));
281 $currentEvent = civicrm_api('Event', 'getsingle', $getEventParams);
283 $this->documentMe($getEventParams, $currentEvent, __FUNCTION__
, __FILE__
, $description, $subfile, 'getsingle');
284 $this->assertEquals(1, $currentEvent['is_full'], ' is full is set in line ' . __LINE__
);
285 $this->assertEquals(0, $currentEvent['available_places'], 'available places is set in line ' . __LINE__
);
287 $this->contactDelete($contactID);
290 * Legacy support for Contribution Type ID. We need to ensure this is supported
291 * as an alias for financial_type_id
293 function testCreateGetEventLegacyContributionTypeID() {
294 $contributionTypeArray = array('contribution_type_id' => 3);
295 if(isset($this->_params
[0]['financial_type_id'])){
296 //in case someone edits $this->_params & invalidates this test :-)
297 unset($this->_params
[0]['financial_type_id']);
299 $result = civicrm_api('event', 'create', $this->_params
[0] +
$contributionTypeArray);
300 $this->assertAPISuccess($result, ' Event Creation Failedon line ' . __LINE__
);
301 $getresult = civicrm_api('event', 'get', array('version' => 3,) +
$contributionTypeArray);
302 $this->assertAPISuccess($result, ' Event Creation on line ' . __LINE__
);
303 $this->assertEquals($getresult['values'][$getresult['id']]['contribution_type_id'], 3);
304 $this->assertEquals($result['id'], $getresult['id']);
305 civicrm_api('event', 'delete', array('version' => 3, 'id' => $result['id']));
307 ///////////////// civicrm_event_create methods
310 * check with complete array + custom field
311 * Note that the test is written on purpose without any
312 * variables specific to participant so it can be replicated into other entities
313 * and / or moved to the automated test suite
315 function testCreateWithCustom() {
316 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
318 $params = $this->_params
[0];
319 $params['custom_' . $ids['custom_field_id']] = "custom string";
321 $result = civicrm_api($this->_entity
, 'create', $params);
322 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
323 $this->assertNotEquals($result['is_error'], 1, $result['error_message'] . ' in line ' . __LINE__
);
325 $check = civicrm_api($this->_entity
, 'get', array('version' => 3, 'return.custom_' . $ids['custom_field_id'] => 1, 'id' => $result['id']));
326 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
328 $this->customFieldDelete($ids['custom_field_id']);
329 $this->customGroupDelete($ids['custom_group_id']);
330 civicrm_api($this->_entity
, 'Delete', array('version' => 3, 'id' => $result['id']));
333 function testCreateEventParamsNotArray() {
335 $result = $this->callAPIFailure('event', 'create', $params);
338 function testCreateEventEmptyParams() {
340 $result = $this->callAPIFailure('event', 'create', $params);
343 function testCreateEventParamsWithoutTitle() {
344 unset($this->_params
['title']);
345 $result = civicrm_api('event', 'create', $this->_params
);
346 $this->assertAPIFailure($result);
349 function testCreateEventParamsWithoutEventTypeId() {
350 unset($this->_params
['event_type_id']);
351 $result = civicrm_api('event', 'create', $this->_params
);
352 $this->assertAPIFailure($result);
355 function testCreateEventParamsWithoutStartDate() {
356 unset($this->_params
['start_date']);
357 $result = civicrm_api('event', 'create', $this->_params
);
358 $this->assertAPIFailure($result);
361 function testCreateEventSuccess() {
362 $result = civicrm_api('Event', 'Create', $this->_params
[0]);
363 $this->documentMe($this->_params
[0], $result, __FUNCTION__
, __FILE__
);
364 $this->assertAPISuccess($result);
365 $this->assertArrayHasKey('id', $result['values'][$result['id']], 'In line ' . __LINE__
);
366 $result = civicrm_api($this->_entity
, 'Get', array('version' => 3, 'id' => $result['id']));
367 civicrm_api($this->_entity
, 'Delete', array('version' => 3, 'id' => $result['id']));
369 $this->assertEquals('2008-10-21 00:00:00', $result['values'][$result['id']]['start_date'], 'start date is not set in line ' . __LINE__
);
370 $this->assertEquals('2008-10-23 00:00:00', $result['values'][$result['id']]['end_date'], 'end date is not set in line ' . __LINE__
);
371 $this->assertEquals('2008-06-01 00:00:00', $result['values'][$result['id']]['registration_start_date'], 'start date is not set in line ' . __LINE__
);
372 $this->assertEquals('2008-10-15 00:00:00', $result['values'][$result['id']]['registration_end_date'], 'end date is not set in line ' . __LINE__
);
373 civicrm_api($this->_entity
, 'Delete', array('version' => 3, 'id' => $result['id']));
376 * Test that passing in Unique field names works
378 function testCreateEventSuccessUniqueFieldNames() {
379 $this->_params
[0]['event_start_date'] = $this->_params
[0]['start_date'];
380 unset($this->_params
[1]['start_date']);
381 $this->_params
[0]['event_title'] = $this->_params
[0]['title'];
382 unset($this->_params
[0]['title']);
383 $result = civicrm_api('Event', 'Create', $this->_params
[0]);
384 $this->assertAPISuccess($result, 'In line ' . __LINE__
);
385 $this->assertArrayHasKey('id', $result['values'][$result['id']], 'In line ' . __LINE__
);
386 $result = civicrm_api($this->_entity
, 'Get', array('version' => 3, 'id' => $result['id']));
387 civicrm_api($this->_entity
, 'Delete', array('version' => 3, 'id' => $result['id']));
389 $this->assertEquals('2008-10-21 00:00:00', $result['values'][$result['id']]['start_date'], 'start date is not set in line ' . __LINE__
);
390 $this->assertEquals('2008-10-23 00:00:00', $result['values'][$result['id']]['end_date'], 'end date is not set in line ' . __LINE__
);
391 $this->assertEquals('2008-06-01 00:00:00', $result['values'][$result['id']]['registration_start_date'], 'start date is not set in line ' . __LINE__
);
392 $this->assertEquals('2008-10-15 00:00:00', $result['values'][$result['id']]['registration_end_date'], 'end date is not set in line ' . __LINE__
);
393 $this->assertEquals($this->_params
[0]['event_title'], $result['values'][$result['id']]['title'], 'end date is not set in line ' . __LINE__
);
395 civicrm_api($this->_entity
, 'Delete', array('version' => 3, 'id' => $result['id']));
398 function testUpdateEvent() {
399 $result = civicrm_api('event', 'create', $this->_params
[1]);
401 $this->assertAPISuccess($result);
403 'id' => $result['id'], 'version' => 3, 'max_participants' => 150,
405 civicrm_api('Event', 'Create', $params);
406 $updated = civicrm_api('Event', 'Get', $params);
407 $this->documentMe($this->_params
, $updated, __FUNCTION__
, __FILE__
);
408 $this->assertEquals($updated['is_error'], 0);
409 $this->assertEquals(150, $updated['values'][$result['id']]['max_participants']);
410 $this->assertEquals('Annual CiviCRM meet 2', $updated['values'][$result['id']]['title']);
411 civicrm_api($this->_entity
, 'Delete', array('version' => 3, 'id' => $result['id']));
414 ///////////////// civicrm_event_delete methods
415 function testDeleteWrongParamsType() {
416 $params = 'Annual CiviCRM meet';
417 $result = $this->callAPIFailure('Event', 'Delete', $params);
420 function testDeleteEmptyParams() {
422 $result = $this->callAPIFailure('Event', 'Delete', $params);
425 function testDelete() {
427 'id' => $this->_eventIds
[0],
428 'version' => $this->_apiversion
,
430 $result = civicrm_api('Event', 'Delete', $params);
431 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
432 $this->assertNotEquals($result['is_error'], 1);
435 * check event_id still supported for delete
437 function testDeleteWithEventId() {
439 'event_id' => $this->_eventIds
[0],
440 'version' => $this->_apiversion
,
442 $result = civicrm_api('Event', 'Delete', $params);
443 $this->assertAPISuccess($result, 'in line ' . __LINE__
);
446 * Trying to delete an event with participants should return error
448 function testDeleteWithExistingParticipant() {
449 $contactID = $this->individualCreate(NULL);
450 $participantID = $this->participantCreate(
452 'contactID' => $contactID,
453 'eventID' => $this->_eventIds
[0],
456 $result = civicrm_api('Event', 'Delete', array('version' => $this->_apiversion
, 'id' => $this->_eventIds
[0]));
457 $this->assertEquals(0, $result['is_error'], "Deleting exist with participants");
460 function testDeleteWithWrongEventId() {
461 $params = array('event_id' => $this->_eventIds
[0], 'version' => $this->_apiversion
);
462 $result = civicrm_api('Event', 'Delete', $params);
463 // try to delete again - there's no such event anymore
464 $params = array('event_id' => $this->_eventIds
[0]);
465 $result = $this->callAPIFailure('Event', 'Delete', $params);
468 ///////////////// civicrm_event_search methods
471 * Test civicrm_event_search with wrong params type
473 function testSearchWrongParamsType() {
474 $params = 'a string';
475 $result = $this->callAPIFailure('event', 'get', $params);
479 * Test civicrm_event_search with empty params
481 function testSearchEmptyParams() {
482 $event = civicrm_api('event', 'create', $this->_params
[1]);
485 'version' => $this->_apiversion
,
488 $result = civicrm_api('event', 'get', $getparams);
489 $this->assertEquals($result['count'], 3, 'In line ' . __LINE__
);
490 $res = $result['values'][0];
491 $this->assertArrayKeyExists('title', $res, 'In line ' . __LINE__
);
492 $this->assertEquals($res['event_type_id'], $this->_params
[1]['event_type_id'], 'In line ' . __LINE__
);
496 * Test civicrm_event_search. Success expected.
498 function testSearch() {
500 'event_type_id' => 1,
503 'return.start_date' => 1,
504 'version' => $this->_apiversion
,
506 $result = civicrm_api('event', 'get', $params);
508 $this->assertEquals($result['values'][$this->_eventIds
[0]]['id'], $this->_eventIds
[0], 'In line ' . __LINE__
);
509 $this->assertEquals($result['values'][$this->_eventIds
[0]]['title'], 'Annual CiviCRM meet', 'In line ' . __LINE__
);
513 * Test civicrm_event_search. Success expected.
514 * return.offset and return.max_results test (CRM-5266)
516 function testSearchWithOffsetAndMaxResults() {
519 while ($maxEvents > 0) {
521 'version' => $this->_apiversion
,
522 'title' => 'Test Event' . $maxEvents,
523 'event_type_id' => 2,
524 'start_date' => 20081021,
527 $events[$maxEvents] = civicrm_api('event', 'create', $params);
531 'version' => $this->_apiversion
,
532 'event_type_id' => 2,
535 'return.offset' => 2,
536 'return.max_results' => 2,
538 $result = civicrm_api('event', 'get', $params);
539 $this->assertAPISuccess($result);
540 $this->assertEquals(2, $result['count'], ' 2 results returned In line ' . __LINE__
);
543 function testEventCreationPermissions() {
545 'event_type_id' => 1, 'start_date' => '2010-10-03', 'title' => 'le cake is a tie', 'check_permissions' => TRUE,
546 'version' => $this->_apiversion
,
548 $config = &CRM_Core_Config
::singleton();
549 $config->userPermissionClass
->permissions
= array('access CiviCRM');
550 $result = $this->callAPIFailure('event', 'create', $params);
551 $this->assertEquals('API permission check failed for event/create call; missing permission: access CiviEvent.', $result['error_message'], 'lacking permissions should not be enough to create an event');
553 $config->userPermissionClass
->permissions
= array('access CiviEvent', 'edit all events', 'access CiviCRM');
554 $result = civicrm_api('event', 'create', $params);
555 $this->assertEquals(0, $result['is_error'], 'overfluous permissions should be enough to create an event');
558 function testgetfields() {
559 $description = "demonstrate use of getfields to interogate api";
560 $params = array('version' => 3, 'action' => 'create');
561 $result = civicrm_api('event', 'getfields', $params);
562 $this->assertEquals(1, $result['values']['title']['api.required'], 'in line ' . __LINE__
);
565 * test api_action param also works
567 function testgetfieldsRest() {
568 $description = "demonstrate use of getfields to interogate api";
569 $params = array('version' => 3, 'api_action' => 'create');
570 $result = civicrm_api('event', 'getfields', $params);
571 $this->assertEquals(1, $result['values']['title']['api.required'], 'in line ' . __LINE__
);
573 function testgetfieldsGet() {
574 $description = "demonstrate use of getfields to interogate api";
575 $params = array('version' => 3, 'action' => 'get');
576 $result = civicrm_api('event', 'getfields', $params);
577 $this->assertEquals('title', $result['values']['event_title']['name'], 'in line ' . __LINE__
);
579 function testgetfieldsDelete() {
580 $description = "demonstrate use of getfields to interogate api";
581 $params = array('version' => 3, 'action' => 'delete');
582 $result = civicrm_api('event', 'getfields', $params);
583 $this->assertEquals(1, $result['values']['id']['api.required']);