From 606477ab04886d7a120925ae3c26d9e066510e91 Mon Sep 17 00:00:00 2001 From: Julio Motol Date: Tue, 21 Jul 2020 14:34:21 +0800 Subject: [PATCH] hotfix : use appropriate tags for css and js (#58) Co-authored-by: Julio Motol --- src/Sri.php | 4 ++-- tests/GenerateSriAssetTest.php | 16 ++++++++++++++-- tests/GenerateSriMixTest.php | 16 ++++++++++++++-- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/Sri.php b/src/Sri.php index 05115f2..aed0ed8 100644 --- a/src/Sri.php +++ b/src/Sri.php @@ -87,9 +87,9 @@ private function generateHtmlTag(string $href, string $path, bool $useCredential $integrity = $this->html($path, $useCredentials); if (Str::endsWith($path, 'css')) { - return ""; - } elseif (Str::endsWith($path, 'js')) { return ""; + } elseif (Str::endsWith($path, 'js')) { + return ""; } else { throw new \Exception('Invalid file'); } diff --git a/tests/GenerateSriAssetTest.php b/tests/GenerateSriAssetTest.php index a2592c4..9106bcc 100644 --- a/tests/GenerateSriAssetTest.php +++ b/tests/GenerateSriAssetTest.php @@ -22,7 +22,10 @@ public function it_generates_css_with_integrity() $hash = hash('sha256', file_get_contents('./tests/files/app.css'), true); $base64Hash = base64_encode($hash); - $this->assertStringContainsString('integrity="sha256-'.$base64Hash.'"', Sri::asset('app.css')); + $asset_string = Sri::asset('app.css'); + + $this->assertStringContainsString('link', $asset_string); + $this->assertStringContainsString('integrity="sha256-'.$base64Hash.'"', $asset_string); } /** @test */ @@ -35,6 +38,7 @@ public function it_generates_css_with_integrity_and_credentials() $this->assertStringContainsString('integrity="sha256-'.$base64Hash.'"', $asset_string); $this->assertStringContainsString('crossorigin="use-credentials"', $asset_string); + $this->assertStringContainsString('link', $asset_string); } /** @test */ @@ -45,6 +49,7 @@ public function it_generates_css_with_integrity_and_attributes() $asset_string = Sri::asset('app.css', false, 'rel="stylesheet"'); + $this->assertStringContainsString('link', $asset_string); $this->assertStringContainsString('integrity="sha256-'.$base64Hash.'"', $asset_string); $this->assertStringContainsString('rel="stylesheet"', $asset_string); } @@ -57,6 +62,7 @@ public function it_generates_css_with_integrity_credentials_and_attributes() $asset_string = Sri::asset('app.css', true, 'rel="stylesheet"'); + $this->assertStringContainsString('link', $asset_string); $this->assertStringContainsString('integrity="sha256-'.$base64Hash.'"', $asset_string); $this->assertStringContainsString('crossorigin="use-credentials"', $asset_string); $this->assertStringContainsString('rel="stylesheet"', $asset_string); @@ -68,7 +74,10 @@ public function it_generates_js_with_integrity() $hash = hash('sha256', file_get_contents('./tests/files/app.js'), true); $base64Hash = base64_encode($hash); - $this->assertStringContainsString('integrity="sha256-'.$base64Hash.'"', Sri::asset('app.js')); + $asset_string = Sri::asset('app.js'); + + $this->assertStringContainsString('script', $asset_string); + $this->assertStringContainsString('integrity="sha256-'.$base64Hash.'"', $asset_string); } /** @test */ @@ -79,6 +88,7 @@ public function it_generates_js_with_integrity_and_credentials() $asset_string = Sri::asset('app.js', true); + $this->assertStringContainsString('script', $asset_string); $this->assertStringContainsString('integrity="sha256-'.$base64Hash.'"', $asset_string); $this->assertStringContainsString('crossorigin="use-credentials"', $asset_string); } @@ -91,6 +101,7 @@ public function it_generates_js_with_integrity_and_attributes() $asset_string = Sri::asset('app.js', false, 'type="application/javascript" async'); + $this->assertStringContainsString('script', $asset_string); $this->assertStringContainsString('integrity="sha256-'.$base64Hash.'"', $asset_string); $this->assertStringContainsString('type="application/javascript" async', $asset_string); } @@ -103,6 +114,7 @@ public function it_generates_js_with_integrity_credentials_and_attributes() $asset_string = Sri::asset('app.js', true, 'type="application/javascript" async'); + $this->assertStringContainsString('script', $asset_string); $this->assertStringContainsString('integrity="sha256-'.$base64Hash.'"', $asset_string); $this->assertStringContainsString('crossorigin="use-credentials"', $asset_string); $this->assertStringContainsString('type="application/javascript" async', $asset_string); diff --git a/tests/GenerateSriMixTest.php b/tests/GenerateSriMixTest.php index 671ac79..8d75c2d 100644 --- a/tests/GenerateSriMixTest.php +++ b/tests/GenerateSriMixTest.php @@ -21,7 +21,10 @@ protected function setUp(): void /** @test */ public function it_generates_css_with_integrity() { - $this->assertStringContainsString('this-hash-is-valid', Sri::mix('css/app.css')); + $mix_string = Sri::mix('css/app.css'); + + $this->assertStringContainsString('link', $mix_string); + $this->assertStringContainsString('this-hash-is-valid', $mix_string); } /** @test */ @@ -29,6 +32,7 @@ public function it_generates_css_with_integrity_and_credentials() { $mix_string = Sri::mix('css/app.css', true); + $this->assertStringContainsString('link', $mix_string); $this->assertStringContainsString('this-hash-is-valid', $mix_string); $this->assertStringContainsString('crossorigin="use-credentials"', $mix_string); } @@ -38,6 +42,7 @@ public function it_generates_css_with_integrity_and_attributes() { $mix_string = Sri::mix('css/app.css', false, 'rel="stylesheet"'); + $this->assertStringContainsString('link', $mix_string); $this->assertStringContainsString('this-hash-is-valid', $mix_string); $this->assertStringContainsString('rel="stylesheet"', $mix_string); } @@ -47,6 +52,7 @@ public function it_generates_css_with_integrity_credentials_and_attributes() { $mix_string = Sri::mix('css/app.css', true, 'rel="stylesheet"'); + $this->assertStringContainsString('link', $mix_string); $this->assertStringContainsString('this-hash-is-valid', $mix_string); $this->assertStringContainsString('crossorigin="use-credentials"', $mix_string); $this->assertStringContainsString('rel="stylesheet"', $mix_string); @@ -55,7 +61,10 @@ public function it_generates_css_with_integrity_credentials_and_attributes() /** @test */ public function it_generates_js_with_integrity() { - $this->assertStringContainsString('this-hash-is-valid', Sri::mix('js/app.js')); + $mix_string = Sri::mix('js/app.js'); + + $this->assertStringContainsString('script', $mix_string); + $this->assertStringContainsString('this-hash-is-valid', $mix_string); } /** @test */ @@ -63,6 +72,7 @@ public function it_generates_js_with_integrity_and_credentials() { $mix_string = Sri::mix('js/app.js', true); + $this->assertStringContainsString('script', $mix_string); $this->assertStringContainsString('this-hash-is-valid', $mix_string); $this->assertStringContainsString('crossorigin="use-credentials"', $mix_string); } @@ -72,6 +82,7 @@ public function it_generates_js_with_integrity_and_attributes() { $mix_string = Sri::mix('js/app.js', false, 'type="application/javascript" async'); + $this->assertStringContainsString('script', $mix_string); $this->assertStringContainsString('this-hash-is-valid', $mix_string); $this->assertStringContainsString('type="application/javascript" async', $mix_string); } @@ -81,6 +92,7 @@ public function it_generates_js_with_integrity_credentials_and_attributes() { $mix_string = Sri::mix('js/app.js', true, 'type="application/javascript" async'); + $this->assertStringContainsString('script', $mix_string); $this->assertStringContainsString('this-hash-is-valid', $mix_string); $this->assertStringContainsString('crossorigin="use-credentials"', $mix_string); $this->assertStringContainsString('type="application/javascript" async', $mix_string);