3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
28 require_once 'CiviTest/CiviUnitTestCase.php';
31 * Test class for Batch API - civicrm_participant_*
33 * @package CiviCRM_APIv3
35 require_once 'CRM/Utils/DeprecatedUtils.php';
36 require_once 'CiviTest/CiviUnitTestCase.php';
39 * Class api_v3_ParticipantTest
41 class api_v3_ParticipantTest
extends CiviUnitTestCase
{
43 protected $_apiversion;
45 protected $_contactID;
46 protected $_contactID2;
47 protected $_createdParticipants;
48 protected $_participantID;
50 protected $_individualId;
58 'name' => 'Participant Create',
59 'description' => 'Test all Participant Create API methods.',
60 'group' => 'CiviCRM API Tests',
65 $this->_apiversion
= 3;
67 $this->_entity
= 'participant';
68 $event = $this->eventCreate(NULL);
69 $this->_eventID
= $event['id'];
71 $this->_contactID
= $this->individualCreate();
73 $this->_createdParticipants
= array();
74 $this->_individualId
= $this->individualCreate();
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
,
85 // to ensure it matches later on
86 'register_date' => '2007-07-21 00:00:00',
87 'source' => 'Online Event Registration: API Testing',
92 $this->eventDelete($this->_eventID
);
93 $tablesToTruncate = array(
94 'civicrm_custom_group', 'civicrm_custom_field', 'civicrm_contact', 'civicrm_participant'
96 // true tells quickCleanup to drop any tables that might have been created in the test
97 $this->quickCleanup($tablesToTruncate, TRUE);
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
106 function testCreateWithCustom() {
107 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
109 $params = $this->_params
;
110 $params['custom_' . $ids['custom_field_id']] = "custom string";
112 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
);
114 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
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__
);
119 $this->customFieldDelete($ids['custom_field_id']);
120 $this->customGroupDelete($ids['custom_group_id']);
124 ///////////////// civicrm_participant_get methods
127 * check with wrong params type
129 function testGetWrongParamsType() {
130 $params = 'a string';
131 $result = $this->callAPIFailure('participant', 'get', $params);
135 * Test civicrm_participant_get with empty params
137 function testGetEmptyParams() {
138 $this->callAPISuccess('participant', 'get', array());
142 * check with participant_id
144 function testGetParticipantIdOnly() {
146 'participant_id' => $this->_participantID
,
150 'participant_register_date',
151 'participant_source',
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__
);
160 'id' => $this->_participantID
,
161 'return' => 'id,participant_register_date,event_id',
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');
171 * check with params id
173 function testGetParamsAsIdOnly() {
175 'id' => $this->_participantID
,
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']);
185 * check with params id
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
));
193 $description = "use nested get to get an event";
194 $subfile = "NestedEventGet";
196 'id' => $this->_participantID
,
197 'api.event.get' => 1,
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']);
206 * Check Participant Get respects return properties
208 function testGetWithReturnProperties() {
210 'contact_id' => $this->_contactID
, 'return.status_id' => 1,
211 'return.participant_status_id' => 1,
212 'options' => array('limit' => 1)
214 $result = $this->callAPISuccess('participant', 'get', $params);
215 $this->assertArrayHasKey('participant_status_id', $result['values'][$result['id']]);
219 * check with contact_id
221 function testGetContactIdOnly() {
223 'contact_id' => $this->_contactID
, );
224 $participant = $this->callAPISuccess('participant', 'get', $params);
226 $this->assertEquals($this->_participantID
, $participant['id'],
227 "In line " . __LINE__
229 $this->assertEquals($this->_eventID
, $participant['values'][$participant['id']]['event_id'],
230 "In line " . __LINE__
232 $this->assertEquals('2007-02-19 00:00:00', $participant['values'][$participant['id']]['participant_register_date'],
233 "In line " . __LINE__
235 $this->assertEquals('Wimbeldon', $participant['values'][$participant['id']]['participant_source'],
236 "In line " . __LINE__
238 $this->assertEquals($participant['id'], $participant['values'][$participant['id']]['id'],
239 "In line " . __LINE__
244 * check with event_id
247 function testGetMultiMatchReturnFirst() {
249 'event_id' => $this->_eventID
,
252 $participant = $this->callAPISuccess('participant', 'get', $params);
253 $this->assertNotNull($participant['id']);
257 * check with event_id
258 * in v3 this should return all participants
260 function testGetMultiMatchNoReturnFirst() {
262 'event_id' => $this->_eventID
,
264 $participant = $this->callAPISuccess('participant', 'get', $params);
265 $this->assertNotNull($participant['count'], 3);
268 ///////////////// civicrm_participant_get methods
271 * Test civicrm_participant_get with empty params
272 * In this case all the participant records are returned.
274 function testSearchEmptyParams() {
275 $result = $this->callAPISuccess('participant', 'get', array());
276 // expecting 3 participant records
277 $this->assertEquals($result['count'], 3);
281 * check with participant_id
283 function testSearchParticipantIdOnly() {
285 'participant_id' => $this->_participantID
,
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');
294 * check with contact_id
296 function testSearchContactIdOnly() {
297 // Should get 2 participant records for this contact.
299 'contact_id' => $this->_contactID2
,
301 $participant = $this->callAPISuccess('participant', 'get', $params);
303 $this->assertEquals($participant['count'], 2);
307 * check with event_id
309 function testSearchByEvent() {
310 // Should get >= 3 participant records for this event. Also testing that last_name and event_title are returned.
312 'event_id' => $this->_eventID
,
313 'return.last_name' => 1,
314 'return.event_title' => 1,
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.");
321 $this->assertEquals($participant['values'][$this->_participantID
]['last_name'], 'Anderson');
322 $this->assertEquals($participant['values'][$this->_participantID
]['event_title'], 'Annual CiviCRM meet');
326 * check with event_id
329 function testSearchByEventWithLimit() {
330 // Should 2 participant records since we're passing rowCount = 2.
332 'event_id' => $this->_eventID
,
335 $participant = $this->callAPISuccess('participant', 'get', $params);
337 $this->assertEquals($participant['count'], 2, 'in line ' . __LINE__
);
340 ///////////////// civicrm_participant_create methods
343 * Test civicrm_participant_create with empty params
345 function testCreateEmptyParams() {
347 $result = $this->callAPIFailure('participant', 'create', $params);
351 * check with event_id
353 function testCreateMissingContactID() {
355 'event_id' => $this->_eventID
,
357 $participant = $this->callAPIFailure('participant', 'create', $params);
361 * check with contact_id
364 function testCreateMissingEventID() {
366 'contact_id' => $this->_contactID
,
368 $participant = $this->callAPIFailure('participant', 'create', $params);
372 * check with contact_id & event_id
374 function testCreateEventIdOnly() {
376 'contact_id' => $this->_contactID
,
377 'event_id' => $this->_eventID
,
379 $participant = $this->callAPISuccess('participant', 'create', $params);
380 $this->getAndCheck($params, $participant['id'], 'participant');
384 * check with complete array
386 function testCreateAllParams() {
387 $params = $this->_params
;
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);
395 * Test to check if receive date is being changed per CRM-9763
397 function testCreateUpdateReceiveDate() {
398 $participant = $this->callAPISuccess('participant', 'create', $this->_params
);
400 'id' => $participant['id'],
403 $this->callAPISuccess('participant', 'create', $update);
404 $this->getAndCheck(array_merge($this->_params
, $update), $participant['id'], 'participant');
407 * Test to check if participant fee level is being changed per CRM-9781
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);
413 'id' => $participant['id'],
416 $update = $this->callAPISuccess('participant', 'create', $update);
418 $this->assertEquals($participant['values'][$participant['id']]['fee_level'],
419 $update['values'][$participant['id']]['fee_level']
422 $this->callAPISuccess('participant', 'delete', array('id' => $participant['id']));
425 * check with complete array
427 function testUpdate() {
428 $participantId = $this->participantCreate(array(
429 'contactID' => $this->_individualId
,
430 'eventID' => $this->_eventID
433 'id' => $participantId,
434 'contact_id' => $this->_individualId
,
435 'event_id' => $this->_eventID
,
438 'register_date' => '2006-01-21',
439 'source' => 'US Open',
441 $participant = $this->callAPISuccess('participant', 'create', $params);
442 $this->getAndCheck($params, $participant['id'], 'participant');
443 $result = $this->participantDelete($params['id']);
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)
451 function testUpdateCreateParticipantFeeLevelNoSeparator() {
453 $myParams = $this->_params +
array('participant_fee_level' => "fee");
454 $participant = $this->callAPISuccess('participant', 'create', $myParams);
455 $this->assertAPISuccess($participant);
457 'id' => $participant['id'],
460 $this->callAPISuccess('participant', 'create', $update);
461 $this->assertEquals($participant['values'][$participant['id']]['fee_level'],
462 $myParams['participant_fee_level']
464 $this->getAndCheck($update, $participant['id'], 'participant');
466 ///////////////// civicrm_participant_update methods
469 * Test civicrm_participant_update with wrong params type
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__
);
478 * check with empty array
480 function testUpdateEmptyParams() {
482 $participant = $this->callAPIFailure('participant', 'create', $params);
483 $this->assertEquals($participant['error_message'], 'Mandatory key(s) missing from params array: event_id, contact_id');
487 * check without event_id
489 function testUpdateWithoutEventId() {
490 $participantId = $this->participantCreate(array('contactID' => $this->_individualId
, 'eventID' => $this->_eventID
));
492 'contact_id' => $this->_individualId
,
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);
505 * check with Invalid participantId
507 function testUpdateWithWrongParticipantId() {
512 'register_date' => '2006-01-21',
513 'source' => 'US Open',
514 'event_level' => 'Donation', );
515 $participant = $this->callAPIFailure('Participant', 'update', $params);
519 * check with Invalid ContactId
521 function testUpdateWithWrongContactId() {
522 $participantId = $this->participantCreate(array(
523 'contactID' => $this->_individualId
,
524 'eventID' => $this->_eventID
,
525 ), $this->_apiversion
);
527 'id' => $participantId,
528 'contact_id' => 12345,
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);
538 ///////////////// civicrm_participant_delete methods
541 * Test civicrm_participant_delete with wrong params type
543 function testDeleteWrongParamsType() {
544 $params = 'a string';
545 $result = $this->callAPIFailure('participant', 'delete', $params);
549 * Test civicrm_participant_delete with empty params
551 function testDeleteEmptyParams() {
553 $result = $this->callAPIFailure('participant', 'delete', $params);
557 * check with participant_id
559 function testParticipantDelete() {
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);
568 * check without participant_id
570 * This should return an error because required param is missing..
572 function testParticipantDeleteMissingID() {
574 'event_id' => $this->_eventID
, );
575 $participant = $this->callAPIFailure('participant', 'delete', $params);
576 $this->assertNotNull($participant['error_message']);
579 * delete with a get - a 'criteria delete'
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__
);
592 * Test creation of a participant with an associated contribution
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";
599 'contact_type' => 'Individual',
600 'display_name' => 'dlobo',
601 'api.participant' => array(
602 'event_id' => $this->_eventID
,
605 'format.only_id' => 1,
607 'api.contribution.create' => array(
608 'financial_type_id' => 1,
609 'total_amount' => 100,
610 'format.only_id' => 1,
612 'api.participant_payment.create' => array(
613 'contribution_id' => '$value.api.contribution.create',
614 'participant_id' => '$value.api.participant',
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']));