Preliminary cleanup - use CRM_Core_Exception, fix comments
[civicrm-core.git] / tests / phpunit / CRM / Event / BAO / ParticipantTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 CRM_Event_BAO_ParticipantTest
30 *
31 * @group headless
32 */
33 class CRM_Event_BAO_ParticipantTest extends CiviUnitTestCase {
34
35 public function setUp() {
36 parent::setUp();
37 $this->_contactId = $this->individualCreate();
38 $event = $this->eventCreate();
39 $this->_eventId = $event['id'];
40 }
41
42 /**
43 * Add() method (add and edit modes of participant)
44 */
45 public function testAdd() {
46 $params = [
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,
56 ];
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
69 $params = array_merge($params, [
70 'id' => $participant->id,
71 'role_id' => 2,
72 'status_id' => 3,
73 ]);
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
85 $this->contactDelete($this->_contactId);
86 $this->eventDelete($this->_eventId);
87 }
88
89 /**
90 * GetValues() method (fetch value of participant)
91 */
92 public function testgetValuesWithValidParams() {
93 $participantId = $this->participantCreate(['contact_id' => $this->_contactId, 'event_id' => $this->_eventId]);
94 $params = ['id' => $participantId];
95 $values = $ids = [];
96
97 $fetchParticipant = CRM_Event_BAO_Participant::getValues($params, $values, $ids);
98 $compareValues = $fetchParticipant[$participantId];
99
100 $params = [
101 'send_receipt' => 1,
102 'is_test' => 0,
103 'is_pay_later' => 0,
104 'event_id' => $this->_eventId,
105 'register_date' => '2007-02-19 00:00:00',
106 'role_id' => 1,
107 'status_id' => 2,
108 'source' => 'Wimbeldon',
109 'contact_id' => $this->_contactId,
110 'id' => $participantId,
111 'campaign_id' => NULL,
112 'fee_level' => NULL,
113 'fee_amount' => NULL,
114 'registered_by_id' => NULL,
115 'discount_id' => NULL,
116 'fee_currency' => NULL,
117 'discount_amount' => NULL,
118 'cart_id' => NULL,
119 'must_wait' => NULL,
120 'transferred_to_contact_id' => NULL,
121 ];
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
129 $this->participantDelete($participantId);
130 $this->contactDelete($this->_contactId);
131 $this->eventDelete($this->_eventId);
132 }
133
134 /**
135 * GetValues() method (checking for behavior when params are empty )
136 */
137 public function testgetValuesWithoutValidParams() {
138 $params = $values = $ids = [];
139 $this->participantCreate(['contact_id' => $this->_contactId, 'event_id' => $this->_eventId]);
140 $fetchParticipant = CRM_Event_BAO_Participant::getValues($params, $values, $ids);
141 $this->assertNull($fetchParticipant);
142
143 $this->contactDelete($this->_contactId);
144 $this->eventDelete($this->_eventId);
145 }
146
147 /**
148 * EventFull() method (checking the event for full )
149 */
150 public function testEventFull() {
151 $eventParams = [
152 'max_participants' => 1,
153 'id' => $this->_eventId,
154 ];
155 CRM_Event_BAO_Event::add($eventParams);
156
157 $participantId = $this->participantCreate(['contact_id' => $this->_contactId, 'event_id' => $this->_eventId]);
158 $eventFull = CRM_Event_BAO_Participant::eventFull($this->_eventId);
159
160 $this->assertEquals($eventFull, 'Sorry! We are already full', 'Checking if Event is full.');
161
162 $this->participantDelete($participantId);
163 $this->contactDelete($this->_contactId);
164 $this->eventDelete($this->_eventId);
165 }
166
167 /**
168 * ImportableFields() method ( Checking the Event's Importable Fields )
169 */
170 public function testimportableFields() {
171 $importableFields = CRM_Event_BAO_Participant::importableFields();
172 $this->assertNotEquals(count($importableFields), 0, 'Checking array not to be empty.');
173
174 $this->contactDelete($this->_contactId);
175 $this->eventDelete($this->_eventId);
176 }
177
178 /**
179 * ParticipantDetails() method ( Checking the Participant Details )
180 */
181 public function testparticipantDetails() {
182 $participant = $this->callAPISuccess('Participant', 'create', ['contact_id' => $this->_contactId, 'event_id' => $this->_eventId]);
183 $params = ['name' => 'Anderson, Anthony', 'title' => 'Annual CiviCRM meet'];
184
185 $participantDetails = CRM_Event_BAO_Participant::participantDetails($participant['id']);
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
191 $this->participantDelete($participant['id']);
192 $this->contactDelete($this->_contactId);
193 $this->eventDelete($this->_eventId);
194 }
195
196 /**
197 * DeleteParticipant() method ( Delete a Participant )
198 */
199 public function testdeleteParticipant() {
200 $params = [
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,
210 ];
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
223 CRM_Event_BAO_Participant::deleteParticipant($participant->id);
224 $this->assertDBNull('CRM_Event_BAO_Participant', $participant->id, 'contact_id', 'id', 'Check DB for deleted Participant.');
225
226 $this->contactDelete($this->_contactId);
227 $this->eventDelete($this->_eventId);
228 }
229
230 /**
231 * CheckDuplicate() method ( Checking for Duplicate Participant returns array of participant id)
232 */
233 public function testcheckDuplicate() {
234 $duplicate = [];
235
236 //Creating 3 new participants
237 for ($i = 0; $i < 3; $i++) {
238 $partiId[] = $this->participantCreate(['contact_id' => $this->_contactId, 'event_id' => $this->_eventId]);
239 }
240
241 $params = ['event_id' => $this->_eventId, 'contact_id' => $this->_contactId];
242 CRM_Event_BAO_Participant::checkDuplicate($params, $duplicate);
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++) {
253 $partidel[] = $this->participantDelete($partiId[$i]);
254 }
255
256 $this->contactDelete($this->_contactId);
257 $this->eventDelete($this->_eventId);
258 }
259
260 /**
261 * Create() method (create and updation of participant)
262 */
263 public function testCreate() {
264 $params = [
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,
275 ];
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
288 $params = array_merge($params, [
289 'id' => $participant->id,
290 'role_id' => 2,
291 'status_id' => 3,
292 'note' => 'Test Event in edit mode is running successfully ....',
293 ]);
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
320 CRM_Event_BAO_Participant::deleteParticipant($participant->id);
321 $this->assertDBNull('CRM_Event_DAO_Participant', $this->_contactId, 'id',
322 'contact_id', 'Check DB for deleted participant. Should be NULL.'
323 );
324
325 $this->contactDelete($this->_contactId);
326 $this->eventDelete($this->_eventId);
327 }
328
329 /**
330 * ExportableFields() method ( Exportable Fields for Participant)
331 */
332 public function testexportableFields() {
333 $exportableFields = CRM_Event_BAO_Participant::exportableFields();
334 $this->assertNotEquals(count($exportableFields), 0, 'Checking array not to be empty.');
335
336 $this->contactDelete($this->_contactId);
337 $this->eventDelete($this->_eventId);
338 }
339
340 /**
341 * FixEventLevel() method (Setting ',' values), resolveDefaults(assinging value to array) method
342 */
343 public function testfixEventLevel() {
344
345 $paramsSet['title'] = 'Price Set';
346 $paramsSet['name'] = CRM_Utils_String::titleToVar('Price Set');
347 $paramsSet['is_active'] = FALSE;
348 $paramsSet['extends'] = 1;
349
350 $priceset = CRM_Price_BAO_PriceSet::create($paramsSet);
351
352 //Checking for priceset added in the table.
353 $this->assertDBCompareValue('CRM_Price_BAO_PriceSet', $priceset->id, 'title',
354 'id', $paramsSet['title'], 'Check DB for created priceset'
355 );
356 $paramsField = [
357 'label' => 'Price Field',
358 'name' => CRM_Utils_String::titleToVar('Price Field'),
359 'html_type' => 'Text',
360 'price' => 10,
361 'option_label' => ['1' => 'Price Field'],
362 'option_value' => ['1' => 10],
363 'option_name' => ['1' => 10],
364 'option_weight' => ['1' => 1],
365 'is_display_amounts' => 1,
366 'weight' => 1,
367 'options_per_line' => 1,
368 'is_active' => ['1' => 1],
369 'price_set_id' => $priceset->id,
370 'is_enter_qty' => 1,
371 ];
372
373 $ids = [];
374 $pricefield = CRM_Price_BAO_PriceField::create($paramsField, $ids);
375
376 //Checking for priceset added in the table.
377 $this->assertDBCompareValue('CRM_Price_BAO_PriceField', $pricefield->id, 'label',
378 'id', $paramsField['label'], 'Check DB for created pricefield'
379 );
380
381 $eventId = $this->_eventId;
382 $participantParams = [
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',
394 ];
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
403 $values = [];
404 $ids = [];
405 $params = ['id' => $participant->id];
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
416 CRM_Price_BAO_PriceField::deleteField($pricefield->id);
417 $this->assertDBNull('CRM_Price_BAO_PriceField', $pricefield->id, 'name',
418 'id', 'Check DB for non-existence of Price Field.'
419 );
420
421 CRM_Price_BAO_PriceSet::deleteSet($priceset->id);
422 $this->assertDBNull('CRM_Price_BAO_PriceSet', $priceset->id, 'title',
423 'id', 'Check DB for non-existence of Price Set.'
424 );
425
426 $this->participantDelete($participant->id);
427 $this->contactDelete($this->_contactId);
428 $this->eventDelete($eventId);
429 }
430
431 }