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;
54 $this->_apiversion
= 3;
56 $this->_entity
= 'participant';
57 $event = $this->eventCreate(NULL);
58 $this->_eventID
= $event['id'];
60 $this->_contactID
= $this->individualCreate();
62 $this->_createdParticipants
= array();
63 $this->_individualId
= $this->individualCreate();
65 $this->_participantID
= $this->participantCreate(array('contact_id' => $this->_contactID
, 'event_id' => $this->_eventID
));
66 $this->_contactID2
= $this->individualCreate();
67 $this->_participantID2
= $this->participantCreate(array('contact_id' => $this->_contactID2
, 'event_id' => $this->_eventID
));
68 $this->_participantID3
= $this->participantCreate(array('contact_id' => $this->_contactID2
, 'event_id' => $this->_eventID
));
69 $this->_params
= array(
70 'contact_id' => $this->_contactID
,
71 'event_id' => $this->_eventID
,
74 // to ensure it matches later on
75 'register_date' => '2007-07-21 00:00:00',
76 'source' => 'Online Event Registration: API Testing',
81 $this->eventDelete($this->_eventID
);
82 $tablesToTruncate = array(
83 'civicrm_custom_group', 'civicrm_custom_field', 'civicrm_contact', 'civicrm_participant'
85 // true tells quickCleanup to drop any tables that might have been created in the test
86 $this->quickCleanup($tablesToTruncate, TRUE);
90 * Check with complete array + custom field
91 * Note that the test is written on purpose without any
92 * variables specific to participant so it can be replicated into other entities
93 * and / or moved to the automated test suite
95 function testCreateWithCustom() {
96 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
98 $params = $this->_params
;
99 $params['custom_' . $ids['custom_field_id']] = "custom string";
101 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
);
103 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
105 $check = $this->callAPISuccess($this->_entity
, 'get', array('id' => $result['id']));
106 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
108 $this->customFieldDelete($ids['custom_field_id']);
109 $this->customGroupDelete($ids['custom_group_id']);
113 ///////////////// civicrm_participant_get methods
116 * Check with wrong params type
118 function testGetWrongParamsType() {
119 $params = 'a string';
120 $result = $this->callAPIFailure('participant', 'get', $params);
124 * Test civicrm_participant_get with empty params
126 function testGetEmptyParams() {
127 $this->callAPISuccess('participant', 'get', array());
131 * Check with participant_id
133 function testGetParticipantIdOnly() {
135 'participant_id' => $this->_participantID
,
139 'participant_register_date',
140 'participant_source',
143 $result = $this->callAPISuccess('participant', 'get', $params);
144 $this->assertAPISuccess($result, " in line " . __LINE__
);
145 $this->assertEquals($result['values'][$this->_participantID
]['event_id'], $this->_eventID
, "in line " . __LINE__
);
146 $this->assertEquals($result['values'][$this->_participantID
]['participant_register_date'], '2007-02-19 00:00:00', "in line " . __LINE__
);
147 $this->assertEquals($result['values'][$this->_participantID
]['participant_source'], 'Wimbeldon', "in line " . __LINE__
);
149 'id' => $this->_participantID
,
150 'return' => 'id,participant_register_date,event_id',
153 $result = $this->callAPISuccess('participant', 'get', $params);
154 $this->assertEquals($result['values'][$this->_participantID
]['event_id'], $this->_eventID
);
155 $this->assertEquals($result['values'][$this->_participantID
]['participant_register_date'], '2007-02-19 00:00:00');
160 * Check with params id
162 function testGetParamsAsIdOnly() {
164 'id' => $this->_participantID
,
166 $result = $this->callAPIAndDocument('participant', 'get', $params, __FUNCTION__
, __FILE__
);
167 $this->assertEquals($result['values'][$this->_participantID
]['event_id'], $this->_eventID
);
168 $this->assertEquals($result['values'][$this->_participantID
]['participant_register_date'], '2007-02-19 00:00:00');
169 $this->assertEquals($result['values'][$this->_participantID
]['participant_source'], 'Wimbeldon');
170 $this->assertEquals($result['id'], $result['values'][$this->_participantID
]['id']);
174 * Check with params id
176 function testGetNestedEventGet() {
177 //create a second event & add participant to it.
178 $event = $this->eventCreate(NULL);
179 $this->callAPISuccess('participant', 'create', array('event_id' => $event['id'], 'contact_id' => $this->_contactID
));
182 $description = "use nested get to get an event";
183 $subfile = "NestedEventGet";
185 'id' => $this->_participantID
,
186 'api.event.get' => 1,
188 $result = $this->callAPIAndDocument('participant', 'get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
189 $this->assertEquals($result['values'][$this->_participantID
]['event_id'], $this->_eventID
);
190 $this->assertEquals($result['values'][$this->_participantID
]['participant_register_date'], '2007-02-19 00:00:00');
191 $this->assertEquals($result['values'][$this->_participantID
]['participant_source'], 'Wimbeldon');
192 $this->assertEquals($this->_eventID
, $result['values'][$this->_participantID
]['api.event.get']['id']);
196 * Check Participant Get respects return properties
198 function testGetWithReturnProperties() {
200 'contact_id' => $this->_contactID
, 'return.status_id' => 1,
201 'return.participant_status_id' => 1,
202 'options' => array('limit' => 1)
204 $result = $this->callAPISuccess('participant', 'get', $params);
205 $this->assertArrayHasKey('participant_status_id', $result['values'][$result['id']]);
209 * Check with contact_id
211 function testGetContactIdOnly() {
213 'contact_id' => $this->_contactID
, );
214 $participant = $this->callAPISuccess('participant', 'get', $params);
216 $this->assertEquals($this->_participantID
, $participant['id'],
217 "In line " . __LINE__
219 $this->assertEquals($this->_eventID
, $participant['values'][$participant['id']]['event_id'],
220 "In line " . __LINE__
222 $this->assertEquals('2007-02-19 00:00:00', $participant['values'][$participant['id']]['participant_register_date'],
223 "In line " . __LINE__
225 $this->assertEquals('Wimbeldon', $participant['values'][$participant['id']]['participant_source'],
226 "In line " . __LINE__
228 $this->assertEquals($participant['id'], $participant['values'][$participant['id']]['id'],
229 "In line " . __LINE__
234 * Check with event_id
237 function testGetMultiMatchReturnFirst() {
239 'event_id' => $this->_eventID
,
242 $participant = $this->callAPISuccess('participant', 'get', $params);
243 $this->assertNotNull($participant['id']);
247 * Check with event_id
248 * in v3 this should return all participants
250 function testGetMultiMatchNoReturnFirst() {
252 'event_id' => $this->_eventID
,
254 $participant = $this->callAPISuccess('participant', 'get', $params);
255 $this->assertNotNull($participant['count'], 3);
258 ///////////////// civicrm_participant_get methods
261 * Test civicrm_participant_get with empty params
262 * In this case all the participant records are returned.
264 function testSearchEmptyParams() {
265 $result = $this->callAPISuccess('participant', 'get', array());
266 // expecting 3 participant records
267 $this->assertEquals($result['count'], 3);
271 * Check with participant_id
273 function testSearchParticipantIdOnly() {
275 'participant_id' => $this->_participantID
,
277 $participant = $this->callAPISuccess('participant', 'get', $params);
278 $this->assertEquals($participant['values'][$this->_participantID
]['event_id'], $this->_eventID
);
279 $this->assertEquals($participant['values'][$this->_participantID
]['participant_register_date'], '2007-02-19 00:00:00');
280 $this->assertEquals($participant['values'][$this->_participantID
]['participant_source'], 'Wimbeldon');
284 * Check with contact_id
286 function testSearchContactIdOnly() {
287 // Should get 2 participant records for this contact.
289 'contact_id' => $this->_contactID2
,
291 $participant = $this->callAPISuccess('participant', 'get', $params);
293 $this->assertEquals($participant['count'], 2);
297 * Check with event_id
299 function testSearchByEvent() {
300 // Should get >= 3 participant records for this event. Also testing that last_name and event_title are returned.
302 'event_id' => $this->_eventID
,
303 'return.last_name' => 1,
304 'return.event_title' => 1,
306 $participant = $this->callAPISuccess('participant', 'get', $params);
307 if ($participant['count'] < 3) {
308 $this->fail("Event search returned less than expected miniumum of 3 records.");
311 $this->assertEquals($participant['values'][$this->_participantID
]['last_name'], 'Anderson');
312 $this->assertEquals($participant['values'][$this->_participantID
]['event_title'], 'Annual CiviCRM meet');
316 * Check with event_id
319 function testSearchByEventWithLimit() {
320 // Should 2 participant records since we're passing rowCount = 2.
322 'event_id' => $this->_eventID
,
325 $participant = $this->callAPISuccess('participant', 'get', $params);
327 $this->assertEquals($participant['count'], 2, 'in line ' . __LINE__
);
330 ///////////////// civicrm_participant_create methods
333 * Test civicrm_participant_create with empty params
335 function testCreateEmptyParams() {
337 $result = $this->callAPIFailure('participant', 'create', $params);
341 * Check with event_id
343 function testCreateMissingContactID() {
345 'event_id' => $this->_eventID
,
347 $participant = $this->callAPIFailure('participant', 'create', $params);
351 * Check with contact_id
354 function testCreateMissingEventID() {
356 'contact_id' => $this->_contactID
,
358 $participant = $this->callAPIFailure('participant', 'create', $params);
362 * Check with contact_id & event_id
364 function testCreateEventIdOnly() {
366 'contact_id' => $this->_contactID
,
367 'event_id' => $this->_eventID
,
369 $participant = $this->callAPISuccess('participant', 'create', $params);
370 $this->getAndCheck($params, $participant['id'], 'participant');
374 * Check with complete array
376 function testCreateAllParams() {
377 $params = $this->_params
;
379 $participant = $this->callAPISuccess('participant', 'create', $params);
380 $this->_participantID
= $participant['id'];
381 // assertDBState compares expected values in $match to actual values in the DB
382 $this->assertDBState('CRM_Event_DAO_Participant', $participant['id'], $params);
386 * Test to check if receive date is being changed per CRM-9763
388 function testCreateUpdateReceiveDate() {
389 $participant = $this->callAPISuccess('participant', 'create', $this->_params
);
391 'id' => $participant['id'],
394 $this->callAPISuccess('participant', 'create', $update);
395 $this->getAndCheck(array_merge($this->_params
, $update), $participant['id'], 'participant');
399 * Test to check if participant fee level is being changed per CRM-9781
401 function testCreateUpdateParticipantFeeLevel() {
402 $myParams = $this->_params +
array('participant_fee_level' => CRM_Core_DAO
::VALUE_SEPARATOR
. "fee" . CRM_Core_DAO
::VALUE_SEPARATOR
);
403 $participant = $this->callAPISuccess('participant', 'create', $myParams);
405 'id' => $participant['id'],
408 $update = $this->callAPISuccess('participant', 'create', $update);
410 $this->assertEquals($participant['values'][$participant['id']]['fee_level'],
411 $update['values'][$participant['id']]['fee_level']
414 $this->callAPISuccess('participant', 'delete', array('id' => $participant['id']));
418 * Test the line items for participant fee with multiple price field values.
420 function testCreateParticipantLineItems() {
421 // Create a price set for this event.
423 $priceset = $this->callAPISuccess('PriceSet', 'create', array(
424 'name' => 'my_price_set',
425 'title' => 'My Price Set',
428 'financial_type_id' => 4,
429 // 'entity' => array('civicrm_event' => array($this->_eventID)),
432 // Add the price set to the event with another API call.
433 // I tried to do this at once, but it did not work.
435 $priceset = $this->callAPISuccess('PriceSet', 'create', array(
436 'entity_table' => 'civicrm_event',
437 'entity_id' => $this->_eventID
,
438 'id' => $priceset['id'],
441 $pricefield = $this->callAPISuccess('PriceField', 'create', array(
442 'price_set_id' => $priceset['id'],
443 'name' => 'mypricefield',
444 'label' => 'My Price Field',
445 'html_type' => 'Text',
447 'is_display_amounts' => 1,
451 $pfv1 = $this->callAPISuccess('PriceFieldValue', 'create', array(
452 'price_field_id' => $pricefield['id'],
453 'name' => 'pricefieldvalue1',
454 'label' => 'pricefieldvalue1',
457 'financial_type_id' => 4,
460 $pfv2 = $this->callAPISuccess('PriceFieldValue', 'create', array(
461 'price_field_id' => $pricefield['id'],
462 'name' => 'pricefieldvalue2',
463 'label' => 'pricefieldvalue2',
466 'financial_type_id' => 4,
469 // pay 2 times price field value 1, and 2 times price field value 2.
470 $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
);
471 $participant = $this->callAPISuccess('participant', 'create', $myParams);
473 // expect 2 line items.
474 $lineItems = $this->callAPISuccess('LineItem', 'get', array(
475 'entity_id' => $participant['id'],
476 'entity_table' => 'civicrm_participant',
479 $this->assertEquals(2, $lineItems['count']);
481 // Check quantity, label and unit price of lines.
482 // TODO: These assertions depend on the order of the line items, which is
483 // technically incorrect.
485 $lineItem = array_pop($lineItems['values']);
486 $this->assertEquals(2, $lineItem['qty']);
487 $this->assertEquals(5, $lineItem['unit_price']);
488 $this->assertEquals('pricefieldvalue2', $lineItem['label']);
490 $lineItem = array_pop($lineItems['values']);
491 $this->assertEquals(2, $lineItem['qty']);
492 $this->assertEquals(20, $lineItem['unit_price']);
493 $this->assertEquals('pricefieldvalue1', $lineItem['label']);
496 $this->callAPISuccess('participant', 'delete', array('id' => $participant['id']));
498 // TODO: I think the price set should be removed, but I don't know how
499 // to decouple it properly from the event. For the moment, I'll just comment
500 // out the lines below.
503 $this->callAPISuccess('PriceFieldValue', 'delete', array('id' => $pfv1['id']));
504 $this->callAPISuccess('PriceFieldValue', 'delete', array('id' => $pfv2['id']));
505 $this->callAPISuccess('PriceField', 'delete', array('id' => $pricefield['id']));
506 $this->callAPISuccess('PriceSet', 'delete', array('id' => $priceset['id']));
511 * Check with complete array
513 function testUpdate() {
514 $participantId = $this->participantCreate(array(
515 'contactID' => $this->_individualId
,
516 'eventID' => $this->_eventID
519 'id' => $participantId,
520 'contact_id' => $this->_individualId
,
521 'event_id' => $this->_eventID
,
524 'register_date' => '2006-01-21',
525 'source' => 'US Open',
527 $participant = $this->callAPISuccess('participant', 'create', $params);
528 $this->getAndCheck($params, $participant['id'], 'participant');
529 $result = $this->participantDelete($params['id']);
533 * Test to check if participant fee level is being changed per CRM-9781
534 * Try again without a custom separater to check that one isn't added
535 * (get & check won't accept an array)
537 function testUpdateCreateParticipantFeeLevelNoSeparator() {
539 $myParams = $this->_params +
array('participant_fee_level' => "fee");
540 $participant = $this->callAPISuccess('participant', 'create', $myParams);
541 $this->assertAPISuccess($participant);
543 'id' => $participant['id'],
546 $this->callAPISuccess('participant', 'create', $update);
547 $this->assertEquals($participant['values'][$participant['id']]['fee_level'],
548 $myParams['participant_fee_level']
550 $this->getAndCheck($update, $participant['id'], 'participant');
552 ///////////////// civicrm_participant_update methods
555 * Test civicrm_participant_update with wrong params type
557 function testUpdateWrongParamsType() {
558 $params = 'a string';
559 $result = $this->callAPIFailure('participant', 'create', $params);
560 $this->assertEquals('Input variable `params` is not an array', $result['error_message'], 'In line ' . __LINE__
);
564 * Check with empty array
566 function testUpdateEmptyParams() {
568 $participant = $this->callAPIFailure('participant', 'create', $params);
569 $this->assertEquals($participant['error_message'], 'Mandatory key(s) missing from params array: event_id, contact_id');
573 * Check without event_id
575 function testUpdateWithoutEventId() {
576 $participantId = $this->participantCreate(array('contactID' => $this->_individualId
, 'eventID' => $this->_eventID
));
578 'contact_id' => $this->_individualId
,
581 'register_date' => '2006-01-21',
582 'source' => 'US Open',
583 'event_level' => 'Donation', );
584 $participant = $this->callAPIFailure('participant', 'create', $params);
585 $this->assertEquals($participant['error_message'], 'Mandatory key(s) missing from params array: event_id');
586 // Cleanup created participant records.
587 $result = $this->participantDelete($participantId);
591 * Check with Invalid participantId
593 function testUpdateWithWrongParticipantId() {
598 'register_date' => '2006-01-21',
599 'source' => 'US Open',
600 'event_level' => 'Donation', );
601 $participant = $this->callAPIFailure('Participant', 'update', $params);
605 * Check with Invalid ContactId
607 function testUpdateWithWrongContactId() {
608 $participantId = $this->participantCreate(array(
609 'contactID' => $this->_individualId
,
610 'eventID' => $this->_eventID
,
611 ), $this->_apiversion
);
613 'id' => $participantId,
614 'contact_id' => 12345,
617 'register_date' => '2006-01-21',
618 'source' => 'US Open',
619 'event_level' => 'Donation', );
620 $participant = $this->callAPIFailure('participant', 'create', $params);
621 $result = $this->participantDelete($participantId);
624 ///////////////// civicrm_participant_delete methods
627 * Test civicrm_participant_delete with wrong params type
629 function testDeleteWrongParamsType() {
630 $params = 'a string';
631 $result = $this->callAPIFailure('participant', 'delete', $params);
635 * Test civicrm_participant_delete with empty params
637 function testDeleteEmptyParams() {
639 $result = $this->callAPIFailure('participant', 'delete', $params);
643 * Check with participant_id
645 function testParticipantDelete() {
647 'id' => $this->_participantID
, );
648 $participant = $this->callAPISuccess('participant', 'delete', $params);
649 $this->assertAPISuccess($participant);
650 $this->assertDBState('CRM_Event_DAO_Participant', $this->_participantID
, NULL, TRUE);
654 * Check without participant_id
656 * This should return an error because required param is missing..
658 function testParticipantDeleteMissingID() {
660 'event_id' => $this->_eventID
, );
661 $participant = $this->callAPIFailure('participant', 'delete', $params);
662 $this->assertNotNull($participant['error_message']);
666 * Delete with a get - a 'criteria delete'
668 function testNestedDelete() {
669 $description = "Criteria delete by nesting a GET & a DELETE";
670 $subfile = "NestedDelete";
671 $participants = $this->callAPISuccess('Participant', 'Get', array());
672 $this->assertEquals($participants['count'], 3);
673 $params = array('contact_id' => $this->_contactID2
, 'api.participant.delete' => 1);
674 $participants = $this->callAPIAndDocument('Participant', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile, 'Get');
675 $check = $this->callAPISuccess('participant', 'getcount', array());
676 $this->assertEquals(1, $check,"only one participant should be left. line " . __LINE__
);
680 * Test creation of a participant with an associated contribution
682 function testCreateParticipantWithPayment() {
683 $description = "single function to create contact w partipation & contribution. Note that in the
684 case of 'contribution' the 'create' is implied (api.contribution.create)";
685 $subfile = "CreateParticipantPayment";
687 'contact_type' => 'Individual',
688 'display_name' => 'dlobo',
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 = $this->callAPIAndDocument('contact', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
707 $this->assertEquals(1, $result['values'][$result['id']]['api.participant_payment.create']['count']);
708 $this->callAPISuccess('contact', 'delete', array('id' => $result['id']));