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;
35 public $_eNoticeCompliant = TRUE;
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 = $this->callAPISuccess('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 'sequential' => TRUE,
149 $result = $this->callAPIAndDocument('event', 'get', $params, __FUNCTION__
, __FILE__
);
150 $this->assertEquals(1, $result['count']);
151 $this->assertEquals($result['values'][0]['id'], $this->_eventIds
[0]);
154 function testGetEventByWrongTitle() {
156 'title' => 'No event with that title',
157 'version' => $this->_apiversion
,
159 $result = civicrm_api('Event', 'Get', $params);
160 $this->assertEquals(0, $result['count']);
162 function testGetEventByIdSort() {
164 'return.sort' => 'id ASC',
165 'return.max_results' => 1,
166 'version' => $this->_apiversion
,
168 $result = civicrm_api('Event', 'Get', $params);
169 $this->assertEquals(1, $result['id'], ' in line ' . __LINE__
);
175 'version' => $this->_apiversion
,
178 $result = civicrm_api('Event', 'Get', $params);
179 $this->assertAPISuccess($result, ' in line ' . __LINE__
);
180 $this->assertEquals(2, $result['id'], ' in line ' . __LINE__
);
186 'version' => $this->_apiversion
,
188 $result = civicrm_api('Event', 'Get', $params);
189 $this->assertEquals(1, $result['id'], ' in line ' . __LINE__
);
194 * Getting the id back of an event.
195 * Does not work yet, bug in API
199 function testGetIdOfEventByEventTitle() {
201 'version' => $this->_apiversion,
202 'title' => 'Annual CiviCRM meet',
206 $result = civicrm_api('Event', 'Get', $params);
212 * Test 'is.Current' option. Existing event is 'old' so only current should be returned
214 function testGetIsCurrent() {
218 $currentEventParams = array('start_date' => date('Y-m-d', strtotime('+ 1 day')),
219 'end_date' => date('Y-m-d', strtotime('+ 1 week')),
221 $currentEventParams = array_merge($this->_params
[1], $currentEventParams);
222 $currentEvent = $this->callAPISuccess('Event', 'Create', $currentEventParams);
223 $description = "demonstrates use of is.Current option";
224 $subfile = "IsCurrentOption";
225 $result = $this->callAPIAndDocument('Event', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
226 $allEvents = $this->callAPISuccess('Event', 'Get', array('version' => 3));
227 $this->callAPISuccess('Event', 'Delete', array('version' => 3, 'id' => $currentEvent['id']));
228 $this->assertEquals(1, $result['count'], 'confirm only one event found in line ' . __LINE__
);
229 $this->assertEquals(3, $allEvents['count'], 'confirm three events exist (ie. two not found) ' . __LINE__
);
230 $this->assertEquals($currentEvent['id'], $result['id'], '');
233 * There has been a schema change & the api needs to buffer developers from it
235 function testGetPaymentProcessorId() {
236 $params = $this->_params
[0];
237 $params['payment_processor_id'] = 1;
238 $params['sequential'] =1;
239 $result = civicrm_api('event', 'create', $params);
240 $this->assertEquals( 1,$result['values'][0]['payment_processor'][0], "handing of payment processor compatibility");
241 $result = civicrm_api('event', 'get', $params);
242 $this->assertEquals($result['values'][0]['payment_processor_id'], 1,"handing get payment processor compatibility");
245 function testInvalidData() {
246 $params = $this->_params
[0];
247 $params['sequential'] =1;
248 $params['loc_block_id'] =100;
249 $result = $this->callAPIFailure('event', 'create', $params);
254 * Test 'is.Current' option. Existing event is 'old' so only current should be returned
256 function testGetSingleReturnIsFull() {
257 $contactID = $this->individualCreate();
259 'id' => $this->_eventIds
[0],
260 'version' => $this->_apiversion
,
261 'max_participants' => 1,
263 $result = civicrm_api('Event', 'Create', $params);
265 $getEventParams = array(
266 'id' => $this->_eventIds
[0],
267 'version' => $this->_apiversion
,
268 'return.is_full' => 1,
271 $currentEvent = civicrm_api('Event', 'getsingle', $getEventParams);
272 $description = "demonstrates use of return is_full ";
273 $subfile = "IsFullOption";
274 $this->assertEquals(0, $currentEvent['is_full'], ' is full is set in line ' . __LINE__
);
275 $this->assertEquals(1, $currentEvent['available_places'], 'available places is set in line ' . __LINE__
);
276 $participant = civicrm_api('Participant', 'create', array('version' => 3, 'participant_status' => 1, 'role_id' => 1, 'contact_id' => $contactID, 'event_id' => $this->_eventIds
[0]));
277 $currentEvent = civicrm_api('Event', 'getsingle', $getEventParams);
279 $this->documentMe($getEventParams, $currentEvent, __FUNCTION__
, __FILE__
, $description, $subfile, 'getsingle');
280 $this->assertEquals(1, $currentEvent['is_full'], ' is full is set in line ' . __LINE__
);
281 $this->assertEquals(0, $currentEvent['available_places'], 'available places is set in line ' . __LINE__
);
283 $this->contactDelete($contactID);
286 * Legacy support for Contribution Type ID. We need to ensure this is supported
287 * as an alias for financial_type_id
289 function testCreateGetEventLegacyContributionTypeID() {
290 $contributionTypeArray = array('contribution_type_id' => 3);
291 if(isset($this->_params
[0]['financial_type_id'])){
292 //in case someone edits $this->_params & invalidates this test :-)
293 unset($this->_params
[0]['financial_type_id']);
295 $result = civicrm_api('event', 'create', $this->_params
[0] +
$contributionTypeArray);
296 $this->assertAPISuccess($result, ' Event Creation Failedon line ' . __LINE__
);
297 $getresult = civicrm_api('event', 'get', array('version' => 3,) +
$contributionTypeArray);
298 $this->assertAPISuccess($result, ' Event Creation on line ' . __LINE__
);
299 $this->assertEquals($getresult['values'][$getresult['id']]['contribution_type_id'], 3);
300 $this->assertEquals($result['id'], $getresult['id']);
301 civicrm_api('event', 'delete', array('version' => 3, 'id' => $result['id']));
303 ///////////////// civicrm_event_create methods
306 * check with complete array + custom field
307 * Note that the test is written on purpose without any
308 * variables specific to participant so it can be replicated into other entities
309 * and / or moved to the automated test suite
311 function testCreateWithCustom() {
312 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
314 $params = $this->_params
[0];
315 $params['custom_' . $ids['custom_field_id']] = "custom string";
317 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
);
319 $check = civicrm_api($this->_entity
, 'get', array('version' => 3, 'return.custom_' . $ids['custom_field_id'] => 1, 'id' => $result['id']));
320 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
322 $this->customFieldDelete($ids['custom_field_id']);
323 $this->customGroupDelete($ids['custom_group_id']);
324 civicrm_api($this->_entity
, 'Delete', array('version' => 3, 'id' => $result['id']));
327 function testCreateEventParamsNotArray() {
329 $result = $this->callAPIFailure('event', 'create', $params);
332 function testCreateEventEmptyParams() {
334 $result = $this->callAPIFailure('event', 'create', $params);
337 function testCreateEventParamsWithoutTitle() {
338 unset($this->_params
['title']);
339 $result = civicrm_api('event', 'create', $this->_params
);
340 $this->assertAPIFailure($result);
343 function testCreateEventParamsWithoutEventTypeId() {
344 unset($this->_params
['event_type_id']);
345 $result = civicrm_api('event', 'create', $this->_params
);
346 $this->assertAPIFailure($result);
349 function testCreateEventParamsWithoutStartDate() {
350 unset($this->_params
['start_date']);
351 $result = civicrm_api('event', 'create', $this->_params
);
352 $this->assertAPIFailure($result);
355 function testCreateEventSuccess() {
356 $result = civicrm_api('Event', 'Create', $this->_params
[0]);
357 $this->documentMe($this->_params
[0], $result, __FUNCTION__
, __FILE__
);
358 $this->assertAPISuccess($result);
359 $this->assertArrayHasKey('id', $result['values'][$result['id']], 'In line ' . __LINE__
);
360 $result = civicrm_api($this->_entity
, 'Get', array('version' => 3, 'id' => $result['id']));
361 civicrm_api($this->_entity
, 'Delete', array('version' => 3, 'id' => $result['id']));
363 $this->assertEquals('2008-10-21 00:00:00', $result['values'][$result['id']]['start_date'], 'start date is not set in line ' . __LINE__
);
364 $this->assertEquals('2008-10-23 00:00:00', $result['values'][$result['id']]['end_date'], 'end date is not set in line ' . __LINE__
);
365 $this->assertEquals('2008-06-01 00:00:00', $result['values'][$result['id']]['registration_start_date'], 'start date is not set in line ' . __LINE__
);
366 $this->assertEquals('2008-10-15 00:00:00', $result['values'][$result['id']]['registration_end_date'], 'end date is not set in line ' . __LINE__
);
367 civicrm_api($this->_entity
, 'Delete', array('version' => 3, 'id' => $result['id']));
370 * Test that passing in Unique field names works
372 function testCreateEventSuccessUniqueFieldNames() {
373 $this->_params
[0]['event_start_date'] = $this->_params
[0]['start_date'];
374 unset($this->_params
[1]['start_date']);
375 $this->_params
[0]['event_title'] = $this->_params
[0]['title'];
376 unset($this->_params
[0]['title']);
377 $result = civicrm_api('Event', 'Create', $this->_params
[0]);
378 $this->assertAPISuccess($result, 'In line ' . __LINE__
);
379 $this->assertArrayHasKey('id', $result['values'][$result['id']], 'In line ' . __LINE__
);
380 $result = civicrm_api($this->_entity
, 'Get', array('version' => 3, 'id' => $result['id']));
381 civicrm_api($this->_entity
, 'Delete', array('version' => 3, 'id' => $result['id']));
383 $this->assertEquals('2008-10-21 00:00:00', $result['values'][$result['id']]['start_date'], 'start date is not set in line ' . __LINE__
);
384 $this->assertEquals('2008-10-23 00:00:00', $result['values'][$result['id']]['end_date'], 'end date is not set in line ' . __LINE__
);
385 $this->assertEquals('2008-06-01 00:00:00', $result['values'][$result['id']]['registration_start_date'], 'start date is not set in line ' . __LINE__
);
386 $this->assertEquals('2008-10-15 00:00:00', $result['values'][$result['id']]['registration_end_date'], 'end date is not set in line ' . __LINE__
);
387 $this->assertEquals($this->_params
[0]['event_title'], $result['values'][$result['id']]['title'], 'end date is not set in line ' . __LINE__
);
389 civicrm_api($this->_entity
, 'Delete', array('version' => 3, 'id' => $result['id']));
392 function testUpdateEvent() {
393 $result = civicrm_api('event', 'create', $this->_params
[1]);
395 $this->assertAPISuccess($result);
397 'id' => $result['id'], 'version' => 3, 'max_participants' => 150,
399 civicrm_api('Event', 'Create', $params);
400 $updated = civicrm_api('Event', 'Get', $params);
401 $this->documentMe($this->_params
, $updated, __FUNCTION__
, __FILE__
);
402 $this->assertEquals($updated['is_error'], 0);
403 $this->assertEquals(150, $updated['values'][$result['id']]['max_participants']);
404 $this->assertEquals('Annual CiviCRM meet 2', $updated['values'][$result['id']]['title']);
405 civicrm_api($this->_entity
, 'Delete', array('version' => 3, 'id' => $result['id']));
408 ///////////////// civicrm_event_delete methods
409 function testDeleteWrongParamsType() {
410 $params = 'Annual CiviCRM meet';
411 $result = $this->callAPIFailure('Event', 'Delete', $params);
414 function testDeleteEmptyParams() {
416 $result = $this->callAPIFailure('Event', 'Delete', $params);
419 function testDelete() {
421 'id' => $this->_eventIds
[0],
422 'version' => $this->_apiversion
,
424 $result = civicrm_api('Event', 'Delete', $params);
425 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
426 $this->assertNotEquals($result['is_error'], 1);
429 * check event_id still supported for delete
431 function testDeleteWithEventId() {
433 'event_id' => $this->_eventIds
[0],
434 'version' => $this->_apiversion
,
436 $result = civicrm_api('Event', 'Delete', $params);
437 $this->assertAPISuccess($result, 'in line ' . __LINE__
);
440 * Trying to delete an event with participants should return error
442 function testDeleteWithExistingParticipant() {
443 $contactID = $this->individualCreate(NULL);
444 $participantID = $this->participantCreate(
446 'contactID' => $contactID,
447 'eventID' => $this->_eventIds
[0],
450 $result = civicrm_api('Event', 'Delete', array('version' => $this->_apiversion
, 'id' => $this->_eventIds
[0]));
451 $this->assertEquals(0, $result['is_error'], "Deleting exist with participants");
454 function testDeleteWithWrongEventId() {
455 $params = array('event_id' => $this->_eventIds
[0], 'version' => $this->_apiversion
);
456 $result = civicrm_api('Event', 'Delete', $params);
457 // try to delete again - there's no such event anymore
458 $params = array('event_id' => $this->_eventIds
[0]);
459 $result = $this->callAPIFailure('Event', 'Delete', $params);
462 ///////////////// civicrm_event_search methods
465 * Test civicrm_event_search with wrong params type
467 function testSearchWrongParamsType() {
468 $params = 'a string';
469 $result = $this->callAPIFailure('event', 'get', $params);
473 * Test civicrm_event_search with empty params
475 function testSearchEmptyParams() {
476 $event = civicrm_api('event', 'create', $this->_params
[1]);
479 'version' => $this->_apiversion
,
482 $result = civicrm_api('event', 'get', $getparams);
483 $this->assertEquals($result['count'], 3, 'In line ' . __LINE__
);
484 $res = $result['values'][0];
485 $this->assertArrayKeyExists('title', $res, 'In line ' . __LINE__
);
486 $this->assertEquals($res['event_type_id'], $this->_params
[1]['event_type_id'], 'In line ' . __LINE__
);
490 * Test civicrm_event_search. Success expected.
492 function testSearch() {
494 'event_type_id' => 1,
497 'return.start_date' => 1,
498 'version' => $this->_apiversion
,
500 $result = civicrm_api('event', 'get', $params);
502 $this->assertEquals($result['values'][$this->_eventIds
[0]]['id'], $this->_eventIds
[0], 'In line ' . __LINE__
);
503 $this->assertEquals($result['values'][$this->_eventIds
[0]]['title'], 'Annual CiviCRM meet', 'In line ' . __LINE__
);
507 * Test civicrm_event_search. Success expected.
508 * return.offset and return.max_results test (CRM-5266)
510 function testSearchWithOffsetAndMaxResults() {
513 while ($maxEvents > 0) {
515 'version' => $this->_apiversion
,
516 'title' => 'Test Event' . $maxEvents,
517 'event_type_id' => 2,
518 'start_date' => 20081021,
521 $events[$maxEvents] = civicrm_api('event', 'create', $params);
525 'version' => $this->_apiversion
,
526 'event_type_id' => 2,
529 'return.offset' => 2,
530 'return.max_results' => 2,
532 $result = civicrm_api('event', 'get', $params);
533 $this->assertAPISuccess($result);
534 $this->assertEquals(2, $result['count'], ' 2 results returned In line ' . __LINE__
);
537 function testEventCreationPermissions() {
539 'event_type_id' => 1, 'start_date' => '2010-10-03', 'title' => 'le cake is a tie', 'check_permissions' => TRUE,
540 'version' => $this->_apiversion
,
542 $config = &CRM_Core_Config
::singleton();
543 $config->userPermissionClass
->permissions
= array('access CiviCRM');
544 $result = $this->callAPIFailure('event', 'create', $params);
545 $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');
547 $config->userPermissionClass
->permissions
= array('access CiviEvent', 'edit all events', 'access CiviCRM');
548 $result = civicrm_api('event', 'create', $params);
549 $this->assertEquals(0, $result['is_error'], 'overfluous permissions should be enough to create an event');
552 function testgetfields() {
553 $description = "demonstrate use of getfields to interogate api";
554 $params = array('version' => 3, 'action' => 'create');
555 $result = civicrm_api('event', 'getfields', $params);
556 $this->assertEquals(1, $result['values']['title']['api.required'], 'in line ' . __LINE__
);
559 * test api_action param also works
561 function testgetfieldsRest() {
562 $description = "demonstrate use of getfields to interogate api";
563 $params = array('version' => 3, 'api_action' => 'create');
564 $result = civicrm_api('event', 'getfields', $params);
565 $this->assertEquals(1, $result['values']['title']['api.required'], 'in line ' . __LINE__
);
567 function testgetfieldsGet() {
568 $description = "demonstrate use of getfields to interogate api";
569 $params = array('version' => 3, 'action' => 'get');
570 $result = civicrm_api('event', 'getfields', $params);
571 $this->assertEquals('title', $result['values']['event_title']['name'], 'in line ' . __LINE__
);
573 function testgetfieldsDelete() {
574 $description = "demonstrate use of getfields to interogate api";
575 $params = array('version' => 3, 'action' => 'delete');
576 $result = civicrm_api('event', 'getfields', $params);
577 $this->assertEquals(1, $result['values']['id']['api.required']);