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']));
426 * Test the line items for participant fee with multiple price field values.
428 function testCreateParticipantLineItems() {
429 // Create a price set for this event.
431 $priceset = $this->callAPISuccess('PriceSet', 'create', array(
432 'name' => 'my_price_set',
433 'title' => 'My Price Set',
436 'financial_type_id' => 4,
437 // 'entity' => array('civicrm_event' => array($this->_eventID)),
440 // Add the price set to the event with another API call.
441 // I tried to do this at once, but it did not work.
443 $priceset = $this->callAPISuccess('PriceSet', 'create', array(
444 'entity_table' => 'civicrm_event',
445 'entity_id' => $this->_eventID
,
446 'id' => $priceset['id'],
449 $pricefield = $this->callAPISuccess('PriceField', 'create', array(
450 'price_set_id' => $priceset['id'],
451 'name' => 'mypricefield',
452 'label' => 'My Price Field',
453 'html_type' => 'Text',
455 'is_display_amounts' => 1,
459 $pfv1 = $this->callAPISuccess('PriceFieldValue', 'create', array(
460 'price_field_id' => $pricefield['id'],
461 'name' => 'pricefieldvalue1',
462 'label' => 'pricefieldvalue1',
465 'financial_type_id' => 4,
468 $pfv2 = $this->callAPISuccess('PriceFieldValue', 'create', array(
469 'price_field_id' => $pricefield['id'],
470 'name' => 'pricefieldvalue2',
471 'label' => 'pricefieldvalue2',
474 'financial_type_id' => 4,
477 // pay 2 times price field value 1, and 2 times price field value 2.
478 $myParams = $this->_params +
array('participant_fee_level' => CRM_Core_DAO
::VALUE_SEPARATOR
. "pricefieldvalue1 - 2" . CRM_Core_DAO
::VALUE_SEPARATOR
. "pricefieldvalue2 - 2" . CRM_Core_DAO
::VALUE_SEPARATOR
);
479 $participant = $this->callAPISuccess('participant', 'create', $myParams);
481 // expect 2 line items.
482 $lineItems = $this->callAPISuccess('LineItem', 'get', array(
483 'entity_id' => $participant['id'],
484 'entity_table' => 'civicrm_participant',
487 $this->assertEquals(2, $lineItems['count']);
489 // Check quantity, label and unit price of lines.
490 // TODO: These assertions depend on the order of the line items, which is
491 // technically incorrect.
493 $lineItem = array_pop($lineItems['values']);
494 $this->assertEquals(2, $lineItem['qty']);
495 $this->assertEquals(5, $lineItem['unit_price']);
496 $this->assertEquals('pricefieldvalue2', $lineItem['label']);
498 $lineItem = array_pop($lineItems['values']);
499 $this->assertEquals(2, $lineItem['qty']);
500 $this->assertEquals(20, $lineItem['unit_price']);
501 $this->assertEquals('pricefieldvalue1', $lineItem['label']);
504 $this->callAPISuccess('participant', 'delete', array('id' => $participant['id']));
506 // TODO: I think the price set should be removed, but I don't know how
507 // to decouple it properly from the event. For the moment, I'll just comment
508 // out the lines below.
511 $this->callAPISuccess('PriceFieldValue', 'delete', array('id' => $pfv1['id']));
512 $this->callAPISuccess('PriceFieldValue', 'delete', array('id' => $pfv2['id']));
513 $this->callAPISuccess('PriceField', 'delete', array('id' => $pricefield['id']));
514 $this->callAPISuccess('PriceSet', 'delete', array('id' => $priceset['id']));
519 * check with complete array
521 function testUpdate() {
522 $participantId = $this->participantCreate(array(
523 'contactID' => $this->_individualId
,
524 'eventID' => $this->_eventID
527 'id' => $participantId,
528 'contact_id' => $this->_individualId
,
529 'event_id' => $this->_eventID
,
532 'register_date' => '2006-01-21',
533 'source' => 'US Open',
535 $participant = $this->callAPISuccess('participant', 'create', $params);
536 $this->getAndCheck($params, $participant['id'], 'participant');
537 $result = $this->participantDelete($params['id']);
541 * Test to check if participant fee level is being changed per CRM-9781
542 * Try again without a custom separater to check that one isn't added
543 * (get & check won't accept an array)
545 function testUpdateCreateParticipantFeeLevelNoSeparator() {
547 $myParams = $this->_params +
array('participant_fee_level' => "fee");
548 $participant = $this->callAPISuccess('participant', 'create', $myParams);
549 $this->assertAPISuccess($participant);
551 'id' => $participant['id'],
554 $this->callAPISuccess('participant', 'create', $update);
555 $this->assertEquals($participant['values'][$participant['id']]['fee_level'],
556 $myParams['participant_fee_level']
558 $this->getAndCheck($update, $participant['id'], 'participant');
560 ///////////////// civicrm_participant_update methods
563 * Test civicrm_participant_update with wrong params type
565 function testUpdateWrongParamsType() {
566 $params = 'a string';
567 $result = $this->callAPIFailure('participant', 'create', $params);
568 $this->assertEquals('Input variable `params` is not an array', $result['error_message'], 'In line ' . __LINE__
);
572 * check with empty array
574 function testUpdateEmptyParams() {
576 $participant = $this->callAPIFailure('participant', 'create', $params);
577 $this->assertEquals($participant['error_message'], 'Mandatory key(s) missing from params array: event_id, contact_id');
581 * check without event_id
583 function testUpdateWithoutEventId() {
584 $participantId = $this->participantCreate(array('contactID' => $this->_individualId
, 'eventID' => $this->_eventID
));
586 'contact_id' => $this->_individualId
,
589 'register_date' => '2006-01-21',
590 'source' => 'US Open',
591 'event_level' => 'Donation', );
592 $participant = $this->callAPIFailure('participant', 'create', $params);
593 $this->assertEquals($participant['error_message'], 'Mandatory key(s) missing from params array: event_id');
594 // Cleanup created participant records.
595 $result = $this->participantDelete($participantId);
599 * check with Invalid participantId
601 function testUpdateWithWrongParticipantId() {
606 'register_date' => '2006-01-21',
607 'source' => 'US Open',
608 'event_level' => 'Donation', );
609 $participant = $this->callAPIFailure('Participant', 'update', $params);
613 * check with Invalid ContactId
615 function testUpdateWithWrongContactId() {
616 $participantId = $this->participantCreate(array(
617 'contactID' => $this->_individualId
,
618 'eventID' => $this->_eventID
,
619 ), $this->_apiversion
);
621 'id' => $participantId,
622 'contact_id' => 12345,
625 'register_date' => '2006-01-21',
626 'source' => 'US Open',
627 'event_level' => 'Donation', );
628 $participant = $this->callAPIFailure('participant', 'create', $params);
629 $result = $this->participantDelete($participantId);
632 ///////////////// civicrm_participant_delete methods
635 * Test civicrm_participant_delete with wrong params type
637 function testDeleteWrongParamsType() {
638 $params = 'a string';
639 $result = $this->callAPIFailure('participant', 'delete', $params);
643 * Test civicrm_participant_delete with empty params
645 function testDeleteEmptyParams() {
647 $result = $this->callAPIFailure('participant', 'delete', $params);
651 * check with participant_id
653 function testParticipantDelete() {
655 'id' => $this->_participantID
, );
656 $participant = $this->callAPISuccess('participant', 'delete', $params);
657 $this->assertAPISuccess($participant);
658 $this->assertDBState('CRM_Event_DAO_Participant', $this->_participantID
, NULL, TRUE);
662 * check without participant_id
664 * This should return an error because required param is missing..
666 function testParticipantDeleteMissingID() {
668 'event_id' => $this->_eventID
, );
669 $participant = $this->callAPIFailure('participant', 'delete', $params);
670 $this->assertNotNull($participant['error_message']);
673 * delete with a get - a 'criteria delete'
675 function testNestedDelete() {
676 $description = "Criteria delete by nesting a GET & a DELETE";
677 $subfile = "NestedDelete";
678 $participants = $this->callAPISuccess('Participant', 'Get', array());
679 $this->assertEquals($participants['count'], 3);
680 $params = array('contact_id' => $this->_contactID2
, 'api.participant.delete' => 1);
681 $participants = $this->callAPIAndDocument('Participant', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile, 'Get');
682 $check = $this->callAPISuccess('participant', 'getcount', array());
683 $this->assertEquals(1, $check,"only one participant should be left. line " . __LINE__
);
686 * Test creation of a participant with an associated contribution
688 function testCreateParticipantWithPayment() {
689 $description = "single function to create contact w partipation & contribution. Note that in the
690 case of 'contribution' the 'create' is implied (api.contribution.create)";
691 $subfile = "CreateParticipantPayment";
693 'contact_type' => 'Individual',
694 'display_name' => 'dlobo',
695 'api.participant' => array(
696 'event_id' => $this->_eventID
,
699 'format.only_id' => 1,
701 'api.contribution.create' => array(
702 'financial_type_id' => 1,
703 'total_amount' => 100,
704 'format.only_id' => 1,
706 'api.participant_payment.create' => array(
707 'contribution_id' => '$value.api.contribution.create',
708 'participant_id' => '$value.api.participant',
712 $result = $this->callAPIAndDocument('contact', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
713 $this->assertEquals(1, $result['values'][$result['id']]['api.participant_payment.create']['count']);
714 $this->callAPISuccess('contact', 'delete', array('id' => $result['id']));