Merge pull request #12178 from jitendrapurohit/membership-4
[civicrm-core.git] / tests / phpunit / api / v3 / ParticipantTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
2fe49090 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
8c9251b3 6 | Copyright CiviCRM LLC (c) 2004-2018 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035 27
6a488035
TO
28/**
29 * Test class for Batch API - civicrm_participant_*
30 *
92915c55 31 * @package CiviCRM_APIv3
6a488035 32 */
4cbe18b8
EM
33
34/**
35 * Class api_v3_ParticipantTest
acb109b7 36 * @group headless
4cbe18b8 37 */
6a488035
TO
38class api_v3_ParticipantTest extends CiviUnitTestCase {
39
40 protected $_apiversion;
41 protected $_entity;
42 protected $_contactID;
43 protected $_contactID2;
44 protected $_createdParticipants;
45 protected $_participantID;
46 protected $_eventID;
47 protected $_individualId;
48 protected $_params;
6a488035 49
00be9182 50 public function setUp() {
6a488035
TO
51 $this->_apiversion = 3;
52 parent::setUp();
92915c55
TO
53 $this->_entity = 'participant';
54 $event = $this->eventCreate(NULL);
6a488035
TO
55 $this->_eventID = $event['id'];
56
e4d5f1e2 57 $this->_contactID = $this->individualCreate();
6a488035
TO
58
59 $this->_createdParticipants = array();
e4d5f1e2 60 $this->_individualId = $this->individualCreate();
6a488035 61
92915c55
TO
62 $this->_participantID = $this->participantCreate(array(
63 'contact_id' => $this->_contactID,
408b79bf 64 'event_id' => $this->_eventID,
92915c55 65 ));
e4d5f1e2 66 $this->_contactID2 = $this->individualCreate();
92915c55
TO
67 $this->_participantID2 = $this->participantCreate(array(
68 'contact_id' => $this->_contactID2,
408b79bf 69 'event_id' => $this->_eventID,
9da10571 70 'registered_by_id' => $this->_participantID,
92915c55
TO
71 ));
72 $this->_participantID3 = $this->participantCreate(array(
73 'contact_id' => $this->_contactID2,
408b79bf 74 'event_id' => $this->_eventID,
92915c55 75 ));
6a488035
TO
76 $this->_params = array(
77 'contact_id' => $this->_contactID,
78 'event_id' => $this->_eventID,
79 'status_id' => 1,
80 'role_id' => 1,
81 // to ensure it matches later on
82 'register_date' => '2007-07-21 00:00:00',
83 'source' => 'Online Event Registration: API Testing',
6a488035
TO
84 );
85 }
86
00be9182 87 public function tearDown() {
6a488035
TO
88 $this->eventDelete($this->_eventID);
89 $tablesToTruncate = array(
92915c55
TO
90 'civicrm_custom_group',
91 'civicrm_custom_field',
92 'civicrm_contact',
93 'civicrm_participant',
6a488035
TO
94 );
95 // true tells quickCleanup to drop any tables that might have been created in the test
96 $this->quickCleanup($tablesToTruncate, TRUE);
97 }
98
7244a956 99 /**
100 * Check that getCount can count past 25.
101 */
102 public function testGetCountLimit() {
103 $contactIDs = [];
104
105 for ($count = $this->callAPISuccessGetCount('Participant', []); $count < 27; $count++) {
106 $contactIDs[] = $contactID = $this->individualCreate();
107 $this->participantCreate(['contact_id' => $contactID, 'event_id' => $this->_eventID]);
108 }
109 $this->callAPISuccessGetCount('Participant', [], 27);
110
111 foreach ($contactIDs as $contactID) {
112 $this->callAPISuccess('Contact', 'delete', ['id' => $contactID]);
113 }
114 }
115
52fa9e7e 116 /**
117 * Test get participants with role_id.
118 */
119 public function testGetParticipantWithRole() {
120 $roleId = array(1, 2, 3);
121 foreach ($roleId as $role) {
122 $this->participantCreate(array(
123 'contact_id' => $this->individualCreate(),
124 'role_id' => $role,
125 'event_id' => $this->_eventID,
126 ));
127 }
128
129 $params = array(
130 'role_id' => 2,
131 );
132 $result = $this->callAPISuccess('participant', 'get', $params);
133 //Assert all the returned participants has a role_id of 2
134 foreach ($result['values'] as $pid => $values) {
135 $this->assertEquals($values['participant_role_id'], 2);
136 }
137
138 $this->participantCreate(array(
139 'id' => $this->_participantID,
140 'role_id' => NULL,
141 'event_id' => $this->_eventID,
142 ));
143
144 $params['role_id'] = array(
145 'IS NULL' => 1,
146 );
147 $result = $this->callAPISuccess('participant', 'get', $params);
148 foreach ($result['values'] as $pid => $values) {
149 $this->assertEquals($values['participant_role_id'], NULL);
150 }
151
152 }
153
6a488035 154 /**
100fef9d 155 * Check with complete array + custom field
6a488035
TO
156 * Note that the test is written on purpose without any
157 * variables specific to participant so it can be replicated into other entities
158 * and / or moved to the automated test suite
159 */
00be9182 160 public function testCreateWithCustom() {
6a488035
TO
161 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
162
163 $params = $this->_params;
164 $params['custom_' . $ids['custom_field_id']] = "custom string";
165
afd404ea 166 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
167
6a488035 168 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
6a488035 169
afd404ea 170 $check = $this->callAPISuccess($this->_entity, 'get', array('id' => $result['id']));
6a488035
TO
171 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
172
173 $this->customFieldDelete($ids['custom_field_id']);
174 $this->customGroupDelete($ids['custom_group_id']);
175 }
176
6a488035 177 /**
fe482240 178 * Check with participant_id.
6a488035 179 */
00be9182 180 public function testGetParticipantIdOnly() {
6a488035
TO
181 $params = array(
182 'participant_id' => $this->_participantID,
6a488035
TO
183 'return' => array(
184 'participant_id',
185 'event_id',
186 'participant_register_date',
187 'participant_source',
21dfd5f5 188 ),
6a488035 189 );
afd404ea 190 $result = $this->callAPISuccess('participant', 'get', $params);
6a488035 191 $this->assertAPISuccess($result, " in line " . __LINE__);
2ce58498 192 $this->assertEquals($result['values'][$this->_participantID]['event_id'], $this->_eventID);
193 $this->assertEquals($result['values'][$this->_participantID]['participant_register_date'], '2007-02-19 00:00:00');
194 $this->assertEquals($result['values'][$this->_participantID]['participant_source'], 'Wimbeldon');
6c6e6187 195 $params = array(
6a488035 196 'id' => $this->_participantID,
6a488035
TO
197 'return' => 'id,participant_register_date,event_id',
198
199 );
afd404ea 200 $result = $this->callAPISuccess('participant', 'get', $params);
6a488035
TO
201 $this->assertEquals($result['values'][$this->_participantID]['event_id'], $this->_eventID);
202 $this->assertEquals($result['values'][$this->_participantID]['participant_register_date'], '2007-02-19 00:00:00');
203
204 }
205
450fcddb
JP
206 /**
207 * Test permission for participant get.
208 */
209 public function testGetParticipantWithPermission() {
210 $config = CRM_Core_Config::singleton();
211 $config->userPermissionClass->permissions = array();
212 $params = array(
213 'event_id' => $this->_eventID,
214 'check_permissions' => TRUE,
215 'return' => array(
216 'participant_id',
217 'event_id',
218 'participant_register_date',
219 'participant_source',
220 ),
221 );
222 $this->callAPIFailure('participant', 'get', $params);
223
224 $params['check_permissions'] = FALSE;
225 $result = $this->callAPISuccess('participant', 'get', $params);
226 $this->assertEquals($result['is_error'], 0);
227 }
228
229
6a488035 230 /**
eceb18cc 231 * Check with params id.
6a488035 232 */
00be9182 233 public function testGetParamsAsIdOnly() {
6a488035
TO
234 $params = array(
235 'id' => $this->_participantID,
6a488035 236 );
afd404ea 237 $result = $this->callAPIAndDocument('participant', 'get', $params, __FUNCTION__, __FILE__);
6a488035
TO
238 $this->assertEquals($result['values'][$this->_participantID]['event_id'], $this->_eventID);
239 $this->assertEquals($result['values'][$this->_participantID]['participant_register_date'], '2007-02-19 00:00:00');
240 $this->assertEquals($result['values'][$this->_participantID]['participant_source'], 'Wimbeldon');
241 $this->assertEquals($result['id'], $result['values'][$this->_participantID]['id']);
242 }
243
244 /**
eceb18cc 245 * Check with params id.
6a488035 246 */
00be9182 247 public function testGetNestedEventGet() {
6a488035
TO
248 //create a second event & add participant to it.
249 $event = $this->eventCreate(NULL);
92915c55
TO
250 $this->callAPISuccess('participant', 'create', array(
251 'event_id' => $event['id'],
408b79bf 252 'contact_id' => $this->_contactID,
92915c55 253 ));
6a488035 254
5c49fee0 255 $description = "Demonstrates use of nested get to fetch event data with participant records.";
92915c55
TO
256 $subfile = "NestedEventGet";
257 $params = array(
6a488035 258 'id' => $this->_participantID,
6a488035
TO
259 'api.event.get' => 1,
260 );
afd404ea 261 $result = $this->callAPIAndDocument('participant', 'get', $params, __FUNCTION__, __FILE__, $description, $subfile);
6a488035
TO
262 $this->assertEquals($result['values'][$this->_participantID]['event_id'], $this->_eventID);
263 $this->assertEquals($result['values'][$this->_participantID]['participant_register_date'], '2007-02-19 00:00:00');
264 $this->assertEquals($result['values'][$this->_participantID]['participant_source'], 'Wimbeldon');
265 $this->assertEquals($this->_eventID, $result['values'][$this->_participantID]['api.event.get']['id']);
266 }
c490a46a
CW
267
268 /**
eceb18cc 269 * Check Participant Get respects return properties.
c490a46a 270 */
00be9182 271 public function testGetWithReturnProperties() {
6a488035 272 $params = array(
6c6e6187 273 'contact_id' => $this->_contactID,
92915c55 274 'return.status_id' => 1,
6a488035 275 'return.participant_status_id' => 1,
21dfd5f5 276 'options' => array('limit' => 1),
6a488035 277 );
afd404ea 278 $result = $this->callAPISuccess('participant', 'get', $params);
6a488035
TO
279 $this->assertArrayHasKey('participant_status_id', $result['values'][$result['id']]);
280 }
281
282 /**
fe482240 283 * Check with contact_id.
6a488035 284 */
00be9182 285 public function testGetContactIdOnly() {
6a488035 286 $params = array(
92915c55
TO
287 'contact_id' => $this->_contactID,
288 );
afd404ea 289 $participant = $this->callAPISuccess('participant', 'get', $params);
6a488035 290
2ce58498 291 $this->assertEquals($this->_participantID, $participant['id']);
292 $this->assertEquals($this->_eventID, $participant['values'][$participant['id']]['event_id']);
293 $this->assertEquals('2007-02-19 00:00:00', $participant['values'][$participant['id']]['participant_register_date']);
294 $this->assertEquals('Wimbeldon', $participant['values'][$participant['id']]['participant_source']);
295 $this->assertEquals($participant['id'], $participant['values'][$participant['id']]['id']);
6a488035
TO
296 }
297
298 /**
fe482240 299 * Check with event_id.
6a488035
TO
300 * fetch first record
301 */
00be9182 302 public function testGetMultiMatchReturnFirst() {
6a488035
TO
303 $params = array(
304 'event_id' => $this->_eventID,
92915c55
TO
305 'rowCount' => 1,
306 );
6a488035 307
afd404ea 308 $participant = $this->callAPISuccess('participant', 'get', $params);
6a488035
TO
309 $this->assertNotNull($participant['id']);
310 }
311
312 /**
fe482240 313 * Check with event_id.
6a488035
TO
314 * in v3 this should return all participants
315 */
00be9182 316 public function testGetMultiMatchNoReturnFirst() {
6a488035
TO
317 $params = array(
318 'event_id' => $this->_eventID,
6a488035 319 );
afd404ea 320 $participant = $this->callAPISuccess('participant', 'get', $params);
6a488035
TO
321 $this->assertNotNull($participant['count'], 3);
322 }
323
324 ///////////////// civicrm_participant_get methods
325
6a488035 326 /**
fe482240 327 * Test civicrm_participant_get with empty params.
6a488035
TO
328 * In this case all the participant records are returned.
329 */
00be9182 330 public function testSearchEmptyParams() {
afd404ea 331 $result = $this->callAPISuccess('participant', 'get', array());
6a488035
TO
332 // expecting 3 participant records
333 $this->assertEquals($result['count'], 3);
334 }
335
336 /**
fe482240 337 * Check with participant_id.
6a488035 338 */
00be9182 339 public function testSearchParticipantIdOnly() {
6a488035
TO
340 $params = array(
341 'participant_id' => $this->_participantID,
6a488035 342 );
afd404ea 343 $participant = $this->callAPISuccess('participant', 'get', $params);
6a488035
TO
344 $this->assertEquals($participant['values'][$this->_participantID]['event_id'], $this->_eventID);
345 $this->assertEquals($participant['values'][$this->_participantID]['participant_register_date'], '2007-02-19 00:00:00');
346 $this->assertEquals($participant['values'][$this->_participantID]['participant_source'], 'Wimbeldon');
347 }
348
349 /**
fe482240 350 * Check with contact_id.
6a488035 351 */
00be9182 352 public function testSearchContactIdOnly() {
6a488035
TO
353 // Should get 2 participant records for this contact.
354 $params = array(
355 'contact_id' => $this->_contactID2,
6a488035 356 );
afd404ea 357 $participant = $this->callAPISuccess('participant', 'get', $params);
6a488035
TO
358
359 $this->assertEquals($participant['count'], 2);
360 }
361
362 /**
fe482240 363 * Check with event_id.
6a488035 364 */
00be9182 365 public function testSearchByEvent() {
6a488035
TO
366 // Should get >= 3 participant records for this event. Also testing that last_name and event_title are returned.
367 $params = array(
368 'event_id' => $this->_eventID,
369 'return.last_name' => 1,
370 'return.event_title' => 1,
6a488035 371 );
afd404ea 372 $participant = $this->callAPISuccess('participant', 'get', $params);
6a488035
TO
373 if ($participant['count'] < 3) {
374 $this->fail("Event search returned less than expected miniumum of 3 records.");
375 }
376
377 $this->assertEquals($participant['values'][$this->_participantID]['last_name'], 'Anderson');
378 $this->assertEquals($participant['values'][$this->_participantID]['event_title'], 'Annual CiviCRM meet');
379 }
380
381 /**
fe482240 382 * Check with event_id.
6a488035
TO
383 * fetch with limit
384 */
00be9182 385 public function testSearchByEventWithLimit() {
6a488035
TO
386 // Should 2 participant records since we're passing rowCount = 2.
387 $params = array(
388 'event_id' => $this->_eventID,
389 'rowCount' => 2,
6a488035 390 );
afd404ea 391 $participant = $this->callAPISuccess('participant', 'get', $params);
6a488035 392
a15773db 393 $this->assertEquals($participant['count'], 2);
6a488035
TO
394 }
395
9da10571
J
396 /**
397 * Test search by lead booker (registered by ID)
398 */
399 public function testSearchByRegisteredById() {
400 $params = array(
401 'registered_by_id' => $this->_participantID,
402 );
403 $participant = $this->callAPISuccess('participant', 'get', $params);
404
405 $this->assertEquals($participant['count'], 1);
406 $this->assertEquals($participant['id'], $this->_participantID2);
407 }
408
6a488035
TO
409 ///////////////// civicrm_participant_create methods
410
6a488035 411 /**
fe482240 412 * Test civicrm_participant_create with empty params.
6a488035 413 */
00be9182 414 public function testCreateEmptyParams() {
6a488035 415 $params = array();
d0e1eff2 416 $result = $this->callAPIFailure('participant', 'create', $params);
6a488035
TO
417 }
418
419 /**
fe482240 420 * Check with event_id.
6a488035 421 */
00be9182 422 public function testCreateMissingContactID() {
2ce58498 423 $this->callAPIFailure('participant', 'create', ['event_id' => $this->_eventID]);
6a488035
TO
424 }
425
426 /**
fe482240 427 * Check with contact_id.
6a488035
TO
428 * without event_id
429 */
00be9182 430 public function testCreateMissingEventID() {
2ce58498 431 $this->callAPIFailure('participant', 'create', ['contact_id' => $this->_contactID]);
6a488035
TO
432 }
433
434 /**
100fef9d 435 * Check with contact_id & event_id
6a488035 436 */
00be9182 437 public function testCreateEventIdOnly() {
6a488035
TO
438 $params = array(
439 'contact_id' => $this->_contactID,
440 'event_id' => $this->_eventID,
6a488035 441 );
afd404ea 442 $participant = $this->callAPISuccess('participant', 'create', $params);
443 $this->getAndCheck($params, $participant['id'], 'participant');
6a488035
TO
444 }
445
446 /**
eceb18cc 447 * Check with complete array.
6a488035 448 */
00be9182 449 public function testCreateAllParams() {
6a488035
TO
450 $params = $this->_params;
451
afd404ea 452 $participant = $this->callAPISuccess('participant', 'create', $params);
6a488035 453 $this->_participantID = $participant['id'];
afd404ea 454 // assertDBState compares expected values in $match to actual values in the DB
455 $this->assertDBState('CRM_Event_DAO_Participant', $participant['id'], $params);
6a488035 456 }
c490a46a
CW
457
458 /**
459 * Test to check if receive date is being changed per CRM-9763
460 */
00be9182 461 public function testCreateUpdateReceiveDate() {
afd404ea 462 $participant = $this->callAPISuccess('participant', 'create', $this->_params);
6a488035 463 $update = array(
92915c55 464 'id' => $participant['id'],
6a488035
TO
465 'status_id' => 2,
466 );
afd404ea 467 $this->callAPISuccess('participant', 'create', $update);
6a488035
TO
468 $this->getAndCheck(array_merge($this->_params, $update), $participant['id'], 'participant');
469 }
c490a46a
CW
470
471 /**
472 * Test to check if participant fee level is being changed per CRM-9781
473 */
00be9182 474 public function testCreateUpdateParticipantFeeLevel() {
6a488035 475 $myParams = $this->_params + array('participant_fee_level' => CRM_Core_DAO::VALUE_SEPARATOR . "fee" . CRM_Core_DAO::VALUE_SEPARATOR);
afd404ea 476 $participant = $this->callAPISuccess('participant', 'create', $myParams);
6a488035 477 $update = array(
6ead217b 478 'id' => $participant['id'],
6a488035
TO
479 'status_id' => 2,
480 );
6ead217b
E
481 $update = $this->callAPISuccess('participant', 'create', $update);
482
483 $this->assertEquals($participant['values'][$participant['id']]['fee_level'],
484 $update['values'][$participant['id']]['fee_level']
6a488035
TO
485 );
486
afd404ea 487 $this->callAPISuccess('participant', 'delete', array('id' => $participant['id']));
6a488035 488 }
3233fd0f
JV
489
490 /**
c490a46a
CW
491 * Test the line items for participant fee with multiple price field values.
492 */
00be9182 493 public function testCreateParticipantLineItems() {
3233fd0f
JV
494 // Create a price set for this event.
495
496 $priceset = $this->callAPISuccess('PriceSet', 'create', array(
497 'name' => 'my_price_set',
498 'title' => 'My Price Set',
499 'is_active' => 1,
500 'extends' => 1,
501 'financial_type_id' => 4,
502 // 'entity' => array('civicrm_event' => array($this->_eventID)),
503 ));
504
505 // Add the price set to the event with another API call.
506 // I tried to do this at once, but it did not work.
507
508 $priceset = $this->callAPISuccess('PriceSet', 'create', array(
509 'entity_table' => 'civicrm_event',
510 'entity_id' => $this->_eventID,
511 'id' => $priceset['id'],
512 ));
513
514 $pricefield = $this->callAPISuccess('PriceField', 'create', array(
515 'price_set_id' => $priceset['id'],
516 'name' => 'mypricefield',
517 'label' => 'My Price Field',
518 'html_type' => 'Text',
519 'is_enter_qty' => 1,
520 'is_display_amounts' => 1,
521 'is_active' => 1,
522 ));
523
524 $pfv1 = $this->callAPISuccess('PriceFieldValue', 'create', array(
525 'price_field_id' => $pricefield['id'],
526 'name' => 'pricefieldvalue1',
527 'label' => 'pricefieldvalue1',
528 'amount' => 20,
529 'is_active' => 1,
530 'financial_type_id' => 4,
531 ));
532
533 $pfv2 = $this->callAPISuccess('PriceFieldValue', 'create', array(
534 'price_field_id' => $pricefield['id'],
535 'name' => 'pricefieldvalue2',
536 'label' => 'pricefieldvalue2',
537 'amount' => 5,
538 'is_active' => 1,
539 'financial_type_id' => 4,
540 ));
541
542 // pay 2 times price field value 1, and 2 times price field value 2.
543 $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);
544 $participant = $this->callAPISuccess('participant', 'create', $myParams);
545
546 // expect 2 line items.
547 $lineItems = $this->callAPISuccess('LineItem', 'get', array(
548 'entity_id' => $participant['id'],
549 'entity_table' => 'civicrm_participant',
550 ));
551
552 $this->assertEquals(2, $lineItems['count']);
553
554 // Check quantity, label and unit price of lines.
31037a42 555 // TODO: These assertions depend on the order of the line items, which is
3233fd0f 556 // technically incorrect.
d4df7755
JV
557
558 $lineItem = array_pop($lineItems['values']);
559 $this->assertEquals(2, $lineItem['qty']);
560 $this->assertEquals(5, $lineItem['unit_price']);
561 $this->assertEquals('pricefieldvalue2', $lineItem['label']);
562
563 $lineItem = array_pop($lineItems['values']);
564 $this->assertEquals(2, $lineItem['qty']);
565 $this->assertEquals(20, $lineItem['unit_price']);
566 $this->assertEquals('pricefieldvalue1', $lineItem['label']);
3233fd0f
JV
567
568 // Cleanup
569 $this->callAPISuccess('participant', 'delete', array('id' => $participant['id']));
570
571 // TODO: I think the price set should be removed, but I don't know how
572 // to decouple it properly from the event. For the moment, I'll just comment
573 // out the lines below.
574
575 /*
576 $this->callAPISuccess('PriceFieldValue', 'delete', array('id' => $pfv1['id']));
577 $this->callAPISuccess('PriceFieldValue', 'delete', array('id' => $pfv2['id']));
578 $this->callAPISuccess('PriceField', 'delete', array('id' => $pricefield['id']));
579 $this->callAPISuccess('PriceSet', 'delete', array('id' => $priceset['id']));
e70a7fc0 580 */
3233fd0f
JV
581 }
582
afd404ea 583 /**
eceb18cc 584 * Check with complete array.
afd404ea 585 */
00be9182 586 public function testUpdate() {
6ead217b
E
587 $participantId = $this->participantCreate(array(
588 'contactID' => $this->_individualId,
21dfd5f5 589 'eventID' => $this->_eventID,
6ead217b 590 ));
afd404ea 591 $params = array(
592 'id' => $participantId,
593 'contact_id' => $this->_individualId,
594 'event_id' => $this->_eventID,
595 'status_id' => 3,
596 'role_id' => 3,
597 'register_date' => '2006-01-21',
598 'source' => 'US Open',
afd404ea 599 );
600 $participant = $this->callAPISuccess('participant', 'create', $params);
601 $this->getAndCheck($params, $participant['id'], 'participant');
602 $result = $this->participantDelete($params['id']);
603 }
604
605 /**
606 * Test to check if participant fee level is being changed per CRM-9781
607 * Try again without a custom separater to check that one isn't added
608 * (get & check won't accept an array)
609 */
00be9182 610 public function testUpdateCreateParticipantFeeLevelNoSeparator() {
6a488035
TO
611
612 $myParams = $this->_params + array('participant_fee_level' => "fee");
afd404ea 613 $participant = $this->callAPISuccess('participant', 'create', $myParams);
6a488035
TO
614 $this->assertAPISuccess($participant);
615 $update = array(
92915c55 616 'id' => $participant['id'],
6a488035
TO
617 'status_id' => 2,
618 );
afd404ea 619 $this->callAPISuccess('participant', 'create', $update);
6a488035
TO
620 $this->assertEquals($participant['values'][$participant['id']]['fee_level'],
621 $myParams['participant_fee_level']
622 );
623 $this->getAndCheck($update, $participant['id'], 'participant');
624 }
625 ///////////////// civicrm_participant_update methods
626
627 /**
fe482240 628 * Test civicrm_participant_update with wrong params type.
6a488035 629 */
00be9182 630 public function testUpdateWrongParamsType() {
6a488035 631 $params = 'a string';
d0e1eff2 632 $result = $this->callAPIFailure('participant', 'create', $params);
ba4a1892 633 $this->assertEquals('Input variable `params` is not an array', $result['error_message']);
6a488035
TO
634 }
635
636 /**
eceb18cc 637 * Check with empty array.
6a488035 638 */
00be9182 639 public function testUpdateEmptyParams() {
afd404ea 640 $params = array();
d0e1eff2 641 $participant = $this->callAPIFailure('participant', 'create', $params);
6a488035
TO
642 $this->assertEquals($participant['error_message'], 'Mandatory key(s) missing from params array: event_id, contact_id');
643 }
644
645 /**
fe482240 646 * Check without event_id.
6a488035 647 */
00be9182 648 public function testUpdateWithoutEventId() {
afd404ea 649 $participantId = $this->participantCreate(array('contactID' => $this->_individualId, 'eventID' => $this->_eventID));
6a488035
TO
650 $params = array(
651 'contact_id' => $this->_individualId,
652 'status_id' => 3,
653 'role_id' => 3,
654 'register_date' => '2006-01-21',
655 'source' => 'US Open',
92915c55
TO
656 'event_level' => 'Donation',
657 );
d0e1eff2 658 $participant = $this->callAPIFailure('participant', 'create', $params);
6a488035
TO
659 $this->assertEquals($participant['error_message'], 'Mandatory key(s) missing from params array: event_id');
660 // Cleanup created participant records.
661 $result = $this->participantDelete($participantId);
662 }
663
664 /**
eceb18cc 665 * Check with Invalid participantId.
6a488035 666 */
00be9182 667 public function testUpdateWithWrongParticipantId() {
6a488035
TO
668 $params = array(
669 'id' => 1234,
670 'status_id' => 3,
671 'role_id' => 3,
672 'register_date' => '2006-01-21',
673 'source' => 'US Open',
92915c55
TO
674 'event_level' => 'Donation',
675 );
d0e1eff2 676 $participant = $this->callAPIFailure('Participant', 'update', $params);
6a488035
TO
677 }
678
679 /**
eceb18cc 680 * Check with Invalid ContactId.
6a488035 681 */
00be9182 682 public function testUpdateWithWrongContactId() {
6a488035
TO
683 $participantId = $this->participantCreate(array(
684 'contactID' => $this->_individualId,
92915c55
TO
685 'eventID' => $this->_eventID,
686 ), $this->_apiversion);
6a488035
TO
687 $params = array(
688 'id' => $participantId,
689 'contact_id' => 12345,
690 'status_id' => 3,
691 'role_id' => 3,
692 'register_date' => '2006-01-21',
693 'source' => 'US Open',
92915c55
TO
694 'event_level' => 'Donation',
695 );
d0e1eff2 696 $participant = $this->callAPIFailure('participant', 'create', $params);
6a488035
TO
697 $result = $this->participantDelete($participantId);
698 }
699
6a488035
TO
700 ///////////////// civicrm_participant_delete methods
701
702 /**
fe482240 703 * Test civicrm_participant_delete with wrong params type.
6a488035 704 */
00be9182 705 public function testDeleteWrongParamsType() {
6a488035 706 $params = 'a string';
d0e1eff2 707 $result = $this->callAPIFailure('participant', 'delete', $params);
6a488035
TO
708 }
709
710 /**
fe482240 711 * Test civicrm_participant_delete with empty params.
6a488035 712 */
00be9182 713 public function testDeleteEmptyParams() {
6a488035 714 $params = array();
d0e1eff2 715 $result = $this->callAPIFailure('participant', 'delete', $params);
6a488035
TO
716 }
717
718 /**
fe482240 719 * Check with participant_id.
6a488035 720 */
00be9182 721 public function testParticipantDelete() {
6a488035 722 $params = array(
92915c55
TO
723 'id' => $this->_participantID,
724 );
afd404ea 725 $participant = $this->callAPISuccess('participant', 'delete', $params);
6a488035
TO
726 $this->assertAPISuccess($participant);
727 $this->assertDBState('CRM_Event_DAO_Participant', $this->_participantID, NULL, TRUE);
728 }
729
730 /**
fe482240 731 * Check without participant_id.
6a488035
TO
732 * and with event_id
733 * This should return an error because required param is missing..
734 */
00be9182 735 public function testParticipantDeleteMissingID() {
6a488035 736 $params = array(
92915c55
TO
737 'event_id' => $this->_eventID,
738 );
d0e1eff2 739 $participant = $this->callAPIFailure('participant', 'delete', $params);
6a488035
TO
740 $this->assertNotNull($participant['error_message']);
741 }
c490a46a
CW
742
743 /**
100fef9d 744 * Delete with a get - a 'criteria delete'
c490a46a 745 */
00be9182 746 public function testNestedDelete() {
5c49fee0 747 $description = "Criteria delete by nesting a GET & a DELETE.";
92915c55 748 $subfile = "NestedDelete";
afd404ea 749 $participants = $this->callAPISuccess('Participant', 'Get', array());
6a488035 750 $this->assertEquals($participants['count'], 3);
afd404ea 751 $params = array('contact_id' => $this->_contactID2, 'api.participant.delete' => 1);
2ce58498 752 $this->callAPIAndDocument('Participant', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile);
81e1a644 753 $check = $this->callAPISuccess('participant', 'getcount', array());
2ce58498 754 $this->assertEquals(1, $check, "only one participant should be left");
6a488035 755 }
c490a46a
CW
756
757 /**
eceb18cc 758 * Test creation of a participant with an associated contribution.
c490a46a 759 */
00be9182 760 public function testCreateParticipantWithPayment() {
5c49fee0
CW
761 $description = "Single function to create contact with partipation & contribution.
762 Note that in the case of 'contribution' the 'create' is implied (api.contribution.create)";
6a488035
TO
763 $subfile = "CreateParticipantPayment";
764 $params = array(
765 'contact_type' => 'Individual',
81e1a644 766 'display_name' => 'dlobo',
767 'api.participant' => array(
6a488035
TO
768 'event_id' => $this->_eventID,
769 'status_id' => 1,
770 'role_id' => 1,
771 'format.only_id' => 1,
772 ),
773 'api.contribution.create' => array(
774 'financial_type_id' => 1,
775 'total_amount' => 100,
776 'format.only_id' => 1,
777 ),
778 'api.participant_payment.create' => array(
779 'contribution_id' => '$value.api.contribution.create',
780 'participant_id' => '$value.api.participant',
781 ),
782 );
783
afd404ea 784 $result = $this->callAPIAndDocument('contact', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
6a488035 785 $this->assertEquals(1, $result['values'][$result['id']]['api.participant_payment.create']['count']);
afd404ea 786 $this->callAPISuccess('contact', 'delete', array('id' => $result['id']));
6a488035 787 }
96025800 788
29a59599
JP
789 /**
790 * Test participant invoke post hook after status update.
791 */
792 public function testPostHookForAdditionalParticipant() {
793 $participantID = $this->participantCreate(array(
794 'contact_id' => $this->_contactID,
795 'status_id' => 5,
796 'event_id' => $this->_eventID,
797 ));
798 $participantID2 = $this->participantCreate(array(
799 'contact_id' => $this->_contactID2,
800 'event_id' => $this->_eventID,
801 'status_id' => 5,
802 'registered_by_id' => $participantID,
803 ));
804
805 $this->hookClass->setHook('civicrm_post', array($this, 'onPost'));
806 $params = array(
807 'id' => $participantID,
808 'status_id' => 1,
809 );
810 $this->callAPISuccess('Participant', 'create', $params);
811
812 $result = $this->callAPISuccess('Participant', 'get', array('source' => 'Post Hook Update'));
813 $this->assertEquals(2, $result['count']);
814
815 $expected = array($participantID, $participantID2);
816 $actual = array_keys($result['values']);
817 $this->checkArrayEquals($expected, $actual);
818 }
819
6a488035 820}