3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
29 require_once 'CiviTest/CiviUnitTestCase.php';
32 * Class api_v3_EventTest
34 class api_v3_EventTest
extends CiviUnitTestCase
{
36 protected $_apiversion;
39 public function setUp() {
41 $this->_apiversion
= 3;
42 $this->_entity
= 'event';
43 $this->_params
= array(
45 'title' => 'Annual CiviCRM meet',
46 'summary' => 'If you have any CiviCRM realted issues or want to track where CiviCRM is heading, Sign up now',
47 'description' => 'This event is intended to give brief idea about progess of CiviCRM and giving solutions to common user issues',
50 'start_date' => 20081021,
51 'end_date' => 20081023,
52 'is_online_registration' => 1,
53 'registration_start_date' => 20080601,
54 'registration_end_date' => '2008-10-15',
55 'max_participants' => 100,
56 'event_full_text' => 'Sorry! We are already full',
59 'is_show_location' => 0,
62 'title' => 'Annual CiviCRM meet 2',
63 'summary' => 'If you have any CiviCRM realted issues or want to track where CiviCRM is heading, Sign up now',
64 'description' => 'This event is intended to give brief idea about progess of CiviCRM and giving solutions to common user issues',
67 'start_date' => 20101021,
68 'end_date' => 20101023,
69 'is_online_registration' => 1,
70 'registration_start_date' => 20100601,
71 'registration_end_date' => '2010-10-15',
72 'max_participants' => 100,
73 'event_full_text' => 'Sorry! We are already full',
76 'is_show_location' => 0,
82 'title' => 'Annual CiviCRM meet',
84 'start_date' => 20081021,
87 'title' => 'Annual CiviCRM meet 2',
89 'start_date' => 20101021,
93 $this->events
= array();
94 $this->eventIds
= array();
95 foreach ($params as $event) {
96 $result = $this->callAPISuccess('Event', 'Create', $event);
97 $this->_events
[] = $result;
98 $this->_eventIds
[] = $result['id'];
102 public function tearDown() {
103 foreach ($this->eventIds
as $eventId) {
104 $this->eventDelete($eventId);
106 $tablesToTruncate = array(
107 'civicrm_participant',
110 $this->quickCleanup($tablesToTruncate, TRUE);
113 ///////////////// civicrm_event_get methods
115 public function testGetEventById() {
117 'id' => $this->_events
[1]['id'],
119 $result = $this->callAPISuccess('event', 'get', $params);
120 $this->assertEquals($result['values'][$this->_eventIds
[1]]['event_title'], 'Annual CiviCRM meet 2');
123 public function testGetEventByEventTitle() {
126 'event_title' => 'Annual CiviCRM meet',
127 'sequential' => TRUE,
130 $result = $this->callAPIAndDocument('event', 'get', $params, __FUNCTION__
, __FILE__
);
131 $this->assertEquals(1, $result['count']);
132 $this->assertEquals($result['values'][0]['id'], $this->_eventIds
[0]);
135 public function testGetEventByWrongTitle() {
137 'title' => 'No event with that title',
139 $result = $this->callAPISuccess('Event', 'Get', $params);
140 $this->assertEquals(0, $result['count']);
143 public function testGetEventByIdSort() {
145 'return.sort' => 'id ASC',
146 'return.max_results' => 1,
148 $result = $this->callAPISuccess('Event', 'Get', $params);
149 $this->assertEquals(1, $result['id'], ' in line ' . __LINE__
);
157 $result = $this->callAPISuccess('Event', 'Get', $params);
158 $this->assertAPISuccess($result, ' in line ' . __LINE__
);
159 $this->assertEquals(2, $result['id'], ' in line ' . __LINE__
);
166 $result = $this->callAPISuccess('Event', 'Get', $params);
167 $this->assertEquals(1, $result['id'], ' in line ' . __LINE__
);
171 * Getting the id back of an event.
172 * Does not work yet, bug in API
176 public function testGetIdOfEventByEventTitle() {
177 $params = array( 'title' => 'Annual CiviCRM meet',
181 $result = $this->callAPISuccess('Event', 'Get', $params);
187 * Test 'is.Current' option. Existing event is 'old' so only current should be returned
189 public function testGetIsCurrent() {
193 $currentEventParams = array(
194 'start_date' => date('Y-m-d', strtotime('+ 1 day')),
195 'end_date' => date('Y-m-d', strtotime('+ 1 week')),
197 $currentEventParams = array_merge($this->_params
[1], $currentEventParams);
198 $currentEvent = $this->callAPISuccess('Event', 'Create', $currentEventParams);
199 $description = "demonstrates use of is.Current option";
200 $subfile = "IsCurrentOption";
201 $result = $this->callAPIAndDocument('Event', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
202 $allEvents = $this->callAPISuccess('Event', 'Get', array());
203 $this->callAPISuccess('Event', 'Delete', array('id' => $currentEvent['id']));
204 $this->assertEquals(1, $result['count'], 'confirm only one event found in line ' . __LINE__
);
205 $this->assertEquals(3, $allEvents['count'], 'confirm three events exist (ie. two not found) ' . __LINE__
);
206 $this->assertEquals($currentEvent['id'], $result['id'], '');
210 * There has been a schema change & the api needs to buffer developers from it
212 public function testGetPaymentProcessorId() {
213 $params = $this->_params
[0];
214 $params['payment_processor_id'] = 1;
215 $params['sequential'] = 1;
216 $result = $this->callAPISuccess('event', 'create', $params);
217 $this->assertEquals(1, $result['values'][0]['payment_processor'][0], "handing of payment processor compatibility");
218 $result = $this->callAPISuccess('event', 'get', $params);
219 $this->assertEquals($result['values'][0]['payment_processor_id'], 1, "handing get payment processor compatibility");
222 public function testInvalidData() {
223 $params = $this->_params
[0];
224 $params['sequential'] = 1;
225 $params['loc_block_id'] = 100;
226 $result = $this->callAPIFailure('event', 'create', $params);
231 * Test 'is.Current' option. Existing event is 'old' so only current should be returned
233 public function testGetSingleReturnIsFull() {
234 $contactID = $this->individualCreate();
236 'id' => $this->_eventIds
[0],
237 'max_participants' => 1,
239 $result = $this->callAPISuccess('Event', 'Create', $params);
241 $getEventParams = array(
242 'id' => $this->_eventIds
[0],
243 'return.is_full' => 1,
246 $currentEvent = $this->callAPISuccess('Event', 'getsingle', $getEventParams);
247 $description = "demonstrates use of return is_full ";
248 $subfile = "IsFullOption";
249 $this->assertEquals(0, $currentEvent['is_full'], ' is full is set in line ' . __LINE__
);
250 $this->assertEquals(1, $currentEvent['available_places'], 'available places is set in line ' . __LINE__
);
251 $participant = $this->callAPISuccess('Participant', 'create', array(
252 'participant_status' => 1,
254 'contact_id' => $contactID,
255 'event_id' => $this->_eventIds
[0]
257 $currentEvent = $this->callAPIAndDocument('Event', 'getsingle', $getEventParams, __FUNCTION__
, __FILE__
, $description, $subfile, 'getsingle');
258 $this->assertEquals(1, $currentEvent['is_full'], ' is full is set in line ' . __LINE__
);
259 $this->assertEquals(0, $currentEvent['available_places'], 'available places is set in line ' . __LINE__
);
261 $this->contactDelete($contactID);
265 * Legacy support for Contribution Type ID. We need to ensure this is supported
266 * as an alias for financial_type_id
268 public function testCreateGetEventLegacyContributionTypeID() {
269 $contributionTypeArray = array('contribution_type_id' => 3);
270 if (isset($this->_params
[0]['financial_type_id'])) {
271 //in case someone edits $this->_params & invalidates this test :-)
272 unset($this->_params
[0]['financial_type_id']);
274 $result = $this->callAPISuccess('event', 'create', $this->_params
[0] +
$contributionTypeArray);
275 $getresult = $this->callAPISuccess('event', 'get', array() +
$contributionTypeArray);
276 $this->assertEquals($getresult['values'][$getresult['id']]['contribution_type_id'], 3);
277 $this->assertEquals($result['id'], $getresult['id']);
278 $this->callAPISuccess('event', 'delete', array('id' => $result['id']));
280 ///////////////// civicrm_event_create methods
283 * Check with complete array + custom field
284 * Note that the test is written on purpose without any
285 * variables specific to participant so it can be replicated into other entities
286 * and / or moved to the automated test suite
288 public function testCreateWithCustom() {
289 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
291 $params = $this->_params
[0];
292 $params['custom_' . $ids['custom_field_id']] = "custom string";
294 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
);
296 $check = $this->callAPISuccess($this->_entity
, 'get', array(
297 'return.custom_' . $ids['custom_field_id'] => 1,
298 'id' => $result['id']
300 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
302 $this->customFieldDelete($ids['custom_field_id']);
303 $this->customGroupDelete($ids['custom_group_id']);
304 $this->callAPISuccess($this->_entity
, 'Delete', array('id' => $result['id']));
308 * Test that an event with a price set can be created
310 public function testCreatePaidEvent() {
311 //@todo alter API so that an integer is converted to an array
312 $priceSetParams = array('price_set_id' => (array) 1, 'is_monetary' => 1);
313 $result = $this->callAPISuccess('Event', 'Create', array_merge($this->_params
[0], $priceSetParams));
314 $event = $this->callAPISuccess('Event', 'getsingle', array('id' => $result['id'], 'return' => 'price_set_id'));
315 $this->assertArrayKeyExists('price_set_id', $event);
318 public function testCreateEventParamsNotArray() {
320 $result = $this->callAPIFailure('event', 'create', $params);
323 public function testCreateEventEmptyParams() {
325 $result = $this->callAPIFailure('event', 'create', $params);
328 public function testCreateEventParamsWithoutTitle() {
329 unset($this->_params
['title']);
330 $result = $this->callAPIFailure('event', 'create', $this->_params
);
331 $this->assertAPIFailure($result);
334 public function testCreateEventParamsWithoutEventTypeId() {
335 unset($this->_params
['event_type_id']);
336 $result = $this->callAPIFailure('event', 'create', $this->_params
);
339 public function testCreateEventParamsWithoutStartDate() {
340 unset($this->_params
['start_date']);
341 $result = $this->callAPIFailure('event', 'create', $this->_params
);
344 public function testCreateEventSuccess() {
345 $result = $this->callAPIAndDocument('Event', 'Create', $this->_params
[0], __FUNCTION__
, __FILE__
);
346 $this->assertArrayHasKey('id', $result['values'][$result['id']], 'In line ' . __LINE__
);
347 $result = $this->callAPISuccess($this->_entity
, 'Get', array('id' => $result['id']));
348 $this->callAPISuccess($this->_entity
, 'Delete', array('id' => $result['id']));
349 $this->assertEquals('2008-10-21 00:00:00', $result['values'][$result['id']]['start_date'], 'start date is not set in line ' . __LINE__
);
350 $this->assertEquals('2008-10-23 00:00:00', $result['values'][$result['id']]['end_date'], 'end date is not set in line ' . __LINE__
);
351 $this->assertEquals('2008-06-01 00:00:00', $result['values'][$result['id']]['registration_start_date'], 'start date is not set in line ' . __LINE__
);
352 $this->assertEquals('2008-10-15 00:00:00', $result['values'][$result['id']]['registration_end_date'], 'end date is not set in line ' . __LINE__
);
356 * Test that passing in Unique field names works
358 public function testCreateEventSuccessUniqueFieldNames() {
359 $this->_params
[0]['event_start_date'] = $this->_params
[0]['start_date'];
360 unset($this->_params
[1]['start_date']);
361 $this->_params
[0]['event_title'] = $this->_params
[0]['title'];
362 unset($this->_params
[0]['title']);
363 $result = $this->callAPISuccess('Event', 'Create', $this->_params
[0]);
364 $this->assertAPISuccess($result, 'In line ' . __LINE__
);
365 $this->assertArrayHasKey('id', $result['values'][$result['id']], 'In line ' . __LINE__
);
366 $result = $this->callAPISuccess($this->_entity
, 'Get', array('id' => $result['id']));
367 $this->callAPISuccess($this->_entity
, 'Delete', array('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 $this->assertEquals($this->_params
[0]['event_title'], $result['values'][$result['id']]['title'], 'end date is not set in line ' . __LINE__
);
376 public function testUpdateEvent() {
377 $result = $this->callAPISuccess('event', 'create', $this->_params
[1]);
380 'id' => $result['id'],
381 'max_participants' => 150,
383 $this->callAPISuccess('Event', 'Create', $params);
384 $updated = $this->callAPISuccess('Event', 'Get', $params, __FUNCTION__
, __FILE__
);
385 $this->assertEquals(150, $updated['values'][$result['id']]['max_participants']);
386 $this->assertEquals('Annual CiviCRM meet 2', $updated['values'][$result['id']]['title']);
387 $this->callAPISuccess($this->_entity
, 'Delete', array('id' => $result['id']));
391 public function testDeleteEmptyParams() {
392 $result = $this->callAPIFailure('Event', 'Delete', array());
395 public function testDelete() {
397 'id' => $this->_eventIds
[0],
399 $result = $this->callAPIAndDocument('Event', 'Delete', $params, __FUNCTION__
, __FILE__
);
403 * Check event_id still supported for delete
405 public function testDeleteWithEventId() {
407 'event_id' => $this->_eventIds
[0],
409 $result = $this->callAPISuccess('Event', 'Delete', $params);
410 $this->assertAPISuccess($result, 'in line ' . __LINE__
);
414 * Trying to delete an event with participants should return error
416 public function testDeleteWithExistingParticipant() {
417 $contactID = $this->individualCreate();
418 $participantID = $this->participantCreate(
420 'contactID' => $contactID,
421 'eventID' => $this->_eventIds
[0],
424 $result = $this->callAPISuccess('Event', 'Delete', array('id' => $this->_eventIds
[0]));
427 public function testDeleteWithWrongEventId() {
428 $params = array('event_id' => $this->_eventIds
[0]);
429 $result = $this->callAPISuccess('Event', 'Delete', $params);
430 // try to delete again - there's no such event anymore
431 $params = array('event_id' => $this->_eventIds
[0]);
432 $result = $this->callAPIFailure('Event', 'Delete', $params);
435 ///////////////// civicrm_event_search methods
438 * Test civicrm_event_search with wrong params type
440 public function testSearchWrongParamsType() {
441 $params = 'a string';
442 $result = $this->callAPIFailure('event', 'get', $params);
446 * Test civicrm_event_search with empty params
448 public function testSearchEmptyParams() {
449 $event = $this->callAPISuccess('event', 'create', $this->_params
[1]);
454 $result = $this->callAPISuccess('event', 'get', $getparams);
455 $this->assertEquals($result['count'], 3, 'In line ' . __LINE__
);
456 $res = $result['values'][0];
457 $this->assertArrayKeyExists('title', $res, 'In line ' . __LINE__
);
458 $this->assertEquals($res['event_type_id'], $this->_params
[1]['event_type_id'], 'In line ' . __LINE__
);
462 * Test civicrm_event_search. Success expected.
464 public function testSearch() {
466 'event_type_id' => 1,
469 'return.start_date' => 1,
471 $result = $this->callAPISuccess('event', 'get', $params);
473 $this->assertEquals($result['values'][$this->_eventIds
[0]]['id'], $this->_eventIds
[0], 'In line ' . __LINE__
);
474 $this->assertEquals($result['values'][$this->_eventIds
[0]]['title'], 'Annual CiviCRM meet', 'In line ' . __LINE__
);
478 * Test civicrm_event_search. Success expected.
479 * return.offset and return.max_results test (CRM-5266)
481 public function testSearchWithOffsetAndMaxResults() {
484 while ($maxEvents > 0) {
486 'title' => 'Test Event' . $maxEvents,
487 'event_type_id' => 2,
488 'start_date' => 20081021,
491 $events[$maxEvents] = $this->callAPISuccess('event', 'create', $params);
495 'event_type_id' => 2,
498 'return.offset' => 2,
499 'return.max_results' => 2,
501 $result = $this->callAPISuccess('event', 'get', $params);
502 $this->assertAPISuccess($result);
503 $this->assertEquals(2, $result['count'], ' 2 results returned In line ' . __LINE__
);
506 public function testEventCreationPermissions() {
508 'event_type_id' => 1,
509 'start_date' => '2010-10-03',
510 'title' => 'le cake is a tie',
511 'check_permissions' => TRUE,
513 $config = &CRM_Core_Config
::singleton();
514 $config->userPermissionClass
->permissions
= array('access CiviCRM');
515 $result = $this->callAPIFailure('event', 'create', $params);
516 $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');
518 $config->userPermissionClass
->permissions
= array('access CiviEvent', 'edit all events', 'access CiviCRM');
519 $result = $this->callAPISuccess('event', 'create', $params);
522 public function testgetfields() {
523 $description = "demonstrate use of getfields to interrogate api";
524 $params = array('action' => 'create');
525 $result = $this->callAPISuccess('event', 'getfields', $params);
526 $this->assertEquals(1, $result['values']['title']['api.required'], 'in line ' . __LINE__
);
530 * Test api_action param also works
532 public function testgetfieldsRest() {
533 $description = "demonstrate use of getfields to interrogate api";
534 $params = array('api_action' => 'create');
535 $result = $this->callAPISuccess('event', 'getfields', $params);
536 $this->assertEquals(1, $result['values']['title']['api.required'], 'in line ' . __LINE__
);
539 public function testgetfieldsGet() {
540 $description = "demonstrate use of getfields to interrogate api";
541 $params = array('action' => 'get');
542 $result = $this->callAPISuccess('event', 'getfields', $params);
543 $this->assertEquals('title', $result['values']['event_title']['name'], 'in line ' . __LINE__
);
546 public function testgetfieldsDelete() {
547 $description = "demonstrate use of getfields to interrogate api";
548 $params = array('action' => 'delete');
549 $result = $this->callAPISuccess('event', 'getfields', $params);
550 $this->assertEquals(1, $result['values']['id']['api.required']);