44 | canShowMessage()): ?>
45 |
46 | Report URI is not configured to use sansec watch
47 |
48 | Configured Report URIs:
49 | getConfigurationPaths() as $path): ?>
50 | getConfigurationStatus($path); ?>
51 |
52 | - = $escaper->escapeHtml($path) ?> = = $escaper->escapeHtml(json_encode($block->getReportUri($path))) ?>
53 |
54 |
55 |
56 |
57 |
Report URI is configured correctly.
58 |
59 |
60 |
--------------------------------------------------------------------------------
/tests/Model/Command/UpdatePoliciesTest.php:
--------------------------------------------------------------------------------
1 | connection = self::createMock(AdapterInterface::class);
27 |
28 | $resourceConnection = self::createStub(ResourceConnection::class);
29 | $resourceConnection
30 | ->method('getConnection')
31 | ->willReturn($this->connection);
32 |
33 | $this->updatePoliciesCommand = new UpdatePolicies($resourceConnection);
34 | }
35 |
36 | #[Test]
37 | public function doesNotExecuteInsertWhenNoPoliciesAreGiven(): void
38 | {
39 | $this->connection
40 | ->expects(self::never())
41 | ->method('insertMultiple');
42 |
43 | /** @noinspection PhpUnhandledExceptionInspection */
44 | $this->updatePoliciesCommand->execute([]);
45 | }
46 |
47 | #[Test]
48 | public function deletesExistingRulesBeforeUpdating(): void
49 | {
50 | $this->connection
51 | ->expects(self::once())
52 | ->method('delete');
53 |
54 | /** @noinspection PhpUnhandledExceptionInspection */
55 | $this->updatePoliciesCommand->execute([]);
56 | }
57 |
58 | #[Test]
59 | public function databaseQueriesAreRunInTransaction(): void
60 | {
61 | $this->connection
62 | ->expects(self::once())
63 | ->method('beginTransaction');
64 |
65 | $this->connection
66 | ->expects(self::once())
67 | ->method('commit');
68 |
69 | /** @noinspection PhpUnhandledExceptionInspection */
70 | $this->updatePoliciesCommand->execute([]);
71 | }
72 |
73 | #[Test]
74 | public function transactionIsRolledBackInCaseOfAnyError(): void
75 | {
76 | self::expectException(CouldNotUpdatePoliciesException::class);
77 |
78 | $this->connection
79 | ->expects(self::once())
80 | ->method('beginTransaction');
81 |
82 | $this->connection
83 | ->method('commit')
84 | ->willThrowException(new \Exception('Something went wrong'));
85 |
86 | $this->connection
87 | ->expects(self::once())
88 | ->method('rollBack');
89 |
90 | $this->updatePoliciesCommand->execute([]);
91 | }
92 | }
93 |
--------------------------------------------------------------------------------
/tests/Model/Query/GetAllPoliciesTest.php:
--------------------------------------------------------------------------------
1 | connection = self::createStub(AdapterInterface::class);
28 | $this->connection
29 | ->method('select')
30 | ->willReturn($select);
31 |
32 | $resourceConnection = self::createStub(ResourceConnection::class);
33 | $resourceConnection
34 | ->method('getConnection')
35 | ->willReturn($this->connection);
36 |
37 | $this->getAllPoliciesQuery = new GetAllPolicies($resourceConnection);
38 | }
39 |
40 | #[Test]
41 | public function returnEmptyListIfAnyExceptionIsThrown(): void
42 | {
43 | $this->connection
44 | ->method('fetchAll')
45 | ->willThrowException(new \Exception('Something went wrong'));
46 |
47 | self::assertCount(0, $this->getAllPoliciesQuery->execute());
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/tests/Service/PolicyUpdaterTest.php:
--------------------------------------------------------------------------------
1 | flagDataMapper = self::createStub(SansecWatchFlagMapper::class);
42 | $this->clock = self::createStub(ClockInterface::class);
43 | $this->flagManager = self::createMock(FlagManager::class);
44 | $this->updatePolicies = self::createMock(UpdatePolicies::class);
45 | $this->updateFpc = self::createMock(UpdateFpc::class);
46 | $eventManager = self::createStub(ManagerInterface::class);
47 |
48 | $this->policyUpdater = new PolicyUpdater(
49 | $this->flagDataMapper,
50 | $this->flagManager,
51 | $this->updatePolicies,
52 | $this->clock,
53 | $this->updateFpc,
54 | $eventManager,
55 | );
56 | }
57 |
58 | #[Test]
59 | public function pageCacheIsClearedIfPoliciesAreUpdated(): void
60 | {
61 | $this->flagDataIsReturned(
62 | new SansecWatchFlag(
63 | hash('sha256', serialize([])),
64 | new DateTimeImmutable(),
65 | new DateTimeImmutable(),
66 | )
67 | );
68 |
69 | $this->updateFpc
70 | ->expects(self::once())
71 | ->method('execute');
72 |
73 | /** @noinspection PhpUnhandledExceptionInspection */
74 | $this->policyUpdater->updatePolicies([new Policy('script-src', '*.integer-net.de')]);
75 | }
76 |
77 | #[Test]
78 | public function pageCacheIsIgnoredIfPoliciesAreNotUpdated(): void
79 | {
80 | $policies = [new Policy('script-src', '*.integer-net.de')];
81 |
82 | $this->flagDataIsReturned(
83 | new SansecWatchFlag(
84 | hash('sha256', serialize($policies)),
85 | new DateTimeImmutable(),
86 | new DateTimeImmutable(),
87 | )
88 | );
89 |
90 | $this->updateFpc
91 | ->expects(self::never())
92 | ->method('execute');
93 |
94 | /** @noinspection PhpUnhandledExceptionInspection */
95 | $this->policyUpdater->updatePolicies($policies);
96 | }
97 |
98 | #[Test]
99 | public function policiesAreNotUpdatedIfHashDoesMatch(): void
100 | {
101 | $policies = [new Policy('script-src', '*.integer-net.de')];
102 |
103 | $this->flagDataIsReturned(
104 | new SansecWatchFlag(
105 | hash('sha256', serialize($policies)),
106 | new DateTimeImmutable(),
107 | new DateTimeImmutable(),
108 | )
109 | );
110 |
111 | $this->updatePolicies
112 | ->expects(self::never())
113 | ->method('execute');
114 |
115 | /** @noinspection PhpUnhandledExceptionInspection */
116 | $this->policyUpdater->updatePolicies($policies);
117 | }
118 |
119 | #[Test]
120 | public function lastCheckedUpIsUpdatedWhenHashMatches(): void
121 | {
122 | $policies = [new Policy('script-src', '*.integer-net.de')];
123 | $hash = hash('sha256', serialize($policies));
124 | $now = new DateTimeImmutable();
125 | $yesterday = $now->sub(DateInterval::createFromDateString('1 day'));
126 |
127 | $this->clock
128 | ->method('now')
129 | ->willReturn($now);
130 |
131 | $this->flagDataIsReturned(new SansecWatchFlag($hash, $now, $yesterday));
132 |
133 | $this->flagManager
134 | ->expects(self::once())
135 | ->method('saveFlag')
136 | ->with(
137 | SansecWatchFlag::CODE,
138 | [
139 | 'hash' => $hash,
140 | 'last_checked_at' => $now->format(DATE_ATOM),
141 | 'last_updated_at' => $yesterday->format(DATE_ATOM),
142 | ]
143 | );
144 |
145 | /** @noinspection PhpUnhandledExceptionInspection */
146 | $this->policyUpdater->updatePolicies($policies);
147 | }
148 |
149 | #[Test]
150 | public function flagDataIsAlwaysUpdatedIfForceIsTrue(): void
151 | {
152 | $policies = [new Policy('script-src', '*.integer-net.de')];
153 | $hash = hash('sha256', serialize($policies));
154 | $now = new DateTimeImmutable();
155 | $yesterday = $now->sub(DateInterval::createFromDateString('1 day'));
156 |
157 | $this->clock
158 | ->method('now')
159 | ->willReturn($now);
160 |
161 | $this->flagDataIsReturned(new SansecWatchFlag($hash, $now, $yesterday));
162 |
163 | $this->updatePolicies
164 | ->expects(self::once())
165 | ->method('execute')
166 | ->with($policies);
167 |
168 | $this->flagManager
169 | ->expects(self::once())
170 | ->method('saveFlag')
171 | ->with(
172 | SansecWatchFlag::CODE,
173 | [
174 | 'hash' => $hash,
175 | 'last_checked_at' => $now->format(DATE_ATOM),
176 | 'last_updated_at' => $now->format(DATE_ATOM),
177 | ]
178 | );
179 |
180 | /** @noinspection PhpUnhandledExceptionInspection */
181 | $this->policyUpdater->updatePolicies($policies, true);
182 | }
183 |
184 | private function flagDataIsReturned(?SansecWatchFlag $flag): void
185 | {
186 | $this->flagManager
187 | ->method('getFlagData')
188 | ->willReturn([]);
189 |
190 | $this->flagDataMapper
191 | ->method('map')
192 | ->willReturn($flag);
193 | }
194 | }
195 |
--------------------------------------------------------------------------------
/tests/Service/UpdateFpcTest.php:
--------------------------------------------------------------------------------
1 | config = self::createMock(Config::class);
35 | $this->pageCacheConfig = self::createStub(PageCacheConfig::class);
36 | $this->cacheTypeList = self::createMock(TypeListInterface::class);
37 |
38 | $this->updateFpc = new UpdateFpc(
39 | $this->config,
40 | $this->pageCacheConfig,
41 | $this->cacheTypeList,
42 | );
43 | }
44 |
45 | #[Test]
46 | public function nothingIsDoneIfFpcIsDisabled(): void
47 | {
48 | $this->pageCacheConfig
49 | ->method('isEnabled')
50 | ->willReturn(false);
51 |
52 | $this->config
53 | ->expects(self::never())
54 | ->method(self::anything());
55 |
56 | $this->updateFpc->execute();
57 | }
58 |
59 | #[Test]
60 | public function nothingIsDoneIfFpcModeIsNone(): void
61 | {
62 | $this->pageCacheConfig
63 | ->method('isEnabled')
64 | ->willReturn(true);
65 |
66 | $this->config
67 | ->expects(self::once())
68 | ->method('getFpcMode')
69 | ->willReturn(FpcMode::NONE);
70 |
71 | $this->cacheTypeList
72 | ->expects(self::never())
73 | ->method(self::anything());
74 |
75 | $this->updateFpc->execute();
76 | }
77 |
78 | #[Test]
79 | public function invalidatesFpcIfModeIfInvalidate(): void
80 | {
81 | $this->pageCacheConfig
82 | ->method('isEnabled')
83 | ->willReturn(true);
84 |
85 | $this->config
86 | ->expects(self::once())
87 | ->method('getFpcMode')
88 | ->willReturn(FpcMode::INVALIDATE);
89 |
90 | $this->cacheTypeList
91 | ->expects(self::once())
92 | ->method('invalidate');
93 |
94 | $this->updateFpc->execute();
95 | }
96 |
97 | #[Test]
98 | public function clearsFpcIfModeIfClear(): void
99 | {
100 | $this->pageCacheConfig
101 | ->method('isEnabled')
102 | ->willReturn(true);
103 |
104 | $this->config
105 | ->expects(self::once())
106 | ->method('getFpcMode')
107 | ->willReturn(FpcMode::CLEAR);
108 |
109 | $this->cacheTypeList
110 | ->expects(self::once())
111 | ->method('cleanType');
112 |
113 | $this->updateFpc->execute();
114 | }
115 | }
116 |
--------------------------------------------------------------------------------