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