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';
37 class api_v3_ParticipantTest
extends CiviUnitTestCase
{
39 protected $_apiversion;
41 protected $_contactID;
42 protected $_contactID2;
43 protected $_createdParticipants;
44 protected $_participantID;
46 protected $_individualId;
51 'name' => 'Participant Create',
52 'description' => 'Test all Participant Create API methods.',
53 'group' => 'CiviCRM API Tests',
58 $this->_apiversion
= 3;
60 $this->_entity
= 'participant';
61 $event = $this->eventCreate(NULL);
62 $this->_eventID
= $event['id'];
64 $this->_contactID
= $this->individualCreate();
66 $this->_createdParticipants
= array();
67 $this->_individualId
= $this->individualCreate();
69 $this->_participantID
= $this->participantCreate(array('contact_id' => $this->_contactID
, 'event_id' => $this->_eventID
));
70 $this->_contactID2
= $this->individualCreate();
71 $this->_participantID2
= $this->participantCreate(array('contact_id' => $this->_contactID2
, 'event_id' => $this->_eventID
));
72 $this->_participantID3
= $this->participantCreate(array('contact_id' => $this->_contactID2
, 'event_id' => $this->_eventID
));
73 $this->_params
= array(
74 'contact_id' => $this->_contactID
,
75 'event_id' => $this->_eventID
,
78 // to ensure it matches later on
79 'register_date' => '2007-07-21 00:00:00',
80 'source' => 'Online Event Registration: API Testing',
85 $this->eventDelete($this->_eventID
);
86 $tablesToTruncate = array(
87 'civicrm_custom_group', 'civicrm_custom_field', 'civicrm_contact', 'civicrm_participant'
89 // true tells quickCleanup to drop any tables that might have been created in the test
90 $this->quickCleanup($tablesToTruncate, TRUE);
94 * check with complete array + custom field
95 * Note that the test is written on purpose without any
96 * variables specific to participant so it can be replicated into other entities
97 * and / or moved to the automated test suite
99 function testCreateWithCustom() {
100 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
102 $params = $this->_params
;
103 $params['custom_' . $ids['custom_field_id']] = "custom string";
105 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
);
107 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
109 $check = $this->callAPISuccess($this->_entity
, 'get', array('id' => $result['id']));
110 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
112 $this->customFieldDelete($ids['custom_field_id']);
113 $this->customGroupDelete($ids['custom_group_id']);
117 ///////////////// civicrm_participant_get methods
120 * check with wrong params type
122 function testGetWrongParamsType() {
123 $params = 'a string';
124 $result = $this->callAPIFailure('participant', 'get', $params);
128 * Test civicrm_participant_get with empty params
130 function testGetEmptyParams() {
131 $this->callAPISuccess('participant', 'get', array());
135 * check with participant_id
137 function testGetParticipantIdOnly() {
139 'participant_id' => $this->_participantID
,
143 'participant_register_date',
144 'participant_source',
147 $result = $this->callAPISuccess('participant', 'get', $params);
148 $this->assertAPISuccess($result, " in line " . __LINE__
);
149 $this->assertEquals($result['values'][$this->_participantID
]['event_id'], $this->_eventID
, "in line " . __LINE__
);
150 $this->assertEquals($result['values'][$this->_participantID
]['participant_register_date'], '2007-02-19 00:00:00', "in line " . __LINE__
);
151 $this->assertEquals($result['values'][$this->_participantID
]['participant_source'], 'Wimbeldon', "in line " . __LINE__
);
153 'id' => $this->_participantID
,
154 'return' => 'id,participant_register_date,event_id',
157 $result = $this->callAPISuccess('participant', 'get', $params);
158 $this->assertEquals($result['values'][$this->_participantID
]['event_id'], $this->_eventID
);
159 $this->assertEquals($result['values'][$this->_participantID
]['participant_register_date'], '2007-02-19 00:00:00');
164 * check with params id
166 function testGetParamsAsIdOnly() {
168 'id' => $this->_participantID
,
170 $result = $this->callAPIAndDocument('participant', 'get', $params, __FUNCTION__
, __FILE__
);
171 $this->assertEquals($result['values'][$this->_participantID
]['event_id'], $this->_eventID
);
172 $this->assertEquals($result['values'][$this->_participantID
]['participant_register_date'], '2007-02-19 00:00:00');
173 $this->assertEquals($result['values'][$this->_participantID
]['participant_source'], 'Wimbeldon');
174 $this->assertEquals($result['id'], $result['values'][$this->_participantID
]['id']);
178 * check with params id
180 function testGetNestedEventGet() {
181 //create a second event & add participant to it.
182 $event = $this->eventCreate(NULL);
183 $this->callAPISuccess('participant', 'create', array('event_id' => $event['id'], 'contact_id' => $this->_contactID
));
186 $description = "use nested get to get an event";
187 $subfile = "NestedEventGet";
189 'id' => $this->_participantID
,
190 'api.event.get' => 1,
192 $result = $this->callAPIAndDocument('participant', 'get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
193 $this->assertEquals($result['values'][$this->_participantID
]['event_id'], $this->_eventID
);
194 $this->assertEquals($result['values'][$this->_participantID
]['participant_register_date'], '2007-02-19 00:00:00');
195 $this->assertEquals($result['values'][$this->_participantID
]['participant_source'], 'Wimbeldon');
196 $this->assertEquals($this->_eventID
, $result['values'][$this->_participantID
]['api.event.get']['id']);
199 * Check Participant Get respects return properties
201 function testGetWithReturnProperties() {
203 'contact_id' => $this->_contactID
, 'return.status_id' => 1,
204 'return.participant_status_id' => 1,
205 'options' => array('limit' => 1)
207 $result = $this->callAPISuccess('participant', 'get', $params);
208 $this->assertArrayHasKey('participant_status_id', $result['values'][$result['id']]);
212 * check with contact_id
214 function testGetContactIdOnly() {
216 'contact_id' => $this->_contactID
, );
217 $participant = $this->callAPISuccess('participant', 'get', $params);
219 $this->assertEquals($this->_participantID
, $participant['id'],
220 "In line " . __LINE__
222 $this->assertEquals($this->_eventID
, $participant['values'][$participant['id']]['event_id'],
223 "In line " . __LINE__
225 $this->assertEquals('2007-02-19 00:00:00', $participant['values'][$participant['id']]['participant_register_date'],
226 "In line " . __LINE__
228 $this->assertEquals('Wimbeldon', $participant['values'][$participant['id']]['participant_source'],
229 "In line " . __LINE__
231 $this->assertEquals($participant['id'], $participant['values'][$participant['id']]['id'],
232 "In line " . __LINE__
237 * check with event_id
240 function testGetMultiMatchReturnFirst() {
242 'event_id' => $this->_eventID
,
245 $participant = $this->callAPISuccess('participant', 'get', $params);
246 $this->assertNotNull($participant['id']);
250 * check with event_id
251 * in v3 this should return all participants
253 function testGetMultiMatchNoReturnFirst() {
255 'event_id' => $this->_eventID
,
257 $participant = $this->callAPISuccess('participant', 'get', $params);
258 $this->assertNotNull($participant['count'], 3);
261 ///////////////// civicrm_participant_get methods
264 * Test civicrm_participant_get with empty params
265 * In this case all the participant records are returned.
267 function testSearchEmptyParams() {
268 $result = $this->callAPISuccess('participant', 'get', array());
269 // expecting 3 participant records
270 $this->assertEquals($result['count'], 3);
274 * check with participant_id
276 function testSearchParticipantIdOnly() {
278 'participant_id' => $this->_participantID
,
280 $participant = $this->callAPISuccess('participant', 'get', $params);
281 $this->assertEquals($participant['values'][$this->_participantID
]['event_id'], $this->_eventID
);
282 $this->assertEquals($participant['values'][$this->_participantID
]['participant_register_date'], '2007-02-19 00:00:00');
283 $this->assertEquals($participant['values'][$this->_participantID
]['participant_source'], 'Wimbeldon');
287 * check with contact_id
289 function testSearchContactIdOnly() {
290 // Should get 2 participant records for this contact.
292 'contact_id' => $this->_contactID2
,
294 $participant = $this->callAPISuccess('participant', 'get', $params);
296 $this->assertEquals($participant['count'], 2);
300 * check with event_id
302 function testSearchByEvent() {
303 // Should get >= 3 participant records for this event. Also testing that last_name and event_title are returned.
305 'event_id' => $this->_eventID
,
306 'return.last_name' => 1,
307 'return.event_title' => 1,
309 $participant = $this->callAPISuccess('participant', 'get', $params);
310 if ($participant['count'] < 3) {
311 $this->fail("Event search returned less than expected miniumum of 3 records.");
314 $this->assertEquals($participant['values'][$this->_participantID
]['last_name'], 'Anderson');
315 $this->assertEquals($participant['values'][$this->_participantID
]['event_title'], 'Annual CiviCRM meet');
319 * check with event_id
322 function testSearchByEventWithLimit() {
323 // Should 2 participant records since we're passing rowCount = 2.
325 'event_id' => $this->_eventID
,
328 $participant = $this->callAPISuccess('participant', 'get', $params);
330 $this->assertEquals($participant['count'], 2, 'in line ' . __LINE__
);
333 ///////////////// civicrm_participant_create methods
336 * Test civicrm_participant_create with empty params
338 function testCreateEmptyParams() {
340 $result = $this->callAPIFailure('participant', 'create', $params);
344 * check with event_id
346 function testCreateMissingContactID() {
348 'event_id' => $this->_eventID
,
350 $participant = $this->callAPIFailure('participant', 'create', $params);
354 * check with contact_id
357 function testCreateMissingEventID() {
359 'contact_id' => $this->_contactID
,
361 $participant = $this->callAPIFailure('participant', 'create', $params);
365 * check with contact_id & event_id
367 function testCreateEventIdOnly() {
369 'contact_id' => $this->_contactID
,
370 'event_id' => $this->_eventID
,
372 $participant = $this->callAPISuccess('participant', 'create', $params);
373 $this->getAndCheck($params, $participant['id'], 'participant');
377 * check with complete array
379 function testCreateAllParams() {
380 $params = $this->_params
;
382 $participant = $this->callAPISuccess('participant', 'create', $params);
383 $this->_participantID
= $participant['id'];
384 // assertDBState compares expected values in $match to actual values in the DB
385 $this->assertDBState('CRM_Event_DAO_Participant', $participant['id'], $params);
388 * Test to check if receive date is being changed per CRM-9763
390 function testCreateUpdateReceiveDate() {
391 $participant = $this->callAPISuccess('participant', 'create', $this->_params
);
393 'id' => $participant['id'],
396 $this->callAPISuccess('participant', 'create', $update);
397 $this->getAndCheck(array_merge($this->_params
, $update), $participant['id'], 'participant');
400 * Test to check if participant fee level is being changed per CRM-9781
402 function testCreateUpdateParticipantFeeLevel() {
403 $myParams = $this->_params +
array('participant_fee_level' => CRM_Core_DAO
::VALUE_SEPARATOR
. "fee" . CRM_Core_DAO
::VALUE_SEPARATOR
);
404 $participant = $this->callAPISuccess('participant', 'create', $myParams);
406 'id' => $participant['id'],
409 $update = $this->callAPISuccess('participant', 'create', $update);
411 $this->assertEquals($participant['values'][$participant['id']]['fee_level'],
412 $update['values'][$participant['id']]['fee_level']
415 $this->callAPISuccess('participant', 'delete', array('id' => $participant['id']));
418 * check with complete array
420 function testUpdate() {
421 $participantId = $this->participantCreate(array(
422 'contactID' => $this->_individualId
,
423 'eventID' => $this->_eventID
426 'id' => $participantId,
427 'contact_id' => $this->_individualId
,
428 'event_id' => $this->_eventID
,
431 'register_date' => '2006-01-21',
432 'source' => 'US Open',
434 $participant = $this->callAPISuccess('participant', 'create', $params);
435 $this->getAndCheck($params, $participant['id'], 'participant');
436 $result = $this->participantDelete($params['id']);
440 * Test to check if participant fee level is being changed per CRM-9781
441 * Try again without a custom separater to check that one isn't added
442 * (get & check won't accept an array)
444 function testUpdateCreateParticipantFeeLevelNoSeparator() {
446 $myParams = $this->_params +
array('participant_fee_level' => "fee");
447 $participant = $this->callAPISuccess('participant', 'create', $myParams);
448 $this->assertAPISuccess($participant);
450 'id' => $participant['id'],
453 $this->callAPISuccess('participant', 'create', $update);
454 $this->assertEquals($participant['values'][$participant['id']]['fee_level'],
455 $myParams['participant_fee_level']
457 $this->getAndCheck($update, $participant['id'], 'participant');
459 ///////////////// civicrm_participant_update methods
462 * Test civicrm_participant_update with wrong params type
464 function testUpdateWrongParamsType() {
465 $params = 'a string';
466 $result = $this->callAPIFailure('participant', 'create', $params);
467 $this->assertEquals('Input variable `params` is not an array', $result['error_message'], 'In line ' . __LINE__
);
471 * check with empty array
473 function testUpdateEmptyParams() {
475 $participant = $this->callAPIFailure('participant', 'create', $params);
476 $this->assertEquals($participant['error_message'], 'Mandatory key(s) missing from params array: event_id, contact_id');
480 * check without event_id
482 function testUpdateWithoutEventId() {
483 $participantId = $this->participantCreate(array('contactID' => $this->_individualId
, 'eventID' => $this->_eventID
));
485 'contact_id' => $this->_individualId
,
488 'register_date' => '2006-01-21',
489 'source' => 'US Open',
490 'event_level' => 'Donation', );
491 $participant = $this->callAPIFailure('participant', 'create', $params);
492 $this->assertEquals($participant['error_message'], 'Mandatory key(s) missing from params array: event_id');
493 // Cleanup created participant records.
494 $result = $this->participantDelete($participantId);
498 * check with Invalid participantId
500 function testUpdateWithWrongParticipantId() {
505 'register_date' => '2006-01-21',
506 'source' => 'US Open',
507 'event_level' => 'Donation', );
508 $participant = $this->callAPIFailure('Participant', 'update', $params);
512 * check with Invalid ContactId
514 function testUpdateWithWrongContactId() {
515 $participantId = $this->participantCreate(array(
516 'contactID' => $this->_individualId
,
517 'eventID' => $this->_eventID
,
518 ), $this->_apiversion
);
520 'id' => $participantId,
521 'contact_id' => 12345,
524 'register_date' => '2006-01-21',
525 'source' => 'US Open',
526 'event_level' => 'Donation', );
527 $participant = $this->callAPIFailure('participant', 'create', $params);
528 $result = $this->participantDelete($participantId);
531 ///////////////// civicrm_participant_delete methods
534 * Test civicrm_participant_delete with wrong params type
536 function testDeleteWrongParamsType() {
537 $params = 'a string';
538 $result = $this->callAPIFailure('participant', 'delete', $params);
542 * Test civicrm_participant_delete with empty params
544 function testDeleteEmptyParams() {
546 $result = $this->callAPIFailure('participant', 'delete', $params);
550 * check with participant_id
552 function testParticipantDelete() {
554 'id' => $this->_participantID
, );
555 $participant = $this->callAPISuccess('participant', 'delete', $params);
556 $this->assertAPISuccess($participant);
557 $this->assertDBState('CRM_Event_DAO_Participant', $this->_participantID
, NULL, TRUE);
561 * check without participant_id
563 * This should return an error because required param is missing..
565 function testParticipantDeleteMissingID() {
567 'event_id' => $this->_eventID
, );
568 $participant = $this->callAPIFailure('participant', 'delete', $params);
569 $this->assertNotNull($participant['error_message']);
572 * delete with a get - a 'criteria delete'
574 function testNestedDelete() {
575 $description = "Criteria delete by nesting a GET & a DELETE";
576 $subfile = "NestedDelete";
577 $participants = $this->callAPISuccess('Participant', 'Get', array());
578 $this->assertEquals($participants['count'], 3);
579 $params = array('contact_id' => $this->_contactID2
, 'api.participant.delete' => 1);
580 $participants = $this->callAPIAndDocument('Participant', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile, 'Get');
581 $check = $this->callAPISuccess('participant', 'getcount', array());
582 $this->assertEquals(1, $check,"only one participant should be left. line " . __LINE__
);
585 * Test creation of a participant with an associated contribution
587 function testCreateParticipantWithPayment() {
588 $description = "single function to create contact w partipation & contribution. Note that in the
589 case of 'contribution' the 'create' is implied (api.contribution.create)";
590 $subfile = "CreateParticipantPayment";
592 'contact_type' => 'Individual',
593 'display_name' => 'dlobo',
594 'api.participant' => array(
595 'event_id' => $this->_eventID
,
598 'format.only_id' => 1,
600 'api.contribution.create' => array(
601 'financial_type_id' => 1,
602 'total_amount' => 100,
603 'format.only_id' => 1,
605 'api.participant_payment.create' => array(
606 'contribution_id' => '$value.api.contribution.create',
607 'participant_id' => '$value.api.participant',
611 $result = $this->callAPIAndDocument('contact', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
612 $this->assertEquals(1, $result['values'][$result['id']]['api.participant_payment.create']['count']);
613 $this->callAPISuccess('contact', 'delete', array('id' => $result['id']));