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