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 @@
- I tried to make off with the big one. Security
-Line 10: stopped me. Darn it. #oscars I tried to make off with the big one. Security
-Line 10: stopped me. Darn it. #oscars
-
-
-
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 @@
-
-Line 3:
-Line 19:
-Line 20:
-
-
-Transformations made from HTML tags to AMP custom tags
--------------------------------------------------------
-
-
-
-
-
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 @@
-
-Line 3:
-Line 19:
-Line 20:
-
-
-Transformations made from HTML tags to AMP custom tags
--------------------------------------------------------
-
-Run
This is a sample
This is a sample
This is a sample
This is a sample