Merge pull request #12178 from jitendrapurohit/membership-4
[civicrm-core.git] / tests / phpunit / api / v3 / ParticipantTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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 = array();
60 $this->_individualId = $this->individualCreate();
61
62 $this->_participantID = $this->participantCreate(array(
63 'contact_id' => $this->_contactID,
64 'event_id' => $this->_eventID,
65 ));
66 $this->_contactID2 = $this->individualCreate();
67 $this->_participantID2 = $this->participantCreate(array(
68 'contact_id' => $this->_contactID2,
69 'event_id' => $this->_eventID,
70 'registered_by_id' => $this->_participantID,
71 ));
72 $this->_participantID3 = $this->participantCreate(array(
73 'contact_id' => $this->_contactID2,
74 'event_id' => $this->_eventID,
75 ));
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',
84 );
85 }
86
87 public function tearDown() {
88 $this->eventDelete($this->_eventID);
89 $tablesToTruncate = array(
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 = 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
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', array('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 = array(
182 'participant_id' => $this->_participantID,
183 'return' => array(
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 = array(
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 = 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
230 /**
231 * Check with params id.
232 */
233 public function testGetParamsAsIdOnly() {
234 $params = array(
235 'id' => $this->_participantID,
236 );
237 $result = $this->callAPIAndDocument('participant', 'get', $params, __FUNCTION__, __FILE__);
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 /**
245 * Check with params id.
246 */
247 public function testGetNestedEventGet() {
248 //create a second event & add participant to it.
249 $event = $this->eventCreate(NULL);
250 $this->callAPISuccess('participant', 'create', array(
251 'event_id' => $event['id'],
252 'contact_id' => $this->_contactID,
253 ));
254
255 $description = "Demonstrates use of nested get to fetch event data with participant records.";
256 $subfile = "NestedEventGet";
257 $params = array(
258 'id' => $this->_participantID,
259 'api.event.get' => 1,
260 );
261 $result = $this->callAPIAndDocument('participant', 'get', $params, __FUNCTION__, __FILE__, $description, $subfile);
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 }
267
268 /**
269 * Check Participant Get respects return properties.
270 */
271 public function testGetWithReturnProperties() {
272 $params = array(
273 'contact_id' => $this->_contactID,
274 'return.status_id' => 1,
275 'return.participant_status_id' => 1,
276 'options' => array('limit' => 1),
277 );
278 $result = $this->callAPISuccess('participant', 'get', $params);
279 $this->assertArrayHasKey('participant_status_id', $result['values'][$result['id']]);
280 }
281
282 /**
283 * Check with contact_id.
284 */
285 public function testGetContactIdOnly() {
286 $params = array(
287 'contact_id' => $this->_contactID,
288 );
289 $participant = $this->callAPISuccess('participant', 'get', $params);
290
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']);
296 }
297
298 /**
299 * Check with event_id.
300 * fetch first record
301 */
302 public function testGetMultiMatchReturnFirst() {
303 $params = array(
304 'event_id' => $this->_eventID,
305 'rowCount' => 1,
306 );
307
308 $participant = $this->callAPISuccess('participant', 'get', $params);
309 $this->assertNotNull($participant['id']);
310 }
311
312 /**
313 * Check with event_id.
314 * in v3 this should return all participants
315 */
316 public function testGetMultiMatchNoReturnFirst() {
317 $params = array(
318 'event_id' => $this->_eventID,
319 );
320 $participant = $this->callAPISuccess('participant', 'get', $params);
321 $this->assertNotNull($participant['count'], 3);
322 }
323
324 ///////////////// civicrm_participant_get methods
325
326 /**
327 * Test civicrm_participant_get with empty params.
328 * In this case all the participant records are returned.
329 */
330 public function testSearchEmptyParams() {
331 $result = $this->callAPISuccess('participant', 'get', array());
332 // expecting 3 participant records
333 $this->assertEquals($result['count'], 3);
334 }
335
336 /**
337 * Check with participant_id.
338 */
339 public function testSearchParticipantIdOnly() {
340 $params = array(
341 'participant_id' => $this->_participantID,
342 );
343 $participant = $this->callAPISuccess('participant', 'get', $params);
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 /**
350 * Check with contact_id.
351 */
352 public function testSearchContactIdOnly() {
353 // Should get 2 participant records for this contact.
354 $params = array(
355 'contact_id' => $this->_contactID2,
356 );
357 $participant = $this->callAPISuccess('participant', 'get', $params);
358
359 $this->assertEquals($participant['count'], 2);
360 }
361
362 /**
363 * Check with event_id.
364 */
365 public function testSearchByEvent() {
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,
371 );
372 $participant = $this->callAPISuccess('participant', 'get', $params);
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 /**
382 * Check with event_id.
383 * fetch with limit
384 */
385 public function testSearchByEventWithLimit() {
386 // Should 2 participant records since we're passing rowCount = 2.
387 $params = array(
388 'event_id' => $this->_eventID,
389 'rowCount' => 2,
390 );
391 $participant = $this->callAPISuccess('participant', 'get', $params);
392
393 $this->assertEquals($participant['count'], 2);
394 }
395
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
409 ///////////////// civicrm_participant_create methods
410
411 /**
412 * Test civicrm_participant_create with empty params.
413 */
414 public function testCreateEmptyParams() {
415 $params = array();
416 $result = $this->callAPIFailure('participant', 'create', $params);
417 }
418
419 /**
420 * Check with event_id.
421 */
422 public function testCreateMissingContactID() {
423 $this->callAPIFailure('participant', 'create', ['event_id' => $this->_eventID]);
424 }
425
426 /**
427 * Check with contact_id.
428 * without event_id
429 */
430 public function testCreateMissingEventID() {
431 $this->callAPIFailure('participant', 'create', ['contact_id' => $this->_contactID]);
432 }
433
434 /**
435 * Check with contact_id & event_id
436 */
437 public function testCreateEventIdOnly() {
438 $params = array(
439 'contact_id' => $this->_contactID,
440 'event_id' => $this->_eventID,
441 );
442 $participant = $this->callAPISuccess('participant', 'create', $params);
443 $this->getAndCheck($params, $participant['id'], 'participant');
444 }
445
446 /**
447 * Check with complete array.
448 */
449 public function testCreateAllParams() {
450 $params = $this->_params;
451
452 $participant = $this->callAPISuccess('participant', 'create', $params);
453 $this->_participantID = $participant['id'];
454 // assertDBState compares expected values in $match to actual values in the DB
455 $this->assertDBState('CRM_Event_DAO_Participant', $participant['id'], $params);
456 }
457
458 /**
459 * Test to check if receive date is being changed per CRM-9763
460 */
461 public function testCreateUpdateReceiveDate() {
462 $participant = $this->callAPISuccess('participant', 'create', $this->_params);
463 $update = array(
464 'id' => $participant['id'],
465 'status_id' => 2,
466 );
467 $this->callAPISuccess('participant', 'create', $update);
468 $this->getAndCheck(array_merge($this->_params, $update), $participant['id'], 'participant');
469 }
470
471 /**
472 * Test to check if participant fee level is being changed per CRM-9781
473 */
474 public function testCreateUpdateParticipantFeeLevel() {
475 $myParams = $this->_params + array('participant_fee_level' => CRM_Core_DAO::VALUE_SEPARATOR . "fee" . CRM_Core_DAO::VALUE_SEPARATOR);
476 $participant = $this->callAPISuccess('participant', 'create', $myParams);
477 $update = array(
478 'id' => $participant['id'],
479 'status_id' => 2,
480 );
481 $update = $this->callAPISuccess('participant', 'create', $update);
482
483 $this->assertEquals($participant['values'][$participant['id']]['fee_level'],
484 $update['values'][$participant['id']]['fee_level']
485 );
486
487 $this->callAPISuccess('participant', 'delete', array('id' => $participant['id']));
488 }
489
490 /**
491 * Test the line items for participant fee with multiple price field values.
492 */
493 public function testCreateParticipantLineItems() {
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.
555 // TODO: These assertions depend on the order of the line items, which is
556 // technically incorrect.
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']);
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']));
580 */
581 }
582
583 /**
584 * Check with complete array.
585 */
586 public function testUpdate() {
587 $participantId = $this->participantCreate(array(
588 'contactID' => $this->_individualId,
589 'eventID' => $this->_eventID,
590 ));
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',
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 */
610 public function testUpdateCreateParticipantFeeLevelNoSeparator() {
611
612 $myParams = $this->_params + array('participant_fee_level' => "fee");
613 $participant = $this->callAPISuccess('participant', 'create', $myParams);
614 $this->assertAPISuccess($participant);
615 $update = array(
616 'id' => $participant['id'],
617 'status_id' => 2,
618 );
619 $this->callAPISuccess('participant', 'create', $update);
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 /**
628 * Test civicrm_participant_update with wrong params type.
629 */
630 public function testUpdateWrongParamsType() {
631 $params = 'a string';
632 $result = $this->callAPIFailure('participant', 'create', $params);
633 $this->assertEquals('Input variable `params` is not an array', $result['error_message']);
634 }
635
636 /**
637 * Check with empty array.
638 */
639 public function testUpdateEmptyParams() {
640 $params = array();
641 $participant = $this->callAPIFailure('participant', 'create', $params);
642 $this->assertEquals($participant['error_message'], 'Mandatory key(s) missing from params array: event_id, contact_id');
643 }
644
645 /**
646 * Check without event_id.
647 */
648 public function testUpdateWithoutEventId() {
649 $participantId = $this->participantCreate(array('contactID' => $this->_individualId, 'eventID' => $this->_eventID));
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',
656 'event_level' => 'Donation',
657 );
658 $participant = $this->callAPIFailure('participant', 'create', $params);
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 /**
665 * Check with Invalid participantId.
666 */
667 public function testUpdateWithWrongParticipantId() {
668 $params = array(
669 'id' => 1234,
670 'status_id' => 3,
671 'role_id' => 3,
672 'register_date' => '2006-01-21',
673 'source' => 'US Open',
674 'event_level' => 'Donation',
675 );
676 $participant = $this->callAPIFailure('Participant', 'update', $params);
677 }
678
679 /**
680 * Check with Invalid ContactId.
681 */
682 public function testUpdateWithWrongContactId() {
683 $participantId = $this->participantCreate(array(
684 'contactID' => $this->_individualId,
685 'eventID' => $this->_eventID,
686 ), $this->_apiversion);
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',
694 'event_level' => 'Donation',
695 );
696 $participant = $this->callAPIFailure('participant', 'create', $params);
697 $result = $this->participantDelete($participantId);
698 }
699
700 ///////////////// civicrm_participant_delete methods
701
702 /**
703 * Test civicrm_participant_delete with wrong params type.
704 */
705 public function testDeleteWrongParamsType() {
706 $params = 'a string';
707 $result = $this->callAPIFailure('participant', 'delete', $params);
708 }
709
710 /**
711 * Test civicrm_participant_delete with empty params.
712 */
713 public function testDeleteEmptyParams() {
714 $params = array();
715 $result = $this->callAPIFailure('participant', 'delete', $params);
716 }
717
718 /**
719 * Check with participant_id.
720 */
721 public function testParticipantDelete() {
722 $params = array(
723 'id' => $this->_participantID,
724 );
725 $participant = $this->callAPISuccess('participant', 'delete', $params);
726 $this->assertAPISuccess($participant);
727 $this->assertDBState('CRM_Event_DAO_Participant', $this->_participantID, NULL, TRUE);
728 }
729
730 /**
731 * Check without participant_id.
732 * and with event_id
733 * This should return an error because required param is missing..
734 */
735 public function testParticipantDeleteMissingID() {
736 $params = array(
737 'event_id' => $this->_eventID,
738 );
739 $participant = $this->callAPIFailure('participant', 'delete', $params);
740 $this->assertNotNull($participant['error_message']);
741 }
742
743 /**
744 * Delete with a get - a 'criteria delete'
745 */
746 public function testNestedDelete() {
747 $description = "Criteria delete by nesting a GET & a DELETE.";
748 $subfile = "NestedDelete";
749 $participants = $this->callAPISuccess('Participant', 'Get', array());
750 $this->assertEquals($participants['count'], 3);
751 $params = array('contact_id' => $this->_contactID2, 'api.participant.delete' => 1);
752 $this->callAPIAndDocument('Participant', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile);
753 $check = $this->callAPISuccess('participant', 'getcount', array());
754 $this->assertEquals(1, $check, "only one participant should be left");
755 }
756
757 /**
758 * Test creation of a participant with an associated contribution.
759 */
760 public function testCreateParticipantWithPayment() {
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)";
763 $subfile = "CreateParticipantPayment";
764 $params = array(
765 'contact_type' => 'Individual',
766 'display_name' => 'dlobo',
767 'api.participant' => array(
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
784 $result = $this->callAPIAndDocument('contact', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
785 $this->assertEquals(1, $result['values'][$result['id']]['api.participant_payment.create']['count']);
786 $this->callAPISuccess('contact', 'delete', array('id' => $result['id']));
787 }
788
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
820 }