Merge pull request #4780 from williamtheaker/patch-1
[civicrm-core.git] / tests / phpunit / api / v3 / EventTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.6 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
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
30 require_once 'CiviTest/CiviUnitTestCase.php';
31
32 /**
33 * Class api_v3_EventTest
34 */
35 class api_v3_EventTest extends CiviUnitTestCase {
36 protected $_params;
37 protected $_apiversion;
38 protected $_entity;
39
40 function setUp() {
41 parent::setUp();
42 $this->_apiversion = 3;
43 $this->_entity = 'event';
44 $this->_params = array(
45 array(
46 'title' => 'Annual CiviCRM meet',
47 'summary' => 'If you have any CiviCRM realted issues or want to track where CiviCRM is heading, Sign up now',
48 'description' => 'This event is intended to give brief idea about progess of CiviCRM and giving solutions to common user issues',
49 'event_type_id' => 1,
50 'is_public' => 1,
51 'start_date' => 20081021,
52 'end_date' => 20081023,
53 'is_online_registration' => 1,
54 'registration_start_date' => 20080601,
55 'registration_end_date' => '2008-10-15',
56 'max_participants' => 100,
57 'event_full_text' => 'Sorry! We are already full',
58 'is_monetary' => 0,
59 'is_active' => 1,
60 'is_show_location' => 0,
61 ),
62 array(
63 'title' => 'Annual CiviCRM meet 2',
64 'summary' => 'If you have any CiviCRM realted issues or want to track where CiviCRM is heading, Sign up now',
65 'description' => 'This event is intended to give brief idea about progess of CiviCRM and giving solutions to common user issues',
66 'event_type_id' => 1,
67 'is_public' => 1,
68 'start_date' => 20101021,
69 'end_date' => 20101023,
70 'is_online_registration' => 1,
71 'registration_start_date' => 20100601,
72 'registration_end_date' => '2010-10-15',
73 'max_participants' => 100,
74 'event_full_text' => 'Sorry! We are already full',
75 'is_monetory' => 0,
76 'is_active' => 1,
77 'is_show_location' => 0,
78 ),
79 );
80
81 $params = array(
82 array(
83 'title' => 'Annual CiviCRM meet',
84 'event_type_id' => 1,
85 'start_date' => 20081021, ),
86 array(
87 'title' => 'Annual CiviCRM meet 2',
88 'event_type_id' => 1,
89 'start_date' => 20101021, ),
90 );
91
92 $this->events = array();
93 $this->eventIds = array();
94 foreach ($params as $event) {
95 $result = $this->callAPISuccess('Event', 'Create', $event);
96 $this->_events[] = $result;
97 $this->_eventIds[] = $result['id'];
98 }
99 }
100
101 function tearDown() {
102 foreach ($this->eventIds as $eventId) {
103 $this->eventDelete($eventId);
104 }
105 $tablesToTruncate = array(
106 'civicrm_participant',
107 'civicrm_event',
108 );
109 $this->quickCleanup($tablesToTruncate, TRUE);
110 }
111
112 ///////////////// civicrm_event_get methods
113
114 function testGetEventById() {
115 $params = array(
116 'id' => $this->_events[1]['id'],);
117 $result = $this->callAPISuccess('event', 'get', $params);
118 $this->assertEquals($result['values'][$this->_eventIds[1]]['event_title'], 'Annual CiviCRM meet 2');
119 }
120
121 function testGetEventByEventTitle() {
122
123 $params = array(
124 'event_title' => 'Annual CiviCRM meet',
125 'sequential' => TRUE,
126 );
127
128 $result = $this->callAPIAndDocument('event', 'get', $params, __FUNCTION__, __FILE__);
129 $this->assertEquals(1, $result['count']);
130 $this->assertEquals($result['values'][0]['id'], $this->_eventIds[0]);
131 }
132
133 function testGetEventByWrongTitle() {
134 $params = array(
135 'title' => 'No event with that title',);
136 $result = $this->callAPISuccess('Event', 'Get', $params);
137 $this->assertEquals(0, $result['count']);
138 }
139 function testGetEventByIdSort() {
140 $params = array(
141 'return.sort' => 'id ASC',
142 'return.max_results' => 1, );
143 $result = $this->callAPISuccess('Event', 'Get', $params);
144 $this->assertEquals(1, $result['id'], ' in line ' . __LINE__);
145 $params = array(
146 'options' => array(
147 'sort' => 'id DESC',
148 'limit' => 1,
149 ), );
150
151 $result = $this->callAPISuccess('Event', 'Get', $params);
152 $this->assertAPISuccess($result, ' in line ' . __LINE__);
153 $this->assertEquals(2, $result['id'], ' in line ' . __LINE__);
154 $params = array(
155 'options' => array(
156 'sort' => 'id ASC',
157 'limit' => 1,
158 ), );
159 $result = $this->callAPISuccess('Event', 'Get', $params);
160 $this->assertEquals(1, $result['id'], ' in line ' . __LINE__);
161
162
163 }
164 /*
165 * Getting the id back of an event.
166 * Does not work yet, bug in API
167 */
168
169 /*
170 function testGetIdOfEventByEventTitle() {
171 $params = array( 'title' => 'Annual CiviCRM meet',
172 'return' => 'id'
173 );
174
175 $result = $this->callAPISuccess('Event', 'Get', $params);
176 }
177 */
178
179
180 /**
181 * Test 'is.Current' option. Existing event is 'old' so only current should be returned
182 */
183 function testGetIsCurrent() {
184 $params = array(
185 'isCurrent' => 1,
186 );
187 $currentEventParams = array('start_date' => date('Y-m-d', strtotime('+ 1 day')),
188 'end_date' => date('Y-m-d', strtotime('+ 1 week')),
189 );
190 $currentEventParams = array_merge($this->_params[1], $currentEventParams);
191 $currentEvent = $this->callAPISuccess('Event', 'Create', $currentEventParams);
192 $description = "demonstrates use of is.Current option";
193 $subfile = "IsCurrentOption";
194 $result = $this->callAPIAndDocument('Event', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile);
195 $allEvents = $this->callAPISuccess('Event', 'Get', array());
196 $this->callAPISuccess('Event', 'Delete', array('id' => $currentEvent['id']));
197 $this->assertEquals(1, $result['count'], 'confirm only one event found in line ' . __LINE__);
198 $this->assertEquals(3, $allEvents['count'], 'confirm three events exist (ie. two not found) ' . __LINE__);
199 $this->assertEquals($currentEvent['id'], $result['id'], '');
200 }
201
202 /**
203 * There has been a schema change & the api needs to buffer developers from it
204 */
205 function testGetPaymentProcessorId() {
206 $params = $this->_params[0];
207 $params['payment_processor_id'] = 1;
208 $params['sequential'] =1;
209 $result = $this->callAPISuccess('event', 'create', $params);
210 $this->assertEquals( 1,$result['values'][0]['payment_processor'][0], "handing of payment processor compatibility");
211 $result = $this->callAPISuccess('event', 'get', $params);
212 $this->assertEquals($result['values'][0]['payment_processor_id'], 1,"handing get payment processor compatibility");
213 }
214
215 function testInvalidData() {
216 $params = $this->_params[0];
217 $params['sequential'] =1;
218 $params['loc_block_id'] =100;
219 $result = $this->callAPIFailure('event', 'create', $params);
220
221 }
222
223 /**
224 * Test 'is.Current' option. Existing event is 'old' so only current should be returned
225 */
226 function testGetSingleReturnIsFull() {
227 $contactID = $this->individualCreate();
228 $params = array(
229 'id' => $this->_eventIds[0], 'max_participants' => 1,
230 );
231 $result = $this->callAPISuccess('Event', 'Create', $params);
232
233 $getEventParams = array(
234 'id' => $this->_eventIds[0], 'return.is_full' => 1,
235 );
236
237 $currentEvent = $this->callAPISuccess('Event', 'getsingle', $getEventParams);
238 $description = "demonstrates use of return is_full ";
239 $subfile = "IsFullOption";
240 $this->assertEquals(0, $currentEvent['is_full'], ' is full is set in line ' . __LINE__);
241 $this->assertEquals(1, $currentEvent['available_places'], 'available places is set in line ' . __LINE__);
242 $participant = $this->callAPISuccess('Participant', 'create', array('participant_status' => 1, 'role_id' => 1, 'contact_id' => $contactID, 'event_id' => $this->_eventIds[0]));
243 $currentEvent = $this->callAPIAndDocument('Event', 'getsingle', $getEventParams, __FUNCTION__, __FILE__, $description, $subfile, 'getsingle');
244 $this->assertEquals(1, $currentEvent['is_full'], ' is full is set in line ' . __LINE__);
245 $this->assertEquals(0, $currentEvent['available_places'], 'available places is set in line ' . __LINE__);
246
247 $this->contactDelete($contactID);
248 }
249 /*
250 * Legacy support for Contribution Type ID. We need to ensure this is supported
251 * as an alias for financial_type_id
252 */
253 function testCreateGetEventLegacyContributionTypeID() {
254 $contributionTypeArray = array('contribution_type_id' => 3);
255 if(isset($this->_params[0]['financial_type_id'])){
256 //in case someone edits $this->_params & invalidates this test :-)
257 unset($this->_params[0]['financial_type_id']);
258 }
259 $result = $this->callAPISuccess('event', 'create', $this->_params[0] + $contributionTypeArray);
260 $getresult = $this->callAPISuccess('event', 'get', array() + $contributionTypeArray);
261 $this->assertEquals($getresult['values'][$getresult['id']]['contribution_type_id'], 3);
262 $this->assertEquals($result['id'], $getresult['id']);
263 $this->callAPISuccess('event', 'delete', array('id' => $result['id']));
264 }
265 ///////////////// civicrm_event_create methods
266
267 /**
268 * Check with complete array + custom field
269 * Note that the test is written on purpose without any
270 * variables specific to participant so it can be replicated into other entities
271 * and / or moved to the automated test suite
272 */
273 function testCreateWithCustom() {
274 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
275
276 $params = $this->_params[0];
277 $params['custom_' . $ids['custom_field_id']] = "custom string";
278
279 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
280
281 $check = $this->callAPISuccess($this->_entity, 'get', array('return.custom_' . $ids['custom_field_id'] => 1, 'id' => $result['id']));
282 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
283
284 $this->customFieldDelete($ids['custom_field_id']);
285 $this->customGroupDelete($ids['custom_group_id']);
286 $this->callAPISuccess($this->_entity, 'Delete', array('id' => $result['id']));
287 }
288
289 /**
290 * Test that an event with a price set can be created
291 */
292 function testCreatePaidEvent() {
293 //@todo alter API so that an integer is converted to an array
294 $priceSetParams = array('price_set_id' => (array) 1, 'is_monetary' => 1);
295 $result = $this->callAPISuccess('Event', 'Create', array_merge($this->_params[0], $priceSetParams));
296 $event = $this->callAPISuccess('Event', 'getsingle', array('id' => $result['id'], 'return' => 'price_set_id'));
297 $this->assertArrayKeyExists('price_set_id', $event);
298 }
299
300 function testCreateEventParamsNotArray() {
301 $params = NULL;
302 $result = $this->callAPIFailure('event', 'create', $params);
303 }
304
305 function testCreateEventEmptyParams() {
306 $params = array();
307 $result = $this->callAPIFailure('event', 'create', $params);
308 }
309
310 function testCreateEventParamsWithoutTitle() {
311 unset($this->_params['title']);
312 $result = $this->callAPIFailure('event', 'create', $this->_params);
313 $this->assertAPIFailure($result);
314 }
315
316 function testCreateEventParamsWithoutEventTypeId() {
317 unset($this->_params['event_type_id']);
318 $result = $this->callAPIFailure('event', 'create', $this->_params);
319 }
320
321 function testCreateEventParamsWithoutStartDate() {
322 unset($this->_params['start_date']);
323 $result = $this->callAPIFailure('event', 'create', $this->_params);
324 }
325
326 function testCreateEventSuccess() {
327 $result = $this->callAPIAndDocument('Event', 'Create', $this->_params[0], __FUNCTION__, __FILE__);
328 $this->assertArrayHasKey('id', $result['values'][$result['id']], 'In line ' . __LINE__);
329 $result = $this->callAPISuccess($this->_entity, 'Get', array('id' => $result['id']));
330 $this->callAPISuccess($this->_entity, 'Delete', array('id' => $result['id']));
331 $this->assertEquals('2008-10-21 00:00:00', $result['values'][$result['id']]['start_date'], 'start date is not set in line ' . __LINE__);
332 $this->assertEquals('2008-10-23 00:00:00', $result['values'][$result['id']]['end_date'], 'end date is not set in line ' . __LINE__);
333 $this->assertEquals('2008-06-01 00:00:00', $result['values'][$result['id']]['registration_start_date'], 'start date is not set in line ' . __LINE__);
334 $this->assertEquals('2008-10-15 00:00:00', $result['values'][$result['id']]['registration_end_date'], 'end date is not set in line ' . __LINE__);
335 }
336
337 /**
338 * Test that passing in Unique field names works
339 */
340 function testCreateEventSuccessUniqueFieldNames() {
341 $this->_params[0]['event_start_date'] = $this->_params[0]['start_date'];
342 unset($this->_params[1]['start_date']);
343 $this->_params[0]['event_title'] = $this->_params[0]['title'];
344 unset($this->_params[0]['title']);
345 $result = $this->callAPISuccess('Event', 'Create', $this->_params[0]);
346 $this->assertAPISuccess($result, 'In line ' . __LINE__);
347 $this->assertArrayHasKey('id', $result['values'][$result['id']], 'In line ' . __LINE__);
348 $result = $this->callAPISuccess($this->_entity, 'Get', array('id' => $result['id']));
349 $this->callAPISuccess($this->_entity, 'Delete', array('id' => $result['id']));
350
351 $this->assertEquals('2008-10-21 00:00:00', $result['values'][$result['id']]['start_date'], 'start date is not set in line ' . __LINE__);
352 $this->assertEquals('2008-10-23 00:00:00', $result['values'][$result['id']]['end_date'], 'end date is not set in line ' . __LINE__);
353 $this->assertEquals('2008-06-01 00:00:00', $result['values'][$result['id']]['registration_start_date'], 'start date is not set in line ' . __LINE__);
354 $this->assertEquals('2008-10-15 00:00:00', $result['values'][$result['id']]['registration_end_date'], 'end date is not set in line ' . __LINE__);
355 $this->assertEquals($this->_params[0]['event_title'], $result['values'][$result['id']]['title'], 'end date is not set in line ' . __LINE__);
356 }
357
358 function testUpdateEvent() {
359 $result = $this->callAPISuccess('event', 'create', $this->_params[1]);
360
361 $params = array(
362 'id' => $result['id'], 'max_participants' => 150,
363 );
364 $this->callAPISuccess('Event', 'Create', $params);
365 $updated = $this->callAPISuccess('Event', 'Get', $params, __FUNCTION__, __FILE__);
366 $this->assertEquals(150, $updated['values'][$result['id']]['max_participants']);
367 $this->assertEquals('Annual CiviCRM meet 2', $updated['values'][$result['id']]['title']);
368 $this->callAPISuccess($this->_entity, 'Delete', array('id' => $result['id']));
369 }
370
371
372 function testDeleteEmptyParams() {
373 $result = $this->callAPIFailure('Event', 'Delete', array());
374 }
375
376 function testDelete() {
377 $params = array(
378 'id' => $this->_eventIds[0],
379 );
380 $result = $this->callAPIAndDocument('Event', 'Delete', $params, __FUNCTION__, __FILE__);
381 }
382
383 /**
384 * Check event_id still supported for delete
385 */
386 function testDeleteWithEventId() {
387 $params = array(
388 'event_id' => $this->_eventIds[0], );
389 $result = $this->callAPISuccess('Event', 'Delete', $params);
390 $this->assertAPISuccess($result, 'in line ' . __LINE__);
391 }
392
393 /**
394 * Trying to delete an event with participants should return error
395 */
396 function testDeleteWithExistingParticipant() {
397 $contactID = $this->individualCreate();
398 $participantID = $this->participantCreate(
399 array(
400 'contactID' => $contactID,
401 'eventID' => $this->_eventIds[0],
402 )
403 );
404 $result = $this->callAPISuccess('Event', 'Delete', array('id' => $this->_eventIds[0]));
405 }
406
407 function testDeleteWithWrongEventId() {
408 $params = array('event_id' => $this->_eventIds[0]);
409 $result = $this->callAPISuccess('Event', 'Delete', $params);
410 // try to delete again - there's no such event anymore
411 $params = array('event_id' => $this->_eventIds[0]);
412 $result = $this->callAPIFailure('Event', 'Delete', $params);
413 }
414
415 ///////////////// civicrm_event_search methods
416
417 /**
418 * Test civicrm_event_search with wrong params type
419 */
420 function testSearchWrongParamsType() {
421 $params = 'a string';
422 $result = $this->callAPIFailure('event', 'get', $params);
423 }
424
425 /**
426 * Test civicrm_event_search with empty params
427 */
428 function testSearchEmptyParams() {
429 $event = $this->callAPISuccess('event', 'create', $this->_params[1]);
430
431 $getparams = array( 'sequential' => 1,
432 );
433 $result = $this->callAPISuccess('event', 'get', $getparams);
434 $this->assertEquals($result['count'], 3, 'In line ' . __LINE__);
435 $res = $result['values'][0];
436 $this->assertArrayKeyExists('title', $res, 'In line ' . __LINE__);
437 $this->assertEquals($res['event_type_id'], $this->_params[1]['event_type_id'], 'In line ' . __LINE__);
438 }
439
440 /**
441 * Test civicrm_event_search. Success expected.
442 */
443 function testSearch() {
444 $params = array(
445 'event_type_id' => 1,
446 'return.title' => 1,
447 'return.id' => 1,
448 'return.start_date' => 1, );
449 $result = $this->callAPISuccess('event', 'get', $params);
450
451 $this->assertEquals($result['values'][$this->_eventIds[0]]['id'], $this->_eventIds[0], 'In line ' . __LINE__);
452 $this->assertEquals($result['values'][$this->_eventIds[0]]['title'], 'Annual CiviCRM meet', 'In line ' . __LINE__);
453 }
454
455 /**
456 * Test civicrm_event_search. Success expected.
457 * return.offset and return.max_results test (CRM-5266)
458 */
459 function testSearchWithOffsetAndMaxResults() {
460 $maxEvents = 5;
461 $events = array();
462 while ($maxEvents > 0) {
463 $params = array( 'title' => 'Test Event' . $maxEvents,
464 'event_type_id' => 2,
465 'start_date' => 20081021,
466 );
467
468 $events[$maxEvents] = $this->callAPISuccess('event', 'create', $params);
469 $maxEvents--;
470 }
471 $params = array( 'event_type_id' => 2,
472 'return.id' => 1,
473 'return.title' => 1,
474 'return.offset' => 2,
475 'return.max_results' => 2,
476 );
477 $result = $this->callAPISuccess('event', 'get', $params);
478 $this->assertAPISuccess($result);
479 $this->assertEquals(2, $result['count'], ' 2 results returned In line ' . __LINE__);
480 }
481
482 function testEventCreationPermissions() {
483 $params = array(
484 'event_type_id' => 1, 'start_date' => '2010-10-03', 'title' => 'le cake is a tie', 'check_permissions' => TRUE, );
485 $config = &CRM_Core_Config::singleton();
486 $config->userPermissionClass->permissions = array('access CiviCRM');
487 $result = $this->callAPIFailure('event', 'create', $params);
488 $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');
489
490 $config->userPermissionClass->permissions = array('access CiviEvent', 'edit all events', 'access CiviCRM');
491 $result = $this->callAPISuccess('event', 'create', $params);
492 }
493
494 function testgetfields() {
495 $description = "demonstrate use of getfields to interrogate api";
496 $params = array('action' => 'create');
497 $result = $this->callAPISuccess('event', 'getfields', $params);
498 $this->assertEquals(1, $result['values']['title']['api.required'], 'in line ' . __LINE__);
499 }
500
501 /**
502 * Test api_action param also works
503 */
504 function testgetfieldsRest() {
505 $description = "demonstrate use of getfields to interrogate api";
506 $params = array('api_action' => 'create');
507 $result = $this->callAPISuccess('event', 'getfields', $params);
508 $this->assertEquals(1, $result['values']['title']['api.required'], 'in line ' . __LINE__);
509 }
510 function testgetfieldsGet() {
511 $description = "demonstrate use of getfields to interrogate api";
512 $params = array('action' => 'get');
513 $result = $this->callAPISuccess('event', 'getfields', $params);
514 $this->assertEquals('title', $result['values']['event_title']['name'], 'in line ' . __LINE__);
515 }
516 function testgetfieldsDelete() {
517 $description = "demonstrate use of getfields to interrogate api";
518 $params = array('action' => 'delete');
519 $result = $this->callAPISuccess('event', 'getfields', $params);
520 $this->assertEquals(1, $result['values']['id']['api.required']);
521 }
522 }
523