Skip to content

Commit

Permalink
#299: Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
m4olivei authored Sep 22, 2021
2 parents 7f4c873 + 4973377 commit eda822e
Show file tree
Hide file tree
Showing 23 changed files with 202 additions and 243 deletions.
13 changes: 8 additions & 5 deletions src/AMP.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
11 changes: 11 additions & 0 deletions src/Validate/ParsedValidatorRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
87 changes: 86 additions & 1 deletion tests/AmpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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');
}

Expand Down Expand Up @@ -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']);
}
}

}
10 changes: 5 additions & 5 deletions tests/test-data/fragment-html/img-anim-test-fragment.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<!-- should transform to amp-anim-->
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/2/2c/Rotating_earth_%28large%29.gif/200px-Rotating_earth_%28large%29.gif">
<img src="file:///tmp/cat_animation.gif" />

<!-- should transform to amp-img because this gif is not animated -->
<img src="https://upload.wikimedia.org/wikipedia/commons/b/bb/Quilt_design_as_46x46_uncompressed_GIF.gif">
<img src="file:///tmp/dog.gif" />

<!-- should transform to amp-anim-->
<img src="https://upload.wikimedia.org/wikipedia/commons/1/14/Animated_PNG_example_bouncing_beach_ball.png">
<img src="file:///tmp/ball_animation.png" />

<!-- should transform to amp-img because this png is not animated -->
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/5/53/Wikimedia-logo.png/240px-Wikimedia-logo.png">
<img src="file:///tmp/kitten.jpg" />

<!-- nonexistent image, should refuse to convert to amp-img and delete it -->
<img src="https://upload.wikimedia.org/wikipedia/commons/e/ee/non-existent-image1234.jpg">
<img src="file:///tmp/nope.jpg" />
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"use_amp_anim_tag" : true,
"remove_non_converted_img_tag" : true
"remove_non_converted_img_tag" : true,
"server_url": "file://"
}
30 changes: 15 additions & 15 deletions tests/test-data/fragment-html/img-anim-test-fragment.html.out
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<!-- should transform to amp-anim-->
<amp-anim src="https://upload.wikimedia.org/wikipedia/commons/thumb/2/2c/Rotating_earth_%28large%29.gif/200px-Rotating_earth_%28large%29.gif" width="200" height="200" layout="responsive"></amp-anim>
<amp-anim src="file:///tmp/cat_animation.gif" width="91" height="91" layout="responsive"></amp-anim>

<!-- should transform to amp-img because this gif is not animated -->
<amp-img src="https://upload.wikimedia.org/wikipedia/commons/b/bb/Quilt_design_as_46x46_uncompressed_GIF.gif" width="46" height="46" layout="responsive"></amp-img>
<amp-img src="file:///tmp/dog.gif" width="598" height="460" layout="responsive"></amp-img>

<!-- should transform to amp-anim-->
<amp-anim src="https://upload.wikimedia.org/wikipedia/commons/1/14/Animated_PNG_example_bouncing_beach_ball.png" width="100" height="100" layout="responsive"></amp-anim>
<amp-anim src="file:///tmp/ball_animation.png" width="100" height="100" layout="responsive"></amp-anim>

<!-- should transform to amp-img because this png is not animated -->
<amp-img src="https://upload.wikimedia.org/wikipedia/commons/thumb/5/53/Wikimedia-logo.png/240px-Wikimedia-logo.png" width="240" height="240" layout="responsive"></amp-img>
<amp-img src="file:///tmp/kitten.jpg" width="512" height="341" layout="responsive"></amp-img>

<!-- nonexistent image, should refuse to convert to amp-img and delete it -->

Expand All @@ -17,46 +17,46 @@
ORIGINAL HTML
---------------
Line 1: <!-- should transform to amp-anim-->
Line 2: <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/2/2c/Rotating_earth_%28large%29.gif/200px-Rotating_earth_%28large%29.gif">
Line 2: <img src="file:///tmp/cat_animation.gif" />
Line 3:
Line 4: <!-- should transform to amp-img because this gif is not animated -->
Line 5: <img src="https://upload.wikimedia.org/wikipedia/commons/b/bb/Quilt_design_as_46x46_uncompressed_GIF.gif">
Line 5: <img src="file:///tmp/dog.gif" />
Line 6:
Line 7: <!-- should transform to amp-anim-->
Line 8: <img src="https://upload.wikimedia.org/wikipedia/commons/1/14/Animated_PNG_example_bouncing_beach_ball.png">
Line 8: <img src="file:///tmp/ball_animation.png" />
Line 9:
Line 10: <!-- should transform to amp-img because this png is not animated -->
Line 11: <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/5/53/Wikimedia-logo.png/240px-Wikimedia-logo.png">
Line 11: <img src="file:///tmp/kitten.jpg" />
Line 12:
Line 13: <!-- nonexistent image, should refuse to convert to amp-img and delete it -->
Line 14: <img src="https://upload.wikimedia.org/wikipedia/commons/e/ee/non-existent-image1234.jpg">
Line 14: <img src="file:///tmp/nope.jpg" />
Line 15:


Transformations made from HTML tags to AMP custom tags
-------------------------------------------------------

<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/2/2c/Rotating_earth_%28large%29.gif/200px-Rotating_earth_%28large%29.gif"> at line 2
<img src="file:///tmp/cat_animation.gif"> at line 2
ACTION TAKEN: img tag was converted to the amp-anim tag.

<img src="https://upload.wikimedia.org/wikipedia/commons/b/bb/Quilt_design_as_46x46_uncompressed_GIF.gif"> at line 5
<img src="file:///tmp/dog.gif"> at line 5
ACTION TAKEN: img tag was converted to the amp-img tag.

<img src="https://upload.wikimedia.org/wikipedia/commons/1/14/Animated_PNG_example_bouncing_beach_ball.png"> at line 8
<img src="file:///tmp/ball_animation.png"> at line 8
ACTION TAKEN: img tag was converted to the amp-anim tag.

<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/5/53/Wikimedia-logo.png/240px-Wikimedia-logo.png"> at line 11
<img src="file:///tmp/kitten.jpg"> at line 11
ACTION TAKEN: img tag was converted to the amp-img tag.

<img src="https://upload.wikimedia.org/wikipedia/commons/e/ee/non-existent-image1234.jpg"> at line 14
<img src="file:///tmp/nope.jpg"> at line 14
ACTION TAKEN: img tag could NOT be converted to the amp-img tag as the image is not accessible.


AMP-HTML Validation Issues and Fixes
-------------------------------------
FAIL

<img src="https://upload.wikimedia.org/wikipedia/commons/e/ee/non-existent-image1234.jpg"> on line 14
<img src="file:///tmp/nope.jpg"> 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.
Expand Down
20 changes: 9 additions & 11 deletions tests/test-data/fragment-html/img-test-fragment.html
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
<!-- Note: this image is in the public domain. https://commons.wikimedia.org/wiki/File:"Birdcatcher"_with_jockey_up.jpg -->

<!-- should transform to amp-img -->
<img src="https://upload.wikimedia.org/wikipedia/commons/e/ee/%22Birdcatcher%22_with_jockey_up.jpg">
<img src="file:///tmp/kitten.jpg" />

<!-- should transform to amp-img with fixed layout, preserving the width and height -->
<img src="https://upload.wikimedia.org/wikipedia/commons/e/ee/%22Birdcatcher%22_with_jockey_up.jpg" width="125" height="96">
<img src="file:///tmp/kitten.jpg" width="125" height="96" />

<!-- nonexistent image, should refuse to convert to amp-img and keep it as it is -->
<img src="https://upload.wikimedia.org/wikipedia/commons/e/ee/non-existent-image1234.jpg">
<img src="file:///tmp/nope.jpg" />

<!-- should provide layout and height width and make layout responsive -->
<amp-img src="https://upload.wikimedia.org/wikipedia/commons/e/ee/%22Birdcatcher%22_with_jockey_up.jpg"></amp-img>
<amp-img src="file:///tmp/kitten.jpg"></amp-img>

<!-- since only width exists, overwrite with height and width from original image -->
<amp-img layout="responsive" src="https://upload.wikimedia.org/wikipedia/commons/e/ee/%22Birdcatcher%22_with_jockey_up.jpg" width="500"></amp-img>
<amp-img layout="responsive" src="file:///tmp/kitten.jpg" width="200"></amp-img>

<!-- since height is illegal, overwrite with height and width from original image -->
<amp-img layout="responsive" src="https://upload.wikimedia.org/wikipedia/commons/e/ee/%22Birdcatcher%22_with_jockey_up.jpg" width="625" height="auto"></amp-img>
<amp-img layout="responsive" src="file:///tmp/kitten.jpg" width="200" height="auto"></amp-img>

<!-- since units are inconsistent, overwrite with height+width from original image -->
<amp-img layout="responsive" src="https://upload.wikimedia.org/wikipedia/commons/e/ee/%22Birdcatcher%22_with_jockey_up.jpg" width="625rem" height="480"></amp-img>
<amp-img layout="responsive" src="file:///tmp/kitten.jpg" width="200rem" height="100"></amp-img>

<!-- should transform to amp-pixel -->
<img src="https://upload.wikimedia.org/wikipedia/commons/c/ce/Transparent.gif">
<img src="file:///tmp/pixel.gif" />

<!-- should transform to amp-img instead of amp-anim because of default option['use_amp_anim_tag'] = false -->
<img src="https://upload.wikimedia.org/wikipedia/commons/b/bb/Quilt_design_as_46x46_uncompressed_GIF.gif">
<img src="file:///tmp/cat_animation.gif" />
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"img_max_fixed_layout_width" : 200
"img_max_fixed_layout_width" : 200,
"server_url": "file://"
}
Loading

0 comments on commit eda822e

Please sign in to comment.