remove duplicate assignments
[civicrm-core.git] / tests / phpunit / api / v3 / ParticipantTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06a1bc01 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
06a1bc01 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26*/
27
28require_once 'CiviTest/CiviUnitTestCase.php';
29
30/**
31 * Test class for Batch API - civicrm_participant_*
32 *
33 * @package CiviCRM_APIv3
34 */
35require_once 'CRM/Utils/DeprecatedUtils.php';
36require_once 'CiviTest/CiviUnitTestCase.php';
37class api_v3_ParticipantTest extends CiviUnitTestCase {
38
39 protected $_apiversion;
40 protected $_entity;
41 protected $_contactID;
42 protected $_contactID2;
43 protected $_createdParticipants;
44 protected $_participantID;
45 protected $_eventID;
46 protected $_individualId;
47 protected $_params;
6a488035
TO
48
49 function get_info() {
50 return array(
51 'name' => 'Participant Create',
52 'description' => 'Test all Participant Create API methods.',
53 'group' => 'CiviCRM API Tests',
54 );
55 }
56
57 function setUp() {
58 $this->_apiversion = 3;
59 parent::setUp();
60 $this->_entity = 'participant';
61 $event = $this->eventCreate(NULL);
62 $this->_eventID = $event['id'];
63
e4d5f1e2 64 $this->_contactID = $this->individualCreate();
6a488035
TO
65
66 $this->_createdParticipants = array();
e4d5f1e2 67 $this->_individualId = $this->individualCreate();
6a488035 68
9fbee5b6 69 $this->_participantID = $this->participantCreate(array('contact_id' => $this->_contactID, 'event_id' => $this->_eventID));
e4d5f1e2 70 $this->_contactID2 = $this->individualCreate();
afd404ea 71 $this->_participantID2 = $this->participantCreate(array('contact_id' => $this->_contactID2, 'event_id' => $this->_eventID));
72 $this->_participantID3 = $this->participantCreate(array('contact_id' => $this->_contactID2, 'event_id' => $this->_eventID));
6a488035
TO
73 $this->_params = array(
74 'contact_id' => $this->_contactID,
75 'event_id' => $this->_eventID,
76 'status_id' => 1,
77 'role_id' => 1,
78 // to ensure it matches later on
79 'register_date' => '2007-07-21 00:00:00',
80 'source' => 'Online Event Registration: API Testing',
6a488035
TO
81 );
82 }
83
84 function tearDown() {
85 $this->eventDelete($this->_eventID);
86 $tablesToTruncate = array(
87 'civicrm_custom_group', 'civicrm_custom_field', 'civicrm_contact', 'civicrm_participant'
88 );
89 // true tells quickCleanup to drop any tables that might have been created in the test
90 $this->quickCleanup($tablesToTruncate, TRUE);
91 }
92
93 /**
94 * check with complete array + custom field
95 * Note that the test is written on purpose without any
96 * variables specific to participant so it can be replicated into other entities
97 * and / or moved to the automated test suite
98 */
99 function testCreateWithCustom() {
100 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
101
102 $params = $this->_params;
103 $params['custom_' . $ids['custom_field_id']] = "custom string";
104
afd404ea 105 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
106
6a488035 107 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
6a488035 108
afd404ea 109 $check = $this->callAPISuccess($this->_entity, 'get', array('id' => $result['id']));
6a488035
TO
110 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
111
112 $this->customFieldDelete($ids['custom_field_id']);
113 $this->customGroupDelete($ids['custom_group_id']);
114 }
115
116
117 ///////////////// civicrm_participant_get methods
118
119 /**
120 * check with wrong params type
121 */
122 function testGetWrongParamsType() {
123 $params = 'a string';
d0e1eff2 124 $result = $this->callAPIFailure('participant', 'get', $params);
6a488035
TO
125 }
126
127 /**
128 * Test civicrm_participant_get with empty params
129 */
130 function testGetEmptyParams() {
d0e1eff2 131 $this->callAPISuccess('participant', 'get', array());
6a488035
TO
132 }
133
134 /**
135 * check with participant_id
136 */
137 function testGetParticipantIdOnly() {
138 $params = array(
139 'participant_id' => $this->_participantID,
6a488035
TO
140 'return' => array(
141 'participant_id',
142 'event_id',
143 'participant_register_date',
144 'participant_source',
145 )
146 );
afd404ea 147 $result = $this->callAPISuccess('participant', 'get', $params);
6a488035
TO
148 $this->assertAPISuccess($result, " in line " . __LINE__);
149 $this->assertEquals($result['values'][$this->_participantID]['event_id'], $this->_eventID, "in line " . __LINE__);
150 $this->assertEquals($result['values'][$this->_participantID]['participant_register_date'], '2007-02-19 00:00:00', "in line " . __LINE__);
151 $this->assertEquals($result['values'][$this->_participantID]['participant_source'], 'Wimbeldon', "in line " . __LINE__);
152 $params = array(
153 'id' => $this->_participantID,
6a488035
TO
154 'return' => 'id,participant_register_date,event_id',
155
156 );
afd404ea 157 $result = $this->callAPISuccess('participant', 'get', $params);
6a488035
TO
158 $this->assertEquals($result['values'][$this->_participantID]['event_id'], $this->_eventID);
159 $this->assertEquals($result['values'][$this->_participantID]['participant_register_date'], '2007-02-19 00:00:00');
160
161 }
162
163 /**
164 * check with params id
165 */
166 function testGetParamsAsIdOnly() {
167 $params = array(
168 'id' => $this->_participantID,
6a488035 169 );
afd404ea 170 $result = $this->callAPIAndDocument('participant', 'get', $params, __FUNCTION__, __FILE__);
6a488035
TO
171 $this->assertEquals($result['values'][$this->_participantID]['event_id'], $this->_eventID);
172 $this->assertEquals($result['values'][$this->_participantID]['participant_register_date'], '2007-02-19 00:00:00');
173 $this->assertEquals($result['values'][$this->_participantID]['participant_source'], 'Wimbeldon');
174 $this->assertEquals($result['id'], $result['values'][$this->_participantID]['id']);
175 }
176
177 /**
178 * check with params id
179 */
180 function testGetNestedEventGet() {
181 //create a second event & add participant to it.
182 $event = $this->eventCreate(NULL);
afd404ea 183 $this->callAPISuccess('participant', 'create', array('event_id' => $event['id'], 'contact_id' => $this->_contactID));
6a488035
TO
184
185
186 $description = "use nested get to get an event";
187 $subfile = "NestedEventGet";
188 $params = array(
189 'id' => $this->_participantID,
6a488035
TO
190 'api.event.get' => 1,
191 );
afd404ea 192 $result = $this->callAPIAndDocument('participant', 'get', $params, __FUNCTION__, __FILE__, $description, $subfile);
6a488035
TO
193 $this->assertEquals($result['values'][$this->_participantID]['event_id'], $this->_eventID);
194 $this->assertEquals($result['values'][$this->_participantID]['participant_register_date'], '2007-02-19 00:00:00');
195 $this->assertEquals($result['values'][$this->_participantID]['participant_source'], 'Wimbeldon');
196 $this->assertEquals($this->_eventID, $result['values'][$this->_participantID]['api.event.get']['id']);
197 }
198 /*
199 * Check Participant Get respects return properties
200 */
201 function testGetWithReturnProperties() {
202 $params = array(
afd404ea 203 'contact_id' => $this->_contactID, 'return.status_id' => 1,
6a488035
TO
204 'return.participant_status_id' => 1,
205 'options' => array('limit' => 1)
206 );
afd404ea 207 $result = $this->callAPISuccess('participant', 'get', $params);
6a488035
TO
208 $this->assertArrayHasKey('participant_status_id', $result['values'][$result['id']]);
209 }
210
211 /**
212 * check with contact_id
213 */
214 function testGetContactIdOnly() {
215 $params = array(
afd404ea 216 'contact_id' => $this->_contactID, );
217 $participant = $this->callAPISuccess('participant', 'get', $params);
6a488035
TO
218
219 $this->assertEquals($this->_participantID, $participant['id'],
220 "In line " . __LINE__
221 );
222 $this->assertEquals($this->_eventID, $participant['values'][$participant['id']]['event_id'],
223 "In line " . __LINE__
224 );
225 $this->assertEquals('2007-02-19 00:00:00', $participant['values'][$participant['id']]['participant_register_date'],
226 "In line " . __LINE__
227 );
228 $this->assertEquals('Wimbeldon', $participant['values'][$participant['id']]['participant_source'],
229 "In line " . __LINE__
230 );
231 $this->assertEquals($participant['id'], $participant['values'][$participant['id']]['id'],
232 "In line " . __LINE__
233 );
234 }
235
236 /**
237 * check with event_id
238 * fetch first record
239 */
240 function testGetMultiMatchReturnFirst() {
241 $params = array(
242 'event_id' => $this->_eventID,
afd404ea 243 'rowCount' => 1, );
6a488035 244
afd404ea 245 $participant = $this->callAPISuccess('participant', 'get', $params);
6a488035
TO
246 $this->assertNotNull($participant['id']);
247 }
248
249 /**
250 * check with event_id
251 * in v3 this should return all participants
252 */
253 function testGetMultiMatchNoReturnFirst() {
254 $params = array(
255 'event_id' => $this->_eventID,
6a488035 256 );
afd404ea 257 $participant = $this->callAPISuccess('participant', 'get', $params);
6a488035
TO
258 $this->assertNotNull($participant['count'], 3);
259 }
260
261 ///////////////// civicrm_participant_get methods
262
6a488035
TO
263 /**
264 * Test civicrm_participant_get with empty params
265 * In this case all the participant records are returned.
266 */
267 function testSearchEmptyParams() {
afd404ea 268 $result = $this->callAPISuccess('participant', 'get', array());
6a488035
TO
269 // expecting 3 participant records
270 $this->assertEquals($result['count'], 3);
271 }
272
273 /**
274 * check with participant_id
275 */
276 function testSearchParticipantIdOnly() {
277 $params = array(
278 'participant_id' => $this->_participantID,
6a488035 279 );
afd404ea 280 $participant = $this->callAPISuccess('participant', 'get', $params);
6a488035
TO
281 $this->assertEquals($participant['values'][$this->_participantID]['event_id'], $this->_eventID);
282 $this->assertEquals($participant['values'][$this->_participantID]['participant_register_date'], '2007-02-19 00:00:00');
283 $this->assertEquals($participant['values'][$this->_participantID]['participant_source'], 'Wimbeldon');
284 }
285
286 /**
287 * check with contact_id
288 */
289 function testSearchContactIdOnly() {
290 // Should get 2 participant records for this contact.
291 $params = array(
292 'contact_id' => $this->_contactID2,
6a488035 293 );
afd404ea 294 $participant = $this->callAPISuccess('participant', 'get', $params);
6a488035
TO
295
296 $this->assertEquals($participant['count'], 2);
297 }
298
299 /**
300 * check with event_id
301 */
302 function testSearchByEvent() {
303 // Should get >= 3 participant records for this event. Also testing that last_name and event_title are returned.
304 $params = array(
305 'event_id' => $this->_eventID,
306 'return.last_name' => 1,
307 'return.event_title' => 1,
6a488035 308 );
afd404ea 309 $participant = $this->callAPISuccess('participant', 'get', $params);
6a488035
TO
310 if ($participant['count'] < 3) {
311 $this->fail("Event search returned less than expected miniumum of 3 records.");
312 }
313
314 $this->assertEquals($participant['values'][$this->_participantID]['last_name'], 'Anderson');
315 $this->assertEquals($participant['values'][$this->_participantID]['event_title'], 'Annual CiviCRM meet');
316 }
317
318 /**
319 * check with event_id
320 * fetch with limit
321 */
322 function testSearchByEventWithLimit() {
323 // Should 2 participant records since we're passing rowCount = 2.
324 $params = array(
325 'event_id' => $this->_eventID,
326 'rowCount' => 2,
6a488035 327 );
afd404ea 328 $participant = $this->callAPISuccess('participant', 'get', $params);
6a488035
TO
329
330 $this->assertEquals($participant['count'], 2, 'in line ' . __LINE__);
331 }
332
333 ///////////////// civicrm_participant_create methods
334
6a488035
TO
335 /**
336 * Test civicrm_participant_create with empty params
337 */
338 function testCreateEmptyParams() {
339 $params = array();
d0e1eff2 340 $result = $this->callAPIFailure('participant', 'create', $params);
6a488035
TO
341 }
342
343 /**
344 * check with event_id
345 */
346 function testCreateMissingContactID() {
347 $params = array(
348 'event_id' => $this->_eventID,
6a488035 349 );
afd404ea 350 $participant = $this->callAPIFailure('participant', 'create', $params);
6a488035
TO
351 }
352
353 /**
354 * check with contact_id
355 * without event_id
356 */
357 function testCreateMissingEventID() {
358 $params = array(
359 'contact_id' => $this->_contactID,
6a488035 360 );
afd404ea 361 $participant = $this->callAPIFailure('participant', 'create', $params);
6a488035
TO
362 }
363
364 /**
365 * check with contact_id & event_id
366 */
367 function testCreateEventIdOnly() {
368 $params = array(
369 'contact_id' => $this->_contactID,
370 'event_id' => $this->_eventID,
6a488035 371 );
afd404ea 372 $participant = $this->callAPISuccess('participant', 'create', $params);
373 $this->getAndCheck($params, $participant['id'], 'participant');
6a488035
TO
374 }
375
376 /**
377 * check with complete array
378 */
379 function testCreateAllParams() {
380 $params = $this->_params;
381
afd404ea 382 $participant = $this->callAPISuccess('participant', 'create', $params);
6a488035 383 $this->_participantID = $participant['id'];
afd404ea 384 // assertDBState compares expected values in $match to actual values in the DB
385 $this->assertDBState('CRM_Event_DAO_Participant', $participant['id'], $params);
6a488035
TO
386 }
387 /*
388 * Test to check if receive date is being changed per CRM-9763
389 */
390 function testCreateUpdateReceiveDate() {
afd404ea 391 $participant = $this->callAPISuccess('participant', 'create', $this->_params);
6a488035 392 $update = array(
afd404ea 393 'id' => $participant['id'],
6a488035
TO
394 'status_id' => 2,
395 );
afd404ea 396 $this->callAPISuccess('participant', 'create', $update);
6a488035
TO
397 $this->getAndCheck(array_merge($this->_params, $update), $participant['id'], 'participant');
398 }
399 /*
400 * Test to check if participant fee level is being changed per CRM-9781
401 */
402 function testCreateUpdateParticipantFeeLevel() {
403 $myParams = $this->_params + array('participant_fee_level' => CRM_Core_DAO::VALUE_SEPARATOR . "fee" . CRM_Core_DAO::VALUE_SEPARATOR);
afd404ea 404 $participant = $this->callAPISuccess('participant', 'create', $myParams);
6a488035 405 $update = array(
6ead217b 406 'id' => $participant['id'],
6a488035
TO
407 'status_id' => 2,
408 );
6ead217b
E
409 $update = $this->callAPISuccess('participant', 'create', $update);
410
411 $this->assertEquals($participant['values'][$participant['id']]['fee_level'],
412 $update['values'][$participant['id']]['fee_level']
6a488035
TO
413 );
414
afd404ea 415 $this->callAPISuccess('participant', 'delete', array('id' => $participant['id']));
6a488035 416 }
afd404ea 417 /**
418 * check with complete array
419 */
420 function testUpdate() {
6ead217b
E
421 $participantId = $this->participantCreate(array(
422 'contactID' => $this->_individualId,
423 'eventID' => $this->_eventID
424 ));
afd404ea 425 $params = array(
426 'id' => $participantId,
427 'contact_id' => $this->_individualId,
428 'event_id' => $this->_eventID,
429 'status_id' => 3,
430 'role_id' => 3,
431 'register_date' => '2006-01-21',
432 'source' => 'US Open',
afd404ea 433 );
434 $participant = $this->callAPISuccess('participant', 'create', $params);
435 $this->getAndCheck($params, $participant['id'], 'participant');
436 $result = $this->participantDelete($params['id']);
437 }
438
439 /**
440 * Test to check if participant fee level is being changed per CRM-9781
441 * Try again without a custom separater to check that one isn't added
442 * (get & check won't accept an array)
443 */
6a488035
TO
444 function testUpdateCreateParticipantFeeLevelNoSeparator() {
445
446 $myParams = $this->_params + array('participant_fee_level' => "fee");
afd404ea 447 $participant = $this->callAPISuccess('participant', 'create', $myParams);
6a488035
TO
448 $this->assertAPISuccess($participant);
449 $update = array(
afd404ea 450 'id' => $participant['id'],
6a488035
TO
451 'status_id' => 2,
452 );
afd404ea 453 $this->callAPISuccess('participant', 'create', $update);
6a488035
TO
454 $this->assertEquals($participant['values'][$participant['id']]['fee_level'],
455 $myParams['participant_fee_level']
456 );
457 $this->getAndCheck($update, $participant['id'], 'participant');
458 }
459 ///////////////// civicrm_participant_update methods
460
461 /**
462 * Test civicrm_participant_update with wrong params type
463 */
464 function testUpdateWrongParamsType() {
465 $params = 'a string';
d0e1eff2 466 $result = $this->callAPIFailure('participant', 'create', $params);
6a488035
TO
467 $this->assertEquals('Input variable `params` is not an array', $result['error_message'], 'In line ' . __LINE__);
468 }
469
470 /**
471 * check with empty array
472 */
473 function testUpdateEmptyParams() {
afd404ea 474 $params = array();
d0e1eff2 475 $participant = $this->callAPIFailure('participant', 'create', $params);
6a488035
TO
476 $this->assertEquals($participant['error_message'], 'Mandatory key(s) missing from params array: event_id, contact_id');
477 }
478
479 /**
480 * check without event_id
481 */
482 function testUpdateWithoutEventId() {
afd404ea 483 $participantId = $this->participantCreate(array('contactID' => $this->_individualId, 'eventID' => $this->_eventID));
6a488035
TO
484 $params = array(
485 'contact_id' => $this->_individualId,
486 'status_id' => 3,
487 'role_id' => 3,
488 'register_date' => '2006-01-21',
489 'source' => 'US Open',
afd404ea 490 'event_level' => 'Donation', );
d0e1eff2 491 $participant = $this->callAPIFailure('participant', 'create', $params);
6a488035
TO
492 $this->assertEquals($participant['error_message'], 'Mandatory key(s) missing from params array: event_id');
493 // Cleanup created participant records.
494 $result = $this->participantDelete($participantId);
495 }
496
497 /**
498 * check with Invalid participantId
499 */
500 function testUpdateWithWrongParticipantId() {
501 $params = array(
502 'id' => 1234,
503 'status_id' => 3,
504 'role_id' => 3,
505 'register_date' => '2006-01-21',
506 'source' => 'US Open',
afd404ea 507 'event_level' => 'Donation', );
d0e1eff2 508 $participant = $this->callAPIFailure('Participant', 'update', $params);
6a488035
TO
509 }
510
511 /**
512 * check with Invalid ContactId
513 */
514 function testUpdateWithWrongContactId() {
515 $participantId = $this->participantCreate(array(
516 'contactID' => $this->_individualId,
517 'eventID' => $this->_eventID,
518 ), $this->_apiversion);
519 $params = array(
520 'id' => $participantId,
521 'contact_id' => 12345,
522 'status_id' => 3,
523 'role_id' => 3,
524 'register_date' => '2006-01-21',
525 'source' => 'US Open',
afd404ea 526 'event_level' => 'Donation', );
d0e1eff2 527 $participant = $this->callAPIFailure('participant', 'create', $params);
6a488035
TO
528 $result = $this->participantDelete($participantId);
529 }
530
6a488035
TO
531 ///////////////// civicrm_participant_delete methods
532
533 /**
534 * Test civicrm_participant_delete with wrong params type
535 */
536 function testDeleteWrongParamsType() {
537 $params = 'a string';
d0e1eff2 538 $result = $this->callAPIFailure('participant', 'delete', $params);
6a488035
TO
539 }
540
541 /**
542 * Test civicrm_participant_delete with empty params
543 */
544 function testDeleteEmptyParams() {
545 $params = array();
d0e1eff2 546 $result = $this->callAPIFailure('participant', 'delete', $params);
6a488035
TO
547 }
548
549 /**
550 * check with participant_id
551 */
552 function testParticipantDelete() {
553 $params = array(
afd404ea 554 'id' => $this->_participantID, );
555 $participant = $this->callAPISuccess('participant', 'delete', $params);
6a488035
TO
556 $this->assertAPISuccess($participant);
557 $this->assertDBState('CRM_Event_DAO_Participant', $this->_participantID, NULL, TRUE);
558 }
559
560 /**
561 * check without participant_id
562 * and with event_id
563 * This should return an error because required param is missing..
564 */
565 function testParticipantDeleteMissingID() {
566 $params = array(
afd404ea 567 'event_id' => $this->_eventID, );
d0e1eff2 568 $participant = $this->callAPIFailure('participant', 'delete', $params);
6a488035
TO
569 $this->assertNotNull($participant['error_message']);
570 }
571 /*
572 * delete with a get - a 'criteria delete'
573 */
574 function testNestedDelete() {
575 $description = "Criteria delete by nesting a GET & a DELETE";
576 $subfile = "NestedDelete";
afd404ea 577 $participants = $this->callAPISuccess('Participant', 'Get', array());
6a488035 578 $this->assertEquals($participants['count'], 3);
afd404ea 579 $params = array('contact_id' => $this->_contactID2, 'api.participant.delete' => 1);
580 $participants = $this->callAPIAndDocument('Participant', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile, 'Get');
81e1a644 581 $check = $this->callAPISuccess('participant', 'getcount', array());
582 $this->assertEquals(1, $check,"only one participant should be left. line " . __LINE__);
6a488035
TO
583 }
584 /*
585 * Test creation of a participant with an associated contribution
586 */
587 function testCreateParticipantWithPayment() {
6a488035
TO
588 $description = "single function to create contact w partipation & contribution. Note that in the
589 case of 'contribution' the 'create' is implied (api.contribution.create)";
590 $subfile = "CreateParticipantPayment";
591 $params = array(
592 'contact_type' => 'Individual',
81e1a644 593 'display_name' => 'dlobo',
594 'api.participant' => array(
6a488035
TO
595 'event_id' => $this->_eventID,
596 'status_id' => 1,
597 'role_id' => 1,
598 'format.only_id' => 1,
599 ),
600 'api.contribution.create' => array(
601 'financial_type_id' => 1,
602 'total_amount' => 100,
603 'format.only_id' => 1,
604 ),
605 'api.participant_payment.create' => array(
606 'contribution_id' => '$value.api.contribution.create',
607 'participant_id' => '$value.api.participant',
608 ),
609 );
610
afd404ea 611 $result = $this->callAPIAndDocument('contact', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
6a488035 612 $this->assertEquals(1, $result['values'][$result['id']]['api.participant_payment.create']['count']);
afd404ea 613 $this->callAPISuccess('contact', 'delete', array('id' => $result['id']));
6a488035 614 }
6a488035
TO
615}
616