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