3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * Class api_v3_EventTest
32 class api_v3_EventTest
extends CiviUnitTestCase
{
34 protected $_apiversion;
37 public function setUp() {
39 $this->_apiversion
= 3;
40 $this->_entity
= 'event';
41 $this->_params
= 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',
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',
57 'is_show_location' => 0,
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',
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',
74 'is_show_location' => 0,
80 'title' => 'Annual CiviCRM meet',
82 'start_date' => 20081021,
85 'title' => 'Annual CiviCRM meet 2',
87 'start_date' => 20101021,
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'];
100 public function tearDown() {
101 foreach ($this->eventIds
as $eventId) {
102 $this->eventDelete($eventId);
104 $tablesToTruncate = array(
105 'civicrm_participant',
108 $this->quickCleanup($tablesToTruncate, TRUE);
112 * civicrm_event_get methods.
114 public function testGetEventById() {
116 'id' => $this->_events
[1]['id'],
118 $result = $this->callAPISuccess('event', 'get', $params);
119 $this->assertEquals($result['values'][$this->_eventIds
[1]]['event_title'], 'Annual CiviCRM meet 2');
123 * Test getLocationEvents() function invokes selectWhereClause() hook
125 public function testGetEventWithPermissionHook() {
126 $address = $this->callAPISuccess('address', 'create', array(
127 'contact_id' => 'null',
128 'location_type_id' => 1,
129 'street_address' => '1234567',
132 'address_id' => $address['id'],
134 $result = $this->callAPISuccess('loc_block', 'create', $params);
136 'id' => $this->_events
[1]['id'],
137 'loc_block_id' => $result['id'],
139 $this->callAPISuccess('Event', 'create', $params);
140 $result = CRM_Event_BAO_Event
::getLocationEvents();
141 $this->assertEquals(1, count($result));
143 $this->hookClass
->setHook('civicrm_selectWhereClause', array($this, 'selectWhereClauseHook'));
144 $result = CRM_Event_BAO_Event
::getLocationEvents();
145 $this->assertEquals(0, count($result));
148 public function testGetEventByEventTitle() {
151 'event_title' => 'Annual CiviCRM meet',
152 'sequential' => TRUE,
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]);
160 public function testGetEventByWrongTitle() {
162 'title' => 'No event with that title',
164 $result = $this->callAPISuccess('Event', 'Get', $params);
165 $this->assertEquals(0, $result['count']);
168 public function testGetEventByIdSort() {
170 'return.sort' => 'id ASC',
171 'return.max_results' => 1,
173 $result = $this->callAPISuccess('Event', 'Get', $params);
174 $this->assertEquals(1, $result['id'], ' in line ' . __LINE__
);
182 $result = $this->callAPISuccess('Event', 'Get', $params);
183 $this->assertAPISuccess($result, ' in line ' . __LINE__
);
184 $this->assertEquals(2, $result['id'], ' in line ' . __LINE__
);
191 $result = $this->callAPISuccess('Event', 'Get', $params);
192 $this->assertEquals(1, $result['id'], ' in line ' . __LINE__
);
196 * Getting the id back of an event.
197 * Does not work yet, bug in API
201 public function testGetIdOfEventByEventTitle() {
202 $params = array( 'title' => 'Annual CiviCRM meet',
206 $result = $this->callAPISuccess('Event', 'Get', $params);
212 * Test 'is.Current' option. Existing event is 'old' so only current should be returned
214 public function testGetIsCurrent() {
218 $currentEventParams = array(
219 'start_date' => date('Y-m-d', strtotime('+ 1 day')),
220 'end_date' => date('Y-m-d', strtotime('+ 1 week')),
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'], '');
235 * There has been a schema change & the api needs to buffer developers from it
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");
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);
256 * Test 'is.Current' option. Existing event is 'old' so only current should be returned
258 public function testGetSingleReturnIsFull() {
259 $contactID = $this->individualCreate();
261 'id' => $this->_eventIds
[0],
262 'max_participants' => 1,
264 $result = $this->callAPISuccess('Event', 'Create', $params);
266 $getEventParams = array(
267 'id' => $this->_eventIds
[0],
268 'return.is_full' => 1,
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,
279 'contact_id' => $contactID,
280 'event_id' => $this->_eventIds
[0],
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__
);
286 $this->contactDelete($contactID);
290 * Legacy support for Contribution Type ID.
292 * We need to ensure this is supported as an alias for financial_type_id.
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']);
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']));
308 * Chaining get event and loc block.
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(
316 'street_address' => 'Kipdorp 24',
317 'postal_code' => '2000',
318 'city' => 'Antwerpen',
319 'country_id' => '1020',
320 'location_type_id' => '1',
322 'api.Event.create' => $eventParams,
325 $createResult = $this->callAPISuccess('LocBlock', 'create', $locBlockParams);
326 $locBlockId = $createResult['id'];
327 $eventId = $createResult['values'][0]['api.Event.create']['id'];
329 // request the event with its loc block:
330 $check = $this->callAPISuccess($this->_entity
, 'getsingle', array(
332 'api.LocBlock.get' => array('id' => '$value.loc_block_id'),
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__
);
342 $this->callAPISuccess($this->_entity
, 'delete', array('id' => $eventId));
346 * Chaining get event and non existing loc block.
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
351 public function testChainingGetNonExistingLocBlock() {
352 $params = $this->_params
[0];
353 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
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'),
360 $this->assertEquals($result['id'], $check['id']);
362 $this->callAPISuccess($this->_entity
, 'Delete', array('id' => $result['id']));
366 * Check with complete array + custom field.
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.
372 public function testCreateWithCustom() {
373 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
375 $params = $this->_params
[0];
376 $params['custom_' . $ids['custom_field_id']] = "custom string";
378 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
);
380 $check = $this->callAPISuccess($this->_entity
, 'get', array(
381 'return.custom_' . $ids['custom_field_id'] => 1,
382 'id' => $result['id'],
384 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
386 $this->customFieldDelete($ids['custom_field_id']);
387 $this->customGroupDelete($ids['custom_group_id']);
388 $this->callAPISuccess($this->_entity
, 'Delete', array('id' => $result['id']));
392 * Check searching on custom fields.
394 * https://issues.civicrm.org/jira/browse/CRM-16036
396 public function testSearchCustomField() {
397 // create custom group with custom field on event
398 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
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',
406 $this->assertEquals(0, $check['count']);
408 $this->customFieldDelete($ids['custom_field_id']);
409 $this->customGroupDelete($ids['custom_group_id']);
413 * Check searching on custom fields with IS NULL.
415 * https://issues.civicrm.org/jira/browse/CRM-20740
417 public function testSearchCustomFieldIsNull() {
418 // create custom group with custom field on event
419 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
421 // Search for events having NULL as the value for this custom
422 // field. This should return all events created in setUp.
423 $check = $this->callAPISuccess($this->_entity
, 'get', array(
424 'custom_' . $ids['custom_field_id'] => array('IS NULL' => 1),
427 $this->assertGreaterThan(0, $check['count']);
429 $this->customFieldDelete($ids['custom_field_id']);
430 $this->customGroupDelete($ids['custom_group_id']);
434 * Test searching on custom fields returning a contact reference.
436 * https://issues.civicrm.org/jira/browse/CRM-16036
438 public function testEventGetCustomContactRefFieldCRM16036() {
439 // Create some contact.
440 $test_contact_name = 'Contact, Test';
441 $contact_save_result = $this->callAPISuccess('contact', 'create', array(
442 'sort_name' => $test_contact_name,
443 'contact_type' => 'Individual',
444 'display_name' => $test_contact_name,
446 $contact_id = $contact_save_result['id'];
448 // I have no clue what this $subfile is about. I just copied it from another
450 $subfile = 'ContactRefCustomField';
451 $description = "Demonstrates get with Contact Reference Custom Field.";
453 // Create a custom group, and add a custom contact reference field.
454 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
456 'custom_group_id' => $ids['custom_group_id'],
457 'name' => 'Worker_Lookup',
458 'label' => 'Worker Lookup',
459 'html_type' => 'Autocomplete-Select',
460 'data_type' => 'ContactReference',
462 'is_searchable' => 1,
465 $customField = $this->callAPISuccess('custom_field', 'create', $params);
467 // Create an event, and add the contact as custom value.
468 $params = $this->_params
;
469 $params['title'] = "My test event.";
470 $params['start_date'] = "2015-03-14";
471 // Just assume that an event type 1 exists.
472 $params['event_type_id'] = 1;
473 $params['custom_' . $customField['id']] = "$contact_id";
475 $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
477 // Retrieve the activity, search for the contact.
478 $result = $this->callAPIAndDocument($this->_entity
, 'get', array(
479 'return.custom_' . $customField['id'] => 1,
480 'custom_' . $customField['id'] => $contact_id,
481 ), __FUNCTION__
, __FILE__
, $description, $subfile);
483 $this->assertEquals($test_contact_name, $result['values'][$result['id']]['custom_' . $customField['id']]);
484 $this->assertEquals($contact_id, $result['values'][$result['id']]['custom_' . $customField['id'] . "_id"], ' in line ' . __LINE__
);
485 // Not sure whether I should test for custom_X_1 and custom_X_1_id as well.
486 // (1 being the id of the record in the custom value table)
488 $this->customFieldDelete($ids['custom_field_id']);
489 $this->customGroupDelete($ids['custom_group_id']);
490 $this->callAPISuccess('contact', 'delete', array(
492 'skip_undelete' => TRUE,
497 * Test searching on custom fields with less than or equal.
501 public function testEventGetCustomFieldLte() {
502 // create custom group with custom field on event
503 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
505 // Create an event, with a custom value.
506 $params = $this->_params
;
507 $params['title'] = "My test event.";
508 $params['start_date'] = "2015-03-14";
509 // Just assume that an event type 1 exists.
510 $params['event_type_id'] = 1;
511 $params['custom_' . $ids['custom_field_id']] = "AAAA";
513 $save_result = $this->callApiSuccess($this->_entity
, 'create', $params);
515 // Retrieve the activity, search for custom field < 'BBBB'
516 $get_result = $this->callAPISuccess($this->_entity
, 'get', array(
517 'return.custom_' . $ids['custom_field_id'] => 1,
518 'custom_' . $ids['custom_field_id'] => array('<=' => 'BBBB'),
521 // Expect that we find the saved event.
522 $this->assertArrayKeyExists($save_result['id'], $get_result['values']);
524 $this->callAPISuccess($this->_entity
, 'Delete', array('id' => $save_result['id']));
528 * Test searching on custom fields with netsted call with id param.
530 * Search for an event on a custom field, and perform a chained call
531 * to retrieve it's (non-existing) loc block, using $value-substitution.
532 * This test just checks whether the event is found, because something
533 * happened in CiviCRM 4.6.5 that broke my fix for CRM-16036, causing
534 * CiviCRM to return 0 results.
535 * Of course, CRM-16168 should also be fixed for this test to pass.
537 public function testEventSearchCustomFieldWithChainedCall() {
538 // Create a custom group, and add a custom contact reference field.
539 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
540 $custom_field_id = $ids['custom_field_id'];
542 // Create an event with a custom value.
543 $params = $this->_params
;
544 $params['title'] = "My test event.";
545 $params['start_date'] = "2015-03-14";
546 // Just assume that an event type 1 exists.
547 $params['event_type_id'] = 1;
548 $params['custom_' . $custom_field_id] = "12345";
550 $this->callAPISuccess($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
);
552 // Retrieve the activity, and chain loc block using $value.
553 $result = $this->callAPISuccess($this->_entity
, 'get', array(
554 'custom_' . $custom_field_id => "12345",
555 'api.LocBlock.get' => array("id" => '$value.loc_block_id'),
558 $this->assertEquals(1, $result['count']);
560 $this->customFieldDelete($ids['custom_field_id']);
561 $this->customGroupDelete($ids['custom_group_id']);
562 $this->callAPISuccess('event', 'delete', array(
563 'id' => $result['id'],
564 'skip_undelete' => TRUE,
570 * Test that an event with a price set can be created.
572 public function testCreatePaidEvent() {
573 //@todo alter API so that an integer is converted to an array
574 $priceSetParams = array('price_set_id' => (array) 1, 'is_monetary' => 1);
575 $result = $this->callAPISuccess('Event', 'Create', array_merge($this->_params
[0], $priceSetParams));
576 $event = $this->callAPISuccess('Event', 'getsingle', array('id' => $result['id'], 'return' => 'price_set_id'));
577 $this->assertArrayKeyExists('price_set_id', $event);
580 public function testCreateEventParamsNotArray() {
582 $result = $this->callAPIFailure('event', 'create', $params);
585 public function testCreateEventEmptyParams() {
587 $result = $this->callAPIFailure('event', 'create', $params);
590 public function testCreateEventParamsWithoutTitle() {
591 unset($this->_params
['title']);
592 $result = $this->callAPIFailure('event', 'create', $this->_params
);
593 $this->assertAPIFailure($result);
596 public function testCreateEventParamsWithoutEventTypeId() {
597 unset($this->_params
['event_type_id']);
598 $result = $this->callAPIFailure('event', 'create', $this->_params
);
601 public function testCreateEventParamsWithoutStartDate() {
602 unset($this->_params
['start_date']);
603 $result = $this->callAPIFailure('event', 'create', $this->_params
);
606 public function testCreateEventSuccess() {
607 $result = $this->callAPIAndDocument('Event', 'Create', $this->_params
[0], __FUNCTION__
, __FILE__
);
608 $this->assertArrayHasKey('id', $result['values'][$result['id']]);
609 $result = $this->callAPISuccess($this->_entity
, 'Get', array('id' => $result['id']));
610 $this->callAPISuccess($this->_entity
, 'Delete', array('id' => $result['id']));
611 $this->assertEquals('2008-10-21 00:00:00', $result['values'][$result['id']]['start_date'], 'start date is not set');
612 $this->assertEquals('2008-10-23 00:00:00', $result['values'][$result['id']]['end_date'], 'end date is not set');
613 $this->assertEquals('2008-06-01 00:00:00', $result['values'][$result['id']]['registration_start_date'], 'start date is not set');
614 $this->assertEquals('2008-10-15 00:00:00', $result['values'][$result['id']]['registration_end_date'], 'end date is not set');
618 * Test that passing in Unique field names works.
620 public function testCreateEventSuccessUniqueFieldNames() {
621 $this->_params
[0]['event_start_date'] = $this->_params
[0]['start_date'];
622 unset($this->_params
[1]['start_date']);
623 $this->_params
[0]['event_title'] = $this->_params
[0]['title'];
624 unset($this->_params
[0]['title']);
625 $result = $this->callAPISuccess('Event', 'Create', $this->_params
[0]);
626 $this->assertAPISuccess($result);
627 $this->assertArrayHasKey('id', $result['values'][$result['id']]);
628 $result = $this->callAPISuccess($this->_entity
, 'Get', array('id' => $result['id']));
629 $this->callAPISuccess($this->_entity
, 'Delete', array('id' => $result['id']));
631 $this->assertEquals('2008-10-21 00:00:00', $result['values'][$result['id']]['start_date'], 'start date is not set in line ' . __LINE__
);
632 $this->assertEquals('2008-10-23 00:00:00', $result['values'][$result['id']]['end_date'], 'end date is not set in line ' . __LINE__
);
633 $this->assertEquals('2008-06-01 00:00:00', $result['values'][$result['id']]['registration_start_date'], 'start date is not set in line ' . __LINE__
);
634 $this->assertEquals('2008-10-15 00:00:00', $result['values'][$result['id']]['registration_end_date'], 'end date is not set in line ' . __LINE__
);
635 $this->assertEquals($this->_params
[0]['event_title'], $result['values'][$result['id']]['title'], 'end date is not set in line ' . __LINE__
);
638 public function testUpdateEvent() {
639 $result = $this->callAPISuccess('event', 'create', $this->_params
[1]);
642 'id' => $result['id'],
643 'max_participants' => 150,
645 $this->callAPISuccess('Event', 'Create', $params);
646 $updated = $this->callAPISuccess('Event', 'Get', $params, __FUNCTION__
, __FILE__
);
647 $this->assertEquals(150, $updated['values'][$result['id']]['max_participants']);
648 $this->assertEquals('Annual CiviCRM meet 2', $updated['values'][$result['id']]['title']);
649 $this->callAPISuccess($this->_entity
, 'Delete', array('id' => $result['id']));
653 public function testDeleteEmptyParams() {
654 $result = $this->callAPIFailure('Event', 'Delete', array());
657 public function testDelete() {
659 'id' => $this->_eventIds
[0],
661 $this->callAPIAndDocument('Event', 'Delete', $params, __FUNCTION__
, __FILE__
);
665 * Check event_id still supported for delete.
667 public function testDeleteWithEventId() {
669 'event_id' => $this->_eventIds
[0],
671 $result = $this->callAPISuccess('Event', 'Delete', $params);
672 $this->assertAPISuccess($result);
676 * Trying to delete an event with participants should return error.
678 public function testDeleteWithExistingParticipant() {
679 $contactID = $this->individualCreate();
680 $this->participantCreate(
682 'contactID' => $contactID,
683 'eventID' => $this->_eventIds
[0],
686 $this->callAPISuccess('Event', 'Delete', array('id' => $this->_eventIds
[0]));
689 public function testDeleteWithWrongEventId() {
690 $params = array('event_id' => $this->_eventIds
[0]);
691 $result = $this->callAPISuccess('Event', 'Delete', $params);
692 // try to delete again - there's no such event anymore
693 $params = array('event_id' => $this->_eventIds
[0]);
694 $result = $this->callAPIFailure('Event', 'Delete', $params);
698 * Test civicrm_event_search with wrong params type.
700 public function testSearchWrongParamsType() {
701 $params = 'a string';
702 $result = $this->callAPIFailure('event', 'get', $params);
706 * Test civicrm_event_search with empty params.
708 public function testSearchEmptyParams() {
709 $this->callAPISuccess('event', 'create', $this->_params
[1]);
714 $result = $this->callAPISuccess('event', 'get', $getParams);
715 $this->assertEquals($result['count'], 3);
716 $res = $result['values'][0];
717 $this->assertArrayKeyExists('title', $res);
718 $this->assertEquals($res['event_type_id'], $this->_params
[1]['event_type_id']);
722 * Test civicrm_event_search. Success expected.
724 public function testSearch() {
726 'event_type_id' => 1,
729 'return.start_date' => 1,
731 $result = $this->callAPISuccess('event', 'get', $params);
733 $this->assertEquals($result['values'][$this->_eventIds
[0]]['id'], $this->_eventIds
[0]);
734 $this->assertEquals($result['values'][$this->_eventIds
[0]]['title'], 'Annual CiviCRM meet');
738 * Test civicrm_event_search.
742 * return.offset and return.max_results test (CRM-5266)
744 public function testSearchWithOffsetAndMaxResults() {
747 while ($maxEvents > 0) {
749 'title' => 'Test Event' . $maxEvents,
750 'event_type_id' => 2,
751 'start_date' => 20081021,
754 $events[$maxEvents] = $this->callAPISuccess('event', 'create', $params);
758 'event_type_id' => 2,
761 'return.offset' => 2,
762 'return.max_results' => 2,
764 $result = $this->callAPISuccess('event', 'get', $params);
765 $this->assertAPISuccess($result);
766 $this->assertEquals(2, $result['count'], ' 2 results returned In line ' . __LINE__
);
769 public function testEventCreationPermissions() {
771 'event_type_id' => 1,
772 'start_date' => '2010-10-03',
773 'title' => 'le cake is a tie',
774 'check_permissions' => TRUE,
776 $config = CRM_Core_Config
::singleton();
777 $config->userPermissionClass
->permissions
= array('access CiviCRM');
778 $result = $this->callAPIFailure('event', 'create', $params);
779 $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');
781 $config->userPermissionClass
->permissions
= array(
786 $result = $this->callAPISuccess('event', 'create', $params);
789 public function testgetfields() {
790 $description = "Demonstrate use of getfields to interrogate api.";
791 $params = array('action' => 'create');
792 $result = $this->callAPISuccess('event', 'getfields', $params);
793 $this->assertEquals(1, $result['values']['is_active']['api.default']);
797 * Test api_action param also works.
799 public function testgetfieldsRest() {
800 $description = "Demonstrate use of getfields to interrogate api.";
801 $params = array('api_action' => 'create');
802 $result = $this->callAPISuccess('event', 'getfields', $params);
803 $this->assertEquals(1, $result['values']['is_active']['api.default']);
806 public function testgetfieldsGet() {
807 $description = "Demonstrate use of getfields to interrogate api.";
808 $params = array('action' => 'get');
809 $result = $this->callAPISuccess('event', 'getfields', $params);
810 $this->assertEquals('title', $result['values']['event_title']['name']);
813 public function testgetfieldsDelete() {
814 $description = "Demonstrate use of getfields to interrogate api.";
815 $params = array('action' => 'delete');
816 $result = $this->callAPISuccess('event', 'getfields', $params);
817 $this->assertEquals(1, $result['values']['id']['api.required']);
820 public function testCreateFromTemplate() {
821 $templateParams = array(
822 'summary' => 'Sign up now to learn the results of this unit test',
823 'description' => 'This event is created from a template, so all the values should be the same as the original ones.',
824 'event_type_id' => 1,
826 'end_date' => '2018-06-25 17:00:00',
827 'is_online_registration' => 1,
828 'registration_start_date' => '2017-06-25 17:00:00',
829 'registration_end_date' => '2018-06-25 17:00:00',
830 'max_participants' => 100,
831 'event_full_text' => 'Sorry! We are already full',
833 $templateResult = $this->callAPISuccess('Event', 'create', array('is_template' => 1, 'template_title' => 'Test tpl') +
$templateParams);
834 $eventResult = $this->callAPISuccess('Event', 'create', array(
835 'template_id' => $templateResult['id'],
837 'start_date' => '2018-06-25 16:00:00',
839 $eventResult = $this->callAPISuccess('Event', 'getsingle', array('id' => $eventResult['id']));
840 foreach ($templateParams as $param => $value) {
841 $this->assertEquals($value, $eventResult[$param]);