4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
10 +--------------------------------------------------------------------+
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
20 namespace api\v
4\Entity
;
22 use Civi\Api4\Participant
;
23 use api\v
4\UnitTestCase
;
28 class ParticipantTest
extends UnitTestCase
{
30 public function setUp(): void
{
33 'tablesToTruncate' => [
35 'civicrm_participant',
38 $this->cleanup($cleanup_params);
41 public function testGetActions() {
42 $result = Participant
::getActions(FALSE)
46 $getParams = $result['get']['params'];
47 $whereDescription = 'Criteria for selecting Participants';
49 $this->assertEquals(TRUE, $getParams['checkPermissions']['default']);
50 $this->assertStringContainsString($whereDescription, $getParams['where']['description']);
53 public function testGet() {
54 $rows = $this->getRowCount('civicrm_participant');
56 $this->markTestSkipped('Participant table must be empty');
60 $result = Participant
::get(FALSE)->execute();
61 $this->assertEquals(0, $result->count(), "count of empty get is not 0");
63 // Check that the $result knows what the inputs were
64 $this->assertEquals('Participant', $result->entity
);
65 $this->assertEquals('get', $result->action
);
66 $this->assertEquals(4, $result->version
);
68 // Create some test related records before proceeding
69 $participantCount = 20;
73 // All events will either have this number or one less because of the
74 // rotating participation creation method.
75 $expectedFirstEventCount = ceil($participantCount / $eventCount);
78 'contacts' => $this->createEntity([
79 'type' => 'Individual',
80 'count' => $contactCount,
83 'events' => $this->createEntity([
85 'count' => $eventCount,
88 'sources' => ['Paddington', 'Springfield', 'Central'],
91 // - create dummy participants record
92 for ($i = 0; $i < $participantCount; $i++
) {
93 $dummy['participants'][$i] = $this->sample([
94 'type' => 'Participant',
96 'event_id' => $dummy['events'][$i %
$eventCount]['id'],
97 'contact_id' => $dummy['contacts'][$i %
$contactCount]['id'],
98 // 3 = number of sources
99 'source' => $dummy['sources'][$i %
3],
103 Participant
::create()
104 ->setValues($dummy['participants'][$i])
105 ->setCheckPermissions(FALSE)
108 $sqlCount = $this->getRowCount('civicrm_participant');
109 $this->assertEquals($participantCount, $sqlCount, "Unexpected count");
111 $firstEventId = $dummy['events'][0]['id'];
112 $secondEventId = $dummy['events'][1]['id'];
113 $firstContactId = $dummy['contacts'][0]['id'];
115 $firstOnlyResult = Participant
::get(FALSE)
116 ->addClause('AND', ['event_id', '=', $firstEventId])
119 $this->assertEquals($expectedFirstEventCount, count($firstOnlyResult),
120 "count of first event is not $expectedFirstEventCount");
122 // get first two events using different methods
123 $firstTwo = Participant
::get(FALSE)
124 ->addWhere('event_id', 'IN', [$firstEventId, $secondEventId])
127 $firstResult = $firstTwo->first();
130 // count should either twice the first event count or one less
131 $this->assertLessThanOrEqual(
132 $expectedFirstEventCount * 2,
137 $this->assertGreaterThanOrEqual(
138 $expectedFirstEventCount * 2 - 1,
143 $firstParticipantResult = Participant
::get(FALSE)
144 ->addWhere('event_id', '=', $firstEventId)
145 ->addWhere('contact_id', '=', $firstContactId)
148 $this->assertEquals(1, count($firstParticipantResult), "more than one registration");
150 $firstParticipantId = $firstParticipantResult->first()['id'];
152 // get a result which excludes $first_participant
153 $otherParticipantResult = Participant
::get(FALSE)
156 ['event_id', '=', $firstEventId],
157 ['contact_id', '=', $firstContactId],
162 // check alternate syntax for NOT
163 $otherParticipantResult2 = Participant
::get(FALSE)
165 ->addClause('NOT', 'AND', [
166 ['event_id', '=', $firstEventId],
167 ['contact_id', '=', $firstContactId],
172 $this->assertEquals($otherParticipantResult, $otherParticipantResult2);
174 $this->assertEquals($participantCount - 1,
175 count($otherParticipantResult),
176 "failed to exclude a single record on complex criteria");
177 // check the record we have excluded is the right one:
180 $otherParticipantResult->offsetExists($firstParticipantId),
181 'excluded wrong record');
183 // check syntax for date-range
185 $getParticipantsById = function($wheres = []) {
186 return Participant
::get(FALSE)
192 $thisYearParticipants = $getParticipantsById([['register_date', '=', 'this.year']]);
193 $this->assertFalse(isset($thisYearParticipants[$firstParticipantId]));
195 $otherYearParticipants = $getParticipantsById([['register_date', '!=', 'this.year']]);
196 $this->assertTrue(isset($otherYearParticipants[$firstParticipantId]));
198 Participant
::update()->setCheckPermissions(FALSE)
199 ->addWhere('id', '=', $firstParticipantId)
200 ->addValue('register_date', 'now')
203 $thisYearParticipants = $getParticipantsById([['register_date', '=', 'this.year']]);
204 $this->assertTrue(isset($thisYearParticipants[$firstParticipantId]));
206 // retrieve a participant record and update some records
208 'source' => "not " . $firstResult['source'],
211 Participant
::update()
212 ->addWhere('event_id', '=', $firstEventId)
213 ->setCheckPermissions(FALSE)
215 ->setValues($patchRecord)
216 ->setCheckPermissions(FALSE)
219 // - delete some records
220 $secondEventId = $dummy['events'][1]['id'];
221 $deleteResult = Participant
::delete()
222 ->addWhere('event_id', '=', $secondEventId)
223 ->setCheckPermissions(FALSE)
225 $expectedDeletes = [2, 7, 12, 17];
226 $this->assertEquals($expectedDeletes, array_column((array) $deleteResult, 'id'),
227 "didn't delete every second record as expected");
229 $sqlCount = $this->getRowCount('civicrm_participant');
231 $participantCount - count($expectedDeletes),
233 "records not gone from database after delete");
235 // Try creating is_test participants
236 foreach ($dummy['contacts'] as $contact) {
237 Participant
::create()
238 ->addValue('is_test', 1)
239 ->addValue('contact_id', $contact['id'])
240 ->addValue('event_id', $secondEventId)
244 // By default is_test participants are hidden
245 $this->assertCount(0, Participant
::get()->selectRowCount()->addWhere('event_id', '=', $secondEventId)->execute());
247 // Test records show up if you add is_test to the query
248 $testParticipants = Participant
::get()->addWhere('event_id', '=', $secondEventId)->addWhere('is_test', '=', 1)->addSelect('id')->execute();
249 $this->assertCount($contactCount, $testParticipants);
251 // Or if you search by id
252 $this->assertCount(1, Participant
::get()->selectRowCount()->addWhere('id', '=', $testParticipants->first()['id'])->execute());