Merge pull request #3276 from davecivicrm/CRM-14700
[civicrm-core.git] / tests / phpunit / api / v3 / EventTest.php
CommitLineData
6a488035 1<?php
6a488035
TO
2
3/*
4 +--------------------------------------------------------------------+
06a1bc01 5 | CiviCRM version 4.5 |
6a488035 6 +--------------------------------------------------------------------+
06a1bc01 7 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
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. |
14 | |
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. |
19 | |
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 +--------------------------------------------------------------------+
27*/
28
29
30require_once 'CiviTest/CiviUnitTestCase.php';
31class api_v3_EventTest extends CiviUnitTestCase {
32 protected $_params;
33 protected $_apiversion;
430ae6dd 34 protected $_entity;
b7c9bc4c 35
430ae6dd 36 function get_info() {
6a488035
TO
37 return array(
38 'name' => 'Event Create',
39 'description' => 'Test all Event Create API methods.',
40 'group' => 'CiviCRM API Tests',
41 );
42 }
43
44 function setUp() {
45 parent::setUp();
46 $this->_apiversion = 3;
47 $this->_entity = 'event';
48 $this->_params = array(
49 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',
53 'event_type_id' => 1,
54 'is_public' => 1,
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',
62 'is_monetary' => 0,
63 'is_active' => 1,
64 'is_show_location' => 0,
6a488035
TO
65 ),
66 array(
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',
70 'event_type_id' => 1,
71 'is_public' => 1,
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',
79 'is_monetory' => 0,
80 'is_active' => 1,
81 'is_show_location' => 0,
6a488035
TO
82 ),
83 );
84
85 $params = array(
86 array(
87 'title' => 'Annual CiviCRM meet',
88 'event_type_id' => 1,
ca985406 89 'start_date' => 20081021, ),
6a488035
TO
90 array(
91 'title' => 'Annual CiviCRM meet 2',
92 'event_type_id' => 1,
ca985406 93 'start_date' => 20101021, ),
6a488035
TO
94 );
95
96 $this->events = array();
97 $this->eventIds = array();
98 foreach ($params as $event) {
226ede24 99 $result = $this->callAPISuccess('Event', 'Create', $event);
6a488035
TO
100 $this->_events[] = $result;
101 $this->_eventIds[] = $result['id'];
102 }
103 }
104
105 function tearDown() {
106 foreach ($this->eventIds as $eventId) {
107 $this->eventDelete($eventId);
108 }
6a488035
TO
109 $tablesToTruncate = array(
110 'civicrm_participant',
111 'civicrm_event',
112 );
113 $this->quickCleanup($tablesToTruncate, TRUE);
114 }
115
116 ///////////////// civicrm_event_get methods
6a488035
TO
117
118 function testGetEventById() {
119 $params = array(
ca985406 120 'id' => $this->_events[1]['id'],);
121 $result = $this->callAPISuccess('event', 'get', $params);
6a488035
TO
122 $this->assertEquals($result['values'][$this->_eventIds[1]]['event_title'], 'Annual CiviCRM meet 2');
123 }
124
125 function testGetEventByEventTitle() {
126
127 $params = array(
128 'event_title' => 'Annual CiviCRM meet',
226ede24 129 'sequential' => TRUE,
6a488035
TO
130 );
131
226ede24 132 $result = $this->callAPIAndDocument('event', 'get', $params, __FUNCTION__, __FILE__);
6a488035 133 $this->assertEquals(1, $result['count']);
226ede24 134 $this->assertEquals($result['values'][0]['id'], $this->_eventIds[0]);
6a488035
TO
135 }
136
137 function testGetEventByWrongTitle() {
138 $params = array(
ca985406 139 'title' => 'No event with that title',);
140 $result = $this->callAPISuccess('Event', 'Get', $params);
6a488035
TO
141 $this->assertEquals(0, $result['count']);
142 }
143 function testGetEventByIdSort() {
b6708aeb 144 $params = array(
6a488035 145 'return.sort' => 'id ASC',
ca985406 146 'return.max_results' => 1, );
147 $result = $this->callAPISuccess('Event', 'Get', $params);
6a488035
TO
148 $this->assertEquals(1, $result['id'], ' in line ' . __LINE__);
149 $params = array(
150 'options' => array(
151 'sort' => 'id DESC',
152 'limit' => 1,
ca985406 153 ), );
6a488035 154
ca985406 155 $result = $this->callAPISuccess('Event', 'Get', $params);
6a488035
TO
156 $this->assertAPISuccess($result, ' in line ' . __LINE__);
157 $this->assertEquals(2, $result['id'], ' in line ' . __LINE__);
158 $params = array(
159 'options' => array(
160 'sort' => 'id ASC',
161 'limit' => 1,
ca985406 162 ), );
163 $result = $this->callAPISuccess('Event', 'Get', $params);
6a488035
TO
164 $this->assertEquals(1, $result['id'], ' in line ' . __LINE__);
165
166
167 }
168 /*
169 * Getting the id back of an event.
170 * Does not work yet, bug in API
171 */
172
173 /*
174 function testGetIdOfEventByEventTitle() {
ca985406 175 $params = array( 'title' => 'Annual CiviCRM meet',
6a488035
TO
176 'return' => 'id'
177 );
178
ca985406 179 $result = $this->callAPISuccess('Event', 'Get', $params);
6a488035
TO
180 }
181 */
182
183
184 /*
185 * Test 'is.Current' option. Existing event is 'old' so only current should be returned
186 */
187 function testGetIsCurrent() {
188 $params = array(
6a488035
TO
189 'isCurrent' => 1,
190 );
191 $currentEventParams = array('start_date' => date('Y-m-d', strtotime('+ 1 day')),
192 'end_date' => date('Y-m-d', strtotime('+ 1 week')),
193 );
194 $currentEventParams = array_merge($this->_params[1], $currentEventParams);
226ede24 195 $currentEvent = $this->callAPISuccess('Event', 'Create', $currentEventParams);
6a488035
TO
196 $description = "demonstrates use of is.Current option";
197 $subfile = "IsCurrentOption";
226ede24 198 $result = $this->callAPIAndDocument('Event', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile);
9f1b81e0 199 $allEvents = $this->callAPISuccess('Event', 'Get', array());
200 $this->callAPISuccess('Event', 'Delete', array('id' => $currentEvent['id']));
6a488035
TO
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'], '');
204 }
205/*
206 * There has been a schema change & the api needs to buffer developers from it
207 */
208 function testGetPaymentProcessorId() {
209 $params = $this->_params[0];
210 $params['payment_processor_id'] = 1;
211 $params['sequential'] =1;
ca985406 212 $result = $this->callAPISuccess('event', 'create', $params);
6a488035 213 $this->assertEquals( 1,$result['values'][0]['payment_processor'][0], "handing of payment processor compatibility");
ca985406 214 $result = $this->callAPISuccess('event', 'get', $params);
6a488035
TO
215 $this->assertEquals($result['values'][0]['payment_processor_id'], 1,"handing get payment processor compatibility");
216 }
217
218 function testInvalidData() {
219 $params = $this->_params[0];
220 $params['sequential'] =1;
221 $params['loc_block_id'] =100;
d0e1eff2 222 $result = $this->callAPIFailure('event', 'create', $params);
6a488035
TO
223
224 }
225
226 /*
227 * Test 'is.Current' option. Existing event is 'old' so only current should be returned
228 */
229 function testGetSingleReturnIsFull() {
230 $contactID = $this->individualCreate();
231 $params = array(
ca985406 232 'id' => $this->_eventIds[0], 'max_participants' => 1,
6a488035 233 );
ca985406 234 $result = $this->callAPISuccess('Event', 'Create', $params);
6a488035
TO
235
236 $getEventParams = array(
ca985406 237 'id' => $this->_eventIds[0], 'return.is_full' => 1,
6a488035
TO
238 );
239
ca985406 240 $currentEvent = $this->callAPISuccess('Event', 'getsingle', $getEventParams);
6a488035
TO
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__);
ca985406 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');
6a488035
TO
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__);
249
250 $this->contactDelete($contactID);
251 }
252 /*
253 * Legacy support for Contribution Type ID. We need to ensure this is supported
254 * as an alias for financial_type_id
255 */
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']);
261 }
ca985406 262 $result = $this->callAPISuccess('event', 'create', $this->_params[0] + $contributionTypeArray);
263 $getresult = $this->callAPISuccess('event', 'get', array() + $contributionTypeArray);
ef0bc919 264 $this->assertEquals($getresult['values'][$getresult['id']]['contribution_type_id'], 3);
6a488035 265 $this->assertEquals($result['id'], $getresult['id']);
ca985406 266 $this->callAPISuccess('event', 'delete', array('id' => $result['id']));
6a488035
TO
267 }
268 ///////////////// civicrm_event_create methods
269
270 /**
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
275 */
276 function testCreateWithCustom() {
277 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
278
279 $params = $this->_params[0];
280 $params['custom_' . $ids['custom_field_id']] = "custom string";
281
226ede24 282 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
6a488035 283
ca985406 284 $check = $this->callAPISuccess($this->_entity, 'get', array('return.custom_' . $ids['custom_field_id'] => 1, 'id' => $result['id']));
6a488035
TO
285 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
286
287 $this->customFieldDelete($ids['custom_field_id']);
288 $this->customGroupDelete($ids['custom_group_id']);
ca985406 289 $this->callAPISuccess($this->_entity, 'Delete', array('id' => $result['id']));
6a488035
TO
290 }
291
292 function testCreateEventParamsNotArray() {
293 $params = NULL;
d0e1eff2 294 $result = $this->callAPIFailure('event', 'create', $params);
6a488035
TO
295 }
296
297 function testCreateEventEmptyParams() {
298 $params = array();
d0e1eff2 299 $result = $this->callAPIFailure('event', 'create', $params);
6a488035
TO
300 }
301
302 function testCreateEventParamsWithoutTitle() {
303 unset($this->_params['title']);
ca985406 304 $result = $this->callAPIFailure('event', 'create', $this->_params);
791c263c 305 $this->assertAPIFailure($result);
6a488035
TO
306 }
307
308 function testCreateEventParamsWithoutEventTypeId() {
309 unset($this->_params['event_type_id']);
ca985406 310 $result = $this->callAPIFailure('event', 'create', $this->_params);
6a488035
TO
311 }
312
313 function testCreateEventParamsWithoutStartDate() {
314 unset($this->_params['start_date']);
ca985406 315 $result = $this->callAPIFailure('event', 'create', $this->_params);
6a488035
TO
316 }
317
318 function testCreateEventSuccess() {
ca985406 319 $result = $this->callAPIAndDocument('Event', 'Create', $this->_params[0], __FUNCTION__, __FILE__);
6a488035 320 $this->assertArrayHasKey('id', $result['values'][$result['id']], 'In line ' . __LINE__);
ca985406 321 $result = $this->callAPISuccess($this->_entity, 'Get', array('id' => $result['id']));
322 $this->callAPISuccess($this->_entity, 'Delete', array('id' => $result['id']));
6a488035
TO
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__);
6a488035
TO
327 }
328 /*
329 * Test that passing in Unique field names works
330 */
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']);
ca985406 336 $result = $this->callAPISuccess('Event', 'Create', $this->_params[0]);
6a488035
TO
337 $this->assertAPISuccess($result, 'In line ' . __LINE__);
338 $this->assertArrayHasKey('id', $result['values'][$result['id']], 'In line ' . __LINE__);
ca985406 339 $result = $this->callAPISuccess($this->_entity, 'Get', array('id' => $result['id']));
340 $this->callAPISuccess($this->_entity, 'Delete', array('id' => $result['id']));
6a488035
TO
341
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__);
6a488035
TO
347 }
348
349 function testUpdateEvent() {
ca985406 350 $result = $this->callAPISuccess('event', 'create', $this->_params[1]);
6a488035 351
6a488035 352 $params = array(
ca985406 353 'id' => $result['id'], 'max_participants' => 150,
6a488035 354 );
ca985406 355 $this->callAPISuccess('Event', 'Create', $params);
fb32de45 356 $updated = $this->callAPISuccess('Event', 'Get', $params, __FUNCTION__, __FILE__);
6a488035
TO
357 $this->assertEquals(150, $updated['values'][$result['id']]['max_participants']);
358 $this->assertEquals('Annual CiviCRM meet 2', $updated['values'][$result['id']]['title']);
ca985406 359 $this->callAPISuccess($this->_entity, 'Delete', array('id' => $result['id']));
6a488035
TO
360 }
361
6a488035
TO
362
363 function testDeleteEmptyParams() {
ca985406 364 $result = $this->callAPIFailure('Event', 'Delete', array());
6a488035
TO
365 }
366
367 function testDelete() {
368 $params = array(
369 'id' => $this->_eventIds[0],
6a488035 370 );
ca985406 371 $result = $this->callAPIAndDocument('Event', 'Delete', $params, __FUNCTION__, __FILE__);
6a488035 372 }
ca985406 373
374 /**
375 * check event_id still supported for delete
376 */
6a488035
TO
377 function testDeleteWithEventId() {
378 $params = array(
ca985406 379 'event_id' => $this->_eventIds[0], );
380 $result = $this->callAPISuccess('Event', 'Delete', $params);
6a488035
TO
381 $this->assertAPISuccess($result, 'in line ' . __LINE__);
382 }
383 /*
384 * Trying to delete an event with participants should return error
385 */
386 function testDeleteWithExistingParticipant() {
e4d5f1e2 387 $contactID = $this->individualCreate();
6a488035
TO
388 $participantID = $this->participantCreate(
389 array(
390 'contactID' => $contactID,
391 'eventID' => $this->_eventIds[0],
392 )
393 );
ca985406 394 $result = $this->callAPISuccess('Event', 'Delete', array('id' => $this->_eventIds[0]));
6a488035
TO
395 }
396
397 function testDeleteWithWrongEventId() {
ca985406 398 $params = array('event_id' => $this->_eventIds[0]);
399 $result = $this->callAPISuccess('Event', 'Delete', $params);
6a488035
TO
400 // try to delete again - there's no such event anymore
401 $params = array('event_id' => $this->_eventIds[0]);
d0e1eff2 402 $result = $this->callAPIFailure('Event', 'Delete', $params);
6a488035
TO
403 }
404
405 ///////////////// civicrm_event_search methods
406
407 /**
408 * Test civicrm_event_search with wrong params type
409 */
410 function testSearchWrongParamsType() {
411 $params = 'a string';
d0e1eff2 412 $result = $this->callAPIFailure('event', 'get', $params);
6a488035
TO
413 }
414
415 /**
416 * Test civicrm_event_search with empty params
417 */
418 function testSearchEmptyParams() {
ca985406 419 $event = $this->callAPISuccess('event', 'create', $this->_params[1]);
6a488035 420
ca985406 421 $getparams = array( 'sequential' => 1,
6a488035 422 );
ca985406 423 $result = $this->callAPISuccess('event', 'get', $getparams);
6a488035
TO
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__);
428 }
429
430 /**
431 * Test civicrm_event_search. Success expected.
432 */
433 function testSearch() {
434 $params = array(
435 'event_type_id' => 1,
436 'return.title' => 1,
437 'return.id' => 1,
ca985406 438 'return.start_date' => 1, );
439 $result = $this->callAPISuccess('event', 'get', $params);
6a488035
TO
440
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__);
443 }
444
445 /**
446 * Test civicrm_event_search. Success expected.
447 * return.offset and return.max_results test (CRM-5266)
448 */
449 function testSearchWithOffsetAndMaxResults() {
450 $maxEvents = 5;
451 $events = array();
452 while ($maxEvents > 0) {
ca985406 453 $params = array( 'title' => 'Test Event' . $maxEvents,
6a488035
TO
454 'event_type_id' => 2,
455 'start_date' => 20081021,
456 );
457
ca985406 458 $events[$maxEvents] = $this->callAPISuccess('event', 'create', $params);
6a488035
TO
459 $maxEvents--;
460 }
ca985406 461 $params = array( 'event_type_id' => 2,
6a488035
TO
462 'return.id' => 1,
463 'return.title' => 1,
464 'return.offset' => 2,
465 'return.max_results' => 2,
466 );
ca985406 467 $result = $this->callAPISuccess('event', 'get', $params);
6a488035
TO
468 $this->assertAPISuccess($result);
469 $this->assertEquals(2, $result['count'], ' 2 results returned In line ' . __LINE__);
470 }
471
472 function testEventCreationPermissions() {
473 $params = array(
ca985406 474 'event_type_id' => 1, 'start_date' => '2010-10-03', 'title' => 'le cake is a tie', 'check_permissions' => TRUE, );
6a488035
TO
475 $config = &CRM_Core_Config::singleton();
476 $config->userPermissionClass->permissions = array('access CiviCRM');
d0e1eff2 477 $result = $this->callAPIFailure('event', 'create', $params);
60ec9f43 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');
6a488035
TO
479
480 $config->userPermissionClass->permissions = array('access CiviEvent', 'edit all events', 'access CiviCRM');
ca985406 481 $result = $this->callAPISuccess('event', 'create', $params);
6a488035
TO
482 }
483
484 function testgetfields() {
485 $description = "demonstrate use of getfields to interogate api";
ca985406 486 $params = array('action' => 'create');
487 $result = $this->callAPISuccess('event', 'getfields', $params);
6a488035
TO
488 $this->assertEquals(1, $result['values']['title']['api.required'], 'in line ' . __LINE__);
489 }
490 /*
491 * test api_action param also works
492 */
493 function testgetfieldsRest() {
494 $description = "demonstrate use of getfields to interogate api";
ca985406 495 $params = array('api_action' => 'create');
496 $result = $this->callAPISuccess('event', 'getfields', $params);
6a488035
TO
497 $this->assertEquals(1, $result['values']['title']['api.required'], 'in line ' . __LINE__);
498 }
499 function testgetfieldsGet() {
500 $description = "demonstrate use of getfields to interogate api";
ca985406 501 $params = array('action' => 'get');
502 $result = $this->callAPISuccess('event', 'getfields', $params);
6a488035
TO
503 $this->assertEquals('title', $result['values']['event_title']['name'], 'in line ' . __LINE__);
504 }
505 function testgetfieldsDelete() {
506 $description = "demonstrate use of getfields to interogate api";
ca985406 507 $params = array('action' => 'delete');
508 $result = $this->callAPISuccess('event', 'getfields', $params);
6a488035
TO
509 $this->assertEquals(1, $result['values']['id']['api.required']);
510 }
511}
512