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