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