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