d7e9d7d40b0f07709fdd44e4ebeb66170958d9a3
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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);
114 * civicrm_event_get methods.
116 public function testGetEventById() {
118 'id' => $this->_events
[1]['id'],
120 $result = $this->callAPISuccess('event', 'get', $params);
121 $this->assertEquals($result['values'][$this->_eventIds
[1]]['event_title'], 'Annual CiviCRM meet 2');
124 public function testGetEventByEventTitle() {
127 'event_title' => 'Annual CiviCRM meet',
128 'sequential' => TRUE,
131 $result = $this->callAPIAndDocument('event', 'get', $params, __FUNCTION__
, __FILE__
);
132 $this->assertEquals(1, $result['count']);
133 $this->assertEquals($result['values'][0]['id'], $this->_eventIds
[0]);
136 public function testGetEventByWrongTitle() {
138 'title' => 'No event with that title',
140 $result = $this->callAPISuccess('Event', 'Get', $params);
141 $this->assertEquals(0, $result['count']);
144 public function testGetEventByIdSort() {
146 'return.sort' => 'id ASC',
147 'return.max_results' => 1,
149 $result = $this->callAPISuccess('Event', 'Get', $params);
150 $this->assertEquals(1, $result['id'], ' in line ' . __LINE__
);
158 $result = $this->callAPISuccess('Event', 'Get', $params);
159 $this->assertAPISuccess($result, ' in line ' . __LINE__
);
160 $this->assertEquals(2, $result['id'], ' in line ' . __LINE__
);
167 $result = $this->callAPISuccess('Event', 'Get', $params);
168 $this->assertEquals(1, $result['id'], ' in line ' . __LINE__
);
172 * Getting the id back of an event.
173 * Does not work yet, bug in API
177 public function testGetIdOfEventByEventTitle() {
178 $params = array( 'title' => 'Annual CiviCRM meet',
182 $result = $this->callAPISuccess('Event', 'Get', $params);
188 * Test 'is.Current' option. Existing event is 'old' so only current should be returned
190 public function testGetIsCurrent() {
194 $currentEventParams = array(
195 'start_date' => date('Y-m-d', strtotime('+ 1 day')),
196 'end_date' => date('Y-m-d', strtotime('+ 1 week')),
198 $currentEventParams = array_merge($this->_params
[1], $currentEventParams);
199 $currentEvent = $this->callAPISuccess('Event', 'Create', $currentEventParams);
200 $description = "Demonstrates use of is.Current option.";
201 $subfile = "IsCurrentOption";
202 $result = $this->callAPIAndDocument('Event', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
203 $allEvents = $this->callAPISuccess('Event', 'Get', array());
204 $this->callAPISuccess('Event', 'Delete', array('id' => $currentEvent['id']));
205 $this->assertEquals(1, $result['count'], 'confirm only one event found in line ' . __LINE__
);
206 $this->assertEquals(3, $allEvents['count'], 'confirm three events exist (ie. two not found) ' . __LINE__
);
207 $this->assertEquals($currentEvent['id'], $result['id'], '');
211 * There has been a schema change & the api needs to buffer developers from it
213 public function testGetPaymentProcessorId() {
214 $params = $this->_params
[0];
215 $params['payment_processor_id'] = 1;
216 $params['sequential'] = 1;
217 $result = $this->callAPISuccess('event', 'create', $params);
218 $this->assertEquals(1, $result['values'][0]['payment_processor'][0], "handing of payment processor compatibility");
219 $result = $this->callAPISuccess('event', 'get', $params);
220 $this->assertEquals($result['values'][0]['payment_processor_id'], 1, "handing get payment processor compatibility");
223 public function testInvalidData() {
224 $params = $this->_params
[0];
225 $params['sequential'] = 1;
226 $params['loc_block_id'] = 100;
227 $result = $this->callAPIFailure('event', 'create', $params);
232 * Test 'is.Current' option. Existing event is 'old' so only current should be returned
234 public function testGetSingleReturnIsFull() {
235 $contactID = $this->individualCreate();
237 'id' => $this->_eventIds
[0],
238 'max_participants' => 1,
240 $result = $this->callAPISuccess('Event', 'Create', $params);
242 $getEventParams = array(
243 'id' => $this->_eventIds
[0],
244 'return.is_full' => 1,
247 $currentEvent = $this->callAPISuccess('Event', 'getsingle', $getEventParams);
248 $description = "Demonstrates use of return is_full .";
249 $subfile = "IsFullOption";
250 $this->assertEquals(0, $currentEvent['is_full'], ' is full is set in line ' . __LINE__
);
251 $this->assertEquals(1, $currentEvent['available_places'], 'available places is set in line ' . __LINE__
);
252 $participant = $this->callAPISuccess('Participant', 'create', array(
253 'participant_status' => 1,
255 'contact_id' => $contactID,
256 'event_id' => $this->_eventIds
[0],
258 $currentEvent = $this->callAPIAndDocument('Event', 'getsingle', $getEventParams, __FUNCTION__
, __FILE__
, $description, $subfile);
259 $this->assertEquals(1, $currentEvent['is_full'], ' is full is set in line ' . __LINE__
);
260 $this->assertEquals(0, $currentEvent['available_places'], 'available places is set in line ' . __LINE__
);
262 $this->contactDelete($contactID);
266 * Legacy support for Contribution Type ID.
268 * We need to ensure this is supported as an alias for financial_type_id.
270 public function testCreateGetEventLegacyContributionTypeID() {
271 $contributionTypeArray = array('contribution_type_id' => 3);
272 if (isset($this->_params
[0]['financial_type_id'])) {
273 //in case someone edits $this->_params & invalidates this test :-)
274 unset($this->_params
[0]['financial_type_id']);
276 $result = $this->callAPISuccess('event', 'create', $this->_params
[0] +
$contributionTypeArray);
277 $getresult = $this->callAPISuccess('event', 'get', array() +
$contributionTypeArray);
278 $this->assertEquals($getresult['values'][$getresult['id']]['contribution_type_id'], 3);
279 $this->assertEquals($result['id'], $getresult['id']);
280 $this->callAPISuccess('event', 'delete', array('id' => $result['id']));
284 * Chaining get event and loc block.
286 public function testChainingGetLocBlock() {
287 // create a loc block and an event for that loc block.
288 $eventParams = $this->_params
[0];
289 $eventParams['loc_bloc_id'] = '$value.id';
290 $locBlockParams = array(
292 'street_address' => 'Kipdorp 24',
293 'postal_code' => '2000',
294 'city' => 'Antwerpen',
295 'country_id' => '1020',
296 'location_type_id' => '1',
298 'api.Event.create' => $eventParams,
301 $createResult = $this->callAPIAndDocument('LocBlock', 'create', $locBlockParams, __FUNCTION__
, __FILE__
);
302 $locBlockId = $createResult['id'];
303 $addressId = $createResult['values'][0]['address_id'];
304 $eventId = $createResult['values'][0]['api.Event.create']['id'];
306 // request the event with its loc block:
307 $check = $this->callAPISuccess($this->_entity
, 'getsingle', array(
309 'api.LocBlock.get' => array('id' => '$value.loc_block_id'),
314 $this->assertEquals($eventId, $check['id'], ' in line ' . __LINE__
);
315 $this->assertEquals(1, $check['api.LocBlock.get']['count'], ' in line ' . __LINE__
);
316 $this->assertEquals($locBlockId, $check['api.LocBlock.get']['id'], ' in line ' . __LINE__
);
319 $this->callAPISuccess($this->_entity
, 'delete', array('id' => $eventId));
323 * Chaining get event and non existing loc block.
325 * Even if there is no loc block, at least the event should be returned.
326 * http://forum.civicrm.org/index.php/topic,36113.0.html
328 public function testChainingGetNonExistingLocBlock() {
329 $params = $this->_params
[0];
330 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
332 $check = $this->callAPISuccess($this->_entity
, 'get', array(
333 'id' => $result['id'],
334 // this chaining request should not break things:
335 'api.LocBlock.get' => array('id' => '$value.loc_block_id'),
337 $this->assertEquals($result['id'], $check['id']);
339 $this->callAPISuccess($this->_entity
, 'Delete', array('id' => $result['id']));
343 * Check with complete array + custom field.
345 * Note that the test is written on purpose without any
346 * variables specific to participant so it can be replicated into other entities
347 * and / or moved to the automated test suite.
349 public function testCreateWithCustom() {
350 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
352 $params = $this->_params
[0];
353 $params['custom_' . $ids['custom_field_id']] = "custom string";
355 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
);
357 $check = $this->callAPISuccess($this->_entity
, 'get', array(
358 'return.custom_' . $ids['custom_field_id'] => 1,
359 'id' => $result['id'],
361 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
363 $this->customFieldDelete($ids['custom_field_id']);
364 $this->customGroupDelete($ids['custom_group_id']);
365 $this->callAPISuccess($this->_entity
, 'Delete', array('id' => $result['id']));
369 * Test that an event with a price set can be created.
371 public function testCreatePaidEvent() {
372 //@todo alter API so that an integer is converted to an array
373 $priceSetParams = array('price_set_id' => (array) 1, 'is_monetary' => 1);
374 $result = $this->callAPISuccess('Event', 'Create', array_merge($this->_params
[0], $priceSetParams));
375 $event = $this->callAPISuccess('Event', 'getsingle', array('id' => $result['id'], 'return' => 'price_set_id'));
376 $this->assertArrayKeyExists('price_set_id', $event);
379 public function testCreateEventParamsNotArray() {
381 $result = $this->callAPIFailure('event', 'create', $params);
384 public function testCreateEventEmptyParams() {
386 $result = $this->callAPIFailure('event', 'create', $params);
389 public function testCreateEventParamsWithoutTitle() {
390 unset($this->_params
['title']);
391 $result = $this->callAPIFailure('event', 'create', $this->_params
);
392 $this->assertAPIFailure($result);
395 public function testCreateEventParamsWithoutEventTypeId() {
396 unset($this->_params
['event_type_id']);
397 $result = $this->callAPIFailure('event', 'create', $this->_params
);
400 public function testCreateEventParamsWithoutStartDate() {
401 unset($this->_params
['start_date']);
402 $result = $this->callAPIFailure('event', 'create', $this->_params
);
405 public function testCreateEventSuccess() {
406 $result = $this->callAPIAndDocument('Event', 'Create', $this->_params
[0], __FUNCTION__
, __FILE__
);
407 $this->assertArrayHasKey('id', $result['values'][$result['id']], 'In line ' . __LINE__
);
408 $result = $this->callAPISuccess($this->_entity
, 'Get', array('id' => $result['id']));
409 $this->callAPISuccess($this->_entity
, 'Delete', array('id' => $result['id']));
410 $this->assertEquals('2008-10-21 00:00:00', $result['values'][$result['id']]['start_date'], 'start date is not set in line ' . __LINE__
);
411 $this->assertEquals('2008-10-23 00:00:00', $result['values'][$result['id']]['end_date'], 'end date is not set in line ' . __LINE__
);
412 $this->assertEquals('2008-06-01 00:00:00', $result['values'][$result['id']]['registration_start_date'], 'start date is not set in line ' . __LINE__
);
413 $this->assertEquals('2008-10-15 00:00:00', $result['values'][$result['id']]['registration_end_date'], 'end date is not set in line ' . __LINE__
);
417 * Test that passing in Unique field names works.
419 public function testCreateEventSuccessUniqueFieldNames() {
420 $this->_params
[0]['event_start_date'] = $this->_params
[0]['start_date'];
421 unset($this->_params
[1]['start_date']);
422 $this->_params
[0]['event_title'] = $this->_params
[0]['title'];
423 unset($this->_params
[0]['title']);
424 $result = $this->callAPISuccess('Event', 'Create', $this->_params
[0]);
425 $this->assertAPISuccess($result, 'In line ' . __LINE__
);
426 $this->assertArrayHasKey('id', $result['values'][$result['id']], 'In line ' . __LINE__
);
427 $result = $this->callAPISuccess($this->_entity
, 'Get', array('id' => $result['id']));
428 $this->callAPISuccess($this->_entity
, 'Delete', array('id' => $result['id']));
430 $this->assertEquals('2008-10-21 00:00:00', $result['values'][$result['id']]['start_date'], 'start date is not set in line ' . __LINE__
);
431 $this->assertEquals('2008-10-23 00:00:00', $result['values'][$result['id']]['end_date'], 'end date is not set in line ' . __LINE__
);
432 $this->assertEquals('2008-06-01 00:00:00', $result['values'][$result['id']]['registration_start_date'], 'start date is not set in line ' . __LINE__
);
433 $this->assertEquals('2008-10-15 00:00:00', $result['values'][$result['id']]['registration_end_date'], 'end date is not set in line ' . __LINE__
);
434 $this->assertEquals($this->_params
[0]['event_title'], $result['values'][$result['id']]['title'], 'end date is not set in line ' . __LINE__
);
437 public function testUpdateEvent() {
438 $result = $this->callAPISuccess('event', 'create', $this->_params
[1]);
441 'id' => $result['id'],
442 'max_participants' => 150,
444 $this->callAPISuccess('Event', 'Create', $params);
445 $updated = $this->callAPISuccess('Event', 'Get', $params, __FUNCTION__
, __FILE__
);
446 $this->assertEquals(150, $updated['values'][$result['id']]['max_participants']);
447 $this->assertEquals('Annual CiviCRM meet 2', $updated['values'][$result['id']]['title']);
448 $this->callAPISuccess($this->_entity
, 'Delete', array('id' => $result['id']));
452 public function testDeleteEmptyParams() {
453 $result = $this->callAPIFailure('Event', 'Delete', array());
456 public function testDelete() {
458 'id' => $this->_eventIds
[0],
460 $result = $this->callAPIAndDocument('Event', 'Delete', $params, __FUNCTION__
, __FILE__
);
464 * Check event_id still supported for delete.
466 public function testDeleteWithEventId() {
468 'event_id' => $this->_eventIds
[0],
470 $result = $this->callAPISuccess('Event', 'Delete', $params);
471 $this->assertAPISuccess($result, 'in line ' . __LINE__
);
475 * Trying to delete an event with participants should return error.
477 public function testDeleteWithExistingParticipant() {
478 $contactID = $this->individualCreate();
479 $participantID = $this->participantCreate(
481 'contactID' => $contactID,
482 'eventID' => $this->_eventIds
[0],
485 $result = $this->callAPISuccess('Event', 'Delete', array('id' => $this->_eventIds
[0]));
488 public function testDeleteWithWrongEventId() {
489 $params = array('event_id' => $this->_eventIds
[0]);
490 $result = $this->callAPISuccess('Event', 'Delete', $params);
491 // try to delete again - there's no such event anymore
492 $params = array('event_id' => $this->_eventIds
[0]);
493 $result = $this->callAPIFailure('Event', 'Delete', $params);
496 ///////////////// civicrm_event_search methods
499 * Test civicrm_event_search with wrong params type.
501 public function testSearchWrongParamsType() {
502 $params = 'a string';
503 $result = $this->callAPIFailure('event', 'get', $params);
507 * Test civicrm_event_search with empty params.
509 public function testSearchEmptyParams() {
510 $this->callAPISuccess('event', 'create', $this->_params
[1]);
515 $result = $this->callAPISuccess('event', 'get', $getParams);
516 $this->assertEquals($result['count'], 3);
517 $res = $result['values'][0];
518 $this->assertArrayKeyExists('title', $res);
519 $this->assertEquals($res['event_type_id'], $this->_params
[1]['event_type_id']);
523 * Test civicrm_event_search. Success expected.
525 public function testSearch() {
527 'event_type_id' => 1,
530 'return.start_date' => 1,
532 $result = $this->callAPISuccess('event', 'get', $params);
534 $this->assertEquals($result['values'][$this->_eventIds
[0]]['id'], $this->_eventIds
[0], 'In line ' . __LINE__
);
535 $this->assertEquals($result['values'][$this->_eventIds
[0]]['title'], 'Annual CiviCRM meet', 'In line ' . __LINE__
);
539 * Test civicrm_event_search.
543 * return.offset and return.max_results test (CRM-5266)
545 public function testSearchWithOffsetAndMaxResults() {
548 while ($maxEvents > 0) {
550 'title' => 'Test Event' . $maxEvents,
551 'event_type_id' => 2,
552 'start_date' => 20081021,
555 $events[$maxEvents] = $this->callAPISuccess('event', 'create', $params);
559 'event_type_id' => 2,
562 'return.offset' => 2,
563 'return.max_results' => 2,
565 $result = $this->callAPISuccess('event', 'get', $params);
566 $this->assertAPISuccess($result);
567 $this->assertEquals(2, $result['count'], ' 2 results returned In line ' . __LINE__
);
570 public function testEventCreationPermissions() {
572 'event_type_id' => 1,
573 'start_date' => '2010-10-03',
574 'title' => 'le cake is a tie',
575 'check_permissions' => TRUE,
577 $config = &CRM_Core_Config
::singleton();
578 $config->userPermissionClass
->permissions
= array('access CiviCRM');
579 $result = $this->callAPIFailure('event', 'create', $params);
580 $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');
582 $config->userPermissionClass
->permissions
= array(
587 $result = $this->callAPISuccess('event', 'create', $params);
590 public function testgetfields() {
591 $description = "Demonstrate use of getfields to interrogate api.";
592 $params = array('action' => 'create');
593 $result = $this->callAPISuccess('event', 'getfields', $params);
594 $this->assertEquals(1, $result['values']['title']['api.required'], 'in line ' . __LINE__
);
598 * Test api_action param also works.
600 public function testgetfieldsRest() {
601 $description = "Demonstrate use of getfields to interrogate api.";
602 $params = array('api_action' => 'create');
603 $result = $this->callAPISuccess('event', 'getfields', $params);
604 $this->assertEquals(1, $result['values']['title']['api.required'], 'in line ' . __LINE__
);
607 public function testgetfieldsGet() {
608 $description = "Demonstrate use of getfields to interrogate api.";
609 $params = array('action' => 'get');
610 $result = $this->callAPISuccess('event', 'getfields', $params);
611 $this->assertEquals('title', $result['values']['event_title']['name'], 'in line ' . __LINE__
);
614 public function testgetfieldsDelete() {
615 $description = "Demonstrate use of getfields to interrogate api.";
616 $params = array('action' => 'delete');
617 $result = $this->callAPISuccess('event', 'getfields', $params);
618 $this->assertEquals(1, $result['values']['id']['api.required']);