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