1b4ea3ac82fd19b4c10e9b1e7d01cf4c7f8cc373
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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;
48 public $_eNoticeCompliant = FALSE;
52 'name' => 'Participant Create',
53 'description' => 'Test all Participant Create API methods.',
54 'group' => 'CiviCRM API Tests',
59 $this->_apiversion
= 3;
61 $this->_entity
= 'participant';
62 $event = $this->eventCreate(NULL);
63 $this->_eventID
= $event['id'];
65 $this->_contactID
= $this->individualCreate(NULL);
67 $this->_createdParticipants
= array();
68 $this->_individualId
= $this->individualCreate(NULL);
70 $this->_participantID
= $this->participantCreate(array('contact_id' => $this->_contactID
, 'event_id' => $this->_eventID
));
71 $this->_contactID2
= $this->individualCreate(NULL);
72 $this->_participantID2
= $this->participantCreate(array('contact_id' => $this->_contactID2
, 'event_id' => $this->_eventID
));
73 $this->_participantID3
= $this->participantCreate(array('contact_id' => $this->_contactID2
, 'event_id' => $this->_eventID
));
74 $this->_params
= array(
75 'contact_id' => $this->_contactID
,
76 'event_id' => $this->_eventID
,
79 // to ensure it matches later on
80 'register_date' => '2007-07-21 00:00:00',
81 'source' => 'Online Event Registration: API Testing',
86 $this->eventDelete($this->_eventID
);
87 $tablesToTruncate = array(
88 'civicrm_custom_group', 'civicrm_custom_field', 'civicrm_contact', 'civicrm_participant'
90 // true tells quickCleanup to drop any tables that might have been created in the test
91 $this->quickCleanup($tablesToTruncate, TRUE);
95 * check with complete array + custom field
96 * Note that the test is written on purpose without any
97 * variables specific to participant so it can be replicated into other entities
98 * and / or moved to the automated test suite
100 function testCreateWithCustom() {
101 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
103 $params = $this->_params
;
104 $params['custom_' . $ids['custom_field_id']] = "custom string";
106 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
);
108 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
110 $check = $this->callAPISuccess($this->_entity
, 'get', array('id' => $result['id']));
111 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
113 $this->customFieldDelete($ids['custom_field_id']);
114 $this->customGroupDelete($ids['custom_group_id']);
118 ///////////////// civicrm_participant_get methods
121 * check with wrong params type
123 function testGetWrongParamsType() {
124 $params = 'a string';
125 $result = $this->callAPIFailure('participant', 'get', $params);
129 * Test civicrm_participant_get with empty params
131 function testGetEmptyParams() {
132 $this->callAPISuccess('participant', 'get', array());
136 * check with participant_id
138 function testGetParticipantIdOnly() {
140 'participant_id' => $this->_participantID
,
144 'participant_register_date',
145 'participant_source',
148 $result = $this->callAPISuccess('participant', 'get', $params);
149 $this->assertAPISuccess($result, " in line " . __LINE__
);
150 $this->assertEquals($result['values'][$this->_participantID
]['event_id'], $this->_eventID
, "in line " . __LINE__
);
151 $this->assertEquals($result['values'][$this->_participantID
]['participant_register_date'], '2007-02-19 00:00:00', "in line " . __LINE__
);
152 $this->assertEquals($result['values'][$this->_participantID
]['participant_source'], 'Wimbeldon', "in line " . __LINE__
);
154 'id' => $this->_participantID
,
155 'return' => 'id,participant_register_date,event_id',
158 $result = $this->callAPISuccess('participant', 'get', $params);
159 $this->assertEquals($result['values'][$this->_participantID
]['event_id'], $this->_eventID
);
160 $this->assertEquals($result['values'][$this->_participantID
]['participant_register_date'], '2007-02-19 00:00:00');
165 * check with params id
167 function testGetParamsAsIdOnly() {
169 'id' => $this->_participantID
,
171 $result = $this->callAPIAndDocument('participant', 'get', $params, __FUNCTION__
, __FILE__
);
172 $this->assertEquals($result['values'][$this->_participantID
]['event_id'], $this->_eventID
);
173 $this->assertEquals($result['values'][$this->_participantID
]['participant_register_date'], '2007-02-19 00:00:00');
174 $this->assertEquals($result['values'][$this->_participantID
]['participant_source'], 'Wimbeldon');
175 $this->assertEquals($result['id'], $result['values'][$this->_participantID
]['id']);
179 * check with params id
181 function testGetNestedEventGet() {
182 //create a second event & add participant to it.
183 $event = $this->eventCreate(NULL);
184 $this->callAPISuccess('participant', 'create', array('event_id' => $event['id'], 'contact_id' => $this->_contactID
));
187 $description = "use nested get to get an event";
188 $subfile = "NestedEventGet";
190 'id' => $this->_participantID
,
191 'api.event.get' => 1,
193 $result = $this->callAPIAndDocument('participant', 'get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
194 $this->assertEquals($result['values'][$this->_participantID
]['event_id'], $this->_eventID
);
195 $this->assertEquals($result['values'][$this->_participantID
]['participant_register_date'], '2007-02-19 00:00:00');
196 $this->assertEquals($result['values'][$this->_participantID
]['participant_source'], 'Wimbeldon');
197 $this->assertEquals($this->_eventID
, $result['values'][$this->_participantID
]['api.event.get']['id']);
200 * Check Participant Get respects return properties
202 function testGetWithReturnProperties() {
204 'contact_id' => $this->_contactID
, 'return.status_id' => 1,
205 'return.participant_status_id' => 1,
206 'options' => array('limit' => 1)
208 $result = $this->callAPISuccess('participant', 'get', $params);
209 $this->assertArrayHasKey('participant_status_id', $result['values'][$result['id']]);
213 * check with contact_id
215 function testGetContactIdOnly() {
217 'contact_id' => $this->_contactID
, );
218 $participant = $this->callAPISuccess('participant', 'get', $params);
220 $this->assertEquals($this->_participantID
, $participant['id'],
221 "In line " . __LINE__
223 $this->assertEquals($this->_eventID
, $participant['values'][$participant['id']]['event_id'],
224 "In line " . __LINE__
226 $this->assertEquals('2007-02-19 00:00:00', $participant['values'][$participant['id']]['participant_register_date'],
227 "In line " . __LINE__
229 $this->assertEquals('Wimbeldon', $participant['values'][$participant['id']]['participant_source'],
230 "In line " . __LINE__
232 $this->assertEquals($participant['id'], $participant['values'][$participant['id']]['id'],
233 "In line " . __LINE__
238 * check with event_id
241 function testGetMultiMatchReturnFirst() {
243 'event_id' => $this->_eventID
,
246 $participant = $this->callAPISuccess('participant', 'get', $params);
247 $this->assertNotNull($participant['id']);
251 * check with event_id
252 * in v3 this should return all participants
254 function testGetMultiMatchNoReturnFirst() {
256 'event_id' => $this->_eventID
,
258 $participant = $this->callAPISuccess('participant', 'get', $params);
259 $this->assertNotNull($participant['count'], 3);
262 ///////////////// civicrm_participant_get methods
265 * Test civicrm_participant_get with empty params
266 * In this case all the participant records are returned.
268 function testSearchEmptyParams() {
269 $result = $this->callAPISuccess('participant', 'get', array());
270 // expecting 3 participant records
271 $this->assertEquals($result['count'], 3);
275 * check with participant_id
277 function testSearchParticipantIdOnly() {
279 'participant_id' => $this->_participantID
,
281 $participant = $this->callAPISuccess('participant', 'get', $params);
282 $this->assertEquals($participant['values'][$this->_participantID
]['event_id'], $this->_eventID
);
283 $this->assertEquals($participant['values'][$this->_participantID
]['participant_register_date'], '2007-02-19 00:00:00');
284 $this->assertEquals($participant['values'][$this->_participantID
]['participant_source'], 'Wimbeldon');
288 * check with contact_id
290 function testSearchContactIdOnly() {
291 // Should get 2 participant records for this contact.
293 'contact_id' => $this->_contactID2
,
295 $participant = $this->callAPISuccess('participant', 'get', $params);
297 $this->assertEquals($participant['count'], 2);
301 * check with event_id
303 function testSearchByEvent() {
304 // Should get >= 3 participant records for this event. Also testing that last_name and event_title are returned.
306 'event_id' => $this->_eventID
,
307 'return.last_name' => 1,
308 'return.event_title' => 1,
310 $participant = $this->callAPISuccess('participant', 'get', $params);
311 if ($participant['count'] < 3) {
312 $this->fail("Event search returned less than expected miniumum of 3 records.");
315 $this->assertEquals($participant['values'][$this->_participantID
]['last_name'], 'Anderson');
316 $this->assertEquals($participant['values'][$this->_participantID
]['event_title'], 'Annual CiviCRM meet');
320 * check with event_id
323 function testSearchByEventWithLimit() {
324 // Should 2 participant records since we're passing rowCount = 2.
326 'event_id' => $this->_eventID
,
329 $participant = $this->callAPISuccess('participant', 'get', $params);
331 $this->assertEquals($participant['count'], 2, 'in line ' . __LINE__
);
334 ///////////////// civicrm_participant_create methods
337 * Test civicrm_participant_create with empty params
339 function testCreateEmptyParams() {
341 $result = $this->callAPIFailure('participant', 'create', $params);
345 * check with event_id
347 function testCreateMissingContactID() {
349 'event_id' => $this->_eventID
,
351 $participant = $this->callAPIFailure('participant', 'create', $params);
355 * check with contact_id
358 function testCreateMissingEventID() {
360 'contact_id' => $this->_contactID
,
362 $participant = $this->callAPIFailure('participant', 'create', $params);
366 * check with contact_id & event_id
368 function testCreateEventIdOnly() {
370 'contact_id' => $this->_contactID
,
371 'event_id' => $this->_eventID
,
373 $participant = $this->callAPISuccess('participant', 'create', $params);
374 $this->getAndCheck($params, $participant['id'], 'participant');
378 * check with complete array
380 function testCreateAllParams() {
381 $params = $this->_params
;
383 $participant = $this->callAPISuccess('participant', 'create', $params);
384 $this->_participantID
= $participant['id'];
385 // assertDBState compares expected values in $match to actual values in the DB
386 $this->assertDBState('CRM_Event_DAO_Participant', $participant['id'], $params);
389 * Test to check if receive date is being changed per CRM-9763
391 function testCreateUpdateReceiveDate() {
392 $participant = $this->callAPISuccess('participant', 'create', $this->_params
);
394 'id' => $participant['id'],
397 $this->callAPISuccess('participant', 'create', $update);
398 $this->getAndCheck(array_merge($this->_params
, $update), $participant['id'], 'participant');
401 * Test to check if participant fee level is being changed per CRM-9781
403 function testCreateUpdateParticipantFeeLevel() {
404 $myParams = $this->_params +
array('participant_fee_level' => CRM_Core_DAO
::VALUE_SEPARATOR
. "fee" . CRM_Core_DAO
::VALUE_SEPARATOR
);
405 $participant = $this->callAPISuccess('participant', 'create', $myParams);
406 $this->assertAPISuccess($participant);
408 'id' => $participant['id'],
411 $this->callAPISuccess('participant', 'create', $update);
412 $this->assertEquals($participant['values'][$participant['id']]['participant_fee_level'],
413 $update['values'][$participant['id']]['participant_fee_level']
416 $this->callAPISuccess('participant', 'delete', array('id' => $participant['id']));
419 * check with complete array
421 function testUpdate() {
422 $participantId = $this->participantCreate(array('contactID' => $this->_individualId
, 'eventID' => $this->_eventID
, $this->_apiversion
));
424 'id' => $participantId,
425 'contact_id' => $this->_individualId
,
426 'event_id' => $this->_eventID
,
429 'register_date' => '2006-01-21',
430 'source' => 'US Open',
431 'event_level' => 'Donation',
433 $participant = $this->callAPISuccess('participant', 'create', $params);
434 $this->getAndCheck($params, $participant['id'], 'participant');
435 $result = $this->participantDelete($params['id']);
439 * Test to check if participant fee level is being changed per CRM-9781
440 * Try again without a custom separater to check that one isn't added
441 * (get & check won't accept an array)
443 function testUpdateCreateParticipantFeeLevelNoSeparator() {
445 $myParams = $this->_params +
array('participant_fee_level' => "fee");
446 $participant = $this->callAPISuccess('participant', 'create', $myParams);
447 $this->assertAPISuccess($participant);
449 'id' => $participant['id'],
452 $this->callAPISuccess('participant', 'create', $update);
453 $this->assertEquals($participant['values'][$participant['id']]['fee_level'],
454 $myParams['participant_fee_level']
456 $this->getAndCheck($update, $participant['id'], 'participant');
458 ///////////////// civicrm_participant_update methods
461 * Test civicrm_participant_update with wrong params type
463 function testUpdateWrongParamsType() {
464 $params = 'a string';
465 $result = $this->callAPIFailure('participant', 'create', $params);
466 $this->assertEquals('Input variable `params` is not an array', $result['error_message'], 'In line ' . __LINE__
);
470 * check with empty array
472 function testUpdateEmptyParams() {
474 $participant = $this->callAPIFailure('participant', 'create', $params);
475 $this->assertEquals($participant['error_message'], 'Mandatory key(s) missing from params array: event_id, contact_id');
479 * check without event_id
481 function testUpdateWithoutEventId() {
482 $participantId = $this->participantCreate(array('contactID' => $this->_individualId
, 'eventID' => $this->_eventID
));
484 'contact_id' => $this->_individualId
,
487 'register_date' => '2006-01-21',
488 'source' => 'US Open',
489 'event_level' => 'Donation', );
490 $participant = $this->callAPIFailure('participant', 'create', $params);
491 $this->assertEquals($participant['error_message'], 'Mandatory key(s) missing from params array: event_id');
492 // Cleanup created participant records.
493 $result = $this->participantDelete($participantId);
497 * check with Invalid participantId
499 function testUpdateWithWrongParticipantId() {
504 'register_date' => '2006-01-21',
505 'source' => 'US Open',
506 'event_level' => 'Donation', );
507 $participant = $this->callAPIFailure('Participant', 'update', $params);
511 * check with Invalid ContactId
513 function testUpdateWithWrongContactId() {
514 $participantId = $this->participantCreate(array(
515 'contactID' => $this->_individualId
,
516 'eventID' => $this->_eventID
,
517 ), $this->_apiversion
);
519 'id' => $participantId,
520 'contact_id' => 12345,
523 'register_date' => '2006-01-21',
524 'source' => 'US Open',
525 'event_level' => 'Donation', );
526 $participant = $this->callAPIFailure('participant', 'create', $params);
527 $result = $this->participantDelete($participantId);
530 ///////////////// civicrm_participant_delete methods
533 * Test civicrm_participant_delete with wrong params type
535 function testDeleteWrongParamsType() {
536 $params = 'a string';
537 $result = $this->callAPIFailure('participant', 'delete', $params);
541 * Test civicrm_participant_delete with empty params
543 function testDeleteEmptyParams() {
545 $result = $this->callAPIFailure('participant', 'delete', $params);
549 * check with participant_id
551 function testParticipantDelete() {
553 'id' => $this->_participantID
, );
554 $participant = $this->callAPISuccess('participant', 'delete', $params);
555 $this->assertAPISuccess($participant);
556 $this->assertDBState('CRM_Event_DAO_Participant', $this->_participantID
, NULL, TRUE);
560 * check without participant_id
562 * This should return an error because required param is missing..
564 function testParticipantDeleteMissingID() {
566 'event_id' => $this->_eventID
, );
567 $participant = $this->callAPIFailure('participant', 'delete', $params);
568 $this->assertNotNull($participant['error_message']);
571 * delete with a get - a 'criteria delete'
573 function testNestedDelete() {
574 $description = "Criteria delete by nesting a GET & a DELETE";
575 $subfile = "NestedDelete";
576 $participants = $this->callAPISuccess('Participant', 'Get', array());
577 $this->assertEquals($participants['count'], 3);
578 $params = array('contact_id' => $this->_contactID2
, 'api.participant.delete' => 1);
579 $participants = $this->callAPIAndDocument('Participant', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile, 'Get');
580 $this->assertEquals(1, $participants['count'], "only one participant should be left. line " . __LINE__
);
583 * Test creation of a participant with an associated contribution
585 function testCreateParticipantWithPayment() {
586 $this->_contributionTypeId
= $this->contributionTypeCreate();
587 $description = "single function to create contact w partipation & contribution. Note that in the
588 case of 'contribution' the 'create' is implied (api.contribution.create)";
589 $subfile = "CreateParticipantPayment";
591 'contact_type' => 'Individual',
592 'display_name' => 'dlobo', 'api.participant' => array(
593 'event_id' => $this->_eventID
,
596 'format.only_id' => 1,
598 'api.contribution.create' => array(
599 'financial_type_id' => 1,
600 'total_amount' => 100,
601 'format.only_id' => 1,
603 'api.participant_payment.create' => array(
604 'contribution_id' => '$value.api.contribution.create',
605 'participant_id' => '$value.api.participant',
609 $result = $this->callAPIAndDocument('contact', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
610 $this->assertEquals(1, $result['values'][$result['id']]['api.participant_payment.create']['count']);
611 $this->callAPISuccess('contact', 'delete', array('id' => $result['id']));