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