Merge pull request #11679 from jitendrapurohit/CRM-21776
[civicrm-core.git] / tests / phpunit / api / v3 / EventTest.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
2fe49090 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
8c9251b3 6 | Copyright CiviCRM LLC (c) 2004-2018 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035 27
e9479dcf
EM
28/**
29 * Class api_v3_EventTest
acb109b7 30 * @group headless
e9479dcf 31 */
6a488035
TO
32class api_v3_EventTest extends CiviUnitTestCase {
33 protected $_params;
34 protected $_apiversion;
430ae6dd 35 protected $_entity;
b7c9bc4c 36
00be9182 37 public function setUp() {
6a488035
TO
38 parent::setUp();
39 $this->_apiversion = 3;
92915c55
TO
40 $this->_entity = 'event';
41 $this->_params = array(
6a488035
TO
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,
6a488035
TO
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,
6a488035
TO
75 ),
76 );
77
78 $params = array(
79 array(
80 'title' => 'Annual CiviCRM meet',
81 'event_type_id' => 1,
92915c55
TO
82 'start_date' => 20081021,
83 ),
6a488035
TO
84 array(
85 'title' => 'Annual CiviCRM meet 2',
86 'event_type_id' => 1,
92915c55
TO
87 'start_date' => 20101021,
88 ),
6a488035
TO
89 );
90
91 $this->events = array();
92 $this->eventIds = array();
93 foreach ($params as $event) {
92915c55
TO
94 $result = $this->callAPISuccess('Event', 'Create', $event);
95 $this->_events[] = $result;
6a488035
TO
96 $this->_eventIds[] = $result['id'];
97 }
98 }
99
00be9182 100 public function tearDown() {
6a488035
TO
101 foreach ($this->eventIds as $eventId) {
102 $this->eventDelete($eventId);
103 }
6a488035
TO
104 $tablesToTruncate = array(
105 'civicrm_participant',
106 'civicrm_event',
107 );
108 $this->quickCleanup($tablesToTruncate, TRUE);
109 }
110
408b79bf 111 /**
fe482240 112 * civicrm_event_get methods.
408b79bf 113 */
00be9182 114 public function testGetEventById() {
6a488035 115 $params = array(
92915c55
TO
116 'id' => $this->_events[1]['id'],
117 );
ca985406 118 $result = $this->callAPISuccess('event', 'get', $params);
6a488035
TO
119 $this->assertEquals($result['values'][$this->_eventIds[1]]['event_title'], 'Annual CiviCRM meet 2');
120 }
121
88ebed7c
JP
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
00be9182 148 public function testGetEventByEventTitle() {
6a488035
TO
149
150 $params = array(
151 'event_title' => 'Annual CiviCRM meet',
226ede24 152 'sequential' => TRUE,
6a488035
TO
153 );
154
226ede24 155 $result = $this->callAPIAndDocument('event', 'get', $params, __FUNCTION__, __FILE__);
6a488035 156 $this->assertEquals(1, $result['count']);
226ede24 157 $this->assertEquals($result['values'][0]['id'], $this->_eventIds[0]);
6a488035
TO
158 }
159
00be9182 160 public function testGetEventByWrongTitle() {
6a488035 161 $params = array(
92915c55
TO
162 'title' => 'No event with that title',
163 );
ca985406 164 $result = $this->callAPISuccess('Event', 'Get', $params);
6a488035
TO
165 $this->assertEquals(0, $result['count']);
166 }
92915c55 167
00be9182 168 public function testGetEventByIdSort() {
b6708aeb 169 $params = array(
6a488035 170 'return.sort' => 'id ASC',
92915c55
TO
171 'return.max_results' => 1,
172 );
ca985406 173 $result = $this->callAPISuccess('Event', 'Get', $params);
6a488035
TO
174 $this->assertEquals(1, $result['id'], ' in line ' . __LINE__);
175 $params = array(
176 'options' => array(
177 'sort' => 'id DESC',
178 'limit' => 1,
92915c55
TO
179 ),
180 );
6a488035 181
ca985406 182 $result = $this->callAPISuccess('Event', 'Get', $params);
6a488035
TO
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,
92915c55
TO
189 ),
190 );
ca985406 191 $result = $this->callAPISuccess('Event', 'Get', $params);
6a488035
TO
192 $this->assertEquals(1, $result['id'], ' in line ' . __LINE__);
193
6a488035
TO
194 }
195 /*
196 * Getting the id back of an event.
197 * Does not work yet, bug in API
198 */
199
200 /*
00be9182 201 public function testGetIdOfEventByEventTitle() {
e70a7fc0
TO
202 $params = array( 'title' => 'Annual CiviCRM meet',
203 'return' => 'id'
204 );
6a488035 205
e70a7fc0 206 $result = $this->callAPISuccess('Event', 'Get', $params);
6a488035 207 }
e70a7fc0 208 */
6a488035
TO
209
210
c490a46a
CW
211 /**
212 * Test 'is.Current' option. Existing event is 'old' so only current should be returned
213 */
00be9182 214 public function testGetIsCurrent() {
6a488035 215 $params = array(
6a488035
TO
216 'isCurrent' => 1,
217 );
6c6e6187 218 $currentEventParams = array(
92915c55 219 'start_date' => date('Y-m-d', strtotime('+ 1 day')),
6a488035
TO
220 'end_date' => date('Y-m-d', strtotime('+ 1 week')),
221 );
222 $currentEventParams = array_merge($this->_params[1], $currentEventParams);
92915c55 223 $currentEvent = $this->callAPISuccess('Event', 'Create', $currentEventParams);
5c49fee0 224 $description = "Demonstrates use of is.Current option.";
92915c55
TO
225 $subfile = "IsCurrentOption";
226 $result = $this->callAPIAndDocument('Event', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile);
9f1b81e0 227 $allEvents = $this->callAPISuccess('Event', 'Get', array());
228 $this->callAPISuccess('Event', 'Delete', array('id' => $currentEvent['id']));
6a488035
TO
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 }
c490a46a
CW
233
234 /**
235 * There has been a schema change & the api needs to buffer developers from it
236 */
00be9182 237 public function testGetPaymentProcessorId() {
6a488035
TO
238 $params = $this->_params[0];
239 $params['payment_processor_id'] = 1;
6c6e6187 240 $params['sequential'] = 1;
ca985406 241 $result = $this->callAPISuccess('event', 'create', $params);
481a74f4 242 $this->assertEquals(1, $result['values'][0]['payment_processor'][0], "handing of payment processor compatibility");
ca985406 243 $result = $this->callAPISuccess('event', 'get', $params);
6c6e6187 244 $this->assertEquals($result['values'][0]['payment_processor_id'], 1, "handing get payment processor compatibility");
6a488035
TO
245 }
246
00be9182 247 public function testInvalidData() {
6a488035 248 $params = $this->_params[0];
6c6e6187
TO
249 $params['sequential'] = 1;
250 $params['loc_block_id'] = 100;
d0e1eff2 251 $result = $this->callAPIFailure('event', 'create', $params);
6a488035
TO
252
253 }
254
c490a46a
CW
255 /**
256 * Test 'is.Current' option. Existing event is 'old' so only current should be returned
257 */
00be9182 258 public function testGetSingleReturnIsFull() {
6a488035
TO
259 $contactID = $this->individualCreate();
260 $params = array(
6c6e6187 261 'id' => $this->_eventIds[0],
92915c55 262 'max_participants' => 1,
6a488035 263 );
ca985406 264 $result = $this->callAPISuccess('Event', 'Create', $params);
6a488035
TO
265
266 $getEventParams = array(
6c6e6187 267 'id' => $this->_eventIds[0],
92915c55 268 'return.is_full' => 1,
6a488035
TO
269 );
270
ca985406 271 $currentEvent = $this->callAPISuccess('Event', 'getsingle', $getEventParams);
5c49fee0 272 $description = "Demonstrates use of return is_full .";
92915c55 273 $subfile = "IsFullOption";
6a488035
TO
274 $this->assertEquals(0, $currentEvent['is_full'], ' is full is set in line ' . __LINE__);
275 $this->assertEquals(1, $currentEvent['available_places'], 'available places is set in line ' . __LINE__);
92915c55
TO
276 $participant = $this->callAPISuccess('Participant', 'create', array(
277 'participant_status' => 1,
278 'role_id' => 1,
279 'contact_id' => $contactID,
408b79bf 280 'event_id' => $this->_eventIds[0],
92915c55 281 ));
a828d7b8 282 $currentEvent = $this->callAPIAndDocument('Event', 'getsingle', $getEventParams, __FUNCTION__, __FILE__, $description, $subfile);
6a488035
TO
283 $this->assertEquals(1, $currentEvent['is_full'], ' is full is set in line ' . __LINE__);
284 $this->assertEquals(0, $currentEvent['available_places'], 'available places is set in line ' . __LINE__);
285
286 $this->contactDelete($contactID);
287 }
92915c55 288
408b79bf 289 /**
fe482240
EM
290 * Legacy support for Contribution Type ID.
291 *
292 * We need to ensure this is supported as an alias for financial_type_id.
6a488035 293 */
00be9182 294 public function testCreateGetEventLegacyContributionTypeID() {
6a488035 295 $contributionTypeArray = array('contribution_type_id' => 3);
9b873358 296 if (isset($this->_params[0]['financial_type_id'])) {
6a488035
TO
297 //in case someone edits $this->_params & invalidates this test :-)
298 unset($this->_params[0]['financial_type_id']);
299 }
ca985406 300 $result = $this->callAPISuccess('event', 'create', $this->_params[0] + $contributionTypeArray);
301 $getresult = $this->callAPISuccess('event', 'get', array() + $contributionTypeArray);
ef0bc919 302 $this->assertEquals($getresult['values'][$getresult['id']]['contribution_type_id'], 3);
6a488035 303 $this->assertEquals($result['id'], $getresult['id']);
ca985406 304 $this->callAPISuccess('event', 'delete', array('id' => $result['id']));
6a488035 305 }
6a488035 306
f8de8583
JV
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 );
3c27d467 325 $createResult = $this->callAPISuccess('LocBlock', 'create', $locBlockParams);
f8de8583 326 $locBlockId = $createResult['id'];
f8de8583
JV
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];
aa1904e1 353 $result = $this->callAPISuccess($this->_entity, 'create', $params);
f8de8583
JV
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 ));
aa1904e1 360 $this->assertEquals($result['id'], $check['id']);
f8de8583
JV
361
362 $this->callAPISuccess($this->_entity, 'Delete', array('id' => $result['id']));
363 }
364
6a488035 365 /**
fe482240
EM
366 * Check with complete array + custom field.
367 *
6a488035
TO
368 * Note that the test is written on purpose without any
369 * variables specific to participant so it can be replicated into other entities
fe482240 370 * and / or moved to the automated test suite.
6a488035 371 */
00be9182 372 public function testCreateWithCustom() {
6a488035
TO
373 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
374
375 $params = $this->_params[0];
376 $params['custom_' . $ids['custom_field_id']] = "custom string";
377
226ede24 378 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
6a488035 379
92915c55
TO
380 $check = $this->callAPISuccess($this->_entity, 'get', array(
381 'return.custom_' . $ids['custom_field_id'] => 1,
408b79bf 382 'id' => $result['id'],
92915c55 383 ));
6a488035
TO
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']);
ca985406 388 $this->callAPISuccess($this->_entity, 'Delete', array('id' => $result['id']));
6a488035
TO
389 }
390
1b68d37e
JV
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
2c4ec68f
JV
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(
1ae9446b 424 'custom_' . $ids['custom_field_id'] => array('IS NULL' => 1),
2c4ec68f
JV
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
d9bd98e8
JV
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
3c27d467 475 $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
d9bd98e8
JV
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
4347617d
JV
496 /**
497 * Test searching on custom fields with less than or equal.
4339a82a 498 *
4347617d
JV
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
3dc81e0d
JV
527 /**
528 * Test searching on custom fields with netsted call with id param.
722cc9f5 529 *
3dc81e0d
JV
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
722cc9f5 533 * happened in CiviCRM 4.6.5 that broke my fix for CRM-16036, causing
3dc81e0d
JV
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'];
722cc9f5 541
3dc81e0d
JV
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.
0f3699bf 553 $result = $this->callAPISuccess($this->_entity, 'get', array(
3dc81e0d
JV
554 'custom_' . $custom_field_id => "12345",
555 'api.LocBlock.get' => array("id" => '$value.loc_block_id'),
0f3699bf 556 ));
2b28667f 557
3dc81e0d
JV
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 }
722cc9f5
JV
567
568
b2cdd843 569 /**
eceb18cc 570 * Test that an event with a price set can be created.
b2cdd843 571 */
00be9182 572 public function testCreatePaidEvent() {
b2cdd843
EM
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
00be9182 580 public function testCreateEventParamsNotArray() {
6a488035 581 $params = NULL;
d0e1eff2 582 $result = $this->callAPIFailure('event', 'create', $params);
6a488035
TO
583 }
584
00be9182 585 public function testCreateEventEmptyParams() {
6a488035 586 $params = array();
d0e1eff2 587 $result = $this->callAPIFailure('event', 'create', $params);
6a488035
TO
588 }
589
00be9182 590 public function testCreateEventParamsWithoutTitle() {
6a488035 591 unset($this->_params['title']);
ca985406 592 $result = $this->callAPIFailure('event', 'create', $this->_params);
791c263c 593 $this->assertAPIFailure($result);
6a488035
TO
594 }
595
00be9182 596 public function testCreateEventParamsWithoutEventTypeId() {
6a488035 597 unset($this->_params['event_type_id']);
ca985406 598 $result = $this->callAPIFailure('event', 'create', $this->_params);
6a488035
TO
599 }
600
00be9182 601 public function testCreateEventParamsWithoutStartDate() {
6a488035 602 unset($this->_params['start_date']);
ca985406 603 $result = $this->callAPIFailure('event', 'create', $this->_params);
6a488035
TO
604 }
605
00be9182 606 public function testCreateEventSuccess() {
ca985406 607 $result = $this->callAPIAndDocument('Event', 'Create', $this->_params[0], __FUNCTION__, __FILE__);
ba4a1892 608 $this->assertArrayHasKey('id', $result['values'][$result['id']]);
ca985406 609 $result = $this->callAPISuccess($this->_entity, 'Get', array('id' => $result['id']));
610 $this->callAPISuccess($this->_entity, 'Delete', array('id' => $result['id']));
3c27d467 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');
6a488035 615 }
c490a46a
CW
616
617 /**
eceb18cc 618 * Test that passing in Unique field names works.
c490a46a 619 */
00be9182 620 public function testCreateEventSuccessUniqueFieldNames() {
6a488035
TO
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']);
ca985406 625 $result = $this->callAPISuccess('Event', 'Create', $this->_params[0]);
a15773db 626 $this->assertAPISuccess($result);
ba4a1892 627 $this->assertArrayHasKey('id', $result['values'][$result['id']]);
ca985406 628 $result = $this->callAPISuccess($this->_entity, 'Get', array('id' => $result['id']));
629 $this->callAPISuccess($this->_entity, 'Delete', array('id' => $result['id']));
6a488035
TO
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__);
6a488035
TO
636 }
637
00be9182 638 public function testUpdateEvent() {
ca985406 639 $result = $this->callAPISuccess('event', 'create', $this->_params[1]);
6a488035 640
6a488035 641 $params = array(
6c6e6187 642 'id' => $result['id'],
92915c55 643 'max_participants' => 150,
6a488035 644 );
ca985406 645 $this->callAPISuccess('Event', 'Create', $params);
fb32de45 646 $updated = $this->callAPISuccess('Event', 'Get', $params, __FUNCTION__, __FILE__);
6a488035
TO
647 $this->assertEquals(150, $updated['values'][$result['id']]['max_participants']);
648 $this->assertEquals('Annual CiviCRM meet 2', $updated['values'][$result['id']]['title']);
ca985406 649 $this->callAPISuccess($this->_entity, 'Delete', array('id' => $result['id']));
6a488035
TO
650 }
651
6a488035 652
00be9182 653 public function testDeleteEmptyParams() {
ca985406 654 $result = $this->callAPIFailure('Event', 'Delete', array());
6a488035
TO
655 }
656
00be9182 657 public function testDelete() {
6a488035
TO
658 $params = array(
659 'id' => $this->_eventIds[0],
6a488035 660 );
3c27d467 661 $this->callAPIAndDocument('Event', 'Delete', $params, __FUNCTION__, __FILE__);
6a488035 662 }
ca985406 663
664 /**
fe482240 665 * Check event_id still supported for delete.
ca985406 666 */
00be9182 667 public function testDeleteWithEventId() {
6a488035 668 $params = array(
92915c55
TO
669 'event_id' => $this->_eventIds[0],
670 );
ca985406 671 $result = $this->callAPISuccess('Event', 'Delete', $params);
a15773db 672 $this->assertAPISuccess($result);
6a488035 673 }
c490a46a
CW
674
675 /**
eceb18cc 676 * Trying to delete an event with participants should return error.
c490a46a 677 */
00be9182 678 public function testDeleteWithExistingParticipant() {
e4d5f1e2 679 $contactID = $this->individualCreate();
3c27d467 680 $this->participantCreate(
6a488035
TO
681 array(
682 'contactID' => $contactID,
683 'eventID' => $this->_eventIds[0],
684 )
685 );
3c27d467 686 $this->callAPISuccess('Event', 'Delete', array('id' => $this->_eventIds[0]));
6a488035
TO
687 }
688
00be9182 689 public function testDeleteWithWrongEventId() {
ca985406 690 $params = array('event_id' => $this->_eventIds[0]);
691 $result = $this->callAPISuccess('Event', 'Delete', $params);
6a488035
TO
692 // try to delete again - there's no such event anymore
693 $params = array('event_id' => $this->_eventIds[0]);
d0e1eff2 694 $result = $this->callAPIFailure('Event', 'Delete', $params);
6a488035
TO
695 }
696
6a488035 697 /**
fe482240 698 * Test civicrm_event_search with wrong params type.
6a488035 699 */
00be9182 700 public function testSearchWrongParamsType() {
6a488035 701 $params = 'a string';
d0e1eff2 702 $result = $this->callAPIFailure('event', 'get', $params);
6a488035
TO
703 }
704
705 /**
d177a2a6 706 * Test civicrm_event_search with empty params.
6a488035 707 */
00be9182 708 public function testSearchEmptyParams() {
d177a2a6 709 $this->callAPISuccess('event', 'create', $this->_params[1]);
6a488035 710
d177a2a6 711 $getParams = array(
92915c55 712 'sequential' => 1,
6a488035 713 );
d177a2a6
EM
714 $result = $this->callAPISuccess('event', 'get', $getParams);
715 $this->assertEquals($result['count'], 3);
6a488035 716 $res = $result['values'][0];
d177a2a6
EM
717 $this->assertArrayKeyExists('title', $res);
718 $this->assertEquals($res['event_type_id'], $this->_params[1]['event_type_id']);
6a488035
TO
719 }
720
721 /**
d177a2a6 722 * Test civicrm_event_search. Success expected.
6a488035 723 */
00be9182 724 public function testSearch() {
6a488035
TO
725 $params = array(
726 'event_type_id' => 1,
727 'return.title' => 1,
728 'return.id' => 1,
92915c55
TO
729 'return.start_date' => 1,
730 );
ca985406 731 $result = $this->callAPISuccess('event', 'get', $params);
6a488035 732
ba4a1892
TM
733 $this->assertEquals($result['values'][$this->_eventIds[0]]['id'], $this->_eventIds[0]);
734 $this->assertEquals($result['values'][$this->_eventIds[0]]['title'], 'Annual CiviCRM meet');
6a488035
TO
735 }
736
737 /**
fe482240
EM
738 * Test civicrm_event_search.
739 *
740 * Success expected.
741 *
d177a2a6 742 * return.offset and return.max_results test (CRM-5266)
6a488035 743 */
00be9182 744 public function testSearchWithOffsetAndMaxResults() {
6a488035
TO
745 $maxEvents = 5;
746 $events = array();
747 while ($maxEvents > 0) {
6c6e6187 748 $params = array(
92915c55 749 'title' => 'Test Event' . $maxEvents,
6a488035
TO
750 'event_type_id' => 2,
751 'start_date' => 20081021,
752 );
753
ca985406 754 $events[$maxEvents] = $this->callAPISuccess('event', 'create', $params);
6a488035
TO
755 $maxEvents--;
756 }
6c6e6187 757 $params = array(
92915c55 758 'event_type_id' => 2,
6a488035
TO
759 'return.id' => 1,
760 'return.title' => 1,
761 'return.offset' => 2,
762 'return.max_results' => 2,
763 );
ca985406 764 $result = $this->callAPISuccess('event', 'get', $params);
6a488035
TO
765 $this->assertAPISuccess($result);
766 $this->assertEquals(2, $result['count'], ' 2 results returned In line ' . __LINE__);
767 }
768
00be9182 769 public function testEventCreationPermissions() {
6a488035 770 $params = array(
6c6e6187 771 'event_type_id' => 1,
92915c55
TO
772 'start_date' => '2010-10-03',
773 'title' => 'le cake is a tie',
774 'check_permissions' => TRUE,
775 );
dbaa9d7d 776 $config = CRM_Core_Config::singleton();
6a488035 777 $config->userPermissionClass->permissions = array('access CiviCRM');
d0e1eff2 778 $result = $this->callAPIFailure('event', 'create', $params);
1644b908 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');
6a488035 780
fe482240
EM
781 $config->userPermissionClass->permissions = array(
782 'access CiviEvent',
783 'edit all events',
784 'access CiviCRM',
785 );
ca985406 786 $result = $this->callAPISuccess('event', 'create', $params);
6a488035
TO
787 }
788
00be9182 789 public function testgetfields() {
5c49fee0 790 $description = "Demonstrate use of getfields to interrogate api.";
ca985406 791 $params = array('action' => 'create');
792 $result = $this->callAPISuccess('event', 'getfields', $params);
48ae8c9d 793 $this->assertEquals(1, $result['values']['is_active']['api.default']);
6a488035 794 }
c490a46a
CW
795
796 /**
fe482240 797 * Test api_action param also works.
c490a46a 798 */
00be9182 799 public function testgetfieldsRest() {
5c49fee0 800 $description = "Demonstrate use of getfields to interrogate api.";
ca985406 801 $params = array('api_action' => 'create');
802 $result = $this->callAPISuccess('event', 'getfields', $params);
48ae8c9d 803 $this->assertEquals(1, $result['values']['is_active']['api.default']);
6a488035 804 }
92915c55 805
00be9182 806 public function testgetfieldsGet() {
5c49fee0 807 $description = "Demonstrate use of getfields to interrogate api.";
ca985406 808 $params = array('action' => 'get');
809 $result = $this->callAPISuccess('event', 'getfields', $params);
ba4a1892 810 $this->assertEquals('title', $result['values']['event_title']['name']);
6a488035 811 }
92915c55 812
00be9182 813 public function testgetfieldsDelete() {
5c49fee0 814 $description = "Demonstrate use of getfields to interrogate api.";
ca985406 815 $params = array('action' => 'delete');
816 $result = $this->callAPISuccess('event', 'getfields', $params);
6a488035
TO
817 $this->assertEquals(1, $result['values']['id']['api.required']);
818 }
96025800 819
bd02e702
CW
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
6a488035 845}