Merge pull request #16469 from civicrm/5.22
[civicrm-core.git] / tests / phpunit / api / v3 / EventTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Class api_v3_EventTest
14 * @group headless
15 */
16 class api_v3_EventTest extends CiviUnitTestCase {
17 protected $_params;
18 protected $_apiversion;
19 protected $_entity;
20
21 public function setUp() {
22 parent::setUp();
23 $this->_apiversion = 3;
24 $this->_entity = 'event';
25 $this->_params = [
26 [
27 'title' => 'Annual CiviCRM meet',
28 'summary' => 'If you have any CiviCRM related issues or want to track where CiviCRM is heading, Sign up now',
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,
42 ],
43 [
44 'title' => 'Annual CiviCRM meet 2',
45 'summary' => 'If you have any CiviCRM related issues or want to track where CiviCRM is heading, Sign up now',
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,
59 ],
60 ];
61
62 $params = [
63 [
64 'title' => 'Annual CiviCRM meet',
65 'event_type_id' => 1,
66 'start_date' => 20081021,
67 ],
68 [
69 'title' => 'Annual CiviCRM meet 2',
70 'event_type_id' => 1,
71 'start_date' => 20101021,
72 ],
73 ];
74
75 $this->events = [];
76 $this->eventIds = [];
77 foreach ($params as $event) {
78 $result = $this->callAPISuccess('Event', 'Create', $event);
79 $this->_events[] = $result;
80 $this->_eventIds[] = $result['id'];
81 }
82 }
83
84 public function tearDown() {
85 foreach ($this->eventIds as $eventId) {
86 $this->eventDelete($eventId);
87 }
88 $tablesToTruncate = [
89 'civicrm_participant',
90 'civicrm_event',
91 ];
92 $this->quickCleanup($tablesToTruncate, TRUE);
93 }
94
95 /**
96 * civicrm_event_get methods.
97 */
98 public function testGetEventById() {
99 $params = [
100 'id' => $this->_events[1]['id'],
101 ];
102 $result = $this->callAPISuccess('event', 'get', $params);
103 $this->assertEquals($result['values'][$this->_eventIds[1]]['event_title'], 'Annual CiviCRM meet 2');
104 }
105
106 /**
107 * Test getLocationEvents() function invokes selectWhereClause() hook
108 */
109 public function testGetEventWithPermissionHook() {
110 $address = $this->callAPISuccess('address', 'create', [
111 'contact_id' => 'null',
112 'location_type_id' => 1,
113 'street_address' => '1234567',
114 ]);
115 $params = [
116 'address_id' => $address['id'],
117 ];
118 $result = $this->callAPISuccess('loc_block', 'create', $params);
119 $params = [
120 'id' => $this->_events[1]['id'],
121 'loc_block_id' => $result['id'],
122 ];
123 $this->callAPISuccess('Event', 'create', $params);
124 $result = CRM_Event_BAO_Event::getLocationEvents();
125 $this->assertEquals(1, count($result));
126
127 $this->hookClass->setHook('civicrm_selectWhereClause', [$this, 'selectWhereClauseHook']);
128 $result = CRM_Event_BAO_Event::getLocationEvents();
129 $this->assertEquals(0, count($result));
130 }
131
132 public function testGetEventByEventTitle() {
133
134 $params = [
135 'event_title' => 'Annual CiviCRM meet',
136 'sequential' => TRUE,
137 ];
138
139 $result = $this->callAPIAndDocument('event', 'get', $params, __FUNCTION__, __FILE__);
140 $this->assertEquals(1, $result['count']);
141 $this->assertEquals($result['values'][0]['id'], $this->_eventIds[0]);
142 }
143
144 /**
145 * @param int $version
146 * @dataProvider versionThreeAndFour
147 */
148 public function testGetEventByWrongTitle($version) {
149 $this->_apiversion = $version;
150 $params = [
151 'title' => 'No event with that title',
152 ];
153 $result = $this->callAPISuccess('Event', 'Get', $params);
154 $this->assertEquals(0, $result['count']);
155 }
156
157 /**
158 * Skip api4 - this api uses deprecated query syntax
159 */
160 public function testGetEventByIdSort() {
161 $params = [
162 'return.sort' => 'id ASC',
163 'return.max_results' => 1,
164 ];
165 $result = $this->callAPISuccess('Event', 'Get', $params);
166 $this->assertEquals(1, $result['id'], ' in line ' . __LINE__);
167 $params = [
168 'options' => [
169 'sort' => 'id DESC',
170 'limit' => 1,
171 ],
172 ];
173
174 $result = $this->callAPISuccess('Event', 'Get', $params);
175 $this->assertAPISuccess($result, ' in line ' . __LINE__);
176 $this->assertEquals(2, $result['id'], ' in line ' . __LINE__);
177 $params = [
178 'options' => [
179 'sort' => 'id ASC',
180 'limit' => 1,
181 ],
182 ];
183 $result = $this->callAPISuccess('Event', 'Get', $params);
184 $this->assertEquals(1, $result['id'], ' in line ' . __LINE__);
185
186 }
187
188 /*
189 * Getting the id back of an event.
190 * Does not work yet, bug in API
191 */
192
193 /*
194 public function testGetIdOfEventByEventTitle() {
195 $params = array( 'title' => 'Annual CiviCRM meet',
196 'return' => 'id'
197 );
198
199 $result = $this->callAPISuccess('Event', 'Get', $params);
200 }
201 */
202
203 /**
204 * Test 'is.Current' option. Existing event is 'old' so only current should be returned
205 * FIXME: Api4
206 */
207 public function testGetIsCurrent() {
208 $params = [
209 'isCurrent' => 1,
210 ];
211 $currentEventParams = [
212 'start_date' => date('Y-m-d', strtotime('+ 1 day')),
213 'end_date' => date('Y-m-d', strtotime('+ 1 week')),
214 ];
215 $currentEventParams = array_merge($this->_params[1], $currentEventParams);
216 $currentEvent = $this->callAPISuccess('Event', 'Create', $currentEventParams);
217 $description = "Demonstrates use of is.Current option.";
218 $subfile = "IsCurrentOption";
219 $result = $this->callAPIAndDocument('Event', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile);
220 $allEvents = $this->callAPISuccess('Event', 'Get', []);
221 $this->callAPISuccess('Event', 'Delete', ['id' => $currentEvent['id']]);
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 }
226
227 /**
228 * There has been a schema change & the api needs to buffer developers from it
229 * FIXME: Api4
230 */
231 public function testGetPaymentProcessorId() {
232 $params = $this->_params[0];
233 $params['payment_processor_id'] = 1;
234 $params['sequential'] = 1;
235 $result = $this->callAPISuccess('event', 'create', $params);
236 $this->assertEquals(1, $result['values'][0]['payment_processor'][0], "handing of payment processor compatibility");
237 $result = $this->callAPISuccess('event', 'get', $params);
238 $this->assertEquals($result['values'][0]['payment_processor_id'], 1, "handing get payment processor compatibility");
239 }
240
241 /**
242 * @param int $version
243 * @dataProvider versionThreeAndFour
244 */
245 public function testInvalidData($version) {
246 $this->_apiversion = $version;
247 $params = $this->_params[0];
248 $params['sequential'] = 1;
249 $params['loc_block_id'] = 100;
250 $result = $this->callAPIFailure('event', 'create', $params);
251
252 }
253
254 /**
255 * Test 'is_full' option.
256 * FIXME: Api4
257 */
258 public function testGetSingleReturnIsFull() {
259 $contactID = $this->individualCreate();
260 $params = [
261 'id' => $this->_eventIds[0],
262 'max_participants' => 1,
263 ];
264 $result = $this->callAPISuccess('Event', 'Create', $params);
265
266 $getEventParams = [
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', [
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 = ['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', [] + $contributionTypeArray);
302 $this->assertEquals($getresult['values'][$getresult['id']]['contribution_type_id'], 3);
303 $this->assertEquals($result['id'], $getresult['id']);
304 $this->callAPISuccess('event', 'delete', ['id' => $result['id']]);
305 }
306
307 /**
308 * Chaining get event and loc block.
309 * @param int $version
310 * @dataProvider versionThreeAndFour
311 */
312 public function testChainingGetLocBlock($version) {
313 $this->_apiversion = $version;
314 // create a loc block and an event for that loc block.
315 $eventParams = $this->_params[0];
316 $eventParams['loc_bloc_id'] = '$value.id';
317 $locBlockParams = [
318 'address' => [
319 'street_address' => 'Kipdorp 24',
320 'postal_code' => '2000',
321 'city' => 'Antwerpen',
322 'country_id' => '1020',
323 'location_type_id' => '1',
324 ],
325 'api.Event.create' => $eventParams,
326 'sequential' => 1,
327 ];
328 $createResult = $this->callAPISuccess('LocBlock', 'create', $locBlockParams);
329 $locBlockId = $createResult['id'];
330 $eventId = $createResult['values'][0]['api.Event.create']['id'];
331
332 // request the event with its loc block:
333 $check = $this->callAPISuccess($this->_entity, 'getsingle', [
334 'id' => $eventId,
335 'api.LocBlock.get' => ['id' => '$value.loc_block_id'],
336 'sequential' => 1,
337 ]);
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
345 $this->callAPISuccess($this->_entity, 'delete', ['id' => $eventId]);
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];
356 $result = $this->callAPISuccess($this->_entity, 'create', $params);
357
358 $check = $this->callAPISuccess($this->_entity, 'get', [
359 'id' => $result['id'],
360 // this chaining request should not break things:
361 'api.LocBlock.get' => ['id' => '$value.loc_block_id'],
362 ]);
363 $this->assertEquals($result['id'], $check['id']);
364
365 $this->callAPISuccess($this->_entity, 'Delete', ['id' => $result['id']]);
366 }
367
368 /**
369 * Check with complete array + custom field.
370 *
371 * Note that the test is written on purpose without any
372 * variables specific to participant so it can be replicated into other entities
373 * and / or moved to the automated test suite.
374 * @param int $version
375 * @dataProvider versionThreeAndFour
376 */
377 public function testCreateWithCustom($version) {
378 $this->_apiversion = $version;
379 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
380
381 $params = $this->_params[0];
382 $params['custom_' . $ids['custom_field_id']] = "custom string";
383
384 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
385
386 $check = $this->callAPISuccess($this->_entity, 'get', [
387 'return.custom_' . $ids['custom_field_id'] => 1,
388 'id' => $result['id'],
389 ]);
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']);
394 $this->callAPISuccess($this->_entity, 'Delete', ['id' => $result['id']]);
395 }
396
397 /**
398 * Check searching on custom fields.
399 *
400 * https://issues.civicrm.org/jira/browse/CRM-16036
401 * @param int $version
402 * @dataProvider versionThreeAndFour
403 */
404 public function testSearchCustomField($version) {
405 $this->_apiversion = $version;
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.
411 $check = $this->callAPISuccess($this->_entity, 'get', [
412 'custom_' . $ids['custom_field_id'] => 'CRM-16036',
413 ]);
414
415 $this->assertEquals(0, $check['count']);
416
417 $this->customFieldDelete($ids['custom_field_id']);
418 $this->customGroupDelete($ids['custom_group_id']);
419 }
420
421 /**
422 * Check searching on custom fields with IS NULL.
423 *
424 * https://issues.civicrm.org/jira/browse/CRM-20740
425 * @param int $version
426 * @dataProvider versionThreeAndFour
427 */
428 public function testSearchCustomFieldIsNull($version) {
429 $this->_apiversion = $version;
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.
435 $check = $this->callAPISuccess($this->_entity, 'get', [
436 'custom_' . $ids['custom_field_id'] => ['IS NULL' => 1],
437 ]);
438
439 $this->assertGreaterThan(0, $check['count']);
440
441 $this->customFieldDelete($ids['custom_field_id']);
442 $this->customGroupDelete($ids['custom_group_id']);
443 }
444
445 /**
446 * Test searching on custom fields returning a contact reference.
447 *
448 * https://issues.civicrm.org/jira/browse/CRM-16036
449 * FIXME: Api4
450 */
451 public function testEventGetCustomContactRefFieldCRM16036() {
452 // Create some contact.
453 $test_contact_name = 'Contact, Test';
454 $contact_save_result = $this->callAPISuccess('contact', 'create', [
455 'sort_name' => $test_contact_name,
456 'contact_type' => 'Individual',
457 'display_name' => $test_contact_name,
458 ]);
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__);
468 $params = [
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,
477 ];
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
488 $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
489
490 // Retrieve the activity, search for the contact.
491 $result = $this->callAPIAndDocument($this->_entity, 'get', [
492 'return.custom_' . $customField['id'] => 1,
493 'custom_' . $customField['id'] => $contact_id,
494 ], __FUNCTION__, __FILE__, $description, $subfile);
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']);
503 $this->callAPISuccess('contact', 'delete', [
504 'id' => $contact_id,
505 'skip_undelete' => TRUE,
506 ]);
507 }
508
509 /**
510 * Test searching on custom fields with less than or equal.
511 *
512 * See CRM-17101.
513 * @param int $version
514 * @dataProvider versionThreeAndFour
515 */
516 public function testEventGetCustomFieldLte($version) {
517 $this->_apiversion = $version;
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'
532 $get_result = $this->callAPISuccess($this->_entity, 'get', [
533 'return.custom_' . $ids['custom_field_id'] => 1,
534 'custom_' . $ids['custom_field_id'] => ['<=' => 'BBBB'],
535 ]);
536
537 // Expect that we find the saved event.
538 $this->assertArrayKeyExists($save_result['id'], $get_result['values']);
539
540 $this->callAPISuccess($this->_entity, 'Delete', ['id' => $save_result['id']]);
541 }
542
543 /**
544 * Test searching on custom fields with netsted call with id param.
545 *
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
549 * happened in CiviCRM 4.6.5 that broke my fix for CRM-16036, causing
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'];
557
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.
569 $result = $this->callAPISuccess($this->_entity, 'get', [
570 'custom_' . $custom_field_id => "12345",
571 'api.LocBlock.get' => ["id" => '$value.loc_block_id'],
572 ]);
573
574 $this->assertEquals(1, $result['count']);
575
576 $this->customFieldDelete($ids['custom_field_id']);
577 $this->customGroupDelete($ids['custom_group_id']);
578 $this->callAPISuccess('event', 'delete', [
579 'id' => $result['id'],
580 'skip_undelete' => TRUE,
581 ]);
582 }
583
584 /**
585 * Test that an event with a price set can be created.
586 * FIXME: Api4
587 */
588 public function testCreatePaidEvent() {
589 //@todo alter API so that an integer is converted to an array
590 $priceSetParams = ['price_set_id' => (array) 1, 'is_monetary' => 1];
591 $result = $this->callAPISuccess('Event', 'Create', array_merge($this->_params[0], $priceSetParams));
592 $event = $this->callAPISuccess('Event', 'getsingle', ['id' => $result['id'], 'return' => 'price_set_id']);
593 $this->assertArrayKeyExists('price_set_id', $event);
594 }
595
596 /**
597 * @param int $version
598 * @dataProvider versionThreeAndFour
599 */
600 public function testCreateEventEmptyParams($version) {
601 $this->_apiversion = $version;
602 $params = [];
603 $result = $this->callAPIFailure('event', 'create', $params);
604 }
605
606 /**
607 * @param int $version
608 * @dataProvider versionThreeAndFour
609 */
610 public function testCreateEventParamsWithoutTitle($version) {
611 $this->_apiversion = $version;
612 unset($this->_params['title']);
613 $result = $this->callAPIFailure('event', 'create', $this->_params);
614 $this->assertAPIFailure($result);
615 }
616
617 /**
618 * @param int $version
619 * @dataProvider versionThreeAndFour
620 */
621 public function testCreateEventParamsWithoutEventTypeId($version) {
622 $this->_apiversion = $version;
623 unset($this->_params['event_type_id']);
624 $result = $this->callAPIFailure('event', 'create', $this->_params);
625 }
626
627 /**
628 * @param int $version
629 * @dataProvider versionThreeAndFour
630 */
631 public function testCreateEventParamsWithoutStartDate($version) {
632 $this->_apiversion = $version;
633 unset($this->_params['start_date']);
634 $result = $this->callAPIFailure('event', 'create', $this->_params, 'start_date');
635 }
636
637 /**
638 * @param int $version
639 * @dataProvider versionThreeAndFour
640 */
641 public function testCreateEventSuccess($version) {
642 $this->_apiversion = $version;
643 $result = $this->callAPIAndDocument('Event', 'Create', $this->_params[0], __FUNCTION__, __FILE__);
644 $this->assertArrayHasKey('id', $result['values'][$result['id']]);
645 $result = $this->callAPISuccess($this->_entity, 'Get', ['id' => $result['id']]);
646 $this->callAPISuccess($this->_entity, 'Delete', ['id' => $result['id']]);
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');
651 }
652
653 /**
654 * Test that passing in Unique field names works.
655 * Skip api4 which doesn't use unique names
656 */
657 public function testCreateEventSuccessUniqueFieldNames() {
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']);
662 $result = $this->callAPISuccess('Event', 'Create', $this->_params[0]);
663 $this->assertAPISuccess($result);
664 $this->assertArrayHasKey('id', $result['values'][$result['id']]);
665 $result = $this->callAPISuccess($this->_entity, 'Get', ['id' => $result['id']]);
666 $this->callAPISuccess($this->_entity, 'Delete', ['id' => $result['id']]);
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__);
673 }
674
675 /**
676 * @param int $version
677 * @dataProvider versionThreeAndFour
678 */
679 public function testUpdateEvent($version) {
680 $this->_apiversion = $version;
681 $result = $this->callAPISuccess('event', 'create', $this->_params[1]);
682
683 $params = [
684 'id' => $result['id'],
685 'max_participants' => 150,
686 ];
687 $this->callAPISuccess('Event', 'Create', $params);
688 $updated = $this->callAPISuccess('Event', 'Get', $params, __FUNCTION__, __FILE__);
689 $this->assertEquals(150, $updated['values'][$result['id']]['max_participants']);
690 $this->assertEquals('Annual CiviCRM meet 2', $updated['values'][$result['id']]['title']);
691 $this->callAPISuccess($this->_entity, 'Delete', ['id' => $result['id']]);
692 }
693
694 /**
695 * @param int $version
696 * @dataProvider versionThreeAndFour
697 */
698 public function testDeleteEmptyParams($version) {
699 $this->_apiversion = $version;
700 $result = $this->callAPIFailure('Event', 'Delete', []);
701 }
702
703 /**
704 * @param int $version
705 * @dataProvider versionThreeAndFour
706 */
707 public function testDelete($version) {
708 $this->_apiversion = $version;
709 $params = [
710 'id' => $this->_eventIds[0],
711 ];
712 $this->callAPIAndDocument('Event', 'Delete', $params, __FUNCTION__, __FILE__);
713 }
714
715 /**
716 * Check event_id still supported for delete.
717 * @param int $version
718 * @dataProvider versionThreeAndFour
719 */
720 public function testDeleteWithEventId($version) {
721 $this->_apiversion = $version;
722 $params = [
723 'event_id' => $this->_eventIds[0],
724 ];
725 $result = $this->callAPISuccess('Event', 'Delete', $params);
726 $this->assertAPISuccess($result);
727 }
728
729 /**
730 * Trying to delete an event with participants should return error.
731 * @param int $version
732 * @dataProvider versionThreeAndFour
733 */
734 public function testDeleteWithExistingParticipant($version) {
735 $this->_apiversion = $version;
736 $contactID = $this->individualCreate();
737 $this->participantCreate(
738 [
739 'contactID' => $contactID,
740 'eventID' => $this->_eventIds[0],
741 ]
742 );
743 $this->callAPISuccess('Event', 'Delete', ['id' => $this->_eventIds[0]]);
744 }
745
746 /**
747 * @param int $version
748 * @dataProvider versionThreeAndFour
749 */
750 public function testDeleteWithWrongEventId($version) {
751 $this->_apiversion = $version;
752 $params = ['event_id' => $this->_eventIds[0]];
753 $result = $this->callAPISuccess('Event', 'Delete', $params);
754 // try to delete again - there's no such event anymore
755 $params = ['event_id' => $this->_eventIds[0]];
756 $result = $this->callAPIFailure('Event', 'Delete', $params);
757 }
758
759 /**
760 * Test civicrm_event_search with empty params.
761 * @param int $version
762 * @dataProvider versionThreeAndFour
763 */
764 public function testSearchEmptyParams($version) {
765 $this->_apiversion = $version;
766 $this->callAPISuccess('event', 'create', $this->_params[1]);
767
768 $getParams = [
769 'sequential' => 1,
770 ];
771 $result = $this->callAPISuccess('event', 'get', $getParams);
772 $this->assertEquals($result['count'], 3);
773 $res = $result['values'][0];
774 $this->assertArrayKeyExists('title', $res);
775 $this->assertEquals($res['event_type_id'], $this->_params[1]['event_type_id']);
776 }
777
778 /**
779 * Test civicrm_event_search. Success expected.
780 * @param int $version
781 * @dataProvider versionThreeAndFour
782 */
783 public function testSearch($version) {
784 $this->_apiversion = $version;
785 $params = [
786 'event_type_id' => 1,
787 'return.title' => 1,
788 'return.id' => 1,
789 'return.start_date' => 1,
790 ];
791 $result = $this->callAPISuccess('event', 'get', $params);
792
793 $this->assertEquals($result['values'][$this->_eventIds[0]]['id'], $this->_eventIds[0]);
794 $this->assertEquals($result['values'][$this->_eventIds[0]]['title'], 'Annual CiviCRM meet');
795 }
796
797 /**
798 * Test civicrm_event_search.
799 *
800 * Success expected.
801 *
802 * return.offset and return.max_results test (CRM-5266)
803 */
804 public function testSearchWithOffsetAndMaxResults() {
805 $maxEvents = 5;
806 $events = [];
807 while ($maxEvents > 0) {
808 $params = [
809 'title' => 'Test Event' . $maxEvents,
810 'event_type_id' => 2,
811 'start_date' => 20081021,
812 ];
813
814 $events[$maxEvents] = $this->callAPISuccess('event', 'create', $params);
815 $maxEvents--;
816 }
817 $params = [
818 'event_type_id' => 2,
819 'return.id' => 1,
820 'return.title' => 1,
821 'return.offset' => 2,
822 'return.max_results' => 2,
823 ];
824 $result = $this->callAPISuccess('event', 'get', $params);
825 $this->assertAPISuccess($result);
826 $this->assertEquals(2, $result['count'], ' 2 results returned In line ' . __LINE__);
827 }
828
829 /**
830 * @param int $version
831 * @dataProvider versionThreeAndFour
832 */
833 public function testEventCreationPermissions($version) {
834 $this->_apiversion = $version;
835 $params = [
836 'event_type_id' => 1,
837 'start_date' => '2010-10-03',
838 'title' => 'le cake is a tie',
839 'check_permissions' => TRUE,
840 ];
841 $config = CRM_Core_Config::singleton();
842 $config->userPermissionClass->permissions = ['access CiviCRM'];
843 $result = $this->callAPIFailure('event', 'create', $params, 'failed');
844
845 $config->userPermissionClass->permissions = [
846 'access CiviEvent',
847 'edit all events',
848 'access CiviCRM',
849 ];
850 $result = $this->callAPISuccess('event', 'create', $params);
851 }
852
853 /**
854 * @param int $version
855 * @dataProvider versionThreeAndFour
856 */
857 public function testgetfields($version) {
858 $this->_apiversion = $version;
859 $description = "Demonstrate use of getfields to interrogate api.";
860 $params = ['action' => 'create'];
861 $result = $this->callAPISuccess('event', 'getfields', $params);
862 $this->assertEquals('is_active', $result['values']['is_active']['name']);
863 }
864
865 /**
866 * Test api_action param also works.
867 */
868 public function testgetfieldsRest() {
869 $description = "Demonstrate use of getfields to interrogate api.";
870 $params = ['api_action' => 'create'];
871 $result = $this->callAPISuccess('event', 'getfields', $params);
872 $this->assertEquals(1, $result['values']['is_active']['api.default']);
873 }
874
875 /**
876 * Skip api4 - output is different
877 */
878 public function testgetfieldsGet() {
879 $description = "Demonstrate use of getfields to interrogate api.";
880 $params = ['action' => 'get'];
881 $result = $this->callAPISuccess('event', 'getfields', $params);
882 $this->assertEquals('title', $result['values']['event_title']['name']);
883 }
884
885 /**
886 * Skip api4 - output is different
887 */
888 public function testgetfieldsDelete() {
889 $description = "Demonstrate use of getfields to interrogate api.";
890 $params = ['action' => 'delete'];
891 $result = $this->callAPISuccess('event', 'getfields', $params);
892 $this->assertEquals(1, $result['values']['id']['api.required']);
893 }
894
895 /**
896 * @param int $version
897 * @dataProvider versionThreeAndFour
898 */
899 public function testCreateFromTemplate($version) {
900 $this->_apiversion = $version;
901 $templateParams = [
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',
912 ];
913 $templateResult = $this->callAPISuccess('Event', 'create', ['is_template' => 1, 'template_title' => 'Test tpl'] + $templateParams);
914 $eventResult = $this->callAPISuccess('Event', 'create', [
915 'template_id' => $templateResult['id'],
916 'title' => 'Clone1',
917 'start_date' => '2018-06-25 16:00:00',
918 ]);
919 $eventResult = $this->callAPISuccess('Event', 'getsingle', ['id' => $eventResult['id']]);
920 foreach ($templateParams as $param => $value) {
921 $this->assertEquals($value, $eventResult[$param], print_r($eventResult, 1));
922 }
923 }
924
925 }