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']);
207 * Check Participant Get respects return properties
209 function testGetWithReturnProperties() {
211 'contact_id' => $this->_contactID
, 'return.status_id' => 1,
212 'return.participant_status_id' => 1,
213 'options' => array('limit' => 1)
215 $result = $this->callAPISuccess('participant', 'get', $params);
216 $this->assertArrayHasKey('participant_status_id', $result['values'][$result['id']]);
220 * Check with contact_id
222 function testGetContactIdOnly() {
224 'contact_id' => $this->_contactID
, );
225 $participant = $this->callAPISuccess('participant', 'get', $params);
227 $this->assertEquals($this->_participantID
, $participant['id'],
228 "In line " . __LINE__
230 $this->assertEquals($this->_eventID
, $participant['values'][$participant['id']]['event_id'],
231 "In line " . __LINE__
233 $this->assertEquals('2007-02-19 00:00:00', $participant['values'][$participant['id']]['participant_register_date'],
234 "In line " . __LINE__
236 $this->assertEquals('Wimbeldon', $participant['values'][$participant['id']]['participant_source'],
237 "In line " . __LINE__
239 $this->assertEquals($participant['id'], $participant['values'][$participant['id']]['id'],
240 "In line " . __LINE__
245 * Check with event_id
248 function testGetMultiMatchReturnFirst() {
250 'event_id' => $this->_eventID
,
253 $participant = $this->callAPISuccess('participant', 'get', $params);
254 $this->assertNotNull($participant['id']);
258 * Check with event_id
259 * in v3 this should return all participants
261 function testGetMultiMatchNoReturnFirst() {
263 'event_id' => $this->_eventID
,
265 $participant = $this->callAPISuccess('participant', 'get', $params);
266 $this->assertNotNull($participant['count'], 3);
269 ///////////////// civicrm_participant_get methods
272 * Test civicrm_participant_get with empty params
273 * In this case all the participant records are returned.
275 function testSearchEmptyParams() {
276 $result = $this->callAPISuccess('participant', 'get', array());
277 // expecting 3 participant records
278 $this->assertEquals($result['count'], 3);
282 * Check with participant_id
284 function testSearchParticipantIdOnly() {
286 'participant_id' => $this->_participantID
,
288 $participant = $this->callAPISuccess('participant', 'get', $params);
289 $this->assertEquals($participant['values'][$this->_participantID
]['event_id'], $this->_eventID
);
290 $this->assertEquals($participant['values'][$this->_participantID
]['participant_register_date'], '2007-02-19 00:00:00');
291 $this->assertEquals($participant['values'][$this->_participantID
]['participant_source'], 'Wimbeldon');
295 * Check with contact_id
297 function testSearchContactIdOnly() {
298 // Should get 2 participant records for this contact.
300 'contact_id' => $this->_contactID2
,
302 $participant = $this->callAPISuccess('participant', 'get', $params);
304 $this->assertEquals($participant['count'], 2);
308 * Check with event_id
310 function testSearchByEvent() {
311 // Should get >= 3 participant records for this event. Also testing that last_name and event_title are returned.
313 'event_id' => $this->_eventID
,
314 'return.last_name' => 1,
315 'return.event_title' => 1,
317 $participant = $this->callAPISuccess('participant', 'get', $params);
318 if ($participant['count'] < 3) {
319 $this->fail("Event search returned less than expected miniumum of 3 records.");
322 $this->assertEquals($participant['values'][$this->_participantID
]['last_name'], 'Anderson');
323 $this->assertEquals($participant['values'][$this->_participantID
]['event_title'], 'Annual CiviCRM meet');
327 * Check with event_id
330 function testSearchByEventWithLimit() {
331 // Should 2 participant records since we're passing rowCount = 2.
333 'event_id' => $this->_eventID
,
336 $participant = $this->callAPISuccess('participant', 'get', $params);
338 $this->assertEquals($participant['count'], 2, 'in line ' . __LINE__
);
341 ///////////////// civicrm_participant_create methods
344 * Test civicrm_participant_create with empty params
346 function testCreateEmptyParams() {
348 $result = $this->callAPIFailure('participant', 'create', $params);
352 * Check with event_id
354 function testCreateMissingContactID() {
356 'event_id' => $this->_eventID
,
358 $participant = $this->callAPIFailure('participant', 'create', $params);
362 * Check with contact_id
365 function testCreateMissingEventID() {
367 'contact_id' => $this->_contactID
,
369 $participant = $this->callAPIFailure('participant', 'create', $params);
373 * Check with contact_id & event_id
375 function testCreateEventIdOnly() {
377 'contact_id' => $this->_contactID
,
378 'event_id' => $this->_eventID
,
380 $participant = $this->callAPISuccess('participant', 'create', $params);
381 $this->getAndCheck($params, $participant['id'], 'participant');
385 * Check with complete array
387 function testCreateAllParams() {
388 $params = $this->_params
;
390 $participant = $this->callAPISuccess('participant', 'create', $params);
391 $this->_participantID
= $participant['id'];
392 // assertDBState compares expected values in $match to actual values in the DB
393 $this->assertDBState('CRM_Event_DAO_Participant', $participant['id'], $params);
397 * Test to check if receive date is being changed per CRM-9763
399 function testCreateUpdateReceiveDate() {
400 $participant = $this->callAPISuccess('participant', 'create', $this->_params
);
402 'id' => $participant['id'],
405 $this->callAPISuccess('participant', 'create', $update);
406 $this->getAndCheck(array_merge($this->_params
, $update), $participant['id'], 'participant');
410 * Test to check if participant fee level is being changed per CRM-9781
412 function testCreateUpdateParticipantFeeLevel() {
413 $myParams = $this->_params +
array('participant_fee_level' => CRM_Core_DAO
::VALUE_SEPARATOR
. "fee" . CRM_Core_DAO
::VALUE_SEPARATOR
);
414 $participant = $this->callAPISuccess('participant', 'create', $myParams);
416 'id' => $participant['id'],
419 $update = $this->callAPISuccess('participant', 'create', $update);
421 $this->assertEquals($participant['values'][$participant['id']]['fee_level'],
422 $update['values'][$participant['id']]['fee_level']
425 $this->callAPISuccess('participant', 'delete', array('id' => $participant['id']));
429 * Test the line items for participant fee with multiple price field values.
431 function testCreateParticipantLineItems() {
432 // Create a price set for this event.
434 $priceset = $this->callAPISuccess('PriceSet', 'create', array(
435 'name' => 'my_price_set',
436 'title' => 'My Price Set',
439 'financial_type_id' => 4,
440 // 'entity' => array('civicrm_event' => array($this->_eventID)),
443 // Add the price set to the event with another API call.
444 // I tried to do this at once, but it did not work.
446 $priceset = $this->callAPISuccess('PriceSet', 'create', array(
447 'entity_table' => 'civicrm_event',
448 'entity_id' => $this->_eventID
,
449 'id' => $priceset['id'],
452 $pricefield = $this->callAPISuccess('PriceField', 'create', array(
453 'price_set_id' => $priceset['id'],
454 'name' => 'mypricefield',
455 'label' => 'My Price Field',
456 'html_type' => 'Text',
458 'is_display_amounts' => 1,
462 $pfv1 = $this->callAPISuccess('PriceFieldValue', 'create', array(
463 'price_field_id' => $pricefield['id'],
464 'name' => 'pricefieldvalue1',
465 'label' => 'pricefieldvalue1',
468 'financial_type_id' => 4,
471 $pfv2 = $this->callAPISuccess('PriceFieldValue', 'create', array(
472 'price_field_id' => $pricefield['id'],
473 'name' => 'pricefieldvalue2',
474 'label' => 'pricefieldvalue2',
477 'financial_type_id' => 4,
480 // pay 2 times price field value 1, and 2 times price field value 2.
481 $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
);
482 $participant = $this->callAPISuccess('participant', 'create', $myParams);
484 // expect 2 line items.
485 $lineItems = $this->callAPISuccess('LineItem', 'get', array(
486 'entity_id' => $participant['id'],
487 'entity_table' => 'civicrm_participant',
490 $this->assertEquals(2, $lineItems['count']);
492 // Check quantity, label and unit price of lines.
493 // TODO: These assertions depend on the order of the line items, which is
494 // technically incorrect.
496 $lineItem = array_pop($lineItems['values']);
497 $this->assertEquals(2, $lineItem['qty']);
498 $this->assertEquals(5, $lineItem['unit_price']);
499 $this->assertEquals('pricefieldvalue2', $lineItem['label']);
501 $lineItem = array_pop($lineItems['values']);
502 $this->assertEquals(2, $lineItem['qty']);
503 $this->assertEquals(20, $lineItem['unit_price']);
504 $this->assertEquals('pricefieldvalue1', $lineItem['label']);
507 $this->callAPISuccess('participant', 'delete', array('id' => $participant['id']));
509 // TODO: I think the price set should be removed, but I don't know how
510 // to decouple it properly from the event. For the moment, I'll just comment
511 // out the lines below.
514 $this->callAPISuccess('PriceFieldValue', 'delete', array('id' => $pfv1['id']));
515 $this->callAPISuccess('PriceFieldValue', 'delete', array('id' => $pfv2['id']));
516 $this->callAPISuccess('PriceField', 'delete', array('id' => $pricefield['id']));
517 $this->callAPISuccess('PriceSet', 'delete', array('id' => $priceset['id']));
522 * Check with complete array
524 function testUpdate() {
525 $participantId = $this->participantCreate(array(
526 'contactID' => $this->_individualId
,
527 'eventID' => $this->_eventID
530 'id' => $participantId,
531 'contact_id' => $this->_individualId
,
532 'event_id' => $this->_eventID
,
535 'register_date' => '2006-01-21',
536 'source' => 'US Open',
538 $participant = $this->callAPISuccess('participant', 'create', $params);
539 $this->getAndCheck($params, $participant['id'], 'participant');
540 $result = $this->participantDelete($params['id']);
544 * Test to check if participant fee level is being changed per CRM-9781
545 * Try again without a custom separater to check that one isn't added
546 * (get & check won't accept an array)
548 function testUpdateCreateParticipantFeeLevelNoSeparator() {
550 $myParams = $this->_params +
array('participant_fee_level' => "fee");
551 $participant = $this->callAPISuccess('participant', 'create', $myParams);
552 $this->assertAPISuccess($participant);
554 'id' => $participant['id'],
557 $this->callAPISuccess('participant', 'create', $update);
558 $this->assertEquals($participant['values'][$participant['id']]['fee_level'],
559 $myParams['participant_fee_level']
561 $this->getAndCheck($update, $participant['id'], 'participant');
563 ///////////////// civicrm_participant_update methods
566 * Test civicrm_participant_update with wrong params type
568 function testUpdateWrongParamsType() {
569 $params = 'a string';
570 $result = $this->callAPIFailure('participant', 'create', $params);
571 $this->assertEquals('Input variable `params` is not an array', $result['error_message'], 'In line ' . __LINE__
);
575 * Check with empty array
577 function testUpdateEmptyParams() {
579 $participant = $this->callAPIFailure('participant', 'create', $params);
580 $this->assertEquals($participant['error_message'], 'Mandatory key(s) missing from params array: event_id, contact_id');
584 * Check without event_id
586 function testUpdateWithoutEventId() {
587 $participantId = $this->participantCreate(array('contactID' => $this->_individualId
, 'eventID' => $this->_eventID
));
589 'contact_id' => $this->_individualId
,
592 'register_date' => '2006-01-21',
593 'source' => 'US Open',
594 'event_level' => 'Donation', );
595 $participant = $this->callAPIFailure('participant', 'create', $params);
596 $this->assertEquals($participant['error_message'], 'Mandatory key(s) missing from params array: event_id');
597 // Cleanup created participant records.
598 $result = $this->participantDelete($participantId);
602 * Check with Invalid participantId
604 function testUpdateWithWrongParticipantId() {
609 'register_date' => '2006-01-21',
610 'source' => 'US Open',
611 'event_level' => 'Donation', );
612 $participant = $this->callAPIFailure('Participant', 'update', $params);
616 * Check with Invalid ContactId
618 function testUpdateWithWrongContactId() {
619 $participantId = $this->participantCreate(array(
620 'contactID' => $this->_individualId
,
621 'eventID' => $this->_eventID
,
622 ), $this->_apiversion
);
624 'id' => $participantId,
625 'contact_id' => 12345,
628 'register_date' => '2006-01-21',
629 'source' => 'US Open',
630 'event_level' => 'Donation', );
631 $participant = $this->callAPIFailure('participant', 'create', $params);
632 $result = $this->participantDelete($participantId);
635 ///////////////// civicrm_participant_delete methods
638 * Test civicrm_participant_delete with wrong params type
640 function testDeleteWrongParamsType() {
641 $params = 'a string';
642 $result = $this->callAPIFailure('participant', 'delete', $params);
646 * Test civicrm_participant_delete with empty params
648 function testDeleteEmptyParams() {
650 $result = $this->callAPIFailure('participant', 'delete', $params);
654 * Check with participant_id
656 function testParticipantDelete() {
658 'id' => $this->_participantID
, );
659 $participant = $this->callAPISuccess('participant', 'delete', $params);
660 $this->assertAPISuccess($participant);
661 $this->assertDBState('CRM_Event_DAO_Participant', $this->_participantID
, NULL, TRUE);
665 * Check without participant_id
667 * This should return an error because required param is missing..
669 function testParticipantDeleteMissingID() {
671 'event_id' => $this->_eventID
, );
672 $participant = $this->callAPIFailure('participant', 'delete', $params);
673 $this->assertNotNull($participant['error_message']);
677 * Delete with a get - a 'criteria delete'
679 function testNestedDelete() {
680 $description = "Criteria delete by nesting a GET & a DELETE";
681 $subfile = "NestedDelete";
682 $participants = $this->callAPISuccess('Participant', 'Get', array());
683 $this->assertEquals($participants['count'], 3);
684 $params = array('contact_id' => $this->_contactID2
, 'api.participant.delete' => 1);
685 $participants = $this->callAPIAndDocument('Participant', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile, 'Get');
686 $check = $this->callAPISuccess('participant', 'getcount', array());
687 $this->assertEquals(1, $check,"only one participant should be left. line " . __LINE__
);
691 * Test creation of a participant with an associated contribution
693 function testCreateParticipantWithPayment() {
694 $description = "single function to create contact w partipation & contribution. Note that in the
695 case of 'contribution' the 'create' is implied (api.contribution.create)";
696 $subfile = "CreateParticipantPayment";
698 'contact_type' => 'Individual',
699 'display_name' => 'dlobo',
700 'api.participant' => array(
701 'event_id' => $this->_eventID
,
704 'format.only_id' => 1,
706 'api.contribution.create' => array(
707 'financial_type_id' => 1,
708 'total_amount' => 100,
709 'format.only_id' => 1,
711 'api.participant_payment.create' => array(
712 'contribution_id' => '$value.api.contribution.create',
713 'participant_id' => '$value.api.participant',
717 $result = $this->callAPIAndDocument('contact', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
718 $this->assertEquals(1, $result['values'][$result['id']]['api.participant_payment.create']['count']);
719 $this->callAPISuccess('contact', 'delete', array('id' => $result['id']));