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