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