-
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathRedirectsServiceCacheTest.php
More file actions
84 lines (67 loc) · 2.62 KB
/
Copy pathRedirectsServiceCacheTest.php
File metadata and controls
84 lines (67 loc) · 2.62 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
<?php
namespace nystudio107\retourtests\unit;
use nystudio107\retour\Retour;
use nystudio107\retour\services\Redirects;
use Craft;
use Codeception\Test\Unit;
use UnitTester;
/**
* Regression tests for multi-site redirect cache partitioning
*/
class RedirectsServiceCacheTest extends Unit
{
protected UnitTester $tester;
private Redirects $redirects;
protected function _before(): void
{
parent::_before();
$this->redirects = Retour::$plugin->redirects;
}
public function testSaveRedirectToCacheUsesExplicitSitePartition(): void
{
$sites = Craft::$app->getSites();
$primarySiteId = (int)$sites->getPrimarySite()->id;
$otherSiteId = $primarySiteId === 1 ? 2 : 1;
$row = [
'id' => 999001,
'siteId' => $otherSiteId,
'enabled' => 1,
'redirectSrcUrl' => '/locations',
'redirectSrcUrlParsed' => '/locations',
'redirectSrcMatch' => 'pathonly',
'redirectMatchType' => 'exactmatch',
'redirectDestUrl' => '/our-locations',
'redirectHttpCode' => 301,
];
$path = '/locations';
$this->redirects->saveRedirectToCache($path, $row, $otherSiteId);
$fromPartition = $this->redirects->getRedirectFromCache($path, $otherSiteId);
$this->assertIsArray($fromPartition);
$this->assertSame($otherSiteId, (int)$fromPartition['siteId']);
$wrongPartition = $this->redirects->getRedirectFromCache($path, $primarySiteId);
$this->assertNotTrue((bool)$wrongPartition);
}
public function testFindRedirectMatchIgnoresCacheEntryForDifferentSite(): void
{
$sites = Craft::$app->getSites();
$primarySiteId = (int)$sites->getPrimarySite()->id;
$otherSiteId = $primarySiteId === 1 ? 2 : 1;
$path = '/retour-cache-regression-' . bin2hex(random_bytes(4));
$row = [
'id' => 999002,
'siteId' => $otherSiteId,
'enabled' => 1,
'redirectSrcUrl' => $path,
'redirectSrcUrlParsed' => $path,
'redirectSrcMatch' => 'pathonly',
'redirectMatchType' => 'exactmatch',
'redirectDestUrl' => '/somewhere-else',
'redirectHttpCode' => 301,
];
$this->redirects->saveRedirectToCache($path, $row, $primarySiteId);
$match = $this->redirects->findRedirectMatch('https://example.test' . $path, $path, $primarySiteId);
$this->assertNull($match);
$stillPoisoned = $this->redirects->getRedirectFromCache($path, $primarySiteId);
$this->assertNotTrue((bool)$stillPoisoned);
}
}