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