[NFC] Fix PHPUnit8 Deprecation warnings in the api_v4 Test Suite
[civicrm-core.git] / tests / phpunit / api / v4 / Entity / ParticipantTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
6 | |
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 +--------------------------------------------------------------------+
11 */
12
13 /**
14 *
15 * @package CRM
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 */
18
19
20 namespace api\v4\Entity;
21
22 use Civi\Api4\Participant;
23 use api\v4\UnitTestCase;
24
25 /**
26 * @group headless
27 */
28 class ParticipantTest extends UnitTestCase {
29
30 public function setUp(): void {
31 parent::setUp();
32 $cleanup_params = [
33 'tablesToTruncate' => [
34 'civicrm_event',
35 'civicrm_participant',
36 ],
37 ];
38 $this->cleanup($cleanup_params);
39 }
40
41 public function testGetActions() {
42 $result = Participant::getActions(FALSE)
43 ->execute()
44 ->indexBy('name');
45
46 $getParams = $result['get']['params'];
47 $whereDescription = 'Criteria for selecting Participants';
48
49 $this->assertEquals(TRUE, $getParams['checkPermissions']['default']);
50 $this->assertStringContainsString($whereDescription, $getParams['where']['description']);
51 }
52
53 public function testGet() {
54 $rows = $this->getRowCount('civicrm_participant');
55 if ($rows > 0) {
56 $this->markTestSkipped('Participant table must be empty');
57 }
58
59 // With no records:
60 $result = Participant::get(FALSE)->execute();
61 $this->assertEquals(0, $result->count(), "count of empty get is not 0");
62
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);
67
68 // Create some test related records before proceeding
69 $participantCount = 20;
70 $contactCount = 7;
71 $eventCount = 5;
72
73 // All events will either have this number or one less because of the
74 // rotating participation creation method.
75 $expectedFirstEventCount = ceil($participantCount / $eventCount);
76
77 $dummy = [
78 'contacts' => $this->createEntity([
79 'type' => 'Individual',
80 'count' => $contactCount,
81 'seq' => 1,
82 ]),
83 'events' => $this->createEntity([
84 'type' => 'Event',
85 'count' => $eventCount,
86 'seq' => 1,
87 ]),
88 'sources' => ['Paddington', 'Springfield', 'Central'],
89 ];
90
91 // - create dummy participants record
92 for ($i = 0; $i < $participantCount; $i++) {
93 $dummy['participants'][$i] = $this->sample([
94 'type' => 'Participant',
95 'overrides' => [
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],
100 ],
101 ])['sample_params'];
102
103 Participant::create()
104 ->setValues($dummy['participants'][$i])
105 ->setCheckPermissions(FALSE)
106 ->execute();
107 }
108 $sqlCount = $this->getRowCount('civicrm_participant');
109 $this->assertEquals($participantCount, $sqlCount, "Unexpected count");
110
111 $firstEventId = $dummy['events'][0]['id'];
112 $secondEventId = $dummy['events'][1]['id'];
113 $firstContactId = $dummy['contacts'][0]['id'];
114
115 $firstOnlyResult = Participant::get(FALSE)
116 ->addClause('AND', ['event_id', '=', $firstEventId])
117 ->execute();
118
119 $this->assertEquals($expectedFirstEventCount, count($firstOnlyResult),
120 "count of first event is not $expectedFirstEventCount");
121
122 // get first two events using different methods
123 $firstTwo = Participant::get(FALSE)
124 ->addWhere('event_id', 'IN', [$firstEventId, $secondEventId])
125 ->execute();
126
127 $firstResult = $firstTwo->first();
128
129 // verify counts
130 // count should either twice the first event count or one less
131 $this->assertLessThanOrEqual(
132 $expectedFirstEventCount * 2,
133 count($firstTwo),
134 "count is too high"
135 );
136
137 $this->assertGreaterThanOrEqual(
138 $expectedFirstEventCount * 2 - 1,
139 count($firstTwo),
140 "count is too low"
141 );
142
143 $firstParticipantResult = Participant::get(FALSE)
144 ->addWhere('event_id', '=', $firstEventId)
145 ->addWhere('contact_id', '=', $firstContactId)
146 ->execute();
147
148 $this->assertEquals(1, count($firstParticipantResult), "more than one registration");
149
150 $firstParticipantId = $firstParticipantResult->first()['id'];
151
152 // get a result which excludes $first_participant
153 $otherParticipantResult = Participant::get(FALSE)
154 ->setSelect(['id'])
155 ->addClause('NOT', [
156 ['event_id', '=', $firstEventId],
157 ['contact_id', '=', $firstContactId],
158 ])
159 ->execute()
160 ->indexBy('id');
161
162 // check alternate syntax for NOT
163 $otherParticipantResult2 = Participant::get(FALSE)
164 ->setSelect(['id'])
165 ->addClause('NOT', 'AND', [
166 ['event_id', '=', $firstEventId],
167 ['contact_id', '=', $firstContactId],
168 ])
169 ->execute()
170 ->indexBy('id');
171
172 $this->assertEquals($otherParticipantResult, $otherParticipantResult2);
173
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:
178
179 $this->assertFalse(
180 $otherParticipantResult->offsetExists($firstParticipantId),
181 'excluded wrong record');
182
183 // check syntax for date-range
184
185 $getParticipantsById = function($wheres = []) {
186 return Participant::get(FALSE)
187 ->setWhere($wheres)
188 ->execute()
189 ->indexBy('id');
190 };
191
192 $thisYearParticipants = $getParticipantsById([['register_date', '=', 'this.year']]);
193 $this->assertFalse(isset($thisYearParticipants[$firstParticipantId]));
194
195 $otherYearParticipants = $getParticipantsById([['register_date', '!=', 'this.year']]);
196 $this->assertTrue(isset($otherYearParticipants[$firstParticipantId]));
197
198 Participant::update()->setCheckPermissions(FALSE)
199 ->addWhere('id', '=', $firstParticipantId)
200 ->addValue('register_date', 'now')
201 ->execute();
202
203 $thisYearParticipants = $getParticipantsById([['register_date', '=', 'this.year']]);
204 $this->assertTrue(isset($thisYearParticipants[$firstParticipantId]));
205
206 // retrieve a participant record and update some records
207 $patchRecord = [
208 'source' => "not " . $firstResult['source'],
209 ];
210
211 Participant::update()
212 ->addWhere('event_id', '=', $firstEventId)
213 ->setCheckPermissions(FALSE)
214 ->setLimit(20)
215 ->setValues($patchRecord)
216 ->setCheckPermissions(FALSE)
217 ->execute();
218
219 // - delete some records
220 $secondEventId = $dummy['events'][1]['id'];
221 $deleteResult = Participant::delete()
222 ->addWhere('event_id', '=', $secondEventId)
223 ->setCheckPermissions(FALSE)
224 ->execute();
225 $expectedDeletes = [2, 7, 12, 17];
226 $this->assertEquals($expectedDeletes, array_column((array) $deleteResult, 'id'),
227 "didn't delete every second record as expected");
228
229 $sqlCount = $this->getRowCount('civicrm_participant');
230 $this->assertEquals(
231 $participantCount - count($expectedDeletes),
232 $sqlCount,
233 "records not gone from database after delete");
234
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)
241 ->execute();
242 }
243
244 // By default is_test participants are hidden
245 $this->assertCount(0, Participant::get()->selectRowCount()->addWhere('event_id', '=', $secondEventId)->execute());
246
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);
250
251 // Or if you search by id
252 $this->assertCount(1, Participant::get()->selectRowCount()->addWhere('id', '=', $testParticipants->first()['id'])->execute());
253 }
254
255 }