INFRA-132 add full stop after comments
[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 /**
114 * civicrm_event_get methods
115 */
116 public function testGetEventById() {
117 $params = array(
118 'id' => $this->_events[1]['id'],
119 );
120 $result = $this->callAPISuccess('event', 'get', $params);
121 $this->assertEquals($result['values'][$this->_eventIds[1]]['event_title'], 'Annual CiviCRM meet 2');
122 }
123
124 public function testGetEventByEventTitle() {
125
126 $params = array(
127 'event_title' => 'Annual CiviCRM meet',
128 'sequential' => TRUE,
129 );
130
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]);
134 }
135
136 public function testGetEventByWrongTitle() {
137 $params = array(
138 'title' => 'No event with that title',
139 );
140 $result = $this->callAPISuccess('Event', 'Get', $params);
141 $this->assertEquals(0, $result['count']);
142 }
143
144 public function testGetEventByIdSort() {
145 $params = array(
146 'return.sort' => 'id ASC',
147 'return.max_results' => 1,
148 );
149 $result = $this->callAPISuccess('Event', 'Get', $params);
150 $this->assertEquals(1, $result['id'], ' in line ' . __LINE__);
151 $params = array(
152 'options' => array(
153 'sort' => 'id DESC',
154 'limit' => 1,
155 ),
156 );
157
158 $result = $this->callAPISuccess('Event', 'Get', $params);
159 $this->assertAPISuccess($result, ' in line ' . __LINE__);
160 $this->assertEquals(2, $result['id'], ' in line ' . __LINE__);
161 $params = array(
162 'options' => array(
163 'sort' => 'id ASC',
164 'limit' => 1,
165 ),
166 );
167 $result = $this->callAPISuccess('Event', 'Get', $params);
168 $this->assertEquals(1, $result['id'], ' in line ' . __LINE__);
169
170 }
171 /*
172 * Getting the id back of an event.
173 * Does not work yet, bug in API
174 */
175
176 /*
177 public function testGetIdOfEventByEventTitle() {
178 $params = array( 'title' => 'Annual CiviCRM meet',
179 'return' => 'id'
180 );
181
182 $result = $this->callAPISuccess('Event', 'Get', $params);
183 }
184 */
185
186
187 /**
188 * Test 'is.Current' option. Existing event is 'old' so only current should be returned
189 */
190 public function testGetIsCurrent() {
191 $params = array(
192 'isCurrent' => 1,
193 );
194 $currentEventParams = array(
195 'start_date' => date('Y-m-d', strtotime('+ 1 day')),
196 'end_date' => date('Y-m-d', strtotime('+ 1 week')),
197 );
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'], '');
208 }
209
210 /**
211 * There has been a schema change & the api needs to buffer developers from it
212 */
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");
221 }
222
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);
228
229 }
230
231 /**
232 * Test 'is.Current' option. Existing event is 'old' so only current should be returned
233 */
234 public function testGetSingleReturnIsFull() {
235 $contactID = $this->individualCreate();
236 $params = array(
237 'id' => $this->_eventIds[0],
238 'max_participants' => 1,
239 );
240 $result = $this->callAPISuccess('Event', 'Create', $params);
241
242 $getEventParams = array(
243 'id' => $this->_eventIds[0],
244 'return.is_full' => 1,
245 );
246
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,
254 'role_id' => 1,
255 'contact_id' => $contactID,
256 'event_id' => $this->_eventIds[0],
257 ));
258 $currentEvent = $this->callAPIAndDocument('Event', 'getsingle', $getEventParams, __FUNCTION__, __FILE__, $description, $subfile, 'getsingle');
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__);
261
262 $this->contactDelete($contactID);
263 }
264
265 /**
266 * Legacy support for Contribution Type ID. We need to ensure this is supported
267 * as an alias for financial_type_id
268 */
269 public function testCreateGetEventLegacyContributionTypeID() {
270 $contributionTypeArray = array('contribution_type_id' => 3);
271 if (isset($this->_params[0]['financial_type_id'])) {
272 //in case someone edits $this->_params & invalidates this test :-)
273 unset($this->_params[0]['financial_type_id']);
274 }
275 $result = $this->callAPISuccess('event', 'create', $this->_params[0] + $contributionTypeArray);
276 $getresult = $this->callAPISuccess('event', 'get', array() + $contributionTypeArray);
277 $this->assertEquals($getresult['values'][$getresult['id']]['contribution_type_id'], 3);
278 $this->assertEquals($result['id'], $getresult['id']);
279 $this->callAPISuccess('event', 'delete', array('id' => $result['id']));
280 }
281 ///////////////// civicrm_event_create methods
282
283 /**
284 * Check with complete array + custom field
285 * Note that the test is written on purpose without any
286 * variables specific to participant so it can be replicated into other entities
287 * and / or moved to the automated test suite
288 */
289 public function testCreateWithCustom() {
290 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
291
292 $params = $this->_params[0];
293 $params['custom_' . $ids['custom_field_id']] = "custom string";
294
295 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
296
297 $check = $this->callAPISuccess($this->_entity, 'get', array(
298 'return.custom_' . $ids['custom_field_id'] => 1,
299 'id' => $result['id'],
300 ));
301 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
302
303 $this->customFieldDelete($ids['custom_field_id']);
304 $this->customGroupDelete($ids['custom_group_id']);
305 $this->callAPISuccess($this->_entity, 'Delete', array('id' => $result['id']));
306 }
307
308 /**
309 * Test that an event with a price set can be created.
310 */
311 public function testCreatePaidEvent() {
312 //@todo alter API so that an integer is converted to an array
313 $priceSetParams = array('price_set_id' => (array) 1, 'is_monetary' => 1);
314 $result = $this->callAPISuccess('Event', 'Create', array_merge($this->_params[0], $priceSetParams));
315 $event = $this->callAPISuccess('Event', 'getsingle', array('id' => $result['id'], 'return' => 'price_set_id'));
316 $this->assertArrayKeyExists('price_set_id', $event);
317 }
318
319 public function testCreateEventParamsNotArray() {
320 $params = NULL;
321 $result = $this->callAPIFailure('event', 'create', $params);
322 }
323
324 public function testCreateEventEmptyParams() {
325 $params = array();
326 $result = $this->callAPIFailure('event', 'create', $params);
327 }
328
329 public function testCreateEventParamsWithoutTitle() {
330 unset($this->_params['title']);
331 $result = $this->callAPIFailure('event', 'create', $this->_params);
332 $this->assertAPIFailure($result);
333 }
334
335 public function testCreateEventParamsWithoutEventTypeId() {
336 unset($this->_params['event_type_id']);
337 $result = $this->callAPIFailure('event', 'create', $this->_params);
338 }
339
340 public function testCreateEventParamsWithoutStartDate() {
341 unset($this->_params['start_date']);
342 $result = $this->callAPIFailure('event', 'create', $this->_params);
343 }
344
345 public function testCreateEventSuccess() {
346 $result = $this->callAPIAndDocument('Event', 'Create', $this->_params[0], __FUNCTION__, __FILE__);
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 $this->assertEquals('2008-10-21 00:00:00', $result['values'][$result['id']]['start_date'], 'start date is not set in line ' . __LINE__);
351 $this->assertEquals('2008-10-23 00:00:00', $result['values'][$result['id']]['end_date'], 'end date is not set in line ' . __LINE__);
352 $this->assertEquals('2008-06-01 00:00:00', $result['values'][$result['id']]['registration_start_date'], 'start date is not set in line ' . __LINE__);
353 $this->assertEquals('2008-10-15 00:00:00', $result['values'][$result['id']]['registration_end_date'], 'end date is not set in line ' . __LINE__);
354 }
355
356 /**
357 * Test that passing in Unique field names works.
358 */
359 public function testCreateEventSuccessUniqueFieldNames() {
360 $this->_params[0]['event_start_date'] = $this->_params[0]['start_date'];
361 unset($this->_params[1]['start_date']);
362 $this->_params[0]['event_title'] = $this->_params[0]['title'];
363 unset($this->_params[0]['title']);
364 $result = $this->callAPISuccess('Event', 'Create', $this->_params[0]);
365 $this->assertAPISuccess($result, 'In line ' . __LINE__);
366 $this->assertArrayHasKey('id', $result['values'][$result['id']], 'In line ' . __LINE__);
367 $result = $this->callAPISuccess($this->_entity, 'Get', array('id' => $result['id']));
368 $this->callAPISuccess($this->_entity, 'Delete', array('id' => $result['id']));
369
370 $this->assertEquals('2008-10-21 00:00:00', $result['values'][$result['id']]['start_date'], 'start date is not set in line ' . __LINE__);
371 $this->assertEquals('2008-10-23 00:00:00', $result['values'][$result['id']]['end_date'], 'end date is not set in line ' . __LINE__);
372 $this->assertEquals('2008-06-01 00:00:00', $result['values'][$result['id']]['registration_start_date'], 'start date is not set in line ' . __LINE__);
373 $this->assertEquals('2008-10-15 00:00:00', $result['values'][$result['id']]['registration_end_date'], 'end date is not set in line ' . __LINE__);
374 $this->assertEquals($this->_params[0]['event_title'], $result['values'][$result['id']]['title'], 'end date is not set in line ' . __LINE__);
375 }
376
377 public function testUpdateEvent() {
378 $result = $this->callAPISuccess('event', 'create', $this->_params[1]);
379
380 $params = array(
381 'id' => $result['id'],
382 'max_participants' => 150,
383 );
384 $this->callAPISuccess('Event', 'Create', $params);
385 $updated = $this->callAPISuccess('Event', 'Get', $params, __FUNCTION__, __FILE__);
386 $this->assertEquals(150, $updated['values'][$result['id']]['max_participants']);
387 $this->assertEquals('Annual CiviCRM meet 2', $updated['values'][$result['id']]['title']);
388 $this->callAPISuccess($this->_entity, 'Delete', array('id' => $result['id']));
389 }
390
391
392 public function testDeleteEmptyParams() {
393 $result = $this->callAPIFailure('Event', 'Delete', array());
394 }
395
396 public function testDelete() {
397 $params = array(
398 'id' => $this->_eventIds[0],
399 );
400 $result = $this->callAPIAndDocument('Event', 'Delete', $params, __FUNCTION__, __FILE__);
401 }
402
403 /**
404 * Check event_id still supported for delete
405 */
406 public function testDeleteWithEventId() {
407 $params = array(
408 'event_id' => $this->_eventIds[0],
409 );
410 $result = $this->callAPISuccess('Event', 'Delete', $params);
411 $this->assertAPISuccess($result, 'in line ' . __LINE__);
412 }
413
414 /**
415 * Trying to delete an event with participants should return error.
416 */
417 public function testDeleteWithExistingParticipant() {
418 $contactID = $this->individualCreate();
419 $participantID = $this->participantCreate(
420 array(
421 'contactID' => $contactID,
422 'eventID' => $this->_eventIds[0],
423 )
424 );
425 $result = $this->callAPISuccess('Event', 'Delete', array('id' => $this->_eventIds[0]));
426 }
427
428 public function testDeleteWithWrongEventId() {
429 $params = array('event_id' => $this->_eventIds[0]);
430 $result = $this->callAPISuccess('Event', 'Delete', $params);
431 // try to delete again - there's no such event anymore
432 $params = array('event_id' => $this->_eventIds[0]);
433 $result = $this->callAPIFailure('Event', 'Delete', $params);
434 }
435
436 ///////////////// civicrm_event_search methods
437
438 /**
439 * Test civicrm_event_search with wrong params type
440 */
441 public function testSearchWrongParamsType() {
442 $params = 'a string';
443 $result = $this->callAPIFailure('event', 'get', $params);
444 }
445
446 /**
447 * Test civicrm_event_search with empty params.
448 */
449 public function testSearchEmptyParams() {
450 $this->callAPISuccess('event', 'create', $this->_params[1]);
451
452 $getParams = array(
453 'sequential' => 1,
454 );
455 $result = $this->callAPISuccess('event', 'get', $getParams);
456 $this->assertEquals($result['count'], 3);
457 $res = $result['values'][0];
458 $this->assertArrayKeyExists('title', $res);
459 $this->assertEquals($res['event_type_id'], $this->_params[1]['event_type_id']);
460 }
461
462 /**
463 * Test civicrm_event_search. Success expected.
464 */
465 public function testSearch() {
466 $params = array(
467 'event_type_id' => 1,
468 'return.title' => 1,
469 'return.id' => 1,
470 'return.start_date' => 1,
471 );
472 $result = $this->callAPISuccess('event', 'get', $params);
473
474 $this->assertEquals($result['values'][$this->_eventIds[0]]['id'], $this->_eventIds[0], 'In line ' . __LINE__);
475 $this->assertEquals($result['values'][$this->_eventIds[0]]['title'], 'Annual CiviCRM meet', 'In line ' . __LINE__);
476 }
477
478 /**
479 * Test civicrm_event_search. Success expected.
480 * return.offset and return.max_results test (CRM-5266)
481 */
482 public function testSearchWithOffsetAndMaxResults() {
483 $maxEvents = 5;
484 $events = array();
485 while ($maxEvents > 0) {
486 $params = array(
487 'title' => 'Test Event' . $maxEvents,
488 'event_type_id' => 2,
489 'start_date' => 20081021,
490 );
491
492 $events[$maxEvents] = $this->callAPISuccess('event', 'create', $params);
493 $maxEvents--;
494 }
495 $params = array(
496 'event_type_id' => 2,
497 'return.id' => 1,
498 'return.title' => 1,
499 'return.offset' => 2,
500 'return.max_results' => 2,
501 );
502 $result = $this->callAPISuccess('event', 'get', $params);
503 $this->assertAPISuccess($result);
504 $this->assertEquals(2, $result['count'], ' 2 results returned In line ' . __LINE__);
505 }
506
507 public function testEventCreationPermissions() {
508 $params = array(
509 'event_type_id' => 1,
510 'start_date' => '2010-10-03',
511 'title' => 'le cake is a tie',
512 'check_permissions' => TRUE,
513 );
514 $config = &CRM_Core_Config::singleton();
515 $config->userPermissionClass->permissions = array('access CiviCRM');
516 $result = $this->callAPIFailure('event', 'create', $params);
517 $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
519 $config->userPermissionClass->permissions = array('access CiviEvent', 'edit all events', 'access CiviCRM');
520 $result = $this->callAPISuccess('event', 'create', $params);
521 }
522
523 public function testgetfields() {
524 $description = "demonstrate use of getfields to interrogate api";
525 $params = array('action' => 'create');
526 $result = $this->callAPISuccess('event', 'getfields', $params);
527 $this->assertEquals(1, $result['values']['title']['api.required'], 'in line ' . __LINE__);
528 }
529
530 /**
531 * Test api_action param also works
532 */
533 public function testgetfieldsRest() {
534 $description = "demonstrate use of getfields to interrogate api";
535 $params = array('api_action' => 'create');
536 $result = $this->callAPISuccess('event', 'getfields', $params);
537 $this->assertEquals(1, $result['values']['title']['api.required'], 'in line ' . __LINE__);
538 }
539
540 public function testgetfieldsGet() {
541 $description = "demonstrate use of getfields to interrogate api";
542 $params = array('action' => 'get');
543 $result = $this->callAPISuccess('event', 'getfields', $params);
544 $this->assertEquals('title', $result['values']['event_title']['name'], 'in line ' . __LINE__);
545 }
546
547 public function testgetfieldsDelete() {
548 $description = "demonstrate use of getfields to interrogate api";
549 $params = array('action' => 'delete');
550 $result = $this->callAPISuccess('event', 'getfields', $params);
551 $this->assertEquals(1, $result['values']['id']['api.required']);
552 }
553
554 }