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('contactID' => $this->_contactID
, 'eventID' => $this->_eventID
));
71 $this->_contactID2
= $this->individualCreate(NULL);
72 $this->_participantID2
= $this->participantCreate(array('contactID' => $this->_contactID2
, 'eventID' => $this->_eventID
, 'version' => $this->_apiversion
));
73 $this->_participantID3
= $this->participantCreate(array('contactID' => $this->_contactID2
, 'eventID' => $this->_eventID
, 'version' => $this->_apiversion
));
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',
82 'version' => $this->_apiversion
,
87 $this->eventDelete($this->_eventID
);
88 $tablesToTruncate = array(
89 'civicrm_custom_group', 'civicrm_custom_field', 'civicrm_contact', 'civicrm_participant'
91 // true tells quickCleanup to drop any tables that might have been created in the test
92 $this->quickCleanup($tablesToTruncate, TRUE);
96 * check with complete array + custom field
97 * Note that the test is written on purpose without any
98 * variables specific to participant so it can be replicated into other entities
99 * and / or moved to the automated test suite
101 function testCreateWithCustom() {
102 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
104 $params = $this->_params
;
105 $params['custom_' . $ids['custom_field_id']] = "custom string";
107 $result = civicrm_api($this->_entity
, 'create', $params);
108 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
109 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
110 $this->assertAPISuccess($result);
112 $check = civicrm_api($this->_entity
, 'get', array('version' => 3, 'id' => $result['id']));
113 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
115 $this->customFieldDelete($ids['custom_field_id']);
116 $this->customGroupDelete($ids['custom_group_id']);
120 ///////////////// civicrm_participant_get methods
123 * check with wrong params type
125 function testGetWrongParamsType() {
126 $params = 'a string';
127 $result = $this->callAPIFailure('participant', 'get', $params);
131 * Test civicrm_participant_get with empty params
133 function testGetEmptyParams() {
134 $this->callAPISuccess('participant', 'get', array());
138 * check with participant_id
140 function testGetParticipantIdOnly() {
142 'participant_id' => $this->_participantID
,
143 'version' => $this->_apiversion
,
147 'participant_register_date',
148 'participant_source',
151 $result = civicrm_api('participant', 'get', $params);
152 $this->assertAPISuccess($result, " in line " . __LINE__
);
153 $this->assertEquals($result['values'][$this->_participantID
]['event_id'], $this->_eventID
, "in line " . __LINE__
);
154 $this->assertEquals($result['values'][$this->_participantID
]['participant_register_date'], '2007-02-19 00:00:00', "in line " . __LINE__
);
155 $this->assertEquals($result['values'][$this->_participantID
]['participant_source'], 'Wimbeldon', "in line " . __LINE__
);
157 'id' => $this->_participantID
,
158 'version' => $this->_apiversion
,
159 'return' => 'id,participant_register_date,event_id',
162 $result = civicrm_api('participant', 'get', $params);
163 $this->assertEquals($result['values'][$this->_participantID
]['event_id'], $this->_eventID
);
164 $this->assertEquals($result['values'][$this->_participantID
]['participant_register_date'], '2007-02-19 00:00:00');
169 * check with params id
171 function testGetParamsAsIdOnly() {
173 'id' => $this->_participantID
,
174 'version' => $this->_apiversion
,
176 $result = civicrm_api('participant', 'get', $params);
177 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
178 $this->assertAPISuccess($result);
179 $this->assertEquals($result['values'][$this->_participantID
]['event_id'], $this->_eventID
);
180 $this->assertEquals($result['values'][$this->_participantID
]['participant_register_date'], '2007-02-19 00:00:00');
181 $this->assertEquals($result['values'][$this->_participantID
]['participant_source'], 'Wimbeldon');
182 $this->assertEquals($result['id'], $result['values'][$this->_participantID
]['id']);
186 * check with params id
188 function testGetNestedEventGet() {
189 //create a second event & add participant to it.
190 $event = $this->eventCreate(NULL);
191 civicrm_api('participant', 'create', array('version' => 3, 'event_id' => $event['id'], 'contact_id' => $this->_contactID
));
194 $description = "use nested get to get an event";
195 $subfile = "NestedEventGet";
197 'id' => $this->_participantID
,
198 'version' => $this->_apiversion
,
199 'api.event.get' => 1,
201 $result = civicrm_api('participant', 'get', $params);
202 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
, $description, $subfile);
203 $this->assertAPISuccess($result);
204 $this->assertEquals($result['values'][$this->_participantID
]['event_id'], $this->_eventID
);
205 $this->assertEquals($result['values'][$this->_participantID
]['participant_register_date'], '2007-02-19 00:00:00');
206 $this->assertEquals($result['values'][$this->_participantID
]['participant_source'], 'Wimbeldon');
207 $this->assertEquals($this->_eventID
, $result['values'][$this->_participantID
]['api.event.get']['id']);
210 * Check Participant Get respects return properties
212 function testGetWithReturnProperties() {
214 'contact_id' => $this->_contactID
,
215 'version' => $this->_apiversion
,
216 'return.status_id' => 1,
217 'return.participant_status_id' => 1,
218 'options' => array('limit' => 1)
220 $result = civicrm_api('participant', 'get', $params);
221 $this->assertArrayHasKey('participant_status_id', $result['values'][$result['id']]);
225 * check with contact_id
227 function testGetContactIdOnly() {
229 'contact_id' => $this->_contactID
,
230 'version' => $this->_apiversion
,
232 $participant = civicrm_api('participant', 'get', $params);
234 $this->assertEquals($this->_participantID
, $participant['id'],
235 "In line " . __LINE__
237 $this->assertEquals($this->_eventID
, $participant['values'][$participant['id']]['event_id'],
238 "In line " . __LINE__
240 $this->assertEquals('2007-02-19 00:00:00', $participant['values'][$participant['id']]['participant_register_date'],
241 "In line " . __LINE__
243 $this->assertEquals('Wimbeldon', $participant['values'][$participant['id']]['participant_source'],
244 "In line " . __LINE__
246 $this->assertEquals($participant['id'], $participant['values'][$participant['id']]['id'],
247 "In line " . __LINE__
252 * check with event_id
255 function testGetMultiMatchReturnFirst() {
257 'event_id' => $this->_eventID
,
259 'version' => $this->_apiversion
,
262 $participant = civicrm_api('participant', 'get', $params);
263 $this->assertNotNull($participant['id']);
267 * check with event_id
268 * in v3 this should return all participants
270 function testGetMultiMatchNoReturnFirst() {
272 'event_id' => $this->_eventID
,
273 'version' => $this->_apiversion
,
275 $participant = civicrm_api('participant', 'get', $params);
276 $this->assertEquals($participant['is_error'], 0);
277 $this->assertNotNull($participant['count'], 3);
280 ///////////////// civicrm_participant_get methods
283 * Test civicrm_participant_get with wrong params type
285 function testSearchWrongParamsType() {
286 $params = 'a string';
287 $result = $this->callAPIFailure('participant', 'get', $params);
291 * Test civicrm_participant_get with empty params
292 * In this case all the participant records are returned.
294 function testSearchEmptyParams() {
295 $params = array('version' => $this->_apiversion
);
296 $result = civicrm_api('participant', 'get', $params);
298 // expecting 3 participant records
299 $this->assertEquals($result['count'], 3);
303 * check with participant_id
305 function testSearchParticipantIdOnly() {
307 'participant_id' => $this->_participantID
,
308 'version' => $this->_apiversion
,
310 $participant = civicrm_api('participant', 'get', $params);
311 $this->assertEquals($participant['values'][$this->_participantID
]['event_id'], $this->_eventID
);
312 $this->assertEquals($participant['values'][$this->_participantID
]['participant_register_date'], '2007-02-19 00:00:00');
313 $this->assertEquals($participant['values'][$this->_participantID
]['participant_source'], 'Wimbeldon');
317 * check with contact_id
319 function testSearchContactIdOnly() {
320 // Should get 2 participant records for this contact.
322 'contact_id' => $this->_contactID2
,
323 'version' => $this->_apiversion
,
325 $participant = civicrm_api('participant', 'get', $params);
327 $this->assertEquals($participant['count'], 2);
331 * check with event_id
333 function testSearchByEvent() {
334 // Should get >= 3 participant records for this event. Also testing that last_name and event_title are returned.
336 'event_id' => $this->_eventID
,
337 'return.last_name' => 1,
338 'return.event_title' => 1,
339 'version' => $this->_apiversion
,
341 $participant = civicrm_api('participant', 'get', $params);
342 if ($participant['count'] < 3) {
343 $this->fail("Event search returned less than expected miniumum of 3 records.");
346 $this->assertEquals($participant['values'][$this->_participantID
]['last_name'], 'Anderson');
347 $this->assertEquals($participant['values'][$this->_participantID
]['event_title'], 'Annual CiviCRM meet');
351 * check with event_id
354 function testSearchByEventWithLimit() {
355 // Should 2 participant records since we're passing rowCount = 2.
357 'event_id' => $this->_eventID
,
359 'version' => $this->_apiversion
,
361 $participant = civicrm_api('participant', 'get', $params);
363 $this->assertEquals($participant['count'], 2, 'in line ' . __LINE__
);
366 ///////////////// civicrm_participant_create methods
369 * Test civicrm_participant_create with wrong params type
371 function testCreateWrongParamsType() {
372 $params = 'a string';
373 $result = $this->callAPIFailure('participant', 'create', $params);
377 * Test civicrm_participant_create with empty params
379 function testCreateEmptyParams() {
381 $result = $this->callAPIFailure('participant', 'create', $params);
385 * check with event_id
387 function testCreateMissingContactID() {
389 'event_id' => $this->_eventID
,
390 'version' => $this->_apiversion
,
392 $participant = civicrm_api('participant', 'create', $params);
393 if (CRM_Utils_Array
::value('id', $participant)) {
394 $this->_createdParticipants
[] = $participant['id'];
396 $this->assertEquals($participant['is_error'], 1);
397 $this->assertNotNull($participant['error_message']);
401 * check with contact_id
404 function testCreateMissingEventID() {
406 'contact_id' => $this->_contactID
,
407 'version' => $this->_apiversion
,
409 $participant = civicrm_api('participant', 'create', $params);
410 if (CRM_Utils_Array
::value('id', $participant)) {
411 $this->_createdParticipants
[] = $participant['id'];
413 $this->assertEquals($participant['is_error'], 1);
414 $this->assertNotNull($participant['error_message']);
418 * check with contact_id & event_id
420 function testCreateEventIdOnly() {
422 'contact_id' => $this->_contactID
,
423 'event_id' => $this->_eventID
,
424 'version' => $this->_apiversion
,
426 $participant = civicrm_api('participant', 'create', $params);
427 $this->assertAPISuccess($participant);
428 $this->_participantID
= $participant['id'];
430 if (!$participant['is_error']) {
431 // assertDBState compares expected values in $match to actual values in the DB
432 unset($params['version']);
433 $this->assertDBState('CRM_Event_DAO_Participant', $participant['id'], $params);
438 * check with complete array
440 function testCreateAllParams() {
441 $params = $this->_params
;
443 $participant = civicrm_api('participant', 'create', $params);
444 $this->assertNotEquals($participant['is_error'], 1, 'in line ' . __LINE__
);
445 $this->_participantID
= $participant['id'];
446 if (!$participant['is_error']) {
447 // assertDBState compares expected values in $match to actual values in the DB
448 unset($params['version']);
449 $this->assertDBState('CRM_Event_DAO_Participant', $participant['id'], $params);
453 * Test to check if receive date is being changed per CRM-9763
455 function testCreateUpdateReceiveDate() {
456 $participant = civicrm_api('participant', 'create', $this->_params
);
459 'id' => $participant['id'],
462 civicrm_api('participant', 'create', $update);
463 $this->getAndCheck(array_merge($this->_params
, $update), $participant['id'], 'participant');
466 * Test to check if participant fee level is being changed per CRM-9781
468 function testCreateUpdateParticipantFeeLevel() {
469 $myParams = $this->_params +
array('participant_fee_level' => CRM_Core_DAO
::VALUE_SEPARATOR
. "fee" . CRM_Core_DAO
::VALUE_SEPARATOR
);
470 $participant = civicrm_api('participant', 'create', $myParams);
471 $this->assertAPISuccess($participant);
474 'id' => $participant['id'],
477 civicrm_api('participant', 'create', $update);
478 $this->assertEquals($participant['values'][$participant['id']]['participant_fee_level'],
479 $update['values'][$participant['id']]['participant_fee_level']
482 civicrm_api('participant', 'delete', array('version' => 3, 'id' => $participant['id']));
485 * Test to check if participant fee level is being changed per CRM-9781
486 * Try again without a custom separater to check that one isn't added
487 * (get & check won't accept an array)
489 function testUpdateCreateParticipantFeeLevelNoSeparator() {
491 $myParams = $this->_params +
array('participant_fee_level' => "fee");
492 $participant = civicrm_api('participant', 'create', $myParams);
493 $this->assertAPISuccess($participant);
496 'id' => $participant['id'],
499 civicrm_api('participant', 'create', $update);
500 $this->assertEquals($participant['values'][$participant['id']]['fee_level'],
501 $myParams['participant_fee_level']
503 $this->getAndCheck($update, $participant['id'], 'participant');
505 ///////////////// civicrm_participant_update methods
508 * Test civicrm_participant_update with wrong params type
510 function testUpdateWrongParamsType() {
511 $params = 'a string';
512 $result = $this->callAPIFailure('participant', 'create', $params);
513 $this->assertEquals('Input variable `params` is not an array', $result['error_message'], 'In line ' . __LINE__
);
517 * check with empty array
519 function testUpdateEmptyParams() {
520 $params = array('version' => $this->_apiversion
);
521 $participant = $this->callAPIFailure('participant', 'create', $params);
522 $this->assertEquals($participant['error_message'], 'Mandatory key(s) missing from params array: event_id, contact_id');
526 * check without event_id
528 function testUpdateWithoutEventId() {
529 $participantId = $this->participantCreate(array('contactID' => $this->_individualId
, 'eventID' => $this->_eventID
, 'version' => $this->_apiversion
));
531 'contact_id' => $this->_individualId
,
534 'register_date' => '2006-01-21',
535 'source' => 'US Open',
536 'event_level' => 'Donation',
537 'version' => $this->_apiversion
,
539 $participant = $this->callAPIFailure('participant', 'create', $params);
540 $this->assertEquals($participant['error_message'], 'Mandatory key(s) missing from params array: event_id');
541 // Cleanup created participant records.
542 $result = $this->participantDelete($participantId);
546 * check with Invalid participantId
548 function testUpdateWithWrongParticipantId() {
553 'register_date' => '2006-01-21',
554 'source' => 'US Open',
555 'event_level' => 'Donation',
556 'version' => $this->_apiversion
,
558 $participant = $this->callAPIFailure('Participant', 'update', $params);
562 * check with Invalid ContactId
564 function testUpdateWithWrongContactId() {
565 $participantId = $this->participantCreate(array(
566 'contactID' => $this->_individualId
,
567 'eventID' => $this->_eventID
,
568 ), $this->_apiversion
);
570 'id' => $participantId,
571 'contact_id' => 12345,
574 'register_date' => '2006-01-21',
575 'source' => 'US Open',
576 'event_level' => 'Donation',
577 'version' => $this->_apiversion
,
579 $participant = $this->callAPIFailure('participant', 'create', $params);
580 $result = $this->participantDelete($participantId);
584 * check with complete array
586 function testUpdate() {
587 $participantId = $this->participantCreate(array('contactID' => $this->_individualId
, 'eventID' => $this->_eventID
, $this->_apiversion
));
589 'id' => $participantId,
590 'contact_id' => $this->_individualId
,
591 'event_id' => $this->_eventID
,
594 'register_date' => '2006-01-21',
595 'source' => 'US Open',
596 'event_level' => 'Donation',
597 'version' => $this->_apiversion
,
599 $participant = civicrm_api('participant', 'create', $params);
600 $this->assertNotEquals($participant['is_error'], 1);
603 if (!$participant['is_error']) {
604 $params['id'] = CRM_Utils_Array
::value('id', $participant);
606 // Create $match array with DAO Field Names and expected values
608 'id' => CRM_Utils_Array
::value('id', $participant),
610 // assertDBState compares expected values in $match to actual values in the DB
611 $this->assertDBState('CRM_Event_DAO_Participant', $participant['id'], $match);
613 // Cleanup created participant records.
614 $result = $this->participantDelete($params['id']);
619 ///////////////// civicrm_participant_delete methods
622 * Test civicrm_participant_delete with wrong params type
624 function testDeleteWrongParamsType() {
625 $params = 'a string';
626 $result = $this->callAPIFailure('participant', 'delete', $params);
630 * Test civicrm_participant_delete with empty params
632 function testDeleteEmptyParams() {
634 $result = $this->callAPIFailure('participant', 'delete', $params);
638 * check with participant_id
640 function testParticipantDelete() {
642 'id' => $this->_participantID
,
643 'version' => $this->_apiversion
,
645 $participant = civicrm_api('participant', 'delete', $params);
646 $this->assertAPISuccess($participant);
647 $this->assertDBState('CRM_Event_DAO_Participant', $this->_participantID
, NULL, TRUE);
651 * check without participant_id
653 * This should return an error because required param is missing..
655 function testParticipantDeleteMissingID() {
657 'event_id' => $this->_eventID
,
658 'version' => $this->_apiversion
,
660 $participant = $this->callAPIFailure('participant', 'delete', $params);
661 $this->assertNotNull($participant['error_message']);
664 * delete with a get - a 'criteria delete'
666 function testNestedDelete() {
667 $description = "Criteria delete by nesting a GET & a DELETE";
668 $subfile = "NestedDelete";
669 $participants = civicrm_api('Participant', 'Get', array('version' => 3));
670 $this->assertEquals($participants['count'], 3);
671 $params = array('version' => 3, 'contact_id' => $this->_contactID2
, 'api.participant.delete' => 1);
672 $participants = civicrm_api('Participant', 'Get', $params);
673 $this->documentMe($params, $participants, __FUNCTION__
, __FILE__
, $description, $subfile, 'Get');
674 $participants = civicrm_api('Participant', 'Get', array('version' => 3));
675 $this->assertEquals(1, $participants['count'], "only one participant should be left. line " . __LINE__
);
678 * Test creation of a participant with an associated contribution
680 function testCreateParticipantWithPayment() {
681 $this->_contributionTypeId
= $this->contributionTypeCreate();
682 $description = "single function to create contact w partipation & contribution. Note that in the
683 case of 'contribution' the 'create' is implied (api.contribution.create)";
684 $subfile = "CreateParticipantPayment";
686 'contact_type' => 'Individual',
687 'display_name' => 'dlobo',
688 'version' => $this->_apiversion
,
689 'api.participant' => array(
690 'event_id' => $this->_eventID
,
693 'format.only_id' => 1,
695 'api.contribution.create' => array(
696 'financial_type_id' => 1,
697 'total_amount' => 100,
698 'format.only_id' => 1,
700 'api.participant_payment.create' => array(
701 'contribution_id' => '$value.api.contribution.create',
702 'participant_id' => '$value.api.participant',
706 $result = civicrm_api('contact', 'create', $params);
707 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
, $description, $subfile);
708 $this->assertEquals(1, $result['values'][$result['id']]['api.participant_payment.create']['count']);
709 civicrm_api('contact', 'delete', array('id' => $result['id'], 'version' => $this->_apiversion
));
712 function testParticipantFormattedwithDuplicateParams() {
713 $participantContact = $this->individualCreate(NULL);
715 'contact_id' => $participantContact,
716 'event_id' => $this->_eventID
,
719 require_once 'CRM/Event/Import/Parser.php';
720 $onDuplicate = CRM_Import_Parser
::DUPLICATE_NOCHECK
;
721 $participant = _civicrm_api3_deprecated_create_participant_formatted($params, $onDuplicate);
722 $this->assertEquals($participant['is_error'], 0);
726 * Test civicrm_participant_formatted with wrong $onDuplicate
728 function testParticipantFormattedwithWrongDuplicateConstant() {
729 $participantContact = $this->individualCreate(NULL);
731 'contact_id' => $participantContact,
732 'event_id' => $this->_eventID
,
736 $participant = _civicrm_api3_deprecated_create_participant_formatted($params, $onDuplicate);
737 $this->assertEquals($participant['is_error'], 0);
740 function testParticipantcheckWithParams() {
741 $participantContact = $this->individualCreate(NULL);
743 'contact_id' => $participantContact,
744 'event_id' => $this->_eventID
,
746 require_once 'CRM/Event/Import/Parser.php';
747 $participant = _civicrm_api3_deprecated_participant_check_params($params);
748 $this->assertEquals($participant, TRUE, 'Check the returned True');
752 * check get with role id - create 2 registrations with different roles.
753 * Test that get without role var returns 2 & with returns one
754 TEST COMMENteD OUT AS HAVE GIVIEN UP ON using filters on get
755 function testGetParamsRole()
757 require_once 'CRM/Event/PseudoConstant.php';
758 CRM_Event_PseudoConstant::flush('participantRole');
759 $participantRole2 = civicrm_api('Participant', 'Create', array('version' => 3, 'id' => $this->_participantID2, 'participant_role_id' => 2));
763 'version' => $this->_apiversion,
766 $result = civicrm_api('participant','get', $params);
767 $this->assertAPISuccess($result);
768 $this->assertEquals($result['count'], 3);
770 $params['participant_role_id'] =2;
771 $result = civicrm_api('participant','get', $params);
773 $this->assertAPISuccess($result, "in line " . __LINE__);
774 $this->assertEquals(2,$result['count'], "in line " . __LINE__);
775 $this->documentMe($params,$result ,__FUNCTION__,__FILE__);