Coding standards cleanup sprint.
[civicrm-core.git] / tests / phpunit / api / v3 / EventTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28
29 require_once 'CiviTest/CiviUnitTestCase.php';
30
31 /**
32 * Class api_v3_EventTest
33 */
34 class api_v3_EventTest extends CiviUnitTestCase {
35 protected $_params;
36 protected $_apiversion;
37 protected $_entity;
38
39 public function setUp() {
40 parent::setUp();
41 $this->_apiversion = 3;
42 $this->_entity = 'event';
43 $this->_params = array(
44 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',
48 'event_type_id' => 1,
49 'is_public' => 1,
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',
57 'is_monetary' => 0,
58 'is_active' => 1,
59 'is_show_location' => 0,
60 ),
61 array(
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',
65 'event_type_id' => 1,
66 'is_public' => 1,
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',
74 'is_monetory' => 0,
75 'is_active' => 1,
76 'is_show_location' => 0,
77 ),
78 );
79
80 $params = array(
81 array(
82 'title' => 'Annual CiviCRM meet',
83 'event_type_id' => 1,
84 'start_date' => 20081021,
85 ),
86 array(
87 'title' => 'Annual CiviCRM meet 2',
88 'event_type_id' => 1,
89 'start_date' => 20101021,
90 ),
91 );
92
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'];
99 }
100 }
101
102 public function tearDown() {
103 foreach ($this->eventIds as $eventId) {
104 $this->eventDelete($eventId);
105 }
106 $tablesToTruncate = array(
107 'civicrm_participant',
108 'civicrm_event',
109 );
110 $this->quickCleanup($tablesToTruncate, TRUE);
111 }
112
113 ///////////////// civicrm_event_get methods
114
115 public function testGetEventById() {
116 $params = array(
117 'id' => $this->_events[1]['id'],
118 );
119 $result = $this->callAPISuccess('event', 'get', $params);
120 $this->assertEquals($result['values'][$this->_eventIds[1]]['event_title'], 'Annual CiviCRM meet 2');
121 }
122
123 public function testGetEventByEventTitle() {
124
125 $params = array(
126 'event_title' => 'Annual CiviCRM meet',
127 'sequential' => TRUE,
128 );
129
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]);
133 }
134
135 public function testGetEventByWrongTitle() {
136 $params = array(
137 'title' => 'No event with that title',
138 );
139 $result = $this->callAPISuccess('Event', 'Get', $params);
140 $this->assertEquals(0, $result['count']);
141 }
142
143 public function testGetEventByIdSort() {
144 $params = array(
145 'return.sort' => 'id ASC',
146 'return.max_results' => 1,
147 );
148 $result = $this->callAPISuccess('Event', 'Get', $params);
149 $this->assertEquals(1, $result['id'], ' in line ' . __LINE__);
150 $params = array(
151 'options' => array(
152 'sort' => 'id DESC',
153 'limit' => 1,
154 ),
155 );
156
157 $result = $this->callAPISuccess('Event', 'Get', $params);
158 $this->assertAPISuccess($result, ' in line ' . __LINE__);
159 $this->assertEquals(2, $result['id'], ' in line ' . __LINE__);
160 $params = array(
161 'options' => array(
162 'sort' => 'id ASC',
163 'limit' => 1,
164 ),
165 );
166 $result = $this->callAPISuccess('Event', 'Get', $params);
167 $this->assertEquals(1, $result['id'], ' in line ' . __LINE__);
168
169 }
170 /*
171 * Getting the id back of an event.
172 * Does not work yet, bug in API
173 */
174
175 /*
176 public function testGetIdOfEventByEventTitle() {
177 $params = array( 'title' => 'Annual CiviCRM meet',
178 'return' => 'id'
179 );
180
181 $result = $this->callAPISuccess('Event', 'Get', $params);
182 }
183 */
184
185
186 /**
187 * Test 'is.Current' option. Existing event is 'old' so only current should be returned
188 */
189 public function testGetIsCurrent() {
190 $params = array(
191 'isCurrent' => 1,
192 );
193 $currentEventParams = array(
194 'start_date' => date('Y-m-d', strtotime('+ 1 day')),
195 'end_date' => date('Y-m-d', strtotime('+ 1 week')),
196 );
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'], '');
207 }
208
209 /**
210 * There has been a schema change & the api needs to buffer developers from it
211 */
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");
220 }
221
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);
227
228 }
229
230 /**
231 * Test 'is.Current' option. Existing event is 'old' so only current should be returned
232 */
233 public function testGetSingleReturnIsFull() {
234 $contactID = $this->individualCreate();
235 $params = array(
236 'id' => $this->_eventIds[0],
237 'max_participants' => 1,
238 );
239 $result = $this->callAPISuccess('Event', 'Create', $params);
240
241 $getEventParams = array(
242 'id' => $this->_eventIds[0],
243 'return.is_full' => 1,
244 );
245
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,
253 'role_id' => 1,
254 'contact_id' => $contactID,
255 'event_id' => $this->_eventIds[0]
256 ));
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__);
260
261 $this->contactDelete($contactID);
262 }
263
264 /*
265 * Legacy support for Contribution Type ID. We need to ensure this is supported
266 * as an alias for financial_type_id
267 */
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']);
273 }
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']));
279 }
280 ///////////////// civicrm_event_create methods
281
282 /**
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
287 */
288 public function testCreateWithCustom() {
289 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
290
291 $params = $this->_params[0];
292 $params['custom_' . $ids['custom_field_id']] = "custom string";
293
294 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
295
296 $check = $this->callAPISuccess($this->_entity, 'get', array(
297 'return.custom_' . $ids['custom_field_id'] => 1,
298 'id' => $result['id']
299 ));
300 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
301
302 $this->customFieldDelete($ids['custom_field_id']);
303 $this->customGroupDelete($ids['custom_group_id']);
304 $this->callAPISuccess($this->_entity, 'Delete', array('id' => $result['id']));
305 }
306
307 /**
308 * Test that an event with a price set can be created
309 */
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);
316 }
317
318 public function testCreateEventParamsNotArray() {
319 $params = NULL;
320 $result = $this->callAPIFailure('event', 'create', $params);
321 }
322
323 public function testCreateEventEmptyParams() {
324 $params = array();
325 $result = $this->callAPIFailure('event', 'create', $params);
326 }
327
328 public function testCreateEventParamsWithoutTitle() {
329 unset($this->_params['title']);
330 $result = $this->callAPIFailure('event', 'create', $this->_params);
331 $this->assertAPIFailure($result);
332 }
333
334 public function testCreateEventParamsWithoutEventTypeId() {
335 unset($this->_params['event_type_id']);
336 $result = $this->callAPIFailure('event', 'create', $this->_params);
337 }
338
339 public function testCreateEventParamsWithoutStartDate() {
340 unset($this->_params['start_date']);
341 $result = $this->callAPIFailure('event', 'create', $this->_params);
342 }
343
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__);
353 }
354
355 /**
356 * Test that passing in Unique field names works
357 */
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']));
368
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__);
374 }
375
376 public function testUpdateEvent() {
377 $result = $this->callAPISuccess('event', 'create', $this->_params[1]);
378
379 $params = array(
380 'id' => $result['id'],
381 'max_participants' => 150,
382 );
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']));
388 }
389
390
391 public function testDeleteEmptyParams() {
392 $result = $this->callAPIFailure('Event', 'Delete', array());
393 }
394
395 public function testDelete() {
396 $params = array(
397 'id' => $this->_eventIds[0],
398 );
399 $result = $this->callAPIAndDocument('Event', 'Delete', $params, __FUNCTION__, __FILE__);
400 }
401
402 /**
403 * Check event_id still supported for delete
404 */
405 public function testDeleteWithEventId() {
406 $params = array(
407 'event_id' => $this->_eventIds[0],
408 );
409 $result = $this->callAPISuccess('Event', 'Delete', $params);
410 $this->assertAPISuccess($result, 'in line ' . __LINE__);
411 }
412
413 /**
414 * Trying to delete an event with participants should return error
415 */
416 public function testDeleteWithExistingParticipant() {
417 $contactID = $this->individualCreate();
418 $participantID = $this->participantCreate(
419 array(
420 'contactID' => $contactID,
421 'eventID' => $this->_eventIds[0],
422 )
423 );
424 $result = $this->callAPISuccess('Event', 'Delete', array('id' => $this->_eventIds[0]));
425 }
426
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);
433 }
434
435 ///////////////// civicrm_event_search methods
436
437 /**
438 * Test civicrm_event_search with wrong params type
439 */
440 public function testSearchWrongParamsType() {
441 $params = 'a string';
442 $result = $this->callAPIFailure('event', 'get', $params);
443 }
444
445 /**
446 * Test civicrm_event_search with empty params
447 */
448 public function testSearchEmptyParams() {
449 $event = $this->callAPISuccess('event', 'create', $this->_params[1]);
450
451 $getparams = array(
452 'sequential' => 1,
453 );
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__);
459 }
460
461 /**
462 * Test civicrm_event_search. Success expected.
463 */
464 public function testSearch() {
465 $params = array(
466 'event_type_id' => 1,
467 'return.title' => 1,
468 'return.id' => 1,
469 'return.start_date' => 1,
470 );
471 $result = $this->callAPISuccess('event', 'get', $params);
472
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__);
475 }
476
477 /**
478 * Test civicrm_event_search. Success expected.
479 * return.offset and return.max_results test (CRM-5266)
480 */
481 public function testSearchWithOffsetAndMaxResults() {
482 $maxEvents = 5;
483 $events = array();
484 while ($maxEvents > 0) {
485 $params = array(
486 'title' => 'Test Event' . $maxEvents,
487 'event_type_id' => 2,
488 'start_date' => 20081021,
489 );
490
491 $events[$maxEvents] = $this->callAPISuccess('event', 'create', $params);
492 $maxEvents--;
493 }
494 $params = array(
495 'event_type_id' => 2,
496 'return.id' => 1,
497 'return.title' => 1,
498 'return.offset' => 2,
499 'return.max_results' => 2,
500 );
501 $result = $this->callAPISuccess('event', 'get', $params);
502 $this->assertAPISuccess($result);
503 $this->assertEquals(2, $result['count'], ' 2 results returned In line ' . __LINE__);
504 }
505
506 public function testEventCreationPermissions() {
507 $params = array(
508 'event_type_id' => 1,
509 'start_date' => '2010-10-03',
510 'title' => 'le cake is a tie',
511 'check_permissions' => TRUE,
512 );
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');
517
518 $config->userPermissionClass->permissions = array('access CiviEvent', 'edit all events', 'access CiviCRM');
519 $result = $this->callAPISuccess('event', 'create', $params);
520 }
521
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__);
527 }
528
529 /**
530 * Test api_action param also works
531 */
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__);
537 }
538
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__);
544 }
545
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']);
551 }
552 }