3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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 +--------------------------------------------------------------------+
29 * Test class for Batch API - civicrm_participant_*
31 * @package CiviCRM_APIv3
33 require_once 'CRM/Utils/DeprecatedUtils.php';
37 * Class api_v3_ParticipantTest
40 class api_v3_ParticipantTest
extends CiviUnitTestCase
{
42 protected $_apiversion;
44 protected $_contactID;
45 protected $_contactID2;
46 protected $_createdParticipants;
47 protected $_participantID;
49 protected $_individualId;
52 public function setUp() {
53 $this->_apiversion
= 3;
55 $this->_entity
= 'participant';
56 $event = $this->eventCreate(NULL);
57 $this->_eventID
= $event['id'];
59 $this->_contactID
= $this->individualCreate();
61 $this->_createdParticipants
= array();
62 $this->_individualId
= $this->individualCreate();
64 $this->_participantID
= $this->participantCreate(array(
65 'contact_id' => $this->_contactID
,
66 'event_id' => $this->_eventID
,
68 $this->_contactID2
= $this->individualCreate();
69 $this->_participantID2
= $this->participantCreate(array(
70 'contact_id' => $this->_contactID2
,
71 'event_id' => $this->_eventID
,
73 $this->_participantID3
= $this->participantCreate(array(
74 'contact_id' => $this->_contactID2
,
75 'event_id' => $this->_eventID
,
77 $this->_params
= array(
78 'contact_id' => $this->_contactID
,
79 'event_id' => $this->_eventID
,
82 // to ensure it matches later on
83 'register_date' => '2007-07-21 00:00:00',
84 'source' => 'Online Event Registration: API Testing',
88 public function tearDown() {
89 $this->eventDelete($this->_eventID
);
90 $tablesToTruncate = array(
91 'civicrm_custom_group',
92 'civicrm_custom_field',
94 '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 public 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 public function testGetWrongParamsType() {
130 $params = 'a string';
131 $result = $this->callAPIFailure('participant', 'get', $params);
135 * Test civicrm_participant_get with empty params.
137 public function testGetEmptyParams() {
138 $this->callAPISuccess('participant', 'get', array());
142 * Check with participant_id.
144 public 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 public 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 public function testGetNestedEventGet() {
188 //create a second event & add participant to it.
189 $event = $this->eventCreate(NULL);
190 $this->callAPISuccess('participant', 'create', array(
191 'event_id' => $event['id'],
192 'contact_id' => $this->_contactID
,
195 $description = "Demonstrates use of nested get to fetch event data with participant records.";
196 $subfile = "NestedEventGet";
198 'id' => $this->_participantID
,
199 'api.event.get' => 1,
201 $result = $this->callAPIAndDocument('participant', 'get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
202 $this->assertEquals($result['values'][$this->_participantID
]['event_id'], $this->_eventID
);
203 $this->assertEquals($result['values'][$this->_participantID
]['participant_register_date'], '2007-02-19 00:00:00');
204 $this->assertEquals($result['values'][$this->_participantID
]['participant_source'], 'Wimbeldon');
205 $this->assertEquals($this->_eventID
, $result['values'][$this->_participantID
]['api.event.get']['id']);
209 * Check Participant Get respects return properties.
211 public function testGetWithReturnProperties() {
213 'contact_id' => $this->_contactID
,
214 'return.status_id' => 1,
215 'return.participant_status_id' => 1,
216 'options' => array('limit' => 1),
218 $result = $this->callAPISuccess('participant', 'get', $params);
219 $this->assertArrayHasKey('participant_status_id', $result['values'][$result['id']]);
223 * Check with contact_id.
225 public function testGetContactIdOnly() {
227 'contact_id' => $this->_contactID
,
229 $participant = $this->callAPISuccess('participant', 'get', $params);
231 $this->assertEquals($this->_participantID
, $participant['id'],
232 "In line " . __LINE__
234 $this->assertEquals($this->_eventID
, $participant['values'][$participant['id']]['event_id'],
235 "In line " . __LINE__
237 $this->assertEquals('2007-02-19 00:00:00', $participant['values'][$participant['id']]['participant_register_date'],
238 "In line " . __LINE__
240 $this->assertEquals('Wimbeldon', $participant['values'][$participant['id']]['participant_source'],
241 "In line " . __LINE__
243 $this->assertEquals($participant['id'], $participant['values'][$participant['id']]['id'],
244 "In line " . __LINE__
249 * Check with event_id.
252 public function testGetMultiMatchReturnFirst() {
254 'event_id' => $this->_eventID
,
258 $participant = $this->callAPISuccess('participant', 'get', $params);
259 $this->assertNotNull($participant['id']);
263 * Check with event_id.
264 * in v3 this should return all participants
266 public function testGetMultiMatchNoReturnFirst() {
268 'event_id' => $this->_eventID
,
270 $participant = $this->callAPISuccess('participant', 'get', $params);
271 $this->assertNotNull($participant['count'], 3);
274 ///////////////// civicrm_participant_get methods
277 * Test civicrm_participant_get with empty params.
278 * In this case all the participant records are returned.
280 public function testSearchEmptyParams() {
281 $result = $this->callAPISuccess('participant', 'get', array());
282 // expecting 3 participant records
283 $this->assertEquals($result['count'], 3);
287 * Check with participant_id.
289 public function testSearchParticipantIdOnly() {
291 'participant_id' => $this->_participantID
,
293 $participant = $this->callAPISuccess('participant', 'get', $params);
294 $this->assertEquals($participant['values'][$this->_participantID
]['event_id'], $this->_eventID
);
295 $this->assertEquals($participant['values'][$this->_participantID
]['participant_register_date'], '2007-02-19 00:00:00');
296 $this->assertEquals($participant['values'][$this->_participantID
]['participant_source'], 'Wimbeldon');
300 * Check with contact_id.
302 public function testSearchContactIdOnly() {
303 // Should get 2 participant records for this contact.
305 'contact_id' => $this->_contactID2
,
307 $participant = $this->callAPISuccess('participant', 'get', $params);
309 $this->assertEquals($participant['count'], 2);
313 * Check with event_id.
315 public function testSearchByEvent() {
316 // Should get >= 3 participant records for this event. Also testing that last_name and event_title are returned.
318 'event_id' => $this->_eventID
,
319 'return.last_name' => 1,
320 'return.event_title' => 1,
322 $participant = $this->callAPISuccess('participant', 'get', $params);
323 if ($participant['count'] < 3) {
324 $this->fail("Event search returned less than expected miniumum of 3 records.");
327 $this->assertEquals($participant['values'][$this->_participantID
]['last_name'], 'Anderson');
328 $this->assertEquals($participant['values'][$this->_participantID
]['event_title'], 'Annual CiviCRM meet');
332 * Check with event_id.
335 public function testSearchByEventWithLimit() {
336 // Should 2 participant records since we're passing rowCount = 2.
338 'event_id' => $this->_eventID
,
341 $participant = $this->callAPISuccess('participant', 'get', $params);
343 $this->assertEquals($participant['count'], 2);
346 ///////////////// civicrm_participant_create methods
349 * Test civicrm_participant_create with empty params.
351 public function testCreateEmptyParams() {
353 $result = $this->callAPIFailure('participant', 'create', $params);
357 * Check with event_id.
359 public function testCreateMissingContactID() {
361 'event_id' => $this->_eventID
,
363 $participant = $this->callAPIFailure('participant', 'create', $params);
367 * Check with contact_id.
370 public function testCreateMissingEventID() {
372 'contact_id' => $this->_contactID
,
374 $participant = $this->callAPIFailure('participant', 'create', $params);
378 * Check with contact_id & event_id
380 public function testCreateEventIdOnly() {
382 'contact_id' => $this->_contactID
,
383 'event_id' => $this->_eventID
,
385 $participant = $this->callAPISuccess('participant', 'create', $params);
386 $this->getAndCheck($params, $participant['id'], 'participant');
390 * Check with complete array.
392 public function testCreateAllParams() {
393 $params = $this->_params
;
395 $participant = $this->callAPISuccess('participant', 'create', $params);
396 $this->_participantID
= $participant['id'];
397 // assertDBState compares expected values in $match to actual values in the DB
398 $this->assertDBState('CRM_Event_DAO_Participant', $participant['id'], $params);
402 * Test to check if receive date is being changed per CRM-9763
404 public function testCreateUpdateReceiveDate() {
405 $participant = $this->callAPISuccess('participant', 'create', $this->_params
);
407 'id' => $participant['id'],
410 $this->callAPISuccess('participant', 'create', $update);
411 $this->getAndCheck(array_merge($this->_params
, $update), $participant['id'], 'participant');
415 * Test to check if participant fee level is being changed per CRM-9781
417 public function testCreateUpdateParticipantFeeLevel() {
418 $myParams = $this->_params +
array('participant_fee_level' => CRM_Core_DAO
::VALUE_SEPARATOR
. "fee" . CRM_Core_DAO
::VALUE_SEPARATOR
);
419 $participant = $this->callAPISuccess('participant', 'create', $myParams);
421 'id' => $participant['id'],
424 $update = $this->callAPISuccess('participant', 'create', $update);
426 $this->assertEquals($participant['values'][$participant['id']]['fee_level'],
427 $update['values'][$participant['id']]['fee_level']
430 $this->callAPISuccess('participant', 'delete', array('id' => $participant['id']));
434 * Test the line items for participant fee with multiple price field values.
436 public function testCreateParticipantLineItems() {
437 // Create a price set for this event.
439 $priceset = $this->callAPISuccess('PriceSet', 'create', array(
440 'name' => 'my_price_set',
441 'title' => 'My Price Set',
444 'financial_type_id' => 4,
445 // 'entity' => array('civicrm_event' => array($this->_eventID)),
448 // Add the price set to the event with another API call.
449 // I tried to do this at once, but it did not work.
451 $priceset = $this->callAPISuccess('PriceSet', 'create', array(
452 'entity_table' => 'civicrm_event',
453 'entity_id' => $this->_eventID
,
454 'id' => $priceset['id'],
457 $pricefield = $this->callAPISuccess('PriceField', 'create', array(
458 'price_set_id' => $priceset['id'],
459 'name' => 'mypricefield',
460 'label' => 'My Price Field',
461 'html_type' => 'Text',
463 'is_display_amounts' => 1,
467 $pfv1 = $this->callAPISuccess('PriceFieldValue', 'create', array(
468 'price_field_id' => $pricefield['id'],
469 'name' => 'pricefieldvalue1',
470 'label' => 'pricefieldvalue1',
473 'financial_type_id' => 4,
476 $pfv2 = $this->callAPISuccess('PriceFieldValue', 'create', array(
477 'price_field_id' => $pricefield['id'],
478 'name' => 'pricefieldvalue2',
479 'label' => 'pricefieldvalue2',
482 'financial_type_id' => 4,
485 // pay 2 times price field value 1, and 2 times price field value 2.
486 $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
);
487 $participant = $this->callAPISuccess('participant', 'create', $myParams);
489 // expect 2 line items.
490 $lineItems = $this->callAPISuccess('LineItem', 'get', array(
491 'entity_id' => $participant['id'],
492 'entity_table' => 'civicrm_participant',
495 $this->assertEquals(2, $lineItems['count']);
497 // Check quantity, label and unit price of lines.
498 // TODO: These assertions depend on the order of the line items, which is
499 // technically incorrect.
501 $lineItem = array_pop($lineItems['values']);
502 $this->assertEquals(2, $lineItem['qty']);
503 $this->assertEquals(5, $lineItem['unit_price']);
504 $this->assertEquals('pricefieldvalue2', $lineItem['label']);
506 $lineItem = array_pop($lineItems['values']);
507 $this->assertEquals(2, $lineItem['qty']);
508 $this->assertEquals(20, $lineItem['unit_price']);
509 $this->assertEquals('pricefieldvalue1', $lineItem['label']);
512 $this->callAPISuccess('participant', 'delete', array('id' => $participant['id']));
514 // TODO: I think the price set should be removed, but I don't know how
515 // to decouple it properly from the event. For the moment, I'll just comment
516 // out the lines below.
519 $this->callAPISuccess('PriceFieldValue', 'delete', array('id' => $pfv1['id']));
520 $this->callAPISuccess('PriceFieldValue', 'delete', array('id' => $pfv2['id']));
521 $this->callAPISuccess('PriceField', 'delete', array('id' => $pricefield['id']));
522 $this->callAPISuccess('PriceSet', 'delete', array('id' => $priceset['id']));
527 * Check with complete array.
529 public function testUpdate() {
530 $participantId = $this->participantCreate(array(
531 'contactID' => $this->_individualId
,
532 'eventID' => $this->_eventID
,
535 'id' => $participantId,
536 'contact_id' => $this->_individualId
,
537 'event_id' => $this->_eventID
,
540 'register_date' => '2006-01-21',
541 'source' => 'US Open',
543 $participant = $this->callAPISuccess('participant', 'create', $params);
544 $this->getAndCheck($params, $participant['id'], 'participant');
545 $result = $this->participantDelete($params['id']);
549 * Test to check if participant fee level is being changed per CRM-9781
550 * Try again without a custom separater to check that one isn't added
551 * (get & check won't accept an array)
553 public function testUpdateCreateParticipantFeeLevelNoSeparator() {
555 $myParams = $this->_params +
array('participant_fee_level' => "fee");
556 $participant = $this->callAPISuccess('participant', 'create', $myParams);
557 $this->assertAPISuccess($participant);
559 'id' => $participant['id'],
562 $this->callAPISuccess('participant', 'create', $update);
563 $this->assertEquals($participant['values'][$participant['id']]['fee_level'],
564 $myParams['participant_fee_level']
566 $this->getAndCheck($update, $participant['id'], 'participant');
568 ///////////////// civicrm_participant_update methods
571 * Test civicrm_participant_update with wrong params type.
573 public function testUpdateWrongParamsType() {
574 $params = 'a string';
575 $result = $this->callAPIFailure('participant', 'create', $params);
576 $this->assertEquals('Input variable `params` is not an array', $result['error_message']);
580 * Check with empty array.
582 public function testUpdateEmptyParams() {
584 $participant = $this->callAPIFailure('participant', 'create', $params);
585 $this->assertEquals($participant['error_message'], 'Mandatory key(s) missing from params array: event_id, contact_id');
589 * Check without event_id.
591 public function testUpdateWithoutEventId() {
592 $participantId = $this->participantCreate(array('contactID' => $this->_individualId
, 'eventID' => $this->_eventID
));
594 'contact_id' => $this->_individualId
,
597 'register_date' => '2006-01-21',
598 'source' => 'US Open',
599 'event_level' => 'Donation',
601 $participant = $this->callAPIFailure('participant', 'create', $params);
602 $this->assertEquals($participant['error_message'], 'Mandatory key(s) missing from params array: event_id');
603 // Cleanup created participant records.
604 $result = $this->participantDelete($participantId);
608 * Check with Invalid participantId.
610 public function testUpdateWithWrongParticipantId() {
615 'register_date' => '2006-01-21',
616 'source' => 'US Open',
617 'event_level' => 'Donation',
619 $participant = $this->callAPIFailure('Participant', 'update', $params);
623 * Check with Invalid ContactId.
625 public function testUpdateWithWrongContactId() {
626 $participantId = $this->participantCreate(array(
627 'contactID' => $this->_individualId
,
628 'eventID' => $this->_eventID
,
629 ), $this->_apiversion
);
631 'id' => $participantId,
632 'contact_id' => 12345,
635 'register_date' => '2006-01-21',
636 'source' => 'US Open',
637 'event_level' => 'Donation',
639 $participant = $this->callAPIFailure('participant', 'create', $params);
640 $result = $this->participantDelete($participantId);
643 ///////////////// civicrm_participant_delete methods
646 * Test civicrm_participant_delete with wrong params type.
648 public function testDeleteWrongParamsType() {
649 $params = 'a string';
650 $result = $this->callAPIFailure('participant', 'delete', $params);
654 * Test civicrm_participant_delete with empty params.
656 public function testDeleteEmptyParams() {
658 $result = $this->callAPIFailure('participant', 'delete', $params);
662 * Check with participant_id.
664 public function testParticipantDelete() {
666 'id' => $this->_participantID
,
668 $participant = $this->callAPISuccess('participant', 'delete', $params);
669 $this->assertAPISuccess($participant);
670 $this->assertDBState('CRM_Event_DAO_Participant', $this->_participantID
, NULL, TRUE);
674 * Check without participant_id.
676 * This should return an error because required param is missing..
678 public function testParticipantDeleteMissingID() {
680 'event_id' => $this->_eventID
,
682 $participant = $this->callAPIFailure('participant', 'delete', $params);
683 $this->assertNotNull($participant['error_message']);
687 * Delete with a get - a 'criteria delete'
689 public function testNestedDelete() {
690 $description = "Criteria delete by nesting a GET & a DELETE.";
691 $subfile = "NestedDelete";
692 $participants = $this->callAPISuccess('Participant', 'Get', array());
693 $this->assertEquals($participants['count'], 3);
694 $params = array('contact_id' => $this->_contactID2
, 'api.participant.delete' => 1);
695 $participants = $this->callAPIAndDocument('Participant', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
696 $check = $this->callAPISuccess('participant', 'getcount', array());
697 $this->assertEquals(1, $check, "only one participant should be left. line " . __LINE__
);
701 * Test creation of a participant with an associated contribution.
703 public function testCreateParticipantWithPayment() {
704 $description = "Single function to create contact with partipation & contribution.
705 Note that in the case of 'contribution' the 'create' is implied (api.contribution.create)";
706 $subfile = "CreateParticipantPayment";
708 'contact_type' => 'Individual',
709 'display_name' => 'dlobo',
710 'api.participant' => array(
711 'event_id' => $this->_eventID
,
714 'format.only_id' => 1,
716 'api.contribution.create' => array(
717 'financial_type_id' => 1,
718 'total_amount' => 100,
719 'format.only_id' => 1,
721 'api.participant_payment.create' => array(
722 'contribution_id' => '$value.api.contribution.create',
723 'participant_id' => '$value.api.participant',
727 $result = $this->callAPIAndDocument('contact', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
728 $this->assertEquals(1, $result['values'][$result['id']]['api.participant_payment.create']['count']);
729 $this->callAPISuccess('contact', 'delete', array('id' => $result['id']));