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