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