Commit | Line | Data |
---|---|---|
ab723d18 TO |
1 | <?php |
2 | ||
3 | namespace E2E\Core; | |
4 | ||
5 | /** | |
6 | * Class PrevNextTest | |
7 | * | |
8 | * Check that the active prev-next service behaves as expected. | |
9 | * | |
10 | * @package E2E\Core | |
47113d77 | 11 | * @group e2e ornery |
ab723d18 TO |
12 | */ |
13 | class PrevNextTest extends \CiviEndToEndTestCase { | |
14 | ||
15 | /** | |
16 | * @var string | |
17 | */ | |
39b959db SL |
18 | protected $cacheKey; |
19 | ||
20 | /** | |
21 | * @var string | |
22 | */ | |
23 | protected $cacheKeyB; | |
ab723d18 TO |
24 | |
25 | /** | |
26 | * @var \CRM_Core_PrevNextCache_Interface | |
27 | */ | |
28 | protected $prevNext; | |
29 | ||
0b49aa04 | 30 | protected function setUp(): void { |
ab723d18 TO |
31 | parent::setUp(); |
32 | $this->prevNext = \Civi::service('prevnext'); | |
33 | $this->cacheKey = 'PrevNextTest_' . \CRM_Utils_String::createRandom(16, \CRM_Utils_String::ALPHANUMERIC); | |
7cbfc706 | 34 | $this->cacheKeyB = 'PrevNextTestb_' . \CRM_Utils_String::createRandom(16, \CRM_Utils_String::ALPHANUMERIC); |
ab723d18 TO |
35 | $this->assertTrue( |
36 | \CRM_Core_DAO::singleValueQuery('SELECT count(*) FROM civicrm_contact') > 25, | |
37 | 'The contact table must have at least 25 records.' | |
38 | ); | |
39 | } | |
40 | ||
5d345864 | 41 | protected function tearDown(): void { |
ab723d18 | 42 | \Civi::service('prevnext')->deleteItem(NULL, $this->cacheKey); |
7cbfc706 | 43 | \Civi::service('prevnext')->deleteItem(NULL, $this->cacheKeyB); |
379b29a0 | 44 | parent::tearDown(); |
ab723d18 TO |
45 | } |
46 | ||
47 | public function testFillSql() { | |
48 | $start = 0; | |
49 | $prefillLimit = 25; | |
50 | $sort = NULL; | |
51 | ||
ea714811 | 52 | $query = new \CRM_Contact_BAO_Query([['sort_name', 'IS NOT NULL', 1, 0, 0]], NULL, NULL, FALSE, FALSE, 1, FALSE, TRUE, FALSE, NULL, 'AND'); |
ab723d18 TO |
53 | $sql = $query->searchQuery($start, $prefillLimit, $sort, FALSE, $query->_includeContactIds, |
54 | FALSE, TRUE, TRUE); | |
2c8b42d5 | 55 | $selectSQL = "SELECT DISTINCT %1, contact_a.id, contact_a.sort_name"; |
ab723d18 TO |
56 | $sql = str_replace(array("SELECT contact_a.id as contact_id", "SELECT contact_a.id as id"), $selectSQL, $sql); |
57 | ||
58 | $this->assertTrue( | |
2c8b42d5 | 59 | $this->prevNext->fillWithSql($this->cacheKey, $sql, [1 => [$this->cacheKey, 'String']]), |
ab723d18 TO |
60 | "fillWithSql should return TRUE on success" |
61 | ); | |
62 | ||
63 | $this->assertEquals(25, $this->prevNext->getCount($this->cacheKey)); | |
64 | $this->assertEquals(0, $this->prevNext->getCount('not-a-key-' . $this->cacheKey)); | |
65 | ||
66 | $all = $this->prevNext->getSelection($this->cacheKey, 'getall')[$this->cacheKey]; | |
67 | $this->assertCount($prefillLimit, $all); | |
68 | $this->assertCount($prefillLimit, array_unique(array_keys($all))); | |
69 | $this->assertEquals([1], array_unique(array_values($all))); | |
70 | ||
71 | $this->assertSelections([]); | |
72 | } | |
73 | ||
74 | public function testFillArray() { | |
75 | $rowSetA = [ | |
76 | ['entity_id1' => 100, 'data' => 'Alice'], | |
77 | ['entity_id1' => 400, 'data' => 'Bob'], | |
78 | ['entity_id1' => 200, 'data' => 'Carol'], | |
79 | ]; | |
80 | $rowSetB = [ | |
81 | ['entity_id1' => 300, 'data' => 'Dave'], | |
82 | ]; | |
83 | ||
84 | $this->assertTrue( | |
85 | $this->prevNext->fillWithArray($this->cacheKey, $rowSetA), | |
86 | "fillWithArray should return TRUE on success" | |
87 | ); | |
88 | $this->assertTrue( | |
89 | $this->prevNext->fillWithArray($this->cacheKey, $rowSetB), | |
90 | "fillWithArray should return TRUE on success" | |
91 | ); | |
92 | ||
93 | $this->assertEquals(4, $this->prevNext->getCount($this->cacheKey)); | |
94 | $this->assertEquals(0, $this->prevNext->getCount('not-a-key-' . $this->cacheKey)); | |
95 | ||
3e9cd8a8 | 96 | $all = $this->assertSelections([100, 400, 200, 300], 'getall', $this->cacheKey); |
ab723d18 TO |
97 | $this->assertEquals([1], array_unique(array_values($all))); |
98 | ||
3e9cd8a8 | 99 | $this->assertSelections([], 'get', $this->cacheKey); |
ab723d18 TO |
100 | } |
101 | ||
2ca46d4d TO |
102 | public function testFetch() { |
103 | $this->testFillArray(); | |
104 | ||
105 | $cids = $this->prevNext->fetch($this->cacheKey, 0, 2); | |
106 | $this->assertEquals([100, 400], $cids); | |
107 | ||
108 | $cids = $this->prevNext->fetch($this->cacheKey, 0, 4); | |
109 | $this->assertEquals([100, 400, 200, 300], $cids); | |
110 | ||
111 | $cids = $this->prevNext->fetch($this->cacheKey, 2, 2); | |
112 | $this->assertEquals([200, 300], $cids); | |
113 | } | |
114 | ||
ab723d18 TO |
115 | public function getFillFunctions() { |
116 | return [ | |
117 | ['testFillSql'], | |
118 | ['testFillArray'], | |
119 | ]; | |
120 | } | |
121 | ||
122 | /** | |
123 | * Select and unselect one item. | |
124 | * | |
125 | * @dataProvider getFillFunctions | |
126 | */ | |
127 | public function testMarkSelection_1($fillFunction) { | |
128 | call_user_func([$this, $fillFunction]); | |
129 | ||
130 | $all = $this->prevNext->getSelection($this->cacheKey, 'getall')[$this->cacheKey]; | |
131 | list ($id1, $id2) = array_keys($all); | |
132 | $this->prevNext->markSelection($this->cacheKey, 'select', $id1); | |
133 | ||
134 | $this->assertSelections([$id1]); | |
135 | ||
136 | $this->prevNext->markSelection($this->cacheKey, 'unselect', $id1); | |
137 | $this->assertSelections([]); | |
138 | } | |
139 | ||
140 | /** | |
141 | * Select and unselect two items. | |
142 | * | |
143 | * @dataProvider getFillFunctions | |
144 | */ | |
145 | public function testMarkSelection_2($fillFunction) { | |
146 | call_user_func([$this, $fillFunction]); | |
147 | ||
148 | $all = $this->prevNext->getSelection($this->cacheKey, 'getall')[$this->cacheKey]; | |
149 | list ($id1, $id2, $id3) = array_keys($all); | |
150 | ||
151 | $this->prevNext->markSelection($this->cacheKey, 'select', [$id1, $id3]); | |
152 | $this->assertSelections([$id1, $id3]); | |
153 | ||
154 | $this->prevNext->markSelection($this->cacheKey, 'unselect', $id1); | |
155 | $this->assertSelections([$id3]); | |
156 | ||
157 | $this->prevNext->markSelection($this->cacheKey, 'select', $id2); | |
158 | $this->assertSelections([$id2, $id3]); | |
159 | ||
160 | $this->prevNext->markSelection($this->cacheKey, 'unselect'); | |
161 | $this->assertSelections([]); | |
162 | } | |
163 | ||
164 | /** | |
165 | * Check the neighbors of the first item. | |
166 | * | |
167 | * @dataProvider getFillFunctions | |
168 | */ | |
169 | public function testGetPosition_first($fillFunction) { | |
170 | call_user_func([$this, $fillFunction]); | |
171 | ||
172 | $all = $this->prevNext->getSelection($this->cacheKey, 'getall')[$this->cacheKey]; | |
173 | list ($id1, $id2, $id3) = array_keys($all); | |
174 | ||
175 | $pos = $this->prevNext->getPositions($this->cacheKey, $id1); | |
176 | ||
177 | $this->assertTrue((bool) $pos['foundEntry']); | |
178 | ||
179 | $this->assertEquals($id2, $pos['next']['id1']); | |
180 | $this->assertTrue(!empty($pos['next']['data'])); | |
181 | ||
182 | $this->assertTrue(!isset($pos['prev'])); | |
183 | } | |
184 | ||
185 | /** | |
186 | * Check the neighbors of a middle item. | |
187 | * | |
188 | * @dataProvider getFillFunctions | |
189 | */ | |
190 | public function testGetPosition_middle($fillFunction) { | |
191 | call_user_func([$this, $fillFunction]); | |
192 | ||
193 | $all = $this->prevNext->getSelection($this->cacheKey, 'getall')[$this->cacheKey]; | |
194 | list ($id1, $id2, $id3) = array_keys($all); | |
195 | ||
196 | $pos = $this->prevNext->getPositions($this->cacheKey, $id2); | |
197 | $this->assertTrue((bool) $pos['foundEntry']); | |
198 | ||
199 | $this->assertEquals($id3, $pos['next']['id1']); | |
200 | $this->assertTrue(!empty($pos['next']['data'])); | |
201 | ||
202 | $this->assertEquals($id1, $pos['prev']['id1']); | |
203 | $this->assertTrue(!empty($pos['prev']['data'])); | |
204 | } | |
205 | ||
206 | /** | |
207 | * Check the neighbors of the last item. | |
208 | * | |
209 | * @dataProvider getFillFunctions | |
210 | */ | |
211 | public function testGetPosition_last($fillFunction) { | |
212 | call_user_func([$this, $fillFunction]); | |
213 | ||
214 | $all = $this->prevNext->getSelection($this->cacheKey, 'getall')[$this->cacheKey]; | |
215 | list ($idLast, $idPrev) = array_reverse(array_keys($all)); | |
216 | ||
217 | $pos = $this->prevNext->getPositions($this->cacheKey, $idLast); | |
218 | $this->assertTrue((bool) $pos['foundEntry']); | |
219 | ||
220 | $this->assertTrue(!isset($pos['next'])); | |
221 | ||
222 | $this->assertEquals($idPrev, $pos['prev']['id1']); | |
223 | $this->assertTrue(!empty($pos['prev']['data'])); | |
224 | } | |
225 | ||
226 | /** | |
227 | * Check the neighbors of the last item. | |
228 | * | |
229 | * @dataProvider getFillFunctions | |
230 | */ | |
231 | public function testGetPosition_invalid($fillFunction) { | |
232 | call_user_func([$this, $fillFunction]); | |
233 | ||
234 | $pos = $this->prevNext->getPositions($this->cacheKey, 99999999); | |
235 | $this->assertFalse((bool) $pos['foundEntry']); | |
236 | $this->assertTrue(!isset($pos['next'])); | |
237 | $this->assertTrue(!isset($pos['prev'])); | |
238 | } | |
239 | ||
073ba4f0 TO |
240 | public function testDeleteByCacheKey() { |
241 | // Add background data | |
242 | $this->prevNext->fillWithArray($this->cacheKeyB, [ | |
243 | ['entity_id1' => 100, 'data' => 'Alice'], | |
244 | ['entity_id1' => 150, 'data' => 'Dave'], | |
245 | ]); | |
246 | $this->prevNext->markSelection($this->cacheKeyB, 'select', 100); | |
247 | $this->assertSelections([100], 'get', $this->cacheKeyB); | |
248 | $this->assertSelections([100, 150], 'getall', $this->cacheKeyB); | |
249 | ||
250 | // Add some data that we're actually working with. | |
ab723d18 TO |
251 | $this->testFillArray(); |
252 | ||
3e9cd8a8 | 253 | $all = $this->assertSelections([100, 400, 200, 300], 'getall', $this->cacheKey); |
ab723d18 TO |
254 | |
255 | list ($id1, $id2, $id3) = array_keys($all); | |
256 | $this->prevNext->markSelection($this->cacheKey, 'select', [$id1, $id3]); | |
3e9cd8a8 | 257 | $this->assertSelections([$id1, $id3], 'get', $this->cacheKey); |
ab723d18 TO |
258 | |
259 | $this->prevNext->deleteItem(NULL, $this->cacheKey); | |
3e9cd8a8 TO |
260 | $this->assertSelections([], 'getall', $this->cacheKey); |
261 | $this->assertSelections([], 'get', $this->cacheKey); | |
073ba4f0 TO |
262 | |
263 | // Ensure background data was untouched. | |
264 | $this->assertSelections([100], 'get', $this->cacheKeyB); | |
265 | $this->assertSelections([100, 150], 'getall', $this->cacheKeyB); | |
266 | } | |
267 | ||
268 | public function testDeleteByEntityId() { | |
269 | // Fill two caches | |
270 | $this->prevNext->fillWithArray($this->cacheKey, [ | |
271 | ['entity_id1' => 100, 'data' => 'Alice'], | |
272 | ['entity_id1' => 150, 'data' => 'Dave'], | |
273 | ]); | |
274 | $this->prevNext->markSelection($this->cacheKey, 'select', 100); | |
275 | $this->assertSelections([100], 'get', $this->cacheKey); | |
276 | $this->assertSelections([100, 150], 'getall', $this->cacheKey); | |
277 | ||
278 | $this->prevNext->fillWithArray($this->cacheKeyB, [ | |
279 | ['entity_id1' => 100, 'data' => 'Alice'], | |
280 | ['entity_id1' => 400, 'data' => 'Bob'], | |
281 | ]); | |
282 | $this->prevNext->markSelection($this->cacheKeyB, 'select', [100, 400]); | |
283 | $this->assertSelections([100, 400], 'get', $this->cacheKeyB); | |
284 | $this->assertSelections([100, 400], 'getall', $this->cacheKeyB); | |
285 | ||
286 | // Delete | |
287 | $this->prevNext->deleteItem(100); | |
288 | $this->assertSelections([], 'get', $this->cacheKey); | |
289 | $this->assertSelections([150], 'getall', $this->cacheKey); | |
290 | $this->assertSelections([400], 'get', $this->cacheKeyB); | |
291 | $this->assertSelections([400], 'getall', $this->cacheKeyB); | |
292 | } | |
293 | ||
294 | public function testDeleteAll() { | |
295 | // Fill two caches | |
296 | $this->prevNext->fillWithArray($this->cacheKey, [ | |
297 | ['entity_id1' => 100, 'data' => 'Alice'], | |
298 | ['entity_id1' => 150, 'data' => 'Dave'], | |
299 | ]); | |
300 | $this->prevNext->markSelection($this->cacheKey, 'select', 100); | |
301 | $this->assertSelections([100], 'get', $this->cacheKey); | |
302 | $this->assertSelections([100, 150], 'getall', $this->cacheKey); | |
303 | ||
304 | $this->prevNext->fillWithArray($this->cacheKeyB, [ | |
305 | ['entity_id1' => 100, 'data' => 'Alice'], | |
306 | ['entity_id1' => 400, 'data' => 'Bob'], | |
307 | ]); | |
308 | $this->prevNext->markSelection($this->cacheKeyB, 'select', [100, 400]); | |
309 | $this->assertSelections([100, 400], 'get', $this->cacheKeyB); | |
310 | $this->assertSelections([100, 400], 'getall', $this->cacheKeyB); | |
311 | ||
312 | // Delete | |
313 | $this->prevNext->deleteItem(NULL, NULL); | |
314 | $this->assertSelections([], 'get', $this->cacheKey); | |
315 | $this->assertSelections([], 'getall', $this->cacheKey); | |
316 | $this->assertSelections([], 'get', $this->cacheKeyB); | |
317 | $this->assertSelections([], 'getall', $this->cacheKeyB); | |
ab723d18 TO |
318 | } |
319 | ||
ab723d18 TO |
320 | /** |
321 | * Assert that the current cacheKey has a list of selected contact IDs. | |
322 | * | |
323 | * @param array $ids | |
324 | * Contact IDs that should be selected. | |
39b959db SL |
325 | * @param string $action |
326 | * @param string|NULL $cacheKey | |
3e9cd8a8 TO |
327 | * @return array |
328 | * Contact IDs that were returned by getSelection($cacheKey, $action) | |
ab723d18 | 329 | */ |
073ba4f0 TO |
330 | protected function assertSelections($ids, $action = 'get', $cacheKey = NULL) { |
331 | if ($cacheKey === NULL) { | |
332 | $cacheKey = $this->cacheKey; | |
333 | } | |
334 | $selected = $this->prevNext->getSelection($cacheKey, $action)[$cacheKey]; | |
8cf1fcac | 335 | $this->assertEquals($ids, array_keys($selected), 'selected cache not correct for ' . $cacheKey |
336 | . ' defined keys are ' . $this->cacheKey . 'and ' . $this->cacheKeyB | |
337 | . ' result from getall is ' . print_r($this->prevNext->getSelection($cacheKey, 'getall'), 1) | |
338 | . ' and the prevNext cache is ' . print_r($this->prevNext, TRUE) | |
339 | ); | |
340 | ||
ab723d18 | 341 | $this->assertCount(count($ids), $selected); |
3e9cd8a8 | 342 | return $selected; |
ab723d18 TO |
343 | } |
344 | ||
345 | } |