Merge pull request #9776 from jitendrapurohit/CRM-19741
[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-2017 |
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->callAPISuccess('LocBlock', 'create', $locBlockParams);
300 $locBlockId = $createResult['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 $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');
565 $this->assertEquals('2008-10-23 00:00:00', $result['values'][$result['id']]['end_date'], 'end date is not set');
566 $this->assertEquals('2008-06-01 00:00:00', $result['values'][$result['id']]['registration_start_date'], 'start date is not set');
567 $this->assertEquals('2008-10-15 00:00:00', $result['values'][$result['id']]['registration_end_date'], 'end date is not set');
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 $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 $this->participantCreate(
634 array(
635 'contactID' => $contactID,
636 'eventID' => $this->_eventIds[0],
637 )
638 );
639 $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 /**
651 * Test civicrm_event_search with wrong params type.
652 */
653 public function testSearchWrongParamsType() {
654 $params = 'a string';
655 $result = $this->callAPIFailure('event', 'get', $params);
656 }
657
658 /**
659 * Test civicrm_event_search with empty params.
660 */
661 public function testSearchEmptyParams() {
662 $this->callAPISuccess('event', 'create', $this->_params[1]);
663
664 $getParams = array(
665 'sequential' => 1,
666 );
667 $result = $this->callAPISuccess('event', 'get', $getParams);
668 $this->assertEquals($result['count'], 3);
669 $res = $result['values'][0];
670 $this->assertArrayKeyExists('title', $res);
671 $this->assertEquals($res['event_type_id'], $this->_params[1]['event_type_id']);
672 }
673
674 /**
675 * Test civicrm_event_search. Success expected.
676 */
677 public function testSearch() {
678 $params = array(
679 'event_type_id' => 1,
680 'return.title' => 1,
681 'return.id' => 1,
682 'return.start_date' => 1,
683 );
684 $result = $this->callAPISuccess('event', 'get', $params);
685
686 $this->assertEquals($result['values'][$this->_eventIds[0]]['id'], $this->_eventIds[0]);
687 $this->assertEquals($result['values'][$this->_eventIds[0]]['title'], 'Annual CiviCRM meet');
688 }
689
690 /**
691 * Test civicrm_event_search.
692 *
693 * Success expected.
694 *
695 * return.offset and return.max_results test (CRM-5266)
696 */
697 public function testSearchWithOffsetAndMaxResults() {
698 $maxEvents = 5;
699 $events = array();
700 while ($maxEvents > 0) {
701 $params = array(
702 'title' => 'Test Event' . $maxEvents,
703 'event_type_id' => 2,
704 'start_date' => 20081021,
705 );
706
707 $events[$maxEvents] = $this->callAPISuccess('event', 'create', $params);
708 $maxEvents--;
709 }
710 $params = array(
711 'event_type_id' => 2,
712 'return.id' => 1,
713 'return.title' => 1,
714 'return.offset' => 2,
715 'return.max_results' => 2,
716 );
717 $result = $this->callAPISuccess('event', 'get', $params);
718 $this->assertAPISuccess($result);
719 $this->assertEquals(2, $result['count'], ' 2 results returned In line ' . __LINE__);
720 }
721
722 public function testEventCreationPermissions() {
723 $params = array(
724 'event_type_id' => 1,
725 'start_date' => '2010-10-03',
726 'title' => 'le cake is a tie',
727 'check_permissions' => TRUE,
728 );
729 $config = &CRM_Core_Config::singleton();
730 $config->userPermissionClass->permissions = array('access CiviCRM');
731 $result = $this->callAPIFailure('event', 'create', $params);
732 $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');
733
734 $config->userPermissionClass->permissions = array(
735 'access CiviEvent',
736 'edit all events',
737 'access CiviCRM',
738 );
739 $result = $this->callAPISuccess('event', 'create', $params);
740 }
741
742 public function testgetfields() {
743 $description = "Demonstrate use of getfields to interrogate api.";
744 $params = array('action' => 'create');
745 $result = $this->callAPISuccess('event', 'getfields', $params);
746 $this->assertEquals(1, $result['values']['is_active']['api.default']);
747 }
748
749 /**
750 * Test api_action param also works.
751 */
752 public function testgetfieldsRest() {
753 $description = "Demonstrate use of getfields to interrogate api.";
754 $params = array('api_action' => 'create');
755 $result = $this->callAPISuccess('event', 'getfields', $params);
756 $this->assertEquals(1, $result['values']['is_active']['api.default']);
757 }
758
759 public function testgetfieldsGet() {
760 $description = "Demonstrate use of getfields to interrogate api.";
761 $params = array('action' => 'get');
762 $result = $this->callAPISuccess('event', 'getfields', $params);
763 $this->assertEquals('title', $result['values']['event_title']['name']);
764 }
765
766 public function testgetfieldsDelete() {
767 $description = "Demonstrate use of getfields to interrogate api.";
768 $params = array('action' => 'delete');
769 $result = $this->callAPISuccess('event', 'getfields', $params);
770 $this->assertEquals(1, $result['values']['id']['api.required']);
771 }
772
773 public function testCreateFromTemplate() {
774 $templateParams = array(
775 'summary' => 'Sign up now to learn the results of this unit test',
776 'description' => 'This event is created from a template, so all the values should be the same as the original ones.',
777 'event_type_id' => 1,
778 'is_public' => 1,
779 'end_date' => '2018-06-25 17:00:00',
780 'is_online_registration' => 1,
781 'registration_start_date' => '2017-06-25 17:00:00',
782 'registration_end_date' => '2018-06-25 17:00:00',
783 'max_participants' => 100,
784 'event_full_text' => 'Sorry! We are already full',
785 );
786 $templateResult = $this->callAPISuccess('Event', 'create', array('is_template' => 1, 'template_title' => 'Test tpl') + $templateParams);
787 $eventResult = $this->callAPISuccess('Event', 'create', array(
788 'template_id' => $templateResult['id'],
789 'title' => 'Clone1',
790 'start_date' => '2018-06-25 16:00:00',
791 ));
792 $eventResult = $this->callAPISuccess('Event', 'getsingle', array('id' => $eventResult['id']));
793 foreach ($templateParams as $param => $value) {
794 $this->assertEquals($value, $eventResult[$param]);
795 }
796 }
797
798 }