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