Merge pull request #10195 from seanmadsen/CRM-20453
[civicrm-core.git] / tests / phpunit / api / v3 / EventTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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 * Class api_v3_EventTest
30 * @group headless
31 */
32 class api_v3_EventTest extends CiviUnitTestCase {
33 protected $_params;
34 protected $_apiversion;
35 protected $_entity;
36
37 public function setUp() {
38 parent::setUp();
39 $this->_apiversion = 3;
40 $this->_entity = 'event';
41 $this->_params = array(
42 array(
43 'title' => 'Annual CiviCRM meet',
44 'summary' => 'If you have any CiviCRM realted issues or want to track where CiviCRM is heading, Sign up now',
45 'description' => 'This event is intended to give brief idea about progess of CiviCRM and giving solutions to common user issues',
46 'event_type_id' => 1,
47 'is_public' => 1,
48 'start_date' => 20081021,
49 'end_date' => 20081023,
50 'is_online_registration' => 1,
51 'registration_start_date' => 20080601,
52 'registration_end_date' => '2008-10-15',
53 'max_participants' => 100,
54 'event_full_text' => 'Sorry! We are already full',
55 'is_monetary' => 0,
56 'is_active' => 1,
57 'is_show_location' => 0,
58 ),
59 array(
60 'title' => 'Annual CiviCRM meet 2',
61 'summary' => 'If you have any CiviCRM realted issues or want to track where CiviCRM is heading, Sign up now',
62 'description' => 'This event is intended to give brief idea about progess of CiviCRM and giving solutions to common user issues',
63 'event_type_id' => 1,
64 'is_public' => 1,
65 'start_date' => 20101021,
66 'end_date' => 20101023,
67 'is_online_registration' => 1,
68 'registration_start_date' => 20100601,
69 'registration_end_date' => '2010-10-15',
70 'max_participants' => 100,
71 'event_full_text' => 'Sorry! We are already full',
72 'is_monetory' => 0,
73 'is_active' => 1,
74 'is_show_location' => 0,
75 ),
76 );
77
78 $params = array(
79 array(
80 'title' => 'Annual CiviCRM meet',
81 'event_type_id' => 1,
82 'start_date' => 20081021,
83 ),
84 array(
85 'title' => 'Annual CiviCRM meet 2',
86 'event_type_id' => 1,
87 'start_date' => 20101021,
88 ),
89 );
90
91 $this->events = array();
92 $this->eventIds = array();
93 foreach ($params as $event) {
94 $result = $this->callAPISuccess('Event', 'Create', $event);
95 $this->_events[] = $result;
96 $this->_eventIds[] = $result['id'];
97 }
98 }
99
100 public function tearDown() {
101 foreach ($this->eventIds as $eventId) {
102 $this->eventDelete($eventId);
103 }
104 $tablesToTruncate = array(
105 'civicrm_participant',
106 'civicrm_event',
107 );
108 $this->quickCleanup($tablesToTruncate, TRUE);
109 }
110
111 /**
112 * civicrm_event_get methods.
113 */
114 public function testGetEventById() {
115 $params = array(
116 'id' => $this->_events[1]['id'],
117 );
118 $result = $this->callAPISuccess('event', 'get', $params);
119 $this->assertEquals($result['values'][$this->_eventIds[1]]['event_title'], 'Annual CiviCRM meet 2');
120 }
121
122 /**
123 * Test getLocationEvents() function invokes selectWhereClause() hook
124 */
125 public function testGetEventWithPermissionHook() {
126 $address = $this->callAPISuccess('address', 'create', array(
127 'contact_id' => 'null',
128 'location_type_id' => 1,
129 'street_address' => '1234567',
130 ));
131 $params = array(
132 'address_id' => $address['id'],
133 );
134 $result = $this->callAPISuccess('loc_block', 'create', $params);
135 $params = array(
136 'id' => $this->_events[1]['id'],
137 'loc_block_id' => $result['id'],
138 );
139 $this->callAPISuccess('Event', 'create', $params);
140 $result = CRM_Event_BAO_Event::getLocationEvents();
141 $this->assertEquals(1, count($result));
142
143 $this->hookClass->setHook('civicrm_selectWhereClause', array($this, 'selectWhereClauseHook'));
144 $result = CRM_Event_BAO_Event::getLocationEvents();
145 $this->assertEquals(0, count($result));
146 }
147
148 public function testGetEventByEventTitle() {
149
150 $params = array(
151 'event_title' => 'Annual CiviCRM meet',
152 'sequential' => TRUE,
153 );
154
155 $result = $this->callAPIAndDocument('event', 'get', $params, __FUNCTION__, __FILE__);
156 $this->assertEquals(1, $result['count']);
157 $this->assertEquals($result['values'][0]['id'], $this->_eventIds[0]);
158 }
159
160 public function testGetEventByWrongTitle() {
161 $params = array(
162 'title' => 'No event with that title',
163 );
164 $result = $this->callAPISuccess('Event', 'Get', $params);
165 $this->assertEquals(0, $result['count']);
166 }
167
168 public function testGetEventByIdSort() {
169 $params = array(
170 'return.sort' => 'id ASC',
171 'return.max_results' => 1,
172 );
173 $result = $this->callAPISuccess('Event', 'Get', $params);
174 $this->assertEquals(1, $result['id'], ' in line ' . __LINE__);
175 $params = array(
176 'options' => array(
177 'sort' => 'id DESC',
178 'limit' => 1,
179 ),
180 );
181
182 $result = $this->callAPISuccess('Event', 'Get', $params);
183 $this->assertAPISuccess($result, ' in line ' . __LINE__);
184 $this->assertEquals(2, $result['id'], ' in line ' . __LINE__);
185 $params = array(
186 'options' => array(
187 'sort' => 'id ASC',
188 'limit' => 1,
189 ),
190 );
191 $result = $this->callAPISuccess('Event', 'Get', $params);
192 $this->assertEquals(1, $result['id'], ' in line ' . __LINE__);
193
194 }
195 /*
196 * Getting the id back of an event.
197 * Does not work yet, bug in API
198 */
199
200 /*
201 public function testGetIdOfEventByEventTitle() {
202 $params = array( 'title' => 'Annual CiviCRM meet',
203 'return' => 'id'
204 );
205
206 $result = $this->callAPISuccess('Event', 'Get', $params);
207 }
208 */
209
210
211 /**
212 * Test 'is.Current' option. Existing event is 'old' so only current should be returned
213 */
214 public function testGetIsCurrent() {
215 $params = array(
216 'isCurrent' => 1,
217 );
218 $currentEventParams = array(
219 'start_date' => date('Y-m-d', strtotime('+ 1 day')),
220 'end_date' => date('Y-m-d', strtotime('+ 1 week')),
221 );
222 $currentEventParams = array_merge($this->_params[1], $currentEventParams);
223 $currentEvent = $this->callAPISuccess('Event', 'Create', $currentEventParams);
224 $description = "Demonstrates use of is.Current option.";
225 $subfile = "IsCurrentOption";
226 $result = $this->callAPIAndDocument('Event', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile);
227 $allEvents = $this->callAPISuccess('Event', 'Get', array());
228 $this->callAPISuccess('Event', 'Delete', array('id' => $currentEvent['id']));
229 $this->assertEquals(1, $result['count'], 'confirm only one event found in line ' . __LINE__);
230 $this->assertEquals(3, $allEvents['count'], 'confirm three events exist (ie. two not found) ' . __LINE__);
231 $this->assertEquals($currentEvent['id'], $result['id'], '');
232 }
233
234 /**
235 * There has been a schema change & the api needs to buffer developers from it
236 */
237 public function testGetPaymentProcessorId() {
238 $params = $this->_params[0];
239 $params['payment_processor_id'] = 1;
240 $params['sequential'] = 1;
241 $result = $this->callAPISuccess('event', 'create', $params);
242 $this->assertEquals(1, $result['values'][0]['payment_processor'][0], "handing of payment processor compatibility");
243 $result = $this->callAPISuccess('event', 'get', $params);
244 $this->assertEquals($result['values'][0]['payment_processor_id'], 1, "handing get payment processor compatibility");
245 }
246
247 public function testInvalidData() {
248 $params = $this->_params[0];
249 $params['sequential'] = 1;
250 $params['loc_block_id'] = 100;
251 $result = $this->callAPIFailure('event', 'create', $params);
252
253 }
254
255 /**
256 * Test 'is.Current' option. Existing event is 'old' so only current should be returned
257 */
258 public function testGetSingleReturnIsFull() {
259 $contactID = $this->individualCreate();
260 $params = array(
261 'id' => $this->_eventIds[0],
262 'max_participants' => 1,
263 );
264 $result = $this->callAPISuccess('Event', 'Create', $params);
265
266 $getEventParams = array(
267 'id' => $this->_eventIds[0],
268 'return.is_full' => 1,
269 );
270
271 $currentEvent = $this->callAPISuccess('Event', 'getsingle', $getEventParams);
272 $description = "Demonstrates use of return is_full .";
273 $subfile = "IsFullOption";
274 $this->assertEquals(0, $currentEvent['is_full'], ' is full is set in line ' . __LINE__);
275 $this->assertEquals(1, $currentEvent['available_places'], 'available places is set in line ' . __LINE__);
276 $participant = $this->callAPISuccess('Participant', 'create', array(
277 'participant_status' => 1,
278 'role_id' => 1,
279 'contact_id' => $contactID,
280 'event_id' => $this->_eventIds[0],
281 ));
282 $currentEvent = $this->callAPIAndDocument('Event', 'getsingle', $getEventParams, __FUNCTION__, __FILE__, $description, $subfile);
283 $this->assertEquals(1, $currentEvent['is_full'], ' is full is set in line ' . __LINE__);
284 $this->assertEquals(0, $currentEvent['available_places'], 'available places is set in line ' . __LINE__);
285
286 $this->contactDelete($contactID);
287 }
288
289 /**
290 * Legacy support for Contribution Type ID.
291 *
292 * We need to ensure this is supported as an alias for financial_type_id.
293 */
294 public function testCreateGetEventLegacyContributionTypeID() {
295 $contributionTypeArray = array('contribution_type_id' => 3);
296 if (isset($this->_params[0]['financial_type_id'])) {
297 //in case someone edits $this->_params & invalidates this test :-)
298 unset($this->_params[0]['financial_type_id']);
299 }
300 $result = $this->callAPISuccess('event', 'create', $this->_params[0] + $contributionTypeArray);
301 $getresult = $this->callAPISuccess('event', 'get', array() + $contributionTypeArray);
302 $this->assertEquals($getresult['values'][$getresult['id']]['contribution_type_id'], 3);
303 $this->assertEquals($result['id'], $getresult['id']);
304 $this->callAPISuccess('event', 'delete', array('id' => $result['id']));
305 }
306
307 /**
308 * Chaining get event and loc block.
309 */
310 public function testChainingGetLocBlock() {
311 // create a loc block and an event for that loc block.
312 $eventParams = $this->_params[0];
313 $eventParams['loc_bloc_id'] = '$value.id';
314 $locBlockParams = array(
315 'address' => array(
316 'street_address' => 'Kipdorp 24',
317 'postal_code' => '2000',
318 'city' => 'Antwerpen',
319 'country_id' => '1020',
320 'location_type_id' => '1',
321 ),
322 'api.Event.create' => $eventParams,
323 'sequential' => 1,
324 );
325 $createResult = $this->callAPISuccess('LocBlock', 'create', $locBlockParams);
326 $locBlockId = $createResult['id'];
327 $eventId = $createResult['values'][0]['api.Event.create']['id'];
328
329 // request the event with its loc block:
330 $check = $this->callAPISuccess($this->_entity, 'getsingle', array(
331 'id' => $eventId,
332 'api.LocBlock.get' => array('id' => '$value.loc_block_id'),
333 'sequential' => 1,
334 ));
335
336 // assert
337 $this->assertEquals($eventId, $check['id'], ' in line ' . __LINE__);
338 $this->assertEquals(1, $check['api.LocBlock.get']['count'], ' in line ' . __LINE__);
339 $this->assertEquals($locBlockId, $check['api.LocBlock.get']['id'], ' in line ' . __LINE__);
340
341 // cleanup
342 $this->callAPISuccess($this->_entity, 'delete', array('id' => $eventId));
343 }
344
345 /**
346 * Chaining get event and non existing loc block.
347 *
348 * Even if there is no loc block, at least the event should be returned.
349 * http://forum.civicrm.org/index.php/topic,36113.0.html
350 */
351 public function testChainingGetNonExistingLocBlock() {
352 $params = $this->_params[0];
353 $result = $this->callAPISuccess($this->_entity, 'create', $params);
354
355 $check = $this->callAPISuccess($this->_entity, 'get', array(
356 'id' => $result['id'],
357 // this chaining request should not break things:
358 'api.LocBlock.get' => array('id' => '$value.loc_block_id'),
359 ));
360 $this->assertEquals($result['id'], $check['id']);
361
362 $this->callAPISuccess($this->_entity, 'Delete', array('id' => $result['id']));
363 }
364
365 /**
366 * Check with complete array + custom field.
367 *
368 * Note that the test is written on purpose without any
369 * variables specific to participant so it can be replicated into other entities
370 * and / or moved to the automated test suite.
371 */
372 public function testCreateWithCustom() {
373 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
374
375 $params = $this->_params[0];
376 $params['custom_' . $ids['custom_field_id']] = "custom string";
377
378 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
379
380 $check = $this->callAPISuccess($this->_entity, 'get', array(
381 'return.custom_' . $ids['custom_field_id'] => 1,
382 'id' => $result['id'],
383 ));
384 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
385
386 $this->customFieldDelete($ids['custom_field_id']);
387 $this->customGroupDelete($ids['custom_group_id']);
388 $this->callAPISuccess($this->_entity, 'Delete', array('id' => $result['id']));
389 }
390
391 /**
392 * Check searching on custom fields.
393 *
394 * https://issues.civicrm.org/jira/browse/CRM-16036
395 */
396 public function testSearchCustomField() {
397 // create custom group with custom field on event
398 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
399
400 // Search for events having CRM-16036 as the value for this custom
401 // field. This should not return anything.
402 $check = $this->callAPISuccess($this->_entity, 'get', array(
403 'custom_' . $ids['custom_field_id'] => 'CRM-16036',
404 ));
405
406 $this->assertEquals(0, $check['count']);
407
408 $this->customFieldDelete($ids['custom_field_id']);
409 $this->customGroupDelete($ids['custom_group_id']);
410 }
411
412 /**
413 * Test searching on custom fields returning a contact reference.
414 *
415 * https://issues.civicrm.org/jira/browse/CRM-16036
416 */
417 public function testEventGetCustomContactRefFieldCRM16036() {
418 // Create some contact.
419 $test_contact_name = 'Contact, Test';
420 $contact_save_result = $this->callAPISuccess('contact', 'create', array(
421 'sort_name' => $test_contact_name,
422 'contact_type' => 'Individual',
423 'display_name' => $test_contact_name,
424 ));
425 $contact_id = $contact_save_result['id'];
426
427 // I have no clue what this $subfile is about. I just copied it from another
428 // unit test.
429 $subfile = 'ContactRefCustomField';
430 $description = "Demonstrates get with Contact Reference Custom Field.";
431
432 // Create a custom group, and add a custom contact reference field.
433 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
434 $params = array(
435 'custom_group_id' => $ids['custom_group_id'],
436 'name' => 'Worker_Lookup',
437 'label' => 'Worker Lookup',
438 'html_type' => 'Autocomplete-Select',
439 'data_type' => 'ContactReference',
440 'weight' => 4,
441 'is_searchable' => 1,
442 'is_active' => 1,
443 );
444 $customField = $this->callAPISuccess('custom_field', 'create', $params);
445
446 // Create an event, and add the contact as custom value.
447 $params = $this->_params;
448 $params['title'] = "My test event.";
449 $params['start_date'] = "2015-03-14";
450 // Just assume that an event type 1 exists.
451 $params['event_type_id'] = 1;
452 $params['custom_' . $customField['id']] = "$contact_id";
453
454 $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
455
456 // Retrieve the activity, search for the contact.
457 $result = $this->callAPIAndDocument($this->_entity, 'get', array(
458 'return.custom_' . $customField['id'] => 1,
459 'custom_' . $customField['id'] => $contact_id,
460 ), __FUNCTION__, __FILE__, $description, $subfile);
461
462 $this->assertEquals($test_contact_name, $result['values'][$result['id']]['custom_' . $customField['id']]);
463 $this->assertEquals($contact_id, $result['values'][$result['id']]['custom_' . $customField['id'] . "_id"], ' in line ' . __LINE__);
464 // Not sure whether I should test for custom_X_1 and custom_X_1_id as well.
465 // (1 being the id of the record in the custom value table)
466
467 $this->customFieldDelete($ids['custom_field_id']);
468 $this->customGroupDelete($ids['custom_group_id']);
469 $this->callAPISuccess('contact', 'delete', array(
470 'id' => $contact_id,
471 'skip_undelete' => TRUE,
472 ));
473 }
474
475 /**
476 * Test searching on custom fields with less than or equal.
477 *
478 * See CRM-17101.
479 */
480 public function testEventGetCustomFieldLte() {
481 // create custom group with custom field on event
482 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
483
484 // Create an event, with a custom value.
485 $params = $this->_params;
486 $params['title'] = "My test event.";
487 $params['start_date'] = "2015-03-14";
488 // Just assume that an event type 1 exists.
489 $params['event_type_id'] = 1;
490 $params['custom_' . $ids['custom_field_id']] = "AAAA";
491
492 $save_result = $this->callApiSuccess($this->_entity, 'create', $params);
493
494 // Retrieve the activity, search for custom field < 'BBBB'
495 $get_result = $this->callAPISuccess($this->_entity, 'get', array(
496 'return.custom_' . $ids['custom_field_id'] => 1,
497 'custom_' . $ids['custom_field_id'] => array('<=' => 'BBBB'),
498 ));
499
500 // Expect that we find the saved event.
501 $this->assertArrayKeyExists($save_result['id'], $get_result['values']);
502
503 $this->callAPISuccess($this->_entity, 'Delete', array('id' => $save_result['id']));
504 }
505
506 /**
507 * Test searching on custom fields with netsted call with id param.
508 *
509 * Search for an event on a custom field, and perform a chained call
510 * to retrieve it's (non-existing) loc block, using $value-substitution.
511 * This test just checks whether the event is found, because something
512 * happened in CiviCRM 4.6.5 that broke my fix for CRM-16036, causing
513 * CiviCRM to return 0 results.
514 * Of course, CRM-16168 should also be fixed for this test to pass.
515 */
516 public function testEventSearchCustomFieldWithChainedCall() {
517 // Create a custom group, and add a custom contact reference field.
518 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
519 $custom_field_id = $ids['custom_field_id'];
520
521 // Create an event with a custom value.
522 $params = $this->_params;
523 $params['title'] = "My test event.";
524 $params['start_date'] = "2015-03-14";
525 // Just assume that an event type 1 exists.
526 $params['event_type_id'] = 1;
527 $params['custom_' . $custom_field_id] = "12345";
528
529 $this->callAPISuccess($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
530
531 // Retrieve the activity, and chain loc block using $value.
532 $result = $this->callAPISuccess($this->_entity, 'get', array(
533 'custom_' . $custom_field_id => "12345",
534 'api.LocBlock.get' => array("id" => '$value.loc_block_id'),
535 ));
536
537 $this->assertEquals(1, $result['count']);
538
539 $this->customFieldDelete($ids['custom_field_id']);
540 $this->customGroupDelete($ids['custom_group_id']);
541 $this->callAPISuccess('event', 'delete', array(
542 'id' => $result['id'],
543 'skip_undelete' => TRUE,
544 ));
545 }
546
547
548 /**
549 * Test that an event with a price set can be created.
550 */
551 public function testCreatePaidEvent() {
552 //@todo alter API so that an integer is converted to an array
553 $priceSetParams = array('price_set_id' => (array) 1, 'is_monetary' => 1);
554 $result = $this->callAPISuccess('Event', 'Create', array_merge($this->_params[0], $priceSetParams));
555 $event = $this->callAPISuccess('Event', 'getsingle', array('id' => $result['id'], 'return' => 'price_set_id'));
556 $this->assertArrayKeyExists('price_set_id', $event);
557 }
558
559 public function testCreateEventParamsNotArray() {
560 $params = NULL;
561 $result = $this->callAPIFailure('event', 'create', $params);
562 }
563
564 public function testCreateEventEmptyParams() {
565 $params = array();
566 $result = $this->callAPIFailure('event', 'create', $params);
567 }
568
569 public function testCreateEventParamsWithoutTitle() {
570 unset($this->_params['title']);
571 $result = $this->callAPIFailure('event', 'create', $this->_params);
572 $this->assertAPIFailure($result);
573 }
574
575 public function testCreateEventParamsWithoutEventTypeId() {
576 unset($this->_params['event_type_id']);
577 $result = $this->callAPIFailure('event', 'create', $this->_params);
578 }
579
580 public function testCreateEventParamsWithoutStartDate() {
581 unset($this->_params['start_date']);
582 $result = $this->callAPIFailure('event', 'create', $this->_params);
583 }
584
585 public function testCreateEventSuccess() {
586 $result = $this->callAPIAndDocument('Event', 'Create', $this->_params[0], __FUNCTION__, __FILE__);
587 $this->assertArrayHasKey('id', $result['values'][$result['id']]);
588 $result = $this->callAPISuccess($this->_entity, 'Get', array('id' => $result['id']));
589 $this->callAPISuccess($this->_entity, 'Delete', array('id' => $result['id']));
590 $this->assertEquals('2008-10-21 00:00:00', $result['values'][$result['id']]['start_date'], 'start date is not set');
591 $this->assertEquals('2008-10-23 00:00:00', $result['values'][$result['id']]['end_date'], 'end date is not set');
592 $this->assertEquals('2008-06-01 00:00:00', $result['values'][$result['id']]['registration_start_date'], 'start date is not set');
593 $this->assertEquals('2008-10-15 00:00:00', $result['values'][$result['id']]['registration_end_date'], 'end date is not set');
594 }
595
596 /**
597 * Test that passing in Unique field names works.
598 */
599 public function testCreateEventSuccessUniqueFieldNames() {
600 $this->_params[0]['event_start_date'] = $this->_params[0]['start_date'];
601 unset($this->_params[1]['start_date']);
602 $this->_params[0]['event_title'] = $this->_params[0]['title'];
603 unset($this->_params[0]['title']);
604 $result = $this->callAPISuccess('Event', 'Create', $this->_params[0]);
605 $this->assertAPISuccess($result);
606 $this->assertArrayHasKey('id', $result['values'][$result['id']]);
607 $result = $this->callAPISuccess($this->_entity, 'Get', array('id' => $result['id']));
608 $this->callAPISuccess($this->_entity, 'Delete', array('id' => $result['id']));
609
610 $this->assertEquals('2008-10-21 00:00:00', $result['values'][$result['id']]['start_date'], 'start date is not set in line ' . __LINE__);
611 $this->assertEquals('2008-10-23 00:00:00', $result['values'][$result['id']]['end_date'], 'end date is not set in line ' . __LINE__);
612 $this->assertEquals('2008-06-01 00:00:00', $result['values'][$result['id']]['registration_start_date'], 'start date is not set in line ' . __LINE__);
613 $this->assertEquals('2008-10-15 00:00:00', $result['values'][$result['id']]['registration_end_date'], 'end date is not set in line ' . __LINE__);
614 $this->assertEquals($this->_params[0]['event_title'], $result['values'][$result['id']]['title'], 'end date is not set in line ' . __LINE__);
615 }
616
617 public function testUpdateEvent() {
618 $result = $this->callAPISuccess('event', 'create', $this->_params[1]);
619
620 $params = array(
621 'id' => $result['id'],
622 'max_participants' => 150,
623 );
624 $this->callAPISuccess('Event', 'Create', $params);
625 $updated = $this->callAPISuccess('Event', 'Get', $params, __FUNCTION__, __FILE__);
626 $this->assertEquals(150, $updated['values'][$result['id']]['max_participants']);
627 $this->assertEquals('Annual CiviCRM meet 2', $updated['values'][$result['id']]['title']);
628 $this->callAPISuccess($this->_entity, 'Delete', array('id' => $result['id']));
629 }
630
631
632 public function testDeleteEmptyParams() {
633 $result = $this->callAPIFailure('Event', 'Delete', array());
634 }
635
636 public function testDelete() {
637 $params = array(
638 'id' => $this->_eventIds[0],
639 );
640 $this->callAPIAndDocument('Event', 'Delete', $params, __FUNCTION__, __FILE__);
641 }
642
643 /**
644 * Check event_id still supported for delete.
645 */
646 public function testDeleteWithEventId() {
647 $params = array(
648 'event_id' => $this->_eventIds[0],
649 );
650 $result = $this->callAPISuccess('Event', 'Delete', $params);
651 $this->assertAPISuccess($result);
652 }
653
654 /**
655 * Trying to delete an event with participants should return error.
656 */
657 public function testDeleteWithExistingParticipant() {
658 $contactID = $this->individualCreate();
659 $this->participantCreate(
660 array(
661 'contactID' => $contactID,
662 'eventID' => $this->_eventIds[0],
663 )
664 );
665 $this->callAPISuccess('Event', 'Delete', array('id' => $this->_eventIds[0]));
666 }
667
668 public function testDeleteWithWrongEventId() {
669 $params = array('event_id' => $this->_eventIds[0]);
670 $result = $this->callAPISuccess('Event', 'Delete', $params);
671 // try to delete again - there's no such event anymore
672 $params = array('event_id' => $this->_eventIds[0]);
673 $result = $this->callAPIFailure('Event', 'Delete', $params);
674 }
675
676 /**
677 * Test civicrm_event_search with wrong params type.
678 */
679 public function testSearchWrongParamsType() {
680 $params = 'a string';
681 $result = $this->callAPIFailure('event', 'get', $params);
682 }
683
684 /**
685 * Test civicrm_event_search with empty params.
686 */
687 public function testSearchEmptyParams() {
688 $this->callAPISuccess('event', 'create', $this->_params[1]);
689
690 $getParams = array(
691 'sequential' => 1,
692 );
693 $result = $this->callAPISuccess('event', 'get', $getParams);
694 $this->assertEquals($result['count'], 3);
695 $res = $result['values'][0];
696 $this->assertArrayKeyExists('title', $res);
697 $this->assertEquals($res['event_type_id'], $this->_params[1]['event_type_id']);
698 }
699
700 /**
701 * Test civicrm_event_search. Success expected.
702 */
703 public function testSearch() {
704 $params = array(
705 'event_type_id' => 1,
706 'return.title' => 1,
707 'return.id' => 1,
708 'return.start_date' => 1,
709 );
710 $result = $this->callAPISuccess('event', 'get', $params);
711
712 $this->assertEquals($result['values'][$this->_eventIds[0]]['id'], $this->_eventIds[0]);
713 $this->assertEquals($result['values'][$this->_eventIds[0]]['title'], 'Annual CiviCRM meet');
714 }
715
716 /**
717 * Test civicrm_event_search.
718 *
719 * Success expected.
720 *
721 * return.offset and return.max_results test (CRM-5266)
722 */
723 public function testSearchWithOffsetAndMaxResults() {
724 $maxEvents = 5;
725 $events = array();
726 while ($maxEvents > 0) {
727 $params = array(
728 'title' => 'Test Event' . $maxEvents,
729 'event_type_id' => 2,
730 'start_date' => 20081021,
731 );
732
733 $events[$maxEvents] = $this->callAPISuccess('event', 'create', $params);
734 $maxEvents--;
735 }
736 $params = array(
737 'event_type_id' => 2,
738 'return.id' => 1,
739 'return.title' => 1,
740 'return.offset' => 2,
741 'return.max_results' => 2,
742 );
743 $result = $this->callAPISuccess('event', 'get', $params);
744 $this->assertAPISuccess($result);
745 $this->assertEquals(2, $result['count'], ' 2 results returned In line ' . __LINE__);
746 }
747
748 public function testEventCreationPermissions() {
749 $params = array(
750 'event_type_id' => 1,
751 'start_date' => '2010-10-03',
752 'title' => 'le cake is a tie',
753 'check_permissions' => TRUE,
754 );
755 $config = &CRM_Core_Config::singleton();
756 $config->userPermissionClass->permissions = array('access CiviCRM');
757 $result = $this->callAPIFailure('event', 'create', $params);
758 $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');
759
760 $config->userPermissionClass->permissions = array(
761 'access CiviEvent',
762 'edit all events',
763 'access CiviCRM',
764 );
765 $result = $this->callAPISuccess('event', 'create', $params);
766 }
767
768 public function testgetfields() {
769 $description = "Demonstrate use of getfields to interrogate api.";
770 $params = array('action' => 'create');
771 $result = $this->callAPISuccess('event', 'getfields', $params);
772 $this->assertEquals(1, $result['values']['is_active']['api.default']);
773 }
774
775 /**
776 * Test api_action param also works.
777 */
778 public function testgetfieldsRest() {
779 $description = "Demonstrate use of getfields to interrogate api.";
780 $params = array('api_action' => 'create');
781 $result = $this->callAPISuccess('event', 'getfields', $params);
782 $this->assertEquals(1, $result['values']['is_active']['api.default']);
783 }
784
785 public function testgetfieldsGet() {
786 $description = "Demonstrate use of getfields to interrogate api.";
787 $params = array('action' => 'get');
788 $result = $this->callAPISuccess('event', 'getfields', $params);
789 $this->assertEquals('title', $result['values']['event_title']['name']);
790 }
791
792 public function testgetfieldsDelete() {
793 $description = "Demonstrate use of getfields to interrogate api.";
794 $params = array('action' => 'delete');
795 $result = $this->callAPISuccess('event', 'getfields', $params);
796 $this->assertEquals(1, $result['values']['id']['api.required']);
797 }
798
799 public function testCreateFromTemplate() {
800 $templateParams = array(
801 'summary' => 'Sign up now to learn the results of this unit test',
802 'description' => 'This event is created from a template, so all the values should be the same as the original ones.',
803 'event_type_id' => 1,
804 'is_public' => 1,
805 'end_date' => '2018-06-25 17:00:00',
806 'is_online_registration' => 1,
807 'registration_start_date' => '2017-06-25 17:00:00',
808 'registration_end_date' => '2018-06-25 17:00:00',
809 'max_participants' => 100,
810 'event_full_text' => 'Sorry! We are already full',
811 );
812 $templateResult = $this->callAPISuccess('Event', 'create', array('is_template' => 1, 'template_title' => 'Test tpl') + $templateParams);
813 $eventResult = $this->callAPISuccess('Event', 'create', array(
814 'template_id' => $templateResult['id'],
815 'title' => 'Clone1',
816 'start_date' => '2018-06-25 16:00:00',
817 ));
818 $eventResult = $this->callAPISuccess('Event', 'getsingle', array('id' => $eventResult['id']));
819 foreach ($templateParams as $param => $value) {
820 $this->assertEquals($value, $eventResult[$param]);
821 }
822 }
823
824 }