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