3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * Class CRM_Event_BAO_ParticipantTest
17 class CRM_Event_BAO_ParticipantTest
extends CiviUnitTestCase
{
19 public function setUp() {
21 $this->_contactId
= $this->individualCreate();
22 $event = $this->eventCreate();
23 $this->_eventId
= $event['id'];
27 * Add() method (add and edit modes of participant)
29 public function testAdd() {
34 'event_id' => $this->_eventId
,
35 'register_date' => date('Y-m-d') . " 00:00:00",
38 'source' => 'Event_' . $this->_eventId
,
39 'contact_id' => $this->_contactId
,
42 // New Participant Created
43 $participant = CRM_Event_BAO_Participant
::add($params);
45 $this->assertDBNotNull('CRM_Event_BAO_Participant', $this->_contactId
, 'id',
46 'contact_id', 'Check DB for Participant of the contact'
49 $this->assertDBCompareValue('CRM_Event_BAO_Participant', $participant->id
, 'contact_id',
50 'id', $this->_contactId
, 'Check DB for contact of the participant'
53 $params = array_merge($params, [
54 'id' => $participant->id
,
60 $updatedParticipant = CRM_Event_BAO_Participant
::add($params);
61 $this->assertDBCompareValue('CRM_Event_BAO_Participant', $updatedParticipant->id
, 'role_id',
62 'id', 2, 'Check DB for updated role id of the participant'
65 $this->assertDBCompareValue('CRM_Event_BAO_Participant', $updatedParticipant->id
, 'status_id',
66 'id', 3, 'Check DB for updated status id of the participant'
69 $this->contactDelete($this->_contactId
);
70 $this->eventDelete($this->_eventId
);
74 * GetValues() method (fetch value of participant)
76 public function testgetValuesWithValidParams() {
77 $participantId = $this->participantCreate(['contact_id' => $this->_contactId
, 'event_id' => $this->_eventId
]);
78 $params = ['id' => $participantId];
81 $fetchParticipant = CRM_Event_BAO_Participant
::getValues($params, $values, $ids);
82 $compareValues = $fetchParticipant[$participantId];
88 'event_id' => $this->_eventId
,
89 'register_date' => '2007-02-19 00:00:00',
92 'source' => 'Wimbeldon',
93 'contact_id' => $this->_contactId
,
94 'id' => $participantId,
95 'campaign_id' => NULL,
98 'registered_by_id' => NULL,
99 'discount_id' => NULL,
100 'fee_currency' => NULL,
101 'discount_amount' => NULL,
104 'transferred_to_contact_id' => NULL,
107 foreach ($compareValues as $key => $value) {
108 if (substr($key, 0, 1) != '_' && $key != 'N') {
109 $this->assertEquals($compareValues->$key, $params[$key], 'Check for ' . $key . ' for given participant');
113 $this->participantDelete($participantId);
114 $this->contactDelete($this->_contactId
);
115 $this->eventDelete($this->_eventId
);
119 * GetValues() method (checking for behavior when params are empty )
121 public function testgetValuesWithoutValidParams() {
122 $params = $values = $ids = [];
123 $this->participantCreate(['contact_id' => $this->_contactId
, 'event_id' => $this->_eventId
]);
124 $fetchParticipant = CRM_Event_BAO_Participant
::getValues($params, $values, $ids);
125 $this->assertNull($fetchParticipant);
127 $this->contactDelete($this->_contactId
);
128 $this->eventDelete($this->_eventId
);
132 * EventFull() method (checking the event for full )
134 public function testEventFull() {
136 'max_participants' => 1,
137 'id' => $this->_eventId
,
139 CRM_Event_BAO_Event
::add($eventParams);
141 $participantId = $this->participantCreate(['contact_id' => $this->_contactId
, 'event_id' => $this->_eventId
]);
142 $eventFull = CRM_Event_BAO_Participant
::eventFull($this->_eventId
);
144 $this->assertEquals($eventFull, 'Sorry! We are already full', 'Checking if Event is full.');
146 $this->participantDelete($participantId);
147 $this->contactDelete($this->_contactId
);
148 $this->eventDelete($this->_eventId
);
152 * ImportableFields() method ( Checking the Event's Importable Fields )
154 public function testimportableFields() {
155 $importableFields = CRM_Event_BAO_Participant
::importableFields();
156 $this->assertNotEquals(count($importableFields), 0, 'Checking array not to be empty.');
158 $this->contactDelete($this->_contactId
);
159 $this->eventDelete($this->_eventId
);
163 * ParticipantDetails() method ( Checking the Participant Details )
165 public function testparticipantDetails() {
166 $participant = $this->callAPISuccess('Participant', 'create', ['contact_id' => $this->_contactId
, 'event_id' => $this->_eventId
]);
167 $params = ['name' => 'Anderson, Anthony', 'title' => 'Annual CiviCRM meet'];
169 $participantDetails = CRM_Event_BAO_Participant
::participantDetails($participant['id']);
171 $this->assertEquals(count($participantDetails), 3, 'Equating the array contains.');
172 $this->assertEquals($participantDetails['name'], $params['name'], 'Checking Name of Participant.');
173 $this->assertEquals($participantDetails['title'], $params['title'], 'Checking Event Title in which participant is enroled.');
175 $this->participantDelete($participant['id']);
176 $this->contactDelete($this->_contactId
);
177 $this->eventDelete($this->_eventId
);
181 * DeleteParticipant() method ( Delete a Participant )
183 public function testdeleteParticipant() {
188 'event_id' => $this->_eventId
,
189 'register_date' => date('Y-m-d') . " 00:00:00",
192 'source' => 'Event_' . $this->_eventId
,
193 'contact_id' => $this->_contactId
,
196 // New Participant Created
197 $participant = CRM_Event_BAO_Participant
::add($params);
199 $this->assertDBNotNull('CRM_Event_BAO_Participant', $this->_contactId
, 'id',
200 'contact_id', 'Check DB for Participant of the contact'
203 $this->assertDBCompareValue('CRM_Event_BAO_Participant', $participant->id
, 'contact_id',
204 'id', $this->_contactId
, 'Check DB for contact of the participant'
207 CRM_Event_BAO_Participant
::deleteParticipant($participant->id
);
208 $this->assertDBNull('CRM_Event_BAO_Participant', $participant->id
, 'contact_id', 'id', 'Check DB for deleted Participant.');
210 $this->contactDelete($this->_contactId
);
211 $this->eventDelete($this->_eventId
);
215 * CheckDuplicate() method ( Checking for Duplicate Participant returns array of participant id)
217 public function testcheckDuplicate() {
220 //Creating 3 new participants
221 for ($i = 0; $i < 3; $i++
) {
222 $partiId[] = $this->participantCreate(['contact_id' => $this->_contactId
, 'event_id' => $this->_eventId
]);
225 $params = ['event_id' => $this->_eventId
, 'contact_id' => $this->_contactId
];
226 CRM_Event_BAO_Participant
::checkDuplicate($params, $duplicate);
228 $this->assertEquals(count($duplicate), 3, 'Equating the array contains with duplicate array.');
230 //Checking for the duplicate participant
231 foreach ($duplicate as $key => $value) {
232 $this->assertEquals($partiId[$key], $duplicate[$key], 'Equating the contactid which is in the database.');
235 //Deleting all participant
236 for ($i = 0; $i < 3; $i++
) {
237 $partidel[] = $this->participantDelete($partiId[$i]);
240 $this->contactDelete($this->_contactId
);
241 $this->eventDelete($this->_eventId
);
245 * Create() method (create and updation of participant)
247 public function testCreate() {
252 'event_id' => $this->_eventId
,
253 'register_date' => date('Y-m-d') . " 00:00:00",
256 'source' => 'Event_' . $this->_eventId
,
257 'contact_id' => $this->_contactId
,
258 'note' => 'Note added for Event_' . $this->_eventId
,
261 $participant = CRM_Event_BAO_Participant
::create($params);
262 //Checking for Contact id in the participant table.
263 $pid = $this->assertDBNotNull('CRM_Event_DAO_Participant', $this->_contactId
, 'id',
264 'contact_id', 'Check DB for Participant of the contact'
267 //Checking for Activity added in the table for relative participant.
268 $this->assertDBCompareValue('CRM_Activity_DAO_Activity', $this->_contactId
, 'source_record_id',
269 'source_contact_id', $participant->id
, 'Check DB for activity added for the participant'
272 $params = array_merge($params, [
273 'id' => $participant->id
,
276 'note' => 'Test Event in edit mode is running successfully ....',
279 $participant = CRM_Event_BAO_Participant
::create($params);
281 //Checking Edited Value of role_id in the database.
282 $this->assertDBCompareValue('CRM_Event_DAO_Participant', $participant->id
, 'role_id',
283 'id', 2, 'Check DB for updated role id of the participant'
286 //Checking Edited Value of status_id in the database.
287 $this->assertDBCompareValue('CRM_Event_DAO_Participant', $participant->id
, 'status_id',
288 'id', 3, 'Check DB for updated status id of the participant'
291 //Checking for Activity added in the table for relative participant.
292 $this->assertDBCompareValue('CRM_Activity_DAO_Activity', $this->_contactId
, 'source_record_id',
293 'source_contact_id', $participant->id
, 'Check DB for activity added for the participant'
296 //Checking for Note added in the table for relative participant.
297 $session = CRM_Core_Session
::singleton();
298 $id = $session->get('userID');
300 $id = $this->_contactId
;
303 //Deleting the Participant created by create function in this function
304 CRM_Event_BAO_Participant
::deleteParticipant($participant->id
);
305 $this->assertDBNull('CRM_Event_DAO_Participant', $this->_contactId
, 'id',
306 'contact_id', 'Check DB for deleted participant. Should be NULL.'
309 $this->contactDelete($this->_contactId
);
310 $this->eventDelete($this->_eventId
);
314 * ExportableFields() method ( Exportable Fields for Participant)
316 public function testexportableFields() {
317 $exportableFields = CRM_Event_BAO_Participant
::exportableFields();
318 $this->assertNotEquals(count($exportableFields), 0, 'Checking array not to be empty.');
320 $this->contactDelete($this->_contactId
);
321 $this->eventDelete($this->_eventId
);
325 * FixEventLevel() method (Setting ',' values), resolveDefaults(assinging value to array) method
327 public function testfixEventLevel() {
329 $paramsSet['title'] = 'Price Set';
330 $paramsSet['name'] = CRM_Utils_String
::titleToVar('Price Set');
331 $paramsSet['is_active'] = FALSE;
332 $paramsSet['extends'] = 1;
334 $priceset = CRM_Price_BAO_PriceSet
::create($paramsSet);
336 //Checking for priceset added in the table.
337 $this->assertDBCompareValue('CRM_Price_BAO_PriceSet', $priceset->id
, 'title',
338 'id', $paramsSet['title'], 'Check DB for created priceset'
341 'label' => 'Price Field',
342 'name' => CRM_Utils_String
::titleToVar('Price Field'),
343 'html_type' => 'Text',
345 'option_label' => ['1' => 'Price Field'],
346 'option_value' => ['1' => 10],
347 'option_name' => ['1' => 10],
348 'option_weight' => ['1' => 1],
349 'is_display_amounts' => 1,
351 'options_per_line' => 1,
352 'is_active' => ['1' => 1],
353 'price_set_id' => $priceset->id
,
358 $pricefield = CRM_Price_BAO_PriceField
::create($paramsField, $ids);
360 //Checking for priceset added in the table.
361 $this->assertDBCompareValue('CRM_Price_BAO_PriceField', $pricefield->id
, 'label',
362 'id', $paramsField['label'], 'Check DB for created pricefield'
365 $eventId = $this->_eventId
;
366 $participantParams = [
370 'event_id' => $eventId,
371 'register_date' => date('Y-m-d') . " 00:00:00",
374 'source' => 'Event_' . $eventId,
375 'contact_id' => $this->_contactId
,
376 'note' => 'Note added for Event_' . $eventId,
377 'fee_level' => '\ 1Price_Field - 55\ 1',
380 $participant = CRM_Event_BAO_Participant
::add($participantParams);
382 //Checking for participant added in the table.
383 $this->assertDBCompareValue('CRM_Event_BAO_Participant', $this->_contactId
, 'id',
384 'contact_id', $participant->id
, 'Check DB for created participant'
389 $params = ['id' => $participant->id
];
391 CRM_Event_BAO_Participant
::getValues($params, $values, $ids);
392 $this->assertNotEquals(count($values), 0, 'Checking for empty array.');
394 CRM_Event_BAO_Participant
::resolveDefaults($values[$participant->id
]);
396 if ($values[$participant->id
]['fee_level']) {
397 CRM_Event_BAO_Participant
::fixEventLevel($values[$participant->id
]['fee_level']);
400 CRM_Price_BAO_PriceField
::deleteField($pricefield->id
);
401 $this->assertDBNull('CRM_Price_BAO_PriceField', $pricefield->id
, 'name',
402 'id', 'Check DB for non-existence of Price Field.'
405 CRM_Price_BAO_PriceSet
::deleteSet($priceset->id
);
406 $this->assertDBNull('CRM_Price_BAO_PriceSet', $priceset->id
, 'title',
407 'id', 'Check DB for non-existence of Price Set.'
410 $this->participantDelete($participant->id
);
411 $this->contactDelete($this->_contactId
);
412 $this->eventDelete($eventId);
416 * Test various self-service eligibility scenarios.
418 * @dataProvider selfServiceScenarios
419 * @param $selfSvcEnabled
420 * @param $selfSvcHours
421 * @param $hoursToEvent
422 * @param $participantStatusId
423 * @param $isBackOffice
424 * @param $successExpected A boolean that indicates whether this test should pass or fail.
426 public function testGetSelfServiceEligibility($selfSvcEnabled, $selfSvcHours, $hoursToEvent, $participantStatusId, $isBackOffice, $successExpected) {
427 $participantId = $this->participantCreate(['contact_id' => $this->_contactId
, 'event_id' => $this->_eventId
, 'status_id' => $participantStatusId]);
428 $now = new Datetime();
429 $startDate = $now->add(new DateInterval("PT{$hoursToEvent}H"))->format('Y-m-d H:i:s');
430 $this->callAPISuccess('Event', 'create', [
431 'id' => $this->_eventId
,
432 'allow_selfcancelxfer' => $selfSvcEnabled,
433 'selfcancelxfer_time' => $selfSvcHours,
434 'start_date' => $startDate,
436 $url = CRM_Utils_System
::url('civicrm/event/info', "reset=1&id={$this->_eventId}");
437 $details = CRM_Event_BAO_Participant
::getSelfServiceEligibility($participantId, $url, $isBackOffice);
438 $this->assertEquals($details['eligible'], $successExpected);
441 public function selfServiceScenarios() {
442 // Standard pass scenario
444 'selfSvcEnabled' => 1,
445 'selfSvcHours' => 12,
446 'hoursToEvent' => 16,
447 'participantStatusId' => 1,
448 'isBackOffice' => FALSE,
449 'successExpected' => TRUE,
451 // Too late to self-service
453 'selfSvcEnabled' => 1,
454 'selfSvcHours' => 12,
456 'participantStatusId' => 1,
457 'isBackOffice' => FALSE,
458 'successExpected' => FALSE,
460 // Participant status is other than "Registered".
462 'selfSvcEnabled' => 1,
463 'selfSvcHours' => 12,
464 'hoursToEvent' => 16,
465 'participantStatusId' => 2,
466 'isBackOffice' => FALSE,
467 'successExpected' => FALSE,
469 // Event doesn't allow self-service
471 'selfSvcEnabled' => 0,
472 'selfSvcHours' => 12,
473 'hoursToEvent' => 16,
474 'participantStatusId' => 1,
475 'isBackOffice' => FALSE,
476 'successExpected' => FALSE,
478 // Cancellation deadline is > 24 hours, still ok to cancel
480 'selfSvcEnabled' => 1,
481 'selfSvcHours' => 36,
482 'hoursToEvent' => 46,
483 'participantStatusId' => 1,
484 'isBackOffice' => FALSE,
485 'successExpected' => TRUE,
487 // Cancellation deadline is > 24 hours, too late to cancel
489 'selfSvcEnabled' => 1,
490 'selfSvcHours' => 36,
491 'hoursToEvent' => 25,
492 'participantStatusId' => 1,
493 'isBackOffice' => FALSE,
494 'successExpected' => FALSE,