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