-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCacheKeyExtensionTest.php
More file actions
480 lines (380 loc) · 16 KB
/
CacheKeyExtensionTest.php
File metadata and controls
480 lines (380 loc) · 16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
<?php
namespace Terraformers\KeysForCache\Tests\Extensions;
use Page;
use PHPUnit\Framework\Attributes\DataProvider;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\ORM\DataList;
use SilverStripe\Versioned\Versioned;
use Terraformers\KeysForCache\Models\CacheKey;
use Terraformers\KeysForCache\Services\ProcessedUpdatesService;
use Terraformers\KeysForCache\Tests\Mocks\Pages\CachePage;
use Terraformers\KeysForCache\Tests\Mocks\Pages\NoCachePage;
class CacheKeyExtensionTest extends SapphireTest
{
protected static $fixture_file = 'CacheKeyExtensionTest.yml'; // phpcs:ignore
/**
* @phpcsSuppress SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint
* @var array
*/
protected static $extra_dataobjects = [
CachePage::class,
];
#[DataProvider('readingModes')]
public function testWriteGeneratesCacheKey(string $readingMode): void
{
$page = Versioned::withVersionedMode(static function () use ($readingMode): CachePage {
// Testing within our requested reading mode
Versioned::set_stage($readingMode);
// Page config is $has_cache_key = true, so when we write this record it should generate a CacheKey
$page = CachePage::create();
// We are performing this test across both reading modes, but we expect CacheKeys to respect the action,
// rather than the reading mode (that being, write() creates DRAFT, and publish() creates LIVE)
$page->write();
return $page;
});
Versioned::withVersionedMode(function () use ($page): void {
// Always fetch in DRAFT stage (because we expect this CacheKey to only be available in DRAFT)
Versioned::set_stage(Versioned::DRAFT);
// Re-fetch all CacheKeys for this ClassName and ID
$cacheKeys = CacheKey::get()->filter([
'RecordClass' => $page->ClassName,
'RecordID' => $page->ID,
]);
// There should be exactly 1
$this->assertCount(1, $cacheKeys);
/** @var CacheKey $cacheKey */
$cacheKey = $cacheKeys->first();
// Performing write() on our Page should mean that the CacheKey is not published, regardless of what
// reading mode we were in
$this->assertFalse($cacheKey->isPublished());
});
}
#[DataProvider('readingModes')]
public function testWriteDoesNotGenerateCacheKey(string $readingMode): void
{
Versioned::withVersionedMode(function () use ($readingMode): void {
// Testing within our requested reading mode
Versioned::set_stage($readingMode);
// Page config is $has_cache_key = false, so when we write this record it should not generate a CacheKey
$page = NoCachePage::create();
$page->write();
// We expect getCacheKey() to always return null
$this->assertNull($page->getCacheKey());
// Re-fetch all CacheKeys for this ClassName and ID
$cacheKeys = CacheKey::get()->filter([
'RecordClass' => $page->ClassName,
'RecordID' => $page->ID,
]);
// There shouldn't be any CacheKeys
$this->assertCount(0, $cacheKeys);
});
}
public function testGetCacheKey(): void
{
// Page config is $has_cache_key = true, so when we write this record it should generate a CacheKey
$page = CachePage::create();
$page->write();
// Fetch all CacheKeys for this ClassName and ID
$keys = CacheKey::get()->filter([
'RecordClass' => $page->ClassName,
'RecordID' => $page->ID,
]);
// There should be exactly 1
$this->assertCount(1, $keys);
/** @var CacheKey $key */
$key = $keys->first();
$pageKey = $page->getCacheKey();
$this->assertNotNull($pageKey);
$this->assertEquals($key->KeyHash, $pageKey);
}
public function testGetCacheKeyRegeneratedDraftOnly(): void
{
// For this test we need a published Page with no CacheKey
/** @var CachePage $page */
$page = CachePage::create();
$page->write();
$page->publishRecursive();
// Fetch all CacheKeys for this ClassName and ID
/** @var DataList|CacheKey[] $keys */
$keys = CacheKey::get()->filter([
'RecordClass' => $page->ClassName,
'RecordID' => $page->ID,
]);
// Delete them, so we know we're at square one with no keys available in the DB
foreach ($keys as $key) {
$key->doArchive();
}
// Now that we're set up for the test, we want to attempt to find the CacheKeyHash for this page while we're in
// a DRAFT reading mode
$keyHash = Versioned::withVersionedMode(static function () use ($page): ?string {
// Specifically fetching with DRAFT reading mode
Versioned::set_stage(Versioned::DRAFT);
// Expected behaviour here is that we should have created a brand new CacheKey for this Page, but we won't
// publish it because we're not in a LIVE reading mode
return $page->getCacheKey();
});
$this->assertNotNull($keyHash);
$this->assertNotEmpty($keyHash);
// Re-fetch CacheKeys for this page
/** @var DataList|CacheKey[] $keys */
$keys = CacheKey::get()->filter([
'RecordClass' => $page->ClassName,
'RecordID' => $page->ID,
]);
// We expect there to only be one
$this->assertCount(1, $keys);
/** @var CacheKey $key */
$key = $keys->first();
// We expect that the CacheKey was not published
$this->assertFalse($key->isPublished());
}
public function testGetCacheKeyRegeneratedAndPublished(): void
{
// For this test we need a published Page with no CacheKey
/** @var CachePage $page */
$page = CachePage::create();
$page->write();
$page->publishRecursive();
// Fetch all CacheKeys for this ClassName and ID
/** @var DataList|CacheKey[] $keys */
$keys = CacheKey::get()->filter([
'RecordClass' => $page->ClassName,
'RecordID' => $page->ID,
]);
// Delete them, so we know we're at square one with no keys available in the DB
foreach ($keys as $key) {
$key->doArchive();
}
// Now that we're set up for the test, we want to attempt to find the CacheKeyHash for this page while we're in
// a LIVE reading mode
$keyHash = Versioned::withVersionedMode(static function () use ($page): ?string {
// Specifically fetching with LIVE reading mode
Versioned::set_stage(Versioned::LIVE);
// Expected behaviour here is that we should have created a brand new CacheKey for this Page, and we should
// publish it because we're in a LIVE reading mode
return $page->getCacheKey();
});
$this->assertNotNull($keyHash);
$this->assertNotEmpty($keyHash);
// Re-fetch CacheKeys for this page
/** @var DataList|CacheKey[] $keys */
$keys = CacheKey::get()->filter([
'RecordClass' => $page->ClassName,
'RecordID' => $page->ID,
]);
// We expect there to only be one
$this->assertCount(1, $keys);
/** @var CacheKey $key */
$key = $keys->first();
// We expect that one CacheKey to have been published
$this->assertTrue($key->isPublished());
}
public function testGetCacheKeyPublishedFromDraft(): void
{
// For this test we need a published Page with no CacheKey
/** @var CachePage $page */
$page = CachePage::create();
$page->write();
$page->publishRecursive();
// Fetch all CacheKeys for this ClassName and ID
/** @var DataList|CacheKey[] $keys */
$keys = CacheKey::get()->filter([
'RecordClass' => $page->ClassName,
'RecordID' => $page->ID,
]);
// Make sure there is only one CacheKey
$this->assertCount(1, $keys);
/** @var CacheKey $key */
$key = $keys->first();
// Save away our KeyHash so that we can check it doesn't change
$keyHashOriginal = $key->KeyHash;
// Unpublish that one CacheKey
$key->doUnpublish();
// Re-fetch our key just to make sure that it is unpublished
$key = CacheKey::get()->byID($key->ID);
$this->assertNotNull($key);
$this->assertFalse($key->isPublished());
// Now that we're set up for the test, we want to attempt to find the CacheKeyHash for this page while we're in
// a LIVE reading mode
$keyHashNew = Versioned::withVersionedMode(static function () use ($page): ?string {
// Specifically fetching with LIVE reading mode
Versioned::set_stage(Versioned::LIVE);
// Expected behaviour here is that we should find the original CacheKey (DRAFT only) and publish it
return $page->getCacheKey();
});
$this->assertNotNull($keyHashNew);
// This is the same CacheKey record as before, so this shouldn't have changed
$this->assertSame($keyHashOriginal, $keyHashNew);
// Re-fetch CacheKeys for this page
/** @var DataList|CacheKey[] $keys */
$keys = CacheKey::get()->filter([
'RecordClass' => $page->ClassName,
'RecordID' => $page->ID,
]);
// We expect there to only be one
$this->assertCount(1, $keys);
/** @var CacheKey $key */
$key = $keys->first();
// We expect that one CacheKey to have been published
$this->assertTrue($key->isPublished());
}
public function testCacheKeyStagesDiffer(): void
{
// Page config is $has_cache_key = true, so when we write this record it should generate a CacheKey
$page = CachePage::create();
$page->write();
$page->publishRecursive();
// Fetch all CacheKeys for this ClassName and ID to make sure we're set up correctly
/** @var DataList|CacheKey[] $keys */
$keys = CacheKey::get()->filter([
'RecordClass' => $page->ClassName,
'RecordID' => $page->ID,
]);
// Make sure we have exactly one
$this->assertCount(1, $keys);
// Ok, so we've confirmed that we only have 1 CacheKey for our Page, now we want to fetch that CacheKey in
// specific Stages (DRAFT vs LIVE)
// Here's our CacheKey in DRAFT
$draftOriginal = Versioned::withVersionedMode(static function () use ($page): ?string {
// Specifically fetching with DRAFT reading mode
Versioned::set_stage(Versioned::DRAFT);
$cacheKey = CacheKey::get()
->filter([
'RecordClass' => $page->ClassName,
'RecordID' => $page->ID,
])
->first();
if (!$cacheKey) {
return null;
}
return $cacheKey->KeyHash;
});
// Here's our CacheKey in LIVE
$liveOriginal = Versioned::withVersionedMode(static function () use ($page): ?string {
// Specifically fetching with DRAFT reading mode
Versioned::set_stage(Versioned::LIVE);
$cacheKey = CacheKey::get()
->filter([
'RecordClass' => $page->ClassName,
'RecordID' => $page->ID,
])
->first();
if (!$cacheKey) {
return null;
}
return $cacheKey->KeyHash;
});
// Make sure they both existed
$this->assertNotNull($draftOriginal);
$this->assertNotNull($liveOriginal);
// These should be exactly the same, because we've recently published our Page
$this->assertEquals($draftOriginal, $liveOriginal);
// Need to flush our service so that keys are updated with future changes
ProcessedUpdatesService::singleton()->flush();
// Cool. So now let's make a DRAFT only change on our Page
$page->forceChange();
$page->write();
// Re-fetch our CacheKeys. This time we'll expect them to be different
// Here's our CacheKey in DRAFT
/** @var CacheKey $draftNew */
$draftNew = Versioned::withVersionedMode(static function () use ($page): ?string {
// Specifically fetching with DRAFT reading mode
Versioned::set_stage(Versioned::DRAFT);
$cacheKey = CacheKey::get()
->filter([
'RecordClass' => $page->ClassName,
'RecordID' => $page->ID,
])
->first();
if (!$cacheKey) {
return null;
}
return $cacheKey->KeyHash;
});
// Here's our CacheKey in LIVE
/** @var CacheKey $liveNew */
$liveNew = Versioned::withVersionedMode(static function () use ($page): ?string {
// Specifically fetching with DRAFT reading mode
Versioned::set_stage(Versioned::LIVE);
$cacheKey = CacheKey::get()
->filter([
'RecordClass' => $page->ClassName,
'RecordID' => $page->ID,
])
->first();
if (!$cacheKey) {
return null;
}
return $cacheKey->KeyHash;
});
// Make sure they both existed
$this->assertNotNull($draftNew);
$this->assertNotNull($liveNew);
// We would expect the new DRAFT to differ from the original DRAFT
$this->assertNotEquals($draftOriginal, $draftNew);
// We would expect the new DRAFT and new LIVE to differ
$this->assertNotEquals($draftNew, $liveNew);
// We would expect the new LIVE to be the same as the original LIVE
$this->assertEquals($liveOriginal, $liveNew);
}
public function testGetCacheKeyNoDb(): void
{
$page = CachePage::create();
$this->assertNull($page->getCacheKey());
}
public function testGetCacheKeyNoKeyConfig(): void
{
$page = NoCachePage::create();
$page->write();
$this->assertNull($page->getCacheKey());
}
public function testUpdateCMSFields(): void
{
Page::config()->set('enable_cache_keys_field', true);
$page = CachePage::create();
$page->write();
$fields = $page->getCMSFields();
$this->assertNotNull($fields->dataFieldByName('CacheKeys'));
}
public function testUpdateCMSFieldsNoDisplay(): void
{
Page::config()->set('enable_cache_keys_field', false);
$page = CachePage::create();
$page->write();
$fields = $page->getCMSFields();
$this->assertNull($fields->dataFieldByName('CacheKeys'));
}
public function testUpdateCMSFieldsNoKeyConfig(): void
{
Page::config()->set('enable_cache_keys_field', true);
$page = NoCachePage::create();
$page->write();
$fields = $page->getCMSFields();
$this->assertNull($fields->dataFieldByName('CacheKeys'));
}
public function testIgnoreList(): void
{
// Add our CachePage to the ignorelist
$ignoreList = CacheKey::config()->get('ignorelist');
$ignoreList['CachePage'] = CachePage::class;
CacheKey::config()->set('ignorelist', $ignoreList);
// Create the Page
$page = CachePage::create();
$page->write();
// No CacheKey should have been generated
$this->assertCount(
0,
CacheKey::get()->filter([
'RecordClass' => $page->ClassName,
'RecordID' => $page->ID,
])
);
}
public static function readingModes(): array
{
return [
[Versioned::DRAFT],
[Versioned::LIVE],
];
}
}