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