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