diff --git a/src/AMP.php b/src/AMP.php index ce00dd0f..6193b458 100644 --- a/src/AMP.php +++ b/src/AMP.php @@ -132,12 +132,15 @@ public function getAmpHtml() * * @see src/Spec/validator-generated.php */ - public function __construct() + public function __construct(ParsedValidatorRules $parsed_rules = NULL) { - // The ParsedValidationRules object is expensive to create. So we maintain a global singleton - // This way the AMP Object creation is actually cheap - /** @var ParsedValidatorRules parsed_rules */ - $this->parsed_rules = ParsedValidatorRules::getSingletonParsedValidatorRules(); + $this->parsed_rules = $parsed_rules; + if (empty($this->parsed_rules)) { + // The ParsedValidationRules object is expensive to create. So we maintain a global singleton + // This way the AMP Object creation is actually cheap + /** @var ParsedValidatorRules parsed_rules */ + $this->parsed_rules = ParsedValidatorRules::getSingletonParsedValidatorRules(); + } /** @var ValidatorRules rules */ $this->rules = $this->parsed_rules->rules; } diff --git a/src/Validate/ParsedValidatorRules.php b/src/Validate/ParsedValidatorRules.php index 7c25fc5a..ce1b429d 100644 --- a/src/Validate/ParsedValidatorRules.php +++ b/src/Validate/ParsedValidatorRules.php @@ -68,6 +68,17 @@ public static function getSingletonParsedValidatorRules() } } + /** + * Should only be used for testing when you need to adjust rules. + * + * @param \Lullabot\AMP\Spec\ValidatorRules $rules + * + * @return \Lullabot\AMP\Validate\ParsedValidatorRules + */ + public static function createParsedValidatorRulesFromValidatorRules(ValidatorRules $rules) { + return new self($rules); + } + /** * Note that this is deliberately protected * diff --git a/tests/AmpTest.php b/tests/AmpTest.php index 3dfeb720..1de7aec9 100644 --- a/tests/AmpTest.php +++ b/tests/AmpTest.php @@ -17,6 +17,10 @@ */ use Lullabot\AMP\AMP; +use Lullabot\AMP\Spec\UrlSpec; +use Lullabot\AMP\Spec\ValidationRulesFactory; +use Lullabot\AMP\Spec\ValidatorRules; +use Lullabot\AMP\Validate\ParsedValidatorRules; use PHPUnit\Framework\TestCase; /** @@ -30,7 +34,9 @@ class AmpTest extends TestCase public function setup() { - $this->amp = new AMP(); + $this->moveImageFixturesToTmp(); + $parsed_rules = $this->getParsedRulesTestSet(); + $this->amp = new AMP($parsed_rules); $this->skip_internet = getenv('AMP_TEST_SKIP_INTERNET'); } @@ -97,4 +103,83 @@ protected function getTestFiles($subdirectory) } } } + + protected function getTestImages($subdirectory) + { + /** @var DirectoryIterator $fileitem */ + foreach (new DirectoryIterator($subdirectory) as $fileitem) { + if (!$fileitem->isFile() || $fileitem->isDot()) { + continue; + } + + yield $fileitem->getPathname(); + } + } + + protected function allowFileProtocolForImages(ValidatorRules $rules) + { + /** @var \Lullabot\AMP\Spec\TagSpec $tag */ + foreach ($rules->tags as $tag) { + if (!in_array($tag->tag_name, ['img', 'amp-pixel'])) { + continue; + } + /** @var \Lullabot\AMP\Spec\AttrSpec $attr */ + foreach ($tag->attrs as $attr) { + if ($attr->name !== 'src') { + continue; + } + $url_spec = new UrlSpec(); + if ($tag->tag_name === 'img') { + $url_spec->allowed_protocol = [ + 'data', + 'http', + 'https', + 'file' + ]; + } + elseif ($tag->tag_name === 'amp-pixel') { + $url_spec->allowed_protocol = [ + 'https', + 'file' + ]; + } + $url_spec->allow_relative = TRUE; + $attr->value_url = $url_spec; + } + } + + /** @var \Lullabot\AMP\Spec\AttrList $attr_list */ + foreach ($rules->attr_lists as $attr_list) { + if ($attr_list->name !== 'mandatory-src-or-srcset') { + continue; + } + /** @var \Lullabot\AMP\Spec\AttrSpec $attr */ + foreach ($attr_list->attrs as $attr) { + if ($attr->name !== 'src') { + continue; + } + $url_spec = new UrlSpec(); + $url_spec->allowed_protocol = ['data', 'http', 'https', 'file']; + $url_spec->allow_relative = TRUE; + $attr->value_url = $url_spec; + } + } + } + + protected function getParsedRulesTestSet() + { + /** @var \Lullabot\AMP\Spec\ValidatorRules $rules */ + $rules = ValidationRulesFactory::createValidationRules(); + $this->allowFileProtocolForImages($rules); + return ParsedValidatorRules::createParsedValidatorRulesFromValidatorRules($rules); + } + + protected function moveImageFixturesToTmp() + { + foreach ($this->getTestImages('tests/test-data/images') as $image) { + $path_info = pathinfo($image); + copy($image, sys_get_temp_dir() . '/' . $path_info['basename']); + } + } + } diff --git a/tests/test-data/fragment-html/img-anim-test-fragment.html b/tests/test-data/fragment-html/img-anim-test-fragment.html index 26cbce10..565d998b 100644 --- a/tests/test-data/fragment-html/img-anim-test-fragment.html +++ b/tests/test-data/fragment-html/img-anim-test-fragment.html @@ -1,14 +1,14 @@ - + - + - + - + - + diff --git a/tests/test-data/fragment-html/img-anim-test-fragment.html.options.json b/tests/test-data/fragment-html/img-anim-test-fragment.html.options.json index e4ef92a8..8b75371d 100644 --- a/tests/test-data/fragment-html/img-anim-test-fragment.html.options.json +++ b/tests/test-data/fragment-html/img-anim-test-fragment.html.options.json @@ -1,4 +1,5 @@ { "use_amp_anim_tag" : true, - "remove_non_converted_img_tag" : true + "remove_non_converted_img_tag" : true, + "server_url": "file://" } diff --git a/tests/test-data/fragment-html/img-anim-test-fragment.html.out b/tests/test-data/fragment-html/img-anim-test-fragment.html.out index 772f89d5..baa4fd45 100644 --- a/tests/test-data/fragment-html/img-anim-test-fragment.html.out +++ b/tests/test-data/fragment-html/img-anim-test-fragment.html.out @@ -1,14 +1,14 @@ - + - + - + - + @@ -17,38 +17,38 @@ ORIGINAL HTML --------------- Line 1: -Line 2: +Line 2: Line 3: Line 4: -Line 5: +Line 5: Line 6: Line 7: -Line 8: +Line 8: Line 9: Line 10: -Line 11: +Line 11: Line 12: Line 13: -Line 14: +Line 14: Line 15: Transformations made from HTML tags to AMP custom tags ------------------------------------------------------- - at line 2 + at line 2 ACTION TAKEN: img tag was converted to the amp-anim tag. - at line 5 + at line 5 ACTION TAKEN: img tag was converted to the amp-img tag. - at line 8 + at line 8 ACTION TAKEN: img tag was converted to the amp-anim tag. - at line 11 + at line 11 ACTION TAKEN: img tag was converted to the amp-img tag. - at line 14 + at line 14 ACTION TAKEN: img tag could NOT be converted to the amp-img tag as the image is not accessible. @@ -56,7 +56,7 @@ AMP-HTML Validation Issues and Fixes ------------------------------------- FAIL - on line 14 + on line 14 - The tag 'img' may only appear as a descendant of tag 'noscript'. Did you mean 'amp-img'? [code: MANDATORY_TAG_ANCESTOR_WITH_HINT category: DISALLOWED_HTML_WITH_AMP_EQUIVALENT see: https://www.ampproject.org/docs/reference/amp-img.html] ACTION TAKEN: img tag was removed due to validation issues. diff --git a/tests/test-data/fragment-html/img-test-fragment.html b/tests/test-data/fragment-html/img-test-fragment.html index 69554435..1228794b 100644 --- a/tests/test-data/fragment-html/img-test-fragment.html +++ b/tests/test-data/fragment-html/img-test-fragment.html @@ -1,28 +1,26 @@ - - - + - + - + - + - + - + - + - + - + diff --git a/tests/test-data/fragment-html/img-test-fragment.html.options.json b/tests/test-data/fragment-html/img-test-fragment.html.options.json index 161df05d..b1298d73 100644 --- a/tests/test-data/fragment-html/img-test-fragment.html.options.json +++ b/tests/test-data/fragment-html/img-test-fragment.html.options.json @@ -1,3 +1,4 @@ { - "img_max_fixed_layout_width" : 200 + "img_max_fixed_layout_width" : 200, + "server_url": "file://" } \ No newline at end of file diff --git a/tests/test-data/fragment-html/img-test-fragment.html.out b/tests/test-data/fragment-html/img-test-fragment.html.out index 20a30bb1..deeee073 100644 --- a/tests/test-data/fragment-html/img-test-fragment.html.out +++ b/tests/test-data/fragment-html/img-test-fragment.html.out @@ -1,82 +1,78 @@ - - - + - + - + - + - + - + - + - + - + ORIGINAL HTML --------------- -Line 1: -Line 2: -Line 3: -Line 4: -Line 5: -Line 6: -Line 7: -Line 8: -Line 9: -Line 10: -Line 11: -Line 12: -Line 13: -Line 14: -Line 15: -Line 16: -Line 17: -Line 18: -Line 19: -Line 20: -Line 21: -Line 22: -Line 23: -Line 24: -Line 25: -Line 26: -Line 27: -Line 28: -Line 29: +Line 1: +Line 2: +Line 3: +Line 4: +Line 5: +Line 6: +Line 7: +Line 8: +Line 9: +Line 10: +Line 11: +Line 12: +Line 13: +Line 14: +Line 15: +Line 16: +Line 17: +Line 18: +Line 19: +Line 20: +Line 21: +Line 22: +Line 23: +Line 24: +Line 25: +Line 26: +Line 27: Transformations made from HTML tags to AMP custom tags ------------------------------------------------------- - at line 4 + at line 2 ACTION TAKEN: img tag was converted to the amp-img tag. - at line 7 + at line 5 ACTION TAKEN: img tag was converted to the amp-img tag. - at line 10 + at line 8 ACTION TAKEN: img tag could NOT be converted to the amp-img tag as the image is not accessible. - at line 25 + at line 23 ACTION TAKEN: img tag was converted to the amp-pixel tag. - at line 28 + at line 26 ACTION TAKEN: img tag was converted to the amp-img tag. @@ -84,26 +80,26 @@ AMP-HTML Validation Issues and Fixes ------------------------------------- FAIL - on line 10 + on line 8 - The tag 'img' may only appear as a descendant of tag 'noscript'. Did you mean 'amp-img'? [code: MANDATORY_TAG_ANCESTOR_WITH_HINT category: DISALLOWED_HTML_WITH_AMP_EQUIVALENT see: https://www.ampproject.org/docs/reference/amp-img.html] - on line 13 + on line 11 - The implied layout 'CONTAINER' is not supported by tag 'amp-img'. [code: IMPLIED_LAYOUT_INVALID category: AMP_LAYOUT_PROBLEM see: https://www.ampproject.org/docs/reference/amp-img.html] ACTION TAKEN: amp-img tried to fix problems with amp-img by trying to fetch height, width from image directly and/or setting layout to responsive - on line 16 + on line 14 - The mandatory attribute 'height' is missing in tag 'amp-img'. [code: MANDATORY_ATTR_MISSING category: AMP_LAYOUT_PROBLEM see: https://www.ampproject.org/docs/reference/amp-img.html] ACTION TAKEN: amp-img tried to fix problems with amp-img by trying to fetch height, width from image directly and/or setting layout to responsive - on line 19 + on line 17 - The attribute 'height' in tag 'amp-img' is set to the invalid value 'auto'. [code: INVALID_ATTR_VALUE category: AMP_LAYOUT_PROBLEM see: https://www.ampproject.org/docs/reference/amp-img.html] ACTION TAKEN: amp-img tried to fix problems with amp-img by trying to fetch height, width from image directly and/or setting layout to responsive - on line 22 + on line 20 - Inconsistent units for width and height in tag 'amp-img' - width is specified in 'rem' whereas height is specified in 'px'. [code: INCONSISTENT_UNITS_FOR_WIDTH_AND_HEIGHT category: AMP_LAYOUT_PROBLEM see: https://www.ampproject.org/docs/reference/amp-img.html] ACTION TAKEN: amp-img tried to fix problems with amp-img by trying to fetch height, width from image directly and/or setting layout to responsive diff --git a/tests/test-data/fragment-html/instagram-fragment-with-caption-oembed-timeout.html b/tests/test-data/fragment-html/instagram-fragment-with-caption-oembed-timeout.html deleted file mode 100644 index cea29f8b..00000000 --- a/tests/test-data/fragment-html/instagram-fragment-with-caption-oembed-timeout.html +++ /dev/null @@ -1,19 +0,0 @@ -
-
-
-
-
-

I tried to make off with the big one. Security - stopped me. Darn it. #oscars

-

- A photo posted by Mark Ruffalo (@markruffalo) on - -

-
-
- diff --git a/tests/test-data/fragment-html/instagram-fragment-with-caption-oembed-timeout.html.options.json b/tests/test-data/fragment-html/instagram-fragment-with-caption-oembed-timeout.html.options.json deleted file mode 100644 index 0531c6d8..00000000 --- a/tests/test-data/fragment-html/instagram-fragment-with-caption-oembed-timeout.html.options.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "_readme" : "requires_internet is just for information for the test runner and has no significance for the functioning of library", - "requires_internet": "true", - "instagram_oembed_enabled": true, - "instagram_oembed_timeout": 30 -} \ No newline at end of file diff --git a/tests/test-data/fragment-html/instagram-fragment-with-caption-oembed-timeout.html.out b/tests/test-data/fragment-html/instagram-fragment-with-caption-oembed-timeout.html.out deleted file mode 100644 index 77b9fe54..00000000 --- a/tests/test-data/fragment-html/instagram-fragment-with-caption-oembed-timeout.html.out +++ /dev/null @@ -1,43 +0,0 @@ - - - - -ORIGINAL HTML ---------------- -Line 1:
-Line 3:
-Line 4:
-Line 5:
-Line 6:
-Line 7:

I tried to make off with the big one. Security -Line 10: stopped me. Darn it. #oscars

-Line 11:

-Line 12: A photo posted by Mark Ruffalo (@markruffalo) on -Line 13: -Line 16:

-Line 17:
-Line 18:
-Line 19: -Line 20: - - -Transformations made from HTML tags to AMP custom tags -------------------------------------------------------- - -
-
-
-
-
-

I tried to make off with the big one. Security - stopped me. Darn it. #oscars

-

- A photo posted by Mark Ruffalo (@markruffalo) on - -

-
-
- diff --git a/tests/test-data/fragment-html/instagram-fragment-with-caption.html.options.json b/tests/test-data/fragment-html/instagram-fragment-with-caption.html.options.json deleted file mode 100644 index 2ff4ce13..00000000 --- a/tests/test-data/fragment-html/instagram-fragment-with-caption.html.options.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "_readme" : "requires_internet is just for information for the test runner and has no significance for the functioning of library", - "requires_internet": "true" -} \ No newline at end of file diff --git a/tests/test-data/fragment-html/instagram-fragment-with-caption.html.out b/tests/test-data/fragment-html/instagram-fragment-with-caption.html.out deleted file mode 100644 index 77b9fe54..00000000 --- a/tests/test-data/fragment-html/instagram-fragment-with-caption.html.out +++ /dev/null @@ -1,43 +0,0 @@ - - - - -ORIGINAL HTML ---------------- -Line 1:
-Line 3:
-Line 4:
-Line 5:
-Line 6:
-Line 7:

I tried to make off with the big one. Security -Line 10: stopped me. Darn it. #oscars

-Line 11:

-Line 12: A photo posted by Mark Ruffalo (@markruffalo) on -Line 13: -Line 16:

-Line 17:
-Line 18:
-Line 19: -Line 20: - - -Transformations made from HTML tags to AMP custom tags -------------------------------------------------------- - -
Run

CNN

- - +

BBC

This is a sample

sample
paragraph

diff --git a/tests/test-data/fragment-html/sample-html-fragment.html.options.json b/tests/test-data/fragment-html/sample-html-fragment.html.options.json index 2ff4ce13..b42a141a 100644 --- a/tests/test-data/fragment-html/sample-html-fragment.html.options.json +++ b/tests/test-data/fragment-html/sample-html-fragment.html.options.json @@ -1,4 +1,5 @@ { "_readme" : "requires_internet is just for information for the test runner and has no significance for the functioning of library", - "requires_internet": "true" + "requires_internet": "true", + "server_url": "file://" } \ No newline at end of file diff --git a/tests/test-data/fragment-html/sample-html-fragment.html.out b/tests/test-data/fragment-html/sample-html-fragment.html.out index 90d79b57..cda9bccb 100644 --- a/tests/test-data/fragment-html/sample-html-fragment.html.out +++ b/tests/test-data/fragment-html/sample-html-fragment.html.out @@ -1,7 +1,6 @@

Run

CNN

- - +

BBC

This is a sample

sample
paragraph @@ -14,24 +13,23 @@ ORIGINAL HTML --------------- Line 1:

Run

Line 2:

CNN

-Line 3: -Line 4: -Line 5:

BBC

-Line 6:

-Line 7:

This is a sample

sample
paragraph

-Line 8: -Line 9: -Line 10: -Line 11: +Line 3: +Line 4:

BBC

+Line 5:

+Line 6:

This is a sample

sample
paragraph

+Line 7: +Line 8: +Line 9: +Line 10: Transformations made from HTML tags to AMP custom tags ------------------------------------------------------- - at line 4 + at line 3 ACTION TAKEN: img tag was converted to the amp-img tag. -