Merge pull request #14222 from mfb/debug-var
[civicrm-core.git] / tests / phpunit / api / v3 / ParticipantTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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
577 // Cleanup
578 $this->callAPISuccess('participant', 'delete', ['id' => $participant['id']]);
579
580 // TODO: I think the price set should be removed, but I don't know how
581 // to decouple it properly from the event. For the moment, I'll just comment
582 // out the lines below.
583
584 /*
585 $this->callAPISuccess('PriceFieldValue', 'delete', array('id' => $pfv1['id']));
586 $this->callAPISuccess('PriceFieldValue', 'delete', array('id' => $pfv2['id']));
587 $this->callAPISuccess('PriceField', 'delete', array('id' => $pricefield['id']));
588 $this->callAPISuccess('PriceSet', 'delete', array('id' => $priceset['id']));
589 */
590 }
591
592 /**
593 * Check with complete array.
594 */
595 public function testUpdate() {
596 $participantId = $this->participantCreate([
597 'contactID' => $this->_individualId,
598 'eventID' => $this->_eventID,
599 ]);
600 $params = [
601 'id' => $participantId,
602 'contact_id' => $this->_individualId,
603 'event_id' => $this->_eventID,
604 'status_id' => 3,
605 'role_id' => 3,
606 'register_date' => '2006-01-21',
607 'source' => 'US Open',
608 ];
609 $participant = $this->callAPISuccess('participant', 'create', $params);
610 $this->getAndCheck($params, $participant['id'], 'participant');
611 $result = $this->participantDelete($params['id']);
612 }
613
614 /**
615 * Test to check if participant fee level is being changed per CRM-9781
616 * Try again without a custom separater to check that one isn't added
617 * (get & check won't accept an array)
618 */
619 public function testUpdateCreateParticipantFeeLevelNoSeparator() {
620
621 $myParams = $this->_params + ['participant_fee_level' => "fee"];
622 $participant = $this->callAPISuccess('participant', 'create', $myParams);
623 $this->assertAPISuccess($participant);
624 $update = [
625 'id' => $participant['id'],
626 'status_id' => 2,
627 ];
628 $this->callAPISuccess('participant', 'create', $update);
629 $this->assertEquals($participant['values'][$participant['id']]['fee_level'],
630 $myParams['participant_fee_level']
631 );
632 $this->getAndCheck($update, $participant['id'], 'participant');
633 }
634
635 ///////////////// civicrm_participant_update methods
636
637 /**
638 * Test civicrm_participant_update with wrong params type.
639 */
640 public function testUpdateWrongParamsType() {
641 $params = 'a string';
642 $result = $this->callAPIFailure('participant', 'create', $params);
643 $this->assertEquals('Input variable `params` is not an array', $result['error_message']);
644 }
645
646 /**
647 * Check with empty array.
648 */
649 public function testUpdateEmptyParams() {
650 $params = [];
651 $participant = $this->callAPIFailure('participant', 'create', $params);
652 $this->assertEquals($participant['error_message'], 'Mandatory key(s) missing from params array: event_id, contact_id');
653 }
654
655 /**
656 * Check without event_id.
657 */
658 public function testUpdateWithoutEventId() {
659 $participantId = $this->participantCreate(['contactID' => $this->_individualId, 'eventID' => $this->_eventID]);
660 $params = [
661 'contact_id' => $this->_individualId,
662 'status_id' => 3,
663 'role_id' => 3,
664 'register_date' => '2006-01-21',
665 'source' => 'US Open',
666 'event_level' => 'Donation',
667 ];
668 $participant = $this->callAPIFailure('participant', 'create', $params);
669 $this->assertEquals($participant['error_message'], 'Mandatory key(s) missing from params array: event_id');
670 // Cleanup created participant records.
671 $result = $this->participantDelete($participantId);
672 }
673
674 /**
675 * Check with Invalid participantId.
676 */
677 public function testUpdateWithWrongParticipantId() {
678 $params = [
679 'id' => 1234,
680 'status_id' => 3,
681 'role_id' => 3,
682 'register_date' => '2006-01-21',
683 'source' => 'US Open',
684 'event_level' => 'Donation',
685 ];
686 $participant = $this->callAPIFailure('Participant', 'update', $params);
687 }
688
689 /**
690 * Check with Invalid ContactId.
691 */
692 public function testUpdateWithWrongContactId() {
693 $participantId = $this->participantCreate([
694 'contactID' => $this->_individualId,
695 'eventID' => $this->_eventID,
696 ], $this->_apiversion);
697 $params = [
698 'id' => $participantId,
699 'contact_id' => 12345,
700 'status_id' => 3,
701 'role_id' => 3,
702 'register_date' => '2006-01-21',
703 'source' => 'US Open',
704 'event_level' => 'Donation',
705 ];
706 $participant = $this->callAPIFailure('participant', 'create', $params);
707 $result = $this->participantDelete($participantId);
708 }
709
710 ///////////////// civicrm_participant_delete methods
711
712 /**
713 * Test civicrm_participant_delete with wrong params type.
714 */
715 public function testDeleteWrongParamsType() {
716 $params = 'a string';
717 $result = $this->callAPIFailure('participant', 'delete', $params);
718 }
719
720 /**
721 * Test civicrm_participant_delete with empty params.
722 */
723 public function testDeleteEmptyParams() {
724 $params = [];
725 $result = $this->callAPIFailure('participant', 'delete', $params);
726 }
727
728 /**
729 * Check with participant_id.
730 */
731 public function testParticipantDelete() {
732 $params = [
733 'id' => $this->_participantID,
734 ];
735 $participant = $this->callAPISuccess('participant', 'delete', $params);
736 $this->assertAPISuccess($participant);
737 $this->assertDBState('CRM_Event_DAO_Participant', $this->_participantID, NULL, TRUE);
738 }
739
740 /**
741 * Check without participant_id.
742 * and with event_id
743 * This should return an error because required param is missing..
744 */
745 public function testParticipantDeleteMissingID() {
746 $params = [
747 'event_id' => $this->_eventID,
748 ];
749 $participant = $this->callAPIFailure('participant', 'delete', $params);
750 $this->assertNotNull($participant['error_message']);
751 }
752
753 /**
754 * Delete with a get - a 'criteria delete'
755 */
756 public function testNestedDelete() {
757 $description = "Criteria delete by nesting a GET & a DELETE.";
758 $subfile = "NestedDelete";
759 $participants = $this->callAPISuccess('Participant', 'Get', []);
760 $this->assertEquals($participants['count'], 3);
761 $params = ['contact_id' => $this->_contactID2, 'api.participant.delete' => 1];
762 $this->callAPIAndDocument('Participant', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile);
763 $check = $this->callAPISuccess('participant', 'getcount', []);
764 $this->assertEquals(1, $check, "only one participant should be left");
765 }
766
767 /**
768 * Test creation of a participant with an associated contribution.
769 */
770 public function testCreateParticipantWithPayment() {
771 $description = "Single function to create contact with partipation & contribution.
772 Note that in the case of 'contribution' the 'create' is implied (api.contribution.create)";
773 $subfile = "CreateParticipantPayment";
774 $params = [
775 'contact_type' => 'Individual',
776 'display_name' => 'dlobo',
777 'api.participant' => [
778 'event_id' => $this->_eventID,
779 'status_id' => 1,
780 'role_id' => 1,
781 'format.only_id' => 1,
782 ],
783 'api.contribution.create' => [
784 'financial_type_id' => 1,
785 'total_amount' => 100,
786 'format.only_id' => 1,
787 ],
788 'api.participant_payment.create' => [
789 'contribution_id' => '$value.api.contribution.create',
790 'participant_id' => '$value.api.participant',
791 ],
792 ];
793
794 $result = $this->callAPIAndDocument('contact', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
795 $this->assertEquals(1, $result['values'][$result['id']]['api.participant_payment.create']['count']);
796 $this->callAPISuccess('contact', 'delete', ['id' => $result['id']]);
797 }
798
799 /**
800 * Test participant invoke post hook after status update.
801 */
802 public function testPostHookForAdditionalParticipant() {
803 $participantID = $this->participantCreate([
804 'contact_id' => $this->_contactID,
805 'status_id' => 5,
806 'event_id' => $this->_eventID,
807 ]);
808 $participantID2 = $this->participantCreate([
809 'contact_id' => $this->_contactID2,
810 'event_id' => $this->_eventID,
811 'status_id' => 5,
812 'registered_by_id' => $participantID,
813 ]);
814
815 $this->hookClass->setHook('civicrm_post', [$this, 'onPost']);
816 $params = [
817 'id' => $participantID,
818 'status_id' => 1,
819 ];
820 $this->callAPISuccess('Participant', 'create', $params);
821
822 $result = $this->callAPISuccess('Participant', 'get', ['source' => 'Post Hook Update']);
823 $this->assertEquals(2, $result['count']);
824
825 $expected = [$participantID, $participantID2];
826 $actual = array_keys($result['values']);
827 $this->checkArrayEquals($expected, $actual);
828 }
829
830 }