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