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