Merge pull request #2326 from eileenmcnaughton/CRM-14069
[civicrm-core.git] / tests / phpunit / api / v3 / ParticipantTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 require_once 'CiviTest/CiviUnitTestCase.php';
29
30 /**
31 * Test class for Batch API - civicrm_participant_*
32 *
33 * @package CiviCRM_APIv3
34 */
35 require_once 'CRM/Utils/DeprecatedUtils.php';
36 require_once 'CiviTest/CiviUnitTestCase.php';
37
38 /**
39 * Class api_v3_ParticipantTest
40 */
41 class api_v3_ParticipantTest extends CiviUnitTestCase {
42
43 protected $_apiversion;
44 protected $_entity;
45 protected $_contactID;
46 protected $_contactID2;
47 protected $_createdParticipants;
48 protected $_participantID;
49 protected $_eventID;
50 protected $_individualId;
51 protected $_params;
52
53 /**
54 * @return array
55 */
56 function get_info() {
57 return array(
58 'name' => 'Participant Create',
59 'description' => 'Test all Participant Create API methods.',
60 'group' => 'CiviCRM API Tests',
61 );
62 }
63
64 function setUp() {
65 $this->_apiversion = 3;
66 parent::setUp();
67 $this->_entity = 'participant';
68 $event = $this->eventCreate(NULL);
69 $this->_eventID = $event['id'];
70
71 $this->_contactID = $this->individualCreate();
72
73 $this->_createdParticipants = array();
74 $this->_individualId = $this->individualCreate();
75
76 $this->_participantID = $this->participantCreate(array('contact_id' => $this->_contactID, 'event_id' => $this->_eventID));
77 $this->_contactID2 = $this->individualCreate();
78 $this->_participantID2 = $this->participantCreate(array('contact_id' => $this->_contactID2, 'event_id' => $this->_eventID));
79 $this->_participantID3 = $this->participantCreate(array('contact_id' => $this->_contactID2, 'event_id' => $this->_eventID));
80 $this->_params = array(
81 'contact_id' => $this->_contactID,
82 'event_id' => $this->_eventID,
83 'status_id' => 1,
84 'role_id' => 1,
85 // to ensure it matches later on
86 'register_date' => '2007-07-21 00:00:00',
87 'source' => 'Online Event Registration: API Testing',
88 );
89 }
90
91 function tearDown() {
92 $this->eventDelete($this->_eventID);
93 $tablesToTruncate = array(
94 'civicrm_custom_group', 'civicrm_custom_field', 'civicrm_contact', 'civicrm_participant'
95 );
96 // true tells quickCleanup to drop any tables that might have been created in the test
97 $this->quickCleanup($tablesToTruncate, TRUE);
98 }
99
100 /**
101 * check with complete array + custom field
102 * Note that the test is written on purpose without any
103 * variables specific to participant so it can be replicated into other entities
104 * and / or moved to the automated test suite
105 */
106 function testCreateWithCustom() {
107 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
108
109 $params = $this->_params;
110 $params['custom_' . $ids['custom_field_id']] = "custom string";
111
112 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
113
114 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
115
116 $check = $this->callAPISuccess($this->_entity, 'get', array('id' => $result['id']));
117 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
118
119 $this->customFieldDelete($ids['custom_field_id']);
120 $this->customGroupDelete($ids['custom_group_id']);
121 }
122
123
124 ///////////////// civicrm_participant_get methods
125
126 /**
127 * check with wrong params type
128 */
129 function testGetWrongParamsType() {
130 $params = 'a string';
131 $result = $this->callAPIFailure('participant', 'get', $params);
132 }
133
134 /**
135 * Test civicrm_participant_get with empty params
136 */
137 function testGetEmptyParams() {
138 $this->callAPISuccess('participant', 'get', array());
139 }
140
141 /**
142 * check with participant_id
143 */
144 function testGetParticipantIdOnly() {
145 $params = array(
146 'participant_id' => $this->_participantID,
147 'return' => array(
148 'participant_id',
149 'event_id',
150 'participant_register_date',
151 'participant_source',
152 )
153 );
154 $result = $this->callAPISuccess('participant', 'get', $params);
155 $this->assertAPISuccess($result, " in line " . __LINE__);
156 $this->assertEquals($result['values'][$this->_participantID]['event_id'], $this->_eventID, "in line " . __LINE__);
157 $this->assertEquals($result['values'][$this->_participantID]['participant_register_date'], '2007-02-19 00:00:00', "in line " . __LINE__);
158 $this->assertEquals($result['values'][$this->_participantID]['participant_source'], 'Wimbeldon', "in line " . __LINE__);
159 $params = array(
160 'id' => $this->_participantID,
161 'return' => 'id,participant_register_date,event_id',
162
163 );
164 $result = $this->callAPISuccess('participant', 'get', $params);
165 $this->assertEquals($result['values'][$this->_participantID]['event_id'], $this->_eventID);
166 $this->assertEquals($result['values'][$this->_participantID]['participant_register_date'], '2007-02-19 00:00:00');
167
168 }
169
170 /**
171 * check with params id
172 */
173 function testGetParamsAsIdOnly() {
174 $params = array(
175 'id' => $this->_participantID,
176 );
177 $result = $this->callAPIAndDocument('participant', 'get', $params, __FUNCTION__, __FILE__);
178 $this->assertEquals($result['values'][$this->_participantID]['event_id'], $this->_eventID);
179 $this->assertEquals($result['values'][$this->_participantID]['participant_register_date'], '2007-02-19 00:00:00');
180 $this->assertEquals($result['values'][$this->_participantID]['participant_source'], 'Wimbeldon');
181 $this->assertEquals($result['id'], $result['values'][$this->_participantID]['id']);
182 }
183
184 /**
185 * check with params id
186 */
187 function testGetNestedEventGet() {
188 //create a second event & add participant to it.
189 $event = $this->eventCreate(NULL);
190 $this->callAPISuccess('participant', 'create', array('event_id' => $event['id'], 'contact_id' => $this->_contactID));
191
192
193 $description = "use nested get to get an event";
194 $subfile = "NestedEventGet";
195 $params = array(
196 'id' => $this->_participantID,
197 'api.event.get' => 1,
198 );
199 $result = $this->callAPIAndDocument('participant', 'get', $params, __FUNCTION__, __FILE__, $description, $subfile);
200 $this->assertEquals($result['values'][$this->_participantID]['event_id'], $this->_eventID);
201 $this->assertEquals($result['values'][$this->_participantID]['participant_register_date'], '2007-02-19 00:00:00');
202 $this->assertEquals($result['values'][$this->_participantID]['participant_source'], 'Wimbeldon');
203 $this->assertEquals($this->_eventID, $result['values'][$this->_participantID]['api.event.get']['id']);
204 }
205 /*
206 * Check Participant Get respects return properties
207 */
208 function testGetWithReturnProperties() {
209 $params = array(
210 'contact_id' => $this->_contactID, 'return.status_id' => 1,
211 'return.participant_status_id' => 1,
212 'options' => array('limit' => 1)
213 );
214 $result = $this->callAPISuccess('participant', 'get', $params);
215 $this->assertArrayHasKey('participant_status_id', $result['values'][$result['id']]);
216 }
217
218 /**
219 * check with contact_id
220 */
221 function testGetContactIdOnly() {
222 $params = array(
223 'contact_id' => $this->_contactID, );
224 $participant = $this->callAPISuccess('participant', 'get', $params);
225
226 $this->assertEquals($this->_participantID, $participant['id'],
227 "In line " . __LINE__
228 );
229 $this->assertEquals($this->_eventID, $participant['values'][$participant['id']]['event_id'],
230 "In line " . __LINE__
231 );
232 $this->assertEquals('2007-02-19 00:00:00', $participant['values'][$participant['id']]['participant_register_date'],
233 "In line " . __LINE__
234 );
235 $this->assertEquals('Wimbeldon', $participant['values'][$participant['id']]['participant_source'],
236 "In line " . __LINE__
237 );
238 $this->assertEquals($participant['id'], $participant['values'][$participant['id']]['id'],
239 "In line " . __LINE__
240 );
241 }
242
243 /**
244 * check with event_id
245 * fetch first record
246 */
247 function testGetMultiMatchReturnFirst() {
248 $params = array(
249 'event_id' => $this->_eventID,
250 'rowCount' => 1, );
251
252 $participant = $this->callAPISuccess('participant', 'get', $params);
253 $this->assertNotNull($participant['id']);
254 }
255
256 /**
257 * check with event_id
258 * in v3 this should return all participants
259 */
260 function testGetMultiMatchNoReturnFirst() {
261 $params = array(
262 'event_id' => $this->_eventID,
263 );
264 $participant = $this->callAPISuccess('participant', 'get', $params);
265 $this->assertNotNull($participant['count'], 3);
266 }
267
268 ///////////////// civicrm_participant_get methods
269
270 /**
271 * Test civicrm_participant_get with empty params
272 * In this case all the participant records are returned.
273 */
274 function testSearchEmptyParams() {
275 $result = $this->callAPISuccess('participant', 'get', array());
276 // expecting 3 participant records
277 $this->assertEquals($result['count'], 3);
278 }
279
280 /**
281 * check with participant_id
282 */
283 function testSearchParticipantIdOnly() {
284 $params = array(
285 'participant_id' => $this->_participantID,
286 );
287 $participant = $this->callAPISuccess('participant', 'get', $params);
288 $this->assertEquals($participant['values'][$this->_participantID]['event_id'], $this->_eventID);
289 $this->assertEquals($participant['values'][$this->_participantID]['participant_register_date'], '2007-02-19 00:00:00');
290 $this->assertEquals($participant['values'][$this->_participantID]['participant_source'], 'Wimbeldon');
291 }
292
293 /**
294 * check with contact_id
295 */
296 function testSearchContactIdOnly() {
297 // Should get 2 participant records for this contact.
298 $params = array(
299 'contact_id' => $this->_contactID2,
300 );
301 $participant = $this->callAPISuccess('participant', 'get', $params);
302
303 $this->assertEquals($participant['count'], 2);
304 }
305
306 /**
307 * check with event_id
308 */
309 function testSearchByEvent() {
310 // Should get >= 3 participant records for this event. Also testing that last_name and event_title are returned.
311 $params = array(
312 'event_id' => $this->_eventID,
313 'return.last_name' => 1,
314 'return.event_title' => 1,
315 );
316 $participant = $this->callAPISuccess('participant', 'get', $params);
317 if ($participant['count'] < 3) {
318 $this->fail("Event search returned less than expected miniumum of 3 records.");
319 }
320
321 $this->assertEquals($participant['values'][$this->_participantID]['last_name'], 'Anderson');
322 $this->assertEquals($participant['values'][$this->_participantID]['event_title'], 'Annual CiviCRM meet');
323 }
324
325 /**
326 * check with event_id
327 * fetch with limit
328 */
329 function testSearchByEventWithLimit() {
330 // Should 2 participant records since we're passing rowCount = 2.
331 $params = array(
332 'event_id' => $this->_eventID,
333 'rowCount' => 2,
334 );
335 $participant = $this->callAPISuccess('participant', 'get', $params);
336
337 $this->assertEquals($participant['count'], 2, 'in line ' . __LINE__);
338 }
339
340 ///////////////// civicrm_participant_create methods
341
342 /**
343 * Test civicrm_participant_create with empty params
344 */
345 function testCreateEmptyParams() {
346 $params = array();
347 $result = $this->callAPIFailure('participant', 'create', $params);
348 }
349
350 /**
351 * check with event_id
352 */
353 function testCreateMissingContactID() {
354 $params = array(
355 'event_id' => $this->_eventID,
356 );
357 $participant = $this->callAPIFailure('participant', 'create', $params);
358 }
359
360 /**
361 * check with contact_id
362 * without event_id
363 */
364 function testCreateMissingEventID() {
365 $params = array(
366 'contact_id' => $this->_contactID,
367 );
368 $participant = $this->callAPIFailure('participant', 'create', $params);
369 }
370
371 /**
372 * check with contact_id & event_id
373 */
374 function testCreateEventIdOnly() {
375 $params = array(
376 'contact_id' => $this->_contactID,
377 'event_id' => $this->_eventID,
378 );
379 $participant = $this->callAPISuccess('participant', 'create', $params);
380 $this->getAndCheck($params, $participant['id'], 'participant');
381 }
382
383 /**
384 * check with complete array
385 */
386 function testCreateAllParams() {
387 $params = $this->_params;
388
389 $participant = $this->callAPISuccess('participant', 'create', $params);
390 $this->_participantID = $participant['id'];
391 // assertDBState compares expected values in $match to actual values in the DB
392 $this->assertDBState('CRM_Event_DAO_Participant', $participant['id'], $params);
393 }
394 /*
395 * Test to check if receive date is being changed per CRM-9763
396 */
397 function testCreateUpdateReceiveDate() {
398 $participant = $this->callAPISuccess('participant', 'create', $this->_params);
399 $update = array(
400 'id' => $participant['id'],
401 'status_id' => 2,
402 );
403 $this->callAPISuccess('participant', 'create', $update);
404 $this->getAndCheck(array_merge($this->_params, $update), $participant['id'], 'participant');
405 }
406 /*
407 * Test to check if participant fee level is being changed per CRM-9781
408 */
409 function testCreateUpdateParticipantFeeLevel() {
410 $myParams = $this->_params + array('participant_fee_level' => CRM_Core_DAO::VALUE_SEPARATOR . "fee" . CRM_Core_DAO::VALUE_SEPARATOR);
411 $participant = $this->callAPISuccess('participant', 'create', $myParams);
412 $update = array(
413 'id' => $participant['id'],
414 'status_id' => 2,
415 );
416 $update = $this->callAPISuccess('participant', 'create', $update);
417
418 $this->assertEquals($participant['values'][$participant['id']]['fee_level'],
419 $update['values'][$participant['id']]['fee_level']
420 );
421
422 $this->callAPISuccess('participant', 'delete', array('id' => $participant['id']));
423 }
424 /**
425 * check with complete array
426 */
427 function testUpdate() {
428 $participantId = $this->participantCreate(array(
429 'contactID' => $this->_individualId,
430 'eventID' => $this->_eventID
431 ));
432 $params = array(
433 'id' => $participantId,
434 'contact_id' => $this->_individualId,
435 'event_id' => $this->_eventID,
436 'status_id' => 3,
437 'role_id' => 3,
438 'register_date' => '2006-01-21',
439 'source' => 'US Open',
440 );
441 $participant = $this->callAPISuccess('participant', 'create', $params);
442 $this->getAndCheck($params, $participant['id'], 'participant');
443 $result = $this->participantDelete($params['id']);
444 }
445
446 /**
447 * Test to check if participant fee level is being changed per CRM-9781
448 * Try again without a custom separater to check that one isn't added
449 * (get & check won't accept an array)
450 */
451 function testUpdateCreateParticipantFeeLevelNoSeparator() {
452
453 $myParams = $this->_params + array('participant_fee_level' => "fee");
454 $participant = $this->callAPISuccess('participant', 'create', $myParams);
455 $this->assertAPISuccess($participant);
456 $update = array(
457 'id' => $participant['id'],
458 'status_id' => 2,
459 );
460 $this->callAPISuccess('participant', 'create', $update);
461 $this->assertEquals($participant['values'][$participant['id']]['fee_level'],
462 $myParams['participant_fee_level']
463 );
464 $this->getAndCheck($update, $participant['id'], 'participant');
465 }
466 ///////////////// civicrm_participant_update methods
467
468 /**
469 * Test civicrm_participant_update with wrong params type
470 */
471 function testUpdateWrongParamsType() {
472 $params = 'a string';
473 $result = $this->callAPIFailure('participant', 'create', $params);
474 $this->assertEquals('Input variable `params` is not an array', $result['error_message'], 'In line ' . __LINE__);
475 }
476
477 /**
478 * check with empty array
479 */
480 function testUpdateEmptyParams() {
481 $params = array();
482 $participant = $this->callAPIFailure('participant', 'create', $params);
483 $this->assertEquals($participant['error_message'], 'Mandatory key(s) missing from params array: event_id, contact_id');
484 }
485
486 /**
487 * check without event_id
488 */
489 function testUpdateWithoutEventId() {
490 $participantId = $this->participantCreate(array('contactID' => $this->_individualId, 'eventID' => $this->_eventID));
491 $params = array(
492 'contact_id' => $this->_individualId,
493 'status_id' => 3,
494 'role_id' => 3,
495 'register_date' => '2006-01-21',
496 'source' => 'US Open',
497 'event_level' => 'Donation', );
498 $participant = $this->callAPIFailure('participant', 'create', $params);
499 $this->assertEquals($participant['error_message'], 'Mandatory key(s) missing from params array: event_id');
500 // Cleanup created participant records.
501 $result = $this->participantDelete($participantId);
502 }
503
504 /**
505 * check with Invalid participantId
506 */
507 function testUpdateWithWrongParticipantId() {
508 $params = array(
509 'id' => 1234,
510 'status_id' => 3,
511 'role_id' => 3,
512 'register_date' => '2006-01-21',
513 'source' => 'US Open',
514 'event_level' => 'Donation', );
515 $participant = $this->callAPIFailure('Participant', 'update', $params);
516 }
517
518 /**
519 * check with Invalid ContactId
520 */
521 function testUpdateWithWrongContactId() {
522 $participantId = $this->participantCreate(array(
523 'contactID' => $this->_individualId,
524 'eventID' => $this->_eventID,
525 ), $this->_apiversion);
526 $params = array(
527 'id' => $participantId,
528 'contact_id' => 12345,
529 'status_id' => 3,
530 'role_id' => 3,
531 'register_date' => '2006-01-21',
532 'source' => 'US Open',
533 'event_level' => 'Donation', );
534 $participant = $this->callAPIFailure('participant', 'create', $params);
535 $result = $this->participantDelete($participantId);
536 }
537
538 ///////////////// civicrm_participant_delete methods
539
540 /**
541 * Test civicrm_participant_delete with wrong params type
542 */
543 function testDeleteWrongParamsType() {
544 $params = 'a string';
545 $result = $this->callAPIFailure('participant', 'delete', $params);
546 }
547
548 /**
549 * Test civicrm_participant_delete with empty params
550 */
551 function testDeleteEmptyParams() {
552 $params = array();
553 $result = $this->callAPIFailure('participant', 'delete', $params);
554 }
555
556 /**
557 * check with participant_id
558 */
559 function testParticipantDelete() {
560 $params = array(
561 'id' => $this->_participantID, );
562 $participant = $this->callAPISuccess('participant', 'delete', $params);
563 $this->assertAPISuccess($participant);
564 $this->assertDBState('CRM_Event_DAO_Participant', $this->_participantID, NULL, TRUE);
565 }
566
567 /**
568 * check without participant_id
569 * and with event_id
570 * This should return an error because required param is missing..
571 */
572 function testParticipantDeleteMissingID() {
573 $params = array(
574 'event_id' => $this->_eventID, );
575 $participant = $this->callAPIFailure('participant', 'delete', $params);
576 $this->assertNotNull($participant['error_message']);
577 }
578 /*
579 * delete with a get - a 'criteria delete'
580 */
581 function testNestedDelete() {
582 $description = "Criteria delete by nesting a GET & a DELETE";
583 $subfile = "NestedDelete";
584 $participants = $this->callAPISuccess('Participant', 'Get', array());
585 $this->assertEquals($participants['count'], 3);
586 $params = array('contact_id' => $this->_contactID2, 'api.participant.delete' => 1);
587 $participants = $this->callAPIAndDocument('Participant', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile, 'Get');
588 $check = $this->callAPISuccess('participant', 'getcount', array());
589 $this->assertEquals(1, $check,"only one participant should be left. line " . __LINE__);
590 }
591 /*
592 * Test creation of a participant with an associated contribution
593 */
594 function testCreateParticipantWithPayment() {
595 $description = "single function to create contact w partipation & contribution. Note that in the
596 case of 'contribution' the 'create' is implied (api.contribution.create)";
597 $subfile = "CreateParticipantPayment";
598 $params = array(
599 'contact_type' => 'Individual',
600 'display_name' => 'dlobo',
601 'api.participant' => array(
602 'event_id' => $this->_eventID,
603 'status_id' => 1,
604 'role_id' => 1,
605 'format.only_id' => 1,
606 ),
607 'api.contribution.create' => array(
608 'financial_type_id' => 1,
609 'total_amount' => 100,
610 'format.only_id' => 1,
611 ),
612 'api.participant_payment.create' => array(
613 'contribution_id' => '$value.api.contribution.create',
614 'participant_id' => '$value.api.participant',
615 ),
616 );
617
618 $result = $this->callAPIAndDocument('contact', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
619 $this->assertEquals(1, $result['values'][$result['id']]['api.participant_payment.create']['count']);
620 $this->callAPISuccess('contact', 'delete', array('id' => $result['id']));
621 }
622 }
623