Skip to content

Commit

Permalink
Updated test with default time zone.
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentmuller committed Jun 6, 2024
1 parent 5724549 commit fabe3c7
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 51 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/php_unit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
ini-values: date.timezone="Europe/Zurich"
ini-values: date.timezone=Europe/Zurich
coverage: xdebug

- name: Install dependencies
Expand Down
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 24 additions & 8 deletions resources/data/csp.dev.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
{
"base-uri": "none",
"media-src": "none",
"object-src": "none",
"default-src": "self",
"form-action": "self",
"manifest-src": "self",
"frame-ancestors": "self",
"script-src": "nonce",
"base-uri": [
"none"
],
"media-src": [
"none"
],
"object-src": [
"none"
],
"default-src": [
"self"
],
"form-action": [
"self"
],
"manifest-src": [
"self"
],
"frame-ancestors": [
"self"
],
"script-src": [
"nonce"
],
"script-src-elem": [
"nonce",
"https://www.google.com",
Expand Down
36 changes: 27 additions & 9 deletions resources/data/csp.prod.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
{
"base-uri": "none",
"media-src": "none",
"object-src": "none",
"default-src": "self",
"form-action": "self",
"manifest-src": "self",
"frame-ancestors": "self",
"script-src": "nonce",
"base-uri": [
"none"
],
"media-src": [
"none"
],
"object-src": [
"none"
],
"default-src": [
"self"
],
"form-action": [
"self"
],
"manifest-src": [
"self"
],
"frame-ancestors": [
"self"
],
"script-src": [
"nonce"
],
"script-src-elem": [
"nonce",
"https://www.google.com",
Expand Down Expand Up @@ -41,5 +57,7 @@
"https://robohash.org",
"https://openweathermap.org"
],
"report-uri": "report"
"report-uri": [
"report"
]
}
38 changes: 15 additions & 23 deletions src/Listener/ResponseListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@
/**
* Listener to add content security policy (CSP) to response.
*
* Processing Content Security Policy violation reports:
*
* @see https://mathiasbynens.be/notes/csp-reports.
* @see https://mathiasbynens.be/notes/csp-reports
* @see https://securityheaders.com/
* @see https://github.com/aidantwoods/SecureHeaders
* @see https://www.sentrium.co.uk/labs/application-security-101-http-headers
*/
class ResponseListener
{
Expand All @@ -57,10 +58,6 @@ class ResponseListener

/**
* The default headers to add.
*
* @see https://securityheaders.com/
* @see https://github.com/aidantwoods/SecureHeaders
* @see https://www.sentrium.co.uk/labs/application-security-101-http-headers
*/
private const DEFAULT_HEADERS = [
'referrer-policy' => 'same-origin',
Expand Down Expand Up @@ -92,7 +89,7 @@ public function onKernelResponse(ResponseEvent $event): void
return;
}

if ($this->debug && $this->isDevRequest($event->getRequest())) {
if ($this->debug && $this->isDevFirewall($event->getRequest())) {
return;
}

Expand Down Expand Up @@ -129,6 +126,8 @@ private function buildCSP(): string
* @psalm-return array<string, string[]>
*
* @throws InvalidArgumentException
*
* @psalm-suppress MixedArgumentTypeCoercion
*/
private function getCSP(): array
{
Expand All @@ -137,9 +136,14 @@ private function getCSP(): array
return [];
}

$content = $this->loadFile();
try {
/* @psalm-var array<string, string[]> $content */
$content = FileUtils::decodeJson($this->file);

return $this->replaceValues($content);
return $this->replaceValues($content);
} catch (\InvalidArgumentException) {
return [];
}
});
}

Expand All @@ -148,23 +152,11 @@ private function getReportURL(): string
return $this->generator->generate(CspReportController::ROUTE_NAME, [], UrlGeneratorInterface::ABSOLUTE_URL);
}

private function isDevRequest(Request $request): bool
private function isDevFirewall(Request $request): bool
{
return self::FIREWALL_DEV === $this->security->getFirewallConfig($request)?->getName();
}

/**
* @psalm-return array<string, string[]>
*/
private function loadFile(): array
{
/* @psalm-var array<string, string|string[]> $content */
$content = FileUtils::decodeJson($this->file);

/** @psalm-var array<string, string[]> */
return \array_map(static fn (string|array $value): array => (array) $value, $content);
}

/**
* @psalm-param array<string, string[]> $array
*
Expand Down
9 changes: 9 additions & 0 deletions tests/Utils/DateUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@
#[CoversClass(DateUtils::class)]
class DateUtilsTest extends TestCase
{
private const TIME_ZONE = 'Europe/Zurich';

protected function setUp(): void
{
\Locale::setDefault(FormatUtils::DEFAULT_LOCALE);
\setlocale(\LC_ALL, FormatUtils::DEFAULT_LOCALE);
\date_default_timezone_set(self::TIME_ZONE);
}

public static function getCompletYears(): \Iterator
Expand Down Expand Up @@ -323,6 +326,12 @@ public function testSubByString(): void
self::assertSame('2020-01-03', $add->format('Y-m-d'));
}

public function testTimeZone(): void
{
$actual = \date_default_timezone_get();
self::assertSame(self::TIME_ZONE, $actual);
}

#[DataProvider('getWeekdayNames')]
public function testWeekdayNames(string $name, int $index, string $firstDay = 'sunday'): void
{
Expand Down
12 changes: 8 additions & 4 deletions tests/Utils/FormatUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,15 @@
class FormatUtilsTest extends TestCase
{
private const DATE_TIME = '2022-02-20 12:59:59';

private const PERCENT_SYMBOL = '%';

private const TIME_STAMP = 1_645_358_399;

private const TIME_ZONE = 'Europe/Zurich';

protected function setUp(): void
{
\Locale::setDefault(FormatUtils::DEFAULT_LOCALE);
\setlocale(\LC_TIME, FormatUtils::DEFAULT_LOCALE);
\setlocale(\LC_ALL, FormatUtils::DEFAULT_LOCALE);
\date_default_timezone_set(self::TIME_ZONE);
}

public static function getAmounts(): \Iterator
Expand Down Expand Up @@ -367,6 +365,12 @@ public function testTimeType(): void
self::assertSame(\IntlDateFormatter::SHORT, FormatUtils::TIME_TYPE);
}

public function testTimeZone(): void
{
$actual = \date_default_timezone_get();
self::assertSame(self::TIME_ZONE, $actual);
}

/**
* @throws \Exception
*/
Expand Down

0 comments on commit fabe3c7

Please sign in to comment.