Merge pull request #15921 from civicrm/5.20
[civicrm-core.git] / tests / phpunit / CRM / Event / BAO / ParticipantTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Class CRM_Event_BAO_ParticipantTest
14 *
15 * @group headless
16 */
17 class CRM_Event_BAO_ParticipantTest extends CiviUnitTestCase {
18
19 public function setUp() {
20 parent::setUp();
21 $this->_contactId = $this->individualCreate();
22 $event = $this->eventCreate();
23 $this->_eventId = $event['id'];
24 }
25
26 /**
27 * Add() method (add and edit modes of participant)
28 */
29 public function testAdd() {
30 $params = [
31 'send_receipt' => 1,
32 'is_test' => 0,
33 'is_pay_later' => 0,
34 'event_id' => $this->_eventId,
35 'register_date' => date('Y-m-d') . " 00:00:00",
36 'role_id' => 1,
37 'status_id' => 1,
38 'source' => 'Event_' . $this->_eventId,
39 'contact_id' => $this->_contactId,
40 ];
41
42 // New Participant Created
43 $participant = CRM_Event_BAO_Participant::add($params);
44
45 $this->assertDBNotNull('CRM_Event_BAO_Participant', $this->_contactId, 'id',
46 'contact_id', 'Check DB for Participant of the contact'
47 );
48
49 $this->assertDBCompareValue('CRM_Event_BAO_Participant', $participant->id, 'contact_id',
50 'id', $this->_contactId, 'Check DB for contact of the participant'
51 );
52
53 $params = array_merge($params, [
54 'id' => $participant->id,
55 'role_id' => 2,
56 'status_id' => 3,
57 ]);
58
59 // Participant Edited
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'
63 );
64
65 $this->assertDBCompareValue('CRM_Event_BAO_Participant', $updatedParticipant->id, 'status_id',
66 'id', 3, 'Check DB for updated status id of the participant'
67 );
68
69 $this->contactDelete($this->_contactId);
70 $this->eventDelete($this->_eventId);
71 }
72
73 /**
74 * GetValues() method (fetch value of participant)
75 */
76 public function testgetValuesWithValidParams() {
77 $participantId = $this->participantCreate(['contact_id' => $this->_contactId, 'event_id' => $this->_eventId]);
78 $params = ['id' => $participantId];
79 $values = $ids = [];
80
81 $fetchParticipant = CRM_Event_BAO_Participant::getValues($params, $values, $ids);
82 $compareValues = $fetchParticipant[$participantId];
83
84 $params = [
85 'send_receipt' => 1,
86 'is_test' => 0,
87 'is_pay_later' => 0,
88 'event_id' => $this->_eventId,
89 'register_date' => '2007-02-19 00:00:00',
90 'role_id' => 1,
91 'status_id' => 2,
92 'source' => 'Wimbeldon',
93 'contact_id' => $this->_contactId,
94 'id' => $participantId,
95 'campaign_id' => NULL,
96 'fee_level' => NULL,
97 'fee_amount' => NULL,
98 'registered_by_id' => NULL,
99 'discount_id' => NULL,
100 'fee_currency' => NULL,
101 'discount_amount' => NULL,
102 'cart_id' => NULL,
103 'must_wait' => NULL,
104 'transferred_to_contact_id' => NULL,
105 ];
106
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');
110 }
111 }
112
113 $this->participantDelete($participantId);
114 $this->contactDelete($this->_contactId);
115 $this->eventDelete($this->_eventId);
116 }
117
118 /**
119 * GetValues() method (checking for behavior when params are empty )
120 */
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);
126
127 $this->contactDelete($this->_contactId);
128 $this->eventDelete($this->_eventId);
129 }
130
131 /**
132 * EventFull() method (checking the event for full )
133 */
134 public function testEventFull() {
135 $eventParams = [
136 'max_participants' => 1,
137 'id' => $this->_eventId,
138 ];
139 CRM_Event_BAO_Event::add($eventParams);
140
141 $participantId = $this->participantCreate(['contact_id' => $this->_contactId, 'event_id' => $this->_eventId]);
142 $eventFull = CRM_Event_BAO_Participant::eventFull($this->_eventId);
143
144 $this->assertEquals($eventFull, 'Sorry! We are already full', 'Checking if Event is full.');
145
146 $this->participantDelete($participantId);
147 $this->contactDelete($this->_contactId);
148 $this->eventDelete($this->_eventId);
149 }
150
151 /**
152 * ImportableFields() method ( Checking the Event's Importable Fields )
153 */
154 public function testimportableFields() {
155 $importableFields = CRM_Event_BAO_Participant::importableFields();
156 $this->assertNotEquals(count($importableFields), 0, 'Checking array not to be empty.');
157
158 $this->contactDelete($this->_contactId);
159 $this->eventDelete($this->_eventId);
160 }
161
162 /**
163 * ParticipantDetails() method ( Checking the Participant Details )
164 */
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'];
168
169 $participantDetails = CRM_Event_BAO_Participant::participantDetails($participant['id']);
170
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.');
174
175 $this->participantDelete($participant['id']);
176 $this->contactDelete($this->_contactId);
177 $this->eventDelete($this->_eventId);
178 }
179
180 /**
181 * DeleteParticipant() method ( Delete a Participant )
182 */
183 public function testdeleteParticipant() {
184 $params = [
185 'send_receipt' => 1,
186 'is_test' => 0,
187 'is_pay_later' => 0,
188 'event_id' => $this->_eventId,
189 'register_date' => date('Y-m-d') . " 00:00:00",
190 'role_id' => 1,
191 'status_id' => 1,
192 'source' => 'Event_' . $this->_eventId,
193 'contact_id' => $this->_contactId,
194 ];
195
196 // New Participant Created
197 $participant = CRM_Event_BAO_Participant::add($params);
198
199 $this->assertDBNotNull('CRM_Event_BAO_Participant', $this->_contactId, 'id',
200 'contact_id', 'Check DB for Participant of the contact'
201 );
202
203 $this->assertDBCompareValue('CRM_Event_BAO_Participant', $participant->id, 'contact_id',
204 'id', $this->_contactId, 'Check DB for contact of the participant'
205 );
206
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.');
209
210 $this->contactDelete($this->_contactId);
211 $this->eventDelete($this->_eventId);
212 }
213
214 /**
215 * CheckDuplicate() method ( Checking for Duplicate Participant returns array of participant id)
216 */
217 public function testcheckDuplicate() {
218 $duplicate = [];
219
220 //Creating 3 new participants
221 for ($i = 0; $i < 3; $i++) {
222 $partiId[] = $this->participantCreate(['contact_id' => $this->_contactId, 'event_id' => $this->_eventId]);
223 }
224
225 $params = ['event_id' => $this->_eventId, 'contact_id' => $this->_contactId];
226 CRM_Event_BAO_Participant::checkDuplicate($params, $duplicate);
227
228 $this->assertEquals(count($duplicate), 3, 'Equating the array contains with duplicate array.');
229
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.');
233 }
234
235 //Deleting all participant
236 for ($i = 0; $i < 3; $i++) {
237 $partidel[] = $this->participantDelete($partiId[$i]);
238 }
239
240 $this->contactDelete($this->_contactId);
241 $this->eventDelete($this->_eventId);
242 }
243
244 /**
245 * Create() method (create and updation of participant)
246 */
247 public function testCreate() {
248 $params = [
249 'send_receipt' => 1,
250 'is_test' => 0,
251 'is_pay_later' => 0,
252 'event_id' => $this->_eventId,
253 'register_date' => date('Y-m-d') . " 00:00:00",
254 'role_id' => 1,
255 'status_id' => 1,
256 'source' => 'Event_' . $this->_eventId,
257 'contact_id' => $this->_contactId,
258 'note' => 'Note added for Event_' . $this->_eventId,
259 ];
260
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'
265 );
266
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'
270 );
271
272 $params = array_merge($params, [
273 'id' => $participant->id,
274 'role_id' => 2,
275 'status_id' => 3,
276 'note' => 'Test Event in edit mode is running successfully ....',
277 ]);
278
279 $participant = CRM_Event_BAO_Participant::create($params);
280
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'
284 );
285
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'
289 );
290
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'
294 );
295
296 //Checking for Note added in the table for relative participant.
297 $session = CRM_Core_Session::singleton();
298 $id = $session->get('userID');
299 if (!$id) {
300 $id = $this->_contactId;
301 }
302
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.'
307 );
308
309 $this->contactDelete($this->_contactId);
310 $this->eventDelete($this->_eventId);
311 }
312
313 /**
314 * ExportableFields() method ( Exportable Fields for Participant)
315 */
316 public function testexportableFields() {
317 $exportableFields = CRM_Event_BAO_Participant::exportableFields();
318 $this->assertNotEquals(count($exportableFields), 0, 'Checking array not to be empty.');
319
320 $this->contactDelete($this->_contactId);
321 $this->eventDelete($this->_eventId);
322 }
323
324 /**
325 * FixEventLevel() method (Setting ',' values), resolveDefaults(assinging value to array) method
326 */
327 public function testfixEventLevel() {
328
329 $paramsSet['title'] = 'Price Set';
330 $paramsSet['name'] = CRM_Utils_String::titleToVar('Price Set');
331 $paramsSet['is_active'] = FALSE;
332 $paramsSet['extends'] = 1;
333
334 $priceset = CRM_Price_BAO_PriceSet::create($paramsSet);
335
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'
339 );
340 $paramsField = [
341 'label' => 'Price Field',
342 'name' => CRM_Utils_String::titleToVar('Price Field'),
343 'html_type' => 'Text',
344 'price' => 10,
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,
350 'weight' => 1,
351 'options_per_line' => 1,
352 'is_active' => ['1' => 1],
353 'price_set_id' => $priceset->id,
354 'is_enter_qty' => 1,
355 ];
356
357 $ids = [];
358 $pricefield = CRM_Price_BAO_PriceField::create($paramsField, $ids);
359
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'
363 );
364
365 $eventId = $this->_eventId;
366 $participantParams = [
367 'send_receipt' => 1,
368 'is_test' => 0,
369 'is_pay_later' => 0,
370 'event_id' => $eventId,
371 'register_date' => date('Y-m-d') . " 00:00:00",
372 'role_id' => 1,
373 'status_id' => 1,
374 'source' => 'Event_' . $eventId,
375 'contact_id' => $this->_contactId,
376 'note' => 'Note added for Event_' . $eventId,
377 'fee_level' => '\ 1Price_Field - 55\ 1',
378 ];
379
380 $participant = CRM_Event_BAO_Participant::add($participantParams);
381
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'
385 );
386
387 $values = [];
388 $ids = [];
389 $params = ['id' => $participant->id];
390
391 CRM_Event_BAO_Participant::getValues($params, $values, $ids);
392 $this->assertNotEquals(count($values), 0, 'Checking for empty array.');
393
394 CRM_Event_BAO_Participant::resolveDefaults($values[$participant->id]);
395
396 if ($values[$participant->id]['fee_level']) {
397 CRM_Event_BAO_Participant::fixEventLevel($values[$participant->id]['fee_level']);
398 }
399
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.'
403 );
404
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.'
408 );
409
410 $this->participantDelete($participant->id);
411 $this->contactDelete($this->_contactId);
412 $this->eventDelete($eventId);
413 }
414
415 }