Merge pull request #14974 from civicrm/5.16
[civicrm-core.git] / tests / phpunit / CRM / Event / BAO / ParticipantTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
2fe49090 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035 27
4cbe18b8
EM
28/**
29 * Class CRM_Event_BAO_ParticipantTest
f78dbf0b 30 *
acb109b7 31 * @group headless
4cbe18b8 32 */
6a488035 33class CRM_Event_BAO_ParticipantTest extends CiviUnitTestCase {
6a488035 34
00be9182 35 public function setUp() {
6a488035 36 parent::setUp();
6ae19242 37 $this->_contactId = $this->individualCreate();
38 $event = $this->eventCreate();
39 $this->_eventId = $event['id'];
6a488035
TO
40 }
41
42 /**
100fef9d 43 * Add() method (add and edit modes of participant)
6a488035 44 */
00be9182 45 public function testAdd() {
f78dbf0b 46 $params = [
6a488035
TO
47 'send_receipt' => 1,
48 'is_test' => 0,
49 'is_pay_later' => 0,
50 'event_id' => $this->_eventId,
51 'register_date' => date('Y-m-d') . " 00:00:00",
52 'role_id' => 1,
53 'status_id' => 1,
54 'source' => 'Event_' . $this->_eventId,
55 'contact_id' => $this->_contactId,
f78dbf0b 56 ];
6a488035
TO
57
58 // New Participant Created
59 $participant = CRM_Event_BAO_Participant::add($params);
60
61 $this->assertDBNotNull('CRM_Event_BAO_Participant', $this->_contactId, 'id',
62 'contact_id', 'Check DB for Participant of the contact'
63 );
64
65 $this->assertDBCompareValue('CRM_Event_BAO_Participant', $participant->id, 'contact_id',
66 'id', $this->_contactId, 'Check DB for contact of the participant'
67 );
68
f78dbf0b 69 $params = array_merge($params, [
92915c55
TO
70 'id' => $participant->id,
71 'role_id' => 2,
72 'status_id' => 3,
f78dbf0b 73 ]);
6a488035
TO
74
75 // Participant Edited
76 $updatedParticipant = CRM_Event_BAO_Participant::add($params);
77 $this->assertDBCompareValue('CRM_Event_BAO_Participant', $updatedParticipant->id, 'role_id',
78 'id', 2, 'Check DB for updated role id of the participant'
79 );
80
81 $this->assertDBCompareValue('CRM_Event_BAO_Participant', $updatedParticipant->id, 'status_id',
82 'id', 3, 'Check DB for updated status id of the participant'
83 );
84
93ac19cd 85 $this->contactDelete($this->_contactId);
6ae19242 86 $this->eventDelete($this->_eventId);
6a488035
TO
87 }
88
89 /**
100fef9d 90 * GetValues() method (fetch value of participant)
6a488035 91 */
00be9182 92 public function testgetValuesWithValidParams() {
f78dbf0b 93 $participantId = $this->participantCreate(['contact_id' => $this->_contactId, 'event_id' => $this->_eventId]);
94 $params = ['id' => $participantId];
95 $values = $ids = [];
6a488035
TO
96
97 $fetchParticipant = CRM_Event_BAO_Participant::getValues($params, $values, $ids);
98 $compareValues = $fetchParticipant[$participantId];
99
f78dbf0b 100 $params = [
6a488035
TO
101 'send_receipt' => 1,
102 'is_test' => 0,
103 'is_pay_later' => 0,
104 'event_id' => $this->_eventId,
6ae19242 105 'register_date' => '2007-02-19 00:00:00',
6a488035 106 'role_id' => 1,
6ae19242 107 'status_id' => 2,
108 'source' => 'Wimbeldon',
6a488035
TO
109 'contact_id' => $this->_contactId,
110 'id' => $participantId,
4b27d4ec 111 'campaign_id' => NULL,
6a488035
TO
112 'fee_level' => NULL,
113 'fee_amount' => NULL,
114 'registered_by_id' => NULL,
115 'discount_id' => NULL,
116 'fee_currency' => NULL,
4b27d4ec
E
117 'discount_amount' => NULL,
118 'cart_id' => NULL,
119 'must_wait' => NULL,
6ae19242 120 'transferred_to_contact_id' => NULL,
f78dbf0b 121 ];
6a488035
TO
122
123 foreach ($compareValues as $key => $value) {
124 if (substr($key, 0, 1) != '_' && $key != 'N') {
125 $this->assertEquals($compareValues->$key, $params[$key], 'Check for ' . $key . ' for given participant');
126 }
127 }
128
6ae19242 129 $this->participantDelete($participantId);
93ac19cd 130 $this->contactDelete($this->_contactId);
6ae19242 131 $this->eventDelete($this->_eventId);
6a488035
TO
132 }
133
134 /**
100fef9d 135 * GetValues() method (checking for behavior when params are empty )
6a488035 136 */
00be9182 137 public function testgetValuesWithoutValidParams() {
f78dbf0b 138 $params = $values = $ids = [];
139 $this->participantCreate(['contact_id' => $this->_contactId, 'event_id' => $this->_eventId]);
6a488035 140 $fetchParticipant = CRM_Event_BAO_Participant::getValues($params, $values, $ids);
a15773db 141 $this->assertNull($fetchParticipant);
6a488035 142
93ac19cd 143 $this->contactDelete($this->_contactId);
6ae19242 144 $this->eventDelete($this->_eventId);
6a488035
TO
145 }
146
147 /**
100fef9d 148 * EventFull() method (checking the event for full )
6a488035 149 */
00be9182 150 public function testEventFull() {
f78dbf0b 151 $eventParams = [
6a488035
TO
152 'max_participants' => 1,
153 'id' => $this->_eventId,
f78dbf0b 154 ];
6a488035
TO
155 CRM_Event_BAO_Event::add($eventParams);
156
f78dbf0b 157 $participantId = $this->participantCreate(['contact_id' => $this->_contactId, 'event_id' => $this->_eventId]);
6a488035
TO
158 $eventFull = CRM_Event_BAO_Participant::eventFull($this->_eventId);
159
6ae19242 160 $this->assertEquals($eventFull, 'Sorry! We are already full', 'Checking if Event is full.');
6a488035 161
6ae19242 162 $this->participantDelete($participantId);
93ac19cd 163 $this->contactDelete($this->_contactId);
6ae19242 164 $this->eventDelete($this->_eventId);
6a488035
TO
165 }
166
167 /**
100fef9d 168 * ImportableFields() method ( Checking the Event's Importable Fields )
6a488035 169 */
00be9182 170 public function testimportableFields() {
6a488035
TO
171 $importableFields = CRM_Event_BAO_Participant::importableFields();
172 $this->assertNotEquals(count($importableFields), 0, 'Checking array not to be empty.');
173
93ac19cd 174 $this->contactDelete($this->_contactId);
6ae19242 175 $this->eventDelete($this->_eventId);
6a488035
TO
176 }
177
178 /**
100fef9d 179 * ParticipantDetails() method ( Checking the Participant Details )
6a488035 180 */
00be9182 181 public function testparticipantDetails() {
f78dbf0b 182 $participant = $this->callAPISuccess('Participant', 'create', ['contact_id' => $this->_contactId, 'event_id' => $this->_eventId]);
183 $params = ['name' => 'Anderson, Anthony', 'title' => 'Annual CiviCRM meet'];
6a488035 184
6ae19242 185 $participantDetails = CRM_Event_BAO_Participant::participantDetails($participant['id']);
6a488035
TO
186
187 $this->assertEquals(count($participantDetails), 3, 'Equating the array contains.');
188 $this->assertEquals($participantDetails['name'], $params['name'], 'Checking Name of Participant.');
189 $this->assertEquals($participantDetails['title'], $params['title'], 'Checking Event Title in which participant is enroled.');
190
6ae19242 191 $this->participantDelete($participant['id']);
93ac19cd 192 $this->contactDelete($this->_contactId);
6ae19242 193 $this->eventDelete($this->_eventId);
6a488035
TO
194 }
195
196 /**
100fef9d 197 * DeleteParticipant() method ( Delete a Participant )
6a488035 198 */
00be9182 199 public function testdeleteParticipant() {
f78dbf0b 200 $params = [
6a488035
TO
201 'send_receipt' => 1,
202 'is_test' => 0,
203 'is_pay_later' => 0,
204 'event_id' => $this->_eventId,
205 'register_date' => date('Y-m-d') . " 00:00:00",
206 'role_id' => 1,
207 'status_id' => 1,
208 'source' => 'Event_' . $this->_eventId,
209 'contact_id' => $this->_contactId,
f78dbf0b 210 ];
6a488035
TO
211
212 // New Participant Created
213 $participant = CRM_Event_BAO_Participant::add($params);
214
215 $this->assertDBNotNull('CRM_Event_BAO_Participant', $this->_contactId, 'id',
216 'contact_id', 'Check DB for Participant of the contact'
217 );
218
219 $this->assertDBCompareValue('CRM_Event_BAO_Participant', $participant->id, 'contact_id',
220 'id', $this->_contactId, 'Check DB for contact of the participant'
221 );
222
93ac19cd 223 CRM_Event_BAO_Participant::deleteParticipant($participant->id);
6a488035
TO
224 $this->assertDBNull('CRM_Event_BAO_Participant', $participant->id, 'contact_id', 'id', 'Check DB for deleted Participant.');
225
93ac19cd 226 $this->contactDelete($this->_contactId);
6ae19242 227 $this->eventDelete($this->_eventId);
6a488035
TO
228 }
229
230 /**
100fef9d 231 * CheckDuplicate() method ( Checking for Duplicate Participant returns array of participant id)
6a488035 232 */
00be9182 233 public function testcheckDuplicate() {
f78dbf0b 234 $duplicate = [];
6a488035
TO
235
236 //Creating 3 new participants
237 for ($i = 0; $i < 3; $i++) {
f78dbf0b 238 $partiId[] = $this->participantCreate(['contact_id' => $this->_contactId, 'event_id' => $this->_eventId]);
6a488035
TO
239 }
240
f78dbf0b 241 $params = ['event_id' => $this->_eventId, 'contact_id' => $this->_contactId];
6ae19242 242 CRM_Event_BAO_Participant::checkDuplicate($params, $duplicate);
6a488035
TO
243
244 $this->assertEquals(count($duplicate), 3, 'Equating the array contains with duplicate array.');
245
246 //Checking for the duplicate participant
247 foreach ($duplicate as $key => $value) {
248 $this->assertEquals($partiId[$key], $duplicate[$key], 'Equating the contactid which is in the database.');
249 }
250
251 //Deleting all participant
252 for ($i = 0; $i < 3; $i++) {
6ae19242 253 $partidel[] = $this->participantDelete($partiId[$i]);
6a488035
TO
254 }
255
93ac19cd 256 $this->contactDelete($this->_contactId);
6ae19242 257 $this->eventDelete($this->_eventId);
6a488035
TO
258 }
259
260 /**
100fef9d 261 * Create() method (create and updation of participant)
6a488035 262 */
00be9182 263 public function testCreate() {
f78dbf0b 264 $params = [
6a488035
TO
265 'send_receipt' => 1,
266 'is_test' => 0,
267 'is_pay_later' => 0,
268 'event_id' => $this->_eventId,
269 'register_date' => date('Y-m-d') . " 00:00:00",
270 'role_id' => 1,
271 'status_id' => 1,
272 'source' => 'Event_' . $this->_eventId,
273 'contact_id' => $this->_contactId,
274 'note' => 'Note added for Event_' . $this->_eventId,
f78dbf0b 275 ];
6a488035
TO
276
277 $participant = CRM_Event_BAO_Participant::create($params);
278 //Checking for Contact id in the participant table.
279 $pid = $this->assertDBNotNull('CRM_Event_DAO_Participant', $this->_contactId, 'id',
280 'contact_id', 'Check DB for Participant of the contact'
281 );
282
283 //Checking for Activity added in the table for relative participant.
284 $this->assertDBCompareValue('CRM_Activity_DAO_Activity', $this->_contactId, 'source_record_id',
285 'source_contact_id', $participant->id, 'Check DB for activity added for the participant'
286 );
287
f78dbf0b 288 $params = array_merge($params, [
6a488035 289 'id' => $participant->id,
92915c55
TO
290 'role_id' => 2,
291 'status_id' => 3,
292 'note' => 'Test Event in edit mode is running successfully ....',
f78dbf0b 293 ]);
6a488035
TO
294
295 $participant = CRM_Event_BAO_Participant::create($params);
296
297 //Checking Edited Value of role_id in the database.
298 $this->assertDBCompareValue('CRM_Event_DAO_Participant', $participant->id, 'role_id',
299 'id', 2, 'Check DB for updated role id of the participant'
300 );
301
302 //Checking Edited Value of status_id in the database.
303 $this->assertDBCompareValue('CRM_Event_DAO_Participant', $participant->id, 'status_id',
304 'id', 3, 'Check DB for updated status id of the participant'
305 );
306
307 //Checking for Activity added in the table for relative participant.
308 $this->assertDBCompareValue('CRM_Activity_DAO_Activity', $this->_contactId, 'source_record_id',
309 'source_contact_id', $participant->id, 'Check DB for activity added for the participant'
310 );
311
312 //Checking for Note added in the table for relative participant.
313 $session = CRM_Core_Session::singleton();
314 $id = $session->get('userID');
315 if (!$id) {
316 $id = $this->_contactId;
317 }
318
319 //Deleting the Participant created by create function in this function
23ead872 320 CRM_Event_BAO_Participant::deleteParticipant($participant->id);
6a488035
TO
321 $this->assertDBNull('CRM_Event_DAO_Participant', $this->_contactId, 'id',
322 'contact_id', 'Check DB for deleted participant. Should be NULL.'
323 );
324
93ac19cd 325 $this->contactDelete($this->_contactId);
6ae19242 326 $this->eventDelete($this->_eventId);
6a488035
TO
327 }
328
329 /**
100fef9d 330 * ExportableFields() method ( Exportable Fields for Participant)
6a488035 331 */
00be9182 332 public function testexportableFields() {
6a488035
TO
333 $exportableFields = CRM_Event_BAO_Participant::exportableFields();
334 $this->assertNotEquals(count($exportableFields), 0, 'Checking array not to be empty.');
335
93ac19cd 336 $this->contactDelete($this->_contactId);
6ae19242 337 $this->eventDelete($this->_eventId);
6a488035
TO
338 }
339
340 /**
100fef9d 341 * FixEventLevel() method (Setting ',' values), resolveDefaults(assinging value to array) method
6a488035 342 */
00be9182 343 public function testfixEventLevel() {
6a488035
TO
344
345 $paramsSet['title'] = 'Price Set';
346 $paramsSet['name'] = CRM_Utils_String::titleToVar('Price Set');
4b27d4ec 347 $paramsSet['is_active'] = FALSE;
6a488035
TO
348 $paramsSet['extends'] = 1;
349
9da8dc8c 350 $priceset = CRM_Price_BAO_PriceSet::create($paramsSet);
6a488035
TO
351
352 //Checking for priceset added in the table.
9da8dc8c 353 $this->assertDBCompareValue('CRM_Price_BAO_PriceSet', $priceset->id, 'title',
6a488035
TO
354 'id', $paramsSet['title'], 'Check DB for created priceset'
355 );
f78dbf0b 356 $paramsField = [
6a488035
TO
357 'label' => 'Price Field',
358 'name' => CRM_Utils_String::titleToVar('Price Field'),
359 'html_type' => 'Text',
360 'price' => 10,
f78dbf0b 361 'option_label' => ['1' => 'Price Field'],
362 'option_value' => ['1' => 10],
363 'option_name' => ['1' => 10],
364 'option_weight' => ['1' => 1],
6a488035
TO
365 'is_display_amounts' => 1,
366 'weight' => 1,
367 'options_per_line' => 1,
f78dbf0b 368 'is_active' => ['1' => 1],
6a488035
TO
369 'price_set_id' => $priceset->id,
370 'is_enter_qty' => 1,
f78dbf0b 371 ];
6a488035 372
f78dbf0b 373 $ids = [];
9da8dc8c 374 $pricefield = CRM_Price_BAO_PriceField::create($paramsField, $ids);
6a488035
TO
375
376 //Checking for priceset added in the table.
9da8dc8c 377 $this->assertDBCompareValue('CRM_Price_BAO_PriceField', $pricefield->id, 'label',
6a488035
TO
378 'id', $paramsField['label'], 'Check DB for created pricefield'
379 );
380
381 $eventId = $this->_eventId;
f78dbf0b 382 $participantParams = [
6a488035
TO
383 'send_receipt' => 1,
384 'is_test' => 0,
385 'is_pay_later' => 0,
386 'event_id' => $eventId,
387 'register_date' => date('Y-m-d') . " 00:00:00",
388 'role_id' => 1,
389 'status_id' => 1,
390 'source' => 'Event_' . $eventId,
391 'contact_id' => $this->_contactId,
392 'note' => 'Note added for Event_' . $eventId,
393 'fee_level' => '\ 1Price_Field - 55\ 1',
f78dbf0b 394 ];
6a488035
TO
395
396 $participant = CRM_Event_BAO_Participant::add($participantParams);
397
398 //Checking for participant added in the table.
399 $this->assertDBCompareValue('CRM_Event_BAO_Participant', $this->_contactId, 'id',
400 'contact_id', $participant->id, 'Check DB for created participant'
401 );
402
f78dbf0b 403 $values = [];
404 $ids = [];
405 $params = ['id' => $participant->id];
6a488035
TO
406
407 CRM_Event_BAO_Participant::getValues($params, $values, $ids);
408 $this->assertNotEquals(count($values), 0, 'Checking for empty array.');
409
410 CRM_Event_BAO_Participant::resolveDefaults($values[$participant->id]);
411
412 if ($values[$participant->id]['fee_level']) {
413 CRM_Event_BAO_Participant::fixEventLevel($values[$participant->id]['fee_level']);
414 }
415
93ac19cd 416 CRM_Price_BAO_PriceField::deleteField($pricefield->id);
9da8dc8c 417 $this->assertDBNull('CRM_Price_BAO_PriceField', $pricefield->id, 'name',
6a488035
TO
418 'id', 'Check DB for non-existence of Price Field.'
419 );
420
93ac19cd 421 CRM_Price_BAO_PriceSet::deleteSet($priceset->id);
9da8dc8c 422 $this->assertDBNull('CRM_Price_BAO_PriceSet', $priceset->id, 'title',
6a488035
TO
423 'id', 'Check DB for non-existence of Price Set.'
424 );
425
6ae19242 426 $this->participantDelete($participant->id);
93ac19cd 427 $this->contactDelete($this->_contactId);
6ae19242 428 $this->eventDelete($eventId);
6a488035 429 }
96025800 430
6a488035 431}