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