diff --git a/src/Attribute.php b/src/Attribute.php index 7fa2627fe..17741377f 100644 --- a/src/Attribute.php +++ b/src/Attribute.php @@ -1118,5 +1118,6 @@ interface Attribute const DATA_WIDGET_ID = 'data-widget-id'; const DATA_WIDGET_TYPE = 'data-widget-type'; - const CROSSORIGIN_ANONYMOUS = 'anonymous'; + const CROSSORIGIN_ANONYMOUS = 'anonymous'; + const CROSSORIGIN_USE_CREDENTIALS = 'use-credentials'; } diff --git a/src/Dom/Document.php b/src/Dom/Document.php index 45059e3d7..a28f0e225 100644 --- a/src/Dom/Document.php +++ b/src/Dom/Document.php @@ -32,8 +32,9 @@ * @property Element|null $viewport The document's viewport meta element. * @property DOMNodeList $ampElements The document's elements. * @property Element $ampCustomStyle The document's ' + ), + $output( + '', + '' + ), + ], + ]; + } + + /** + * Test the transform() method. + * + * @covers \AmpProject\Optimizer\Transformer\AmpRuntimePreloads::transform() + * @covers \AmpProject\Optimizer\Transformer\AmpRuntimePreloads::isAmpRuntimeCssNeeded() + * @covers \AmpProject\Optimizer\Transformer\AmpRuntimePreloads::isAmpRuntimeScriptNeeded() + * @covers \AmpProject\Optimizer\Transformer\AmpRuntimePreloads::getAmpRuntimeCssHost() + * @covers \AmpProject\Optimizer\Transformer\AmpRuntimePreloads::getAmpRuntimeScriptHost() + * @dataProvider dataTransform() + * + * @param string $source String of source HTML. + * @param string $expectedHtml String of expected HTML output. + * @param ErrorCollection|Error[] $expectedErrors Set of expected errors. + * @param array $config Configuration data to use. + */ + public function testTransform($source, $expectedHtml, $expectedErrors = [], $config = []) + { + $document = Document::fromHtml($source); + $transformer = new AmpRuntimePreloads(); + $errors = new ErrorCollection(); + + $transformer->transform($document, $errors); + + $this->assertSimilarMarkup($expectedHtml, $document->saveHTML()); + $this->assertSameErrors($expectedErrors, $errors); + } +} diff --git a/tests/Optimizer/Transformer/GoogleFontsPreconnectTest.php b/tests/Optimizer/Transformer/GoogleFontsPreconnectTest.php new file mode 100644 index 000000000..611d2322c --- /dev/null +++ b/tests/Optimizer/Transformer/GoogleFontsPreconnectTest.php @@ -0,0 +1,87 @@ +' + . TestMarkup::META_CHARSET . TestMarkup::META_VIEWPORT . $hints . TestMarkup::SCRIPT_AMPRUNTIME + . TestMarkup::LINK_FAVICON . TestMarkup::LINK_CANONICAL . TestMarkup::STYLE_AMPBOILERPLATE . TestMarkup::NOSCRIPT_AMPBOILERPLATE + . $head + . ''; + }; + + $output = static function ($head, $hints = '') { + return TestMarkup::DOCTYPE . '' + . TestMarkup::META_CHARSET . TestMarkup::META_VIEWPORT . $hints . TestMarkup::SCRIPT_AMPRUNTIME + . TestMarkup::LINK_FAVICON . TestMarkup::LINK_CANONICAL . TestMarkup::STYLE_AMPBOILERPLATE . TestMarkup::NOSCRIPT_AMPBOILERPLATE + . $head + . ''; + }; + + return [ + 'documents with google fonts do preconnect' => [ + $input( + '' + ), + $output( + '', + '' + . '' + ), + ], + + 'documents without google fonts do not preconnect' => [ + $input(''), + $output(''), + ], + ]; + } + + /** + * Test the transform() method. + * + * @covers \AmpProject\Optimizer\Transformer\GoogleFontsPreconnect::transform() + * @dataProvider dataTransform() + * + * @param string $source String of source HTML. + * @param string $expectedHtml String of expected HTML output. + * @param ErrorCollection|Error[] $expectedErrors Set of expected errors. + * @param array $config Configuration data to use. + */ + public function testTransform($source, $expectedHtml, $expectedErrors = [], $config = []) + { + $document = Document::fromHtml($source); + $transformer = new GoogleFontsPreconnect(); + $errors = new ErrorCollection(); + + $transformer->transform($document, $errors); + + $this->assertSimilarMarkup($expectedHtml, $document->saveHTML()); + $this->assertSameErrors($expectedErrors, $errors); + } +} diff --git a/tests/src/MarkupComparison.php b/tests/src/MarkupComparison.php index 2a4c57278..85faaabd9 100644 --- a/tests/src/MarkupComparison.php +++ b/tests/src/MarkupComparison.php @@ -77,6 +77,10 @@ protected function assertSimilarMarkup($expected, $actual) $actual = preg_replace('/\s+style="([^"]+?)(?:;)?"/i', ' style="$1;"', $actual); $expected = preg_replace('/\s+style="([^"]+?)(?:;)?"/i', ' style="$1;"', $expected); + // Decode special HTML characters (`& => &`). + $actual = htmlspecialchars_decode($actual, ENT_COMPAT | ENT_HTML5); + $expected = htmlspecialchars_decode($expected, ENT_COMPAT | ENT_HTML5); + $normalizeAttributes = static function ($element) { // Extract attributes for the given element. if (!preg_match('#^(<[a-z0-9-]+)(\s[^>]+)>$#i', $element, $matches)) {