|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Class AMP_Srcset_Sanitizer_Test. |
| 4 | + * |
| 5 | + * @package AMP |
| 6 | + */ |
| 7 | + |
| 8 | +use AmpProject\AmpWP\Tests\Helpers\MarkupComparison; |
| 9 | + |
| 10 | +/** |
| 11 | + * Class AMP_Srcset_Sanitizer_Test |
| 12 | + * |
| 13 | + * @coversDefaultClass AMP_Srcset_Sanitizer |
| 14 | + */ |
| 15 | +class AMP_Srcset_Sanitizer_Test extends WP_UnitTestCase { |
| 16 | + |
| 17 | + use MarkupComparison; |
| 18 | + |
| 19 | + /** |
| 20 | + * Provide the data to test the sanitize() method. |
| 21 | + * |
| 22 | + * @return array[] Test data. |
| 23 | + */ |
| 24 | + public function data_sanitize() { |
| 25 | + return [ |
| 26 | + 'img_with_valid_srcset' => [ |
| 27 | + '<img src="https://example.com/image.jpg" srcset="https://example.com/image.jpg, https://example.com/image-1.jpg 512w, https://example.com/image-2.jpg 1024w , https://example.com/image-3.jpg 300w, https://example.com/image-4.jpg 768w" width="350" height="150">', |
| 28 | + null, |
| 29 | + ], |
| 30 | + |
| 31 | + 'img_with_duplicate_img_candidate_but_same_url' => [ |
| 32 | + '<img src="https://example.com/image.jpg" srcset="https://example.com/image.jpg, https://example.com/image-1.jpg 1024w, https://example.com/image-1.jpg 1024w , https://example.com/image-2.jpg 300w, https://example.com/image-3.jpg 768w" width="350" height="150">', |
| 33 | + '<img src="https://example.com/image.jpg" srcset="https://example.com/image.jpg 1x, https://example.com/image-1.jpg 1024w, https://example.com/image-2.jpg 300w, https://example.com/image-3.jpg 768w" width="350" height="150">', |
| 34 | + ], |
| 35 | + |
| 36 | + 'img_with_duplicate_img_candidate_but_different_url' => [ |
| 37 | + '<img src="https://example.com/image.jpg" srcset="https://example.com/image.jpg, https://example.com/image-1.jpg 1024w, https://example.com/image-2.jpg 1024w , https://example.com/image-2.jpg 300w, https://example.com/image-3.jpg 768w" width="350" height="150">', |
| 38 | + '<img src="https://example.com/image.jpg" srcset="https://example.com/image.jpg 1x, https://example.com/image-1.jpg 1024w, https://example.com/image-2.jpg 300w, https://example.com/image-3.jpg 768w" width="350" height="150">', |
| 39 | + [ AMP_Tag_And_Attribute_Sanitizer::DUPLICATE_DIMENSIONS ], |
| 40 | + ], |
| 41 | + |
| 42 | + 'amp_img_srcset_missing_comma' => [ |
| 43 | + '<img src="https://example.com/image.jpg" height="100" width="200" srcset="https://example.com/image-1.jpg 1024w https://example.com/image-2.jpg 1024w">', |
| 44 | + '<img src="https://example.com/image.jpg" height="100" width="200">', |
| 45 | + [ AMP_Tag_And_Attribute_Sanitizer::INVALID_ATTR_VALUE ], |
| 46 | + ], |
| 47 | + |
| 48 | + 'amp_img_srcset_empty' => [ |
| 49 | + '<img src="https://example.com/image.jpg" height="100" width="200" srcset="">', |
| 50 | + null, |
| 51 | + ], |
| 52 | + |
| 53 | + 'amp_img_srcset_whitespace' => [ |
| 54 | + '<img src="https://example.com/image.jpg" height="100" width="200" srcset=" ">', |
| 55 | + '<img src="https://example.com/image.jpg" height="100" width="200">', |
| 56 | + [ AMP_Tag_And_Attribute_Sanitizer::INVALID_ATTR_VALUE ], |
| 57 | + ], |
| 58 | + |
| 59 | + 'amp_img_srcset_invalid_bare_number' => [ |
| 60 | + '<img src="https://example.com/image.jpg" height="100" width="200" srcset="1">', |
| 61 | + '<img src="https://example.com/image.jpg" height="100" width="200">', |
| 62 | + [ AMP_Tag_And_Attribute_Sanitizer::INVALID_ATTR_VALUE ], |
| 63 | + ], |
| 64 | + |
| 65 | + 'amp_img_srcset_invalid_dimension_unit' => [ |
| 66 | + '<img src="https://example.com/image.jpg" height="100" width="200" srcset="https://example.com/image.jpg 500px">', |
| 67 | + '<img src="https://example.com/image.jpg" height="100" width="200">', |
| 68 | + [ AMP_Tag_And_Attribute_Sanitizer::INVALID_ATTR_VALUE ], |
| 69 | + ], |
| 70 | + |
| 71 | + 'amp_img_srcset_invalid_space_in_dimension' => [ |
| 72 | + '<img src="https://example.com/image.jpg" height="100" width="200" srcset="https://example.com/image.jpg 2 x">', |
| 73 | + '<img src="https://example.com/image.jpg" height="100" width="200">', |
| 74 | + [ AMP_Tag_And_Attribute_Sanitizer::INVALID_ATTR_VALUE ], |
| 75 | + ], |
| 76 | + |
| 77 | + 'amp_img_srcset_invalid_dimension_type' => [ |
| 78 | + '<img src="https://example.com/image.jpg" height="100" width="200" srcset="https://example.com/image.jpg 5kw">', |
| 79 | + '<img src="https://example.com/image.jpg" height="100" width="200">', |
| 80 | + [ AMP_Tag_And_Attribute_Sanitizer::INVALID_ATTR_VALUE ], |
| 81 | + ], |
| 82 | + |
| 83 | + 'amp_img_srcset_invalid_width_descriptor' => [ |
| 84 | + '<img src="https://example.com/image.jpg" height="100" width="200" srcset="https://example.com/image.jpg 5.2w">', |
| 85 | + '<img src="https://example.com/image.jpg" height="100" width="200">', |
| 86 | + [ AMP_Tag_And_Attribute_Sanitizer::INVALID_ATTR_VALUE ], |
| 87 | + ], |
| 88 | + |
| 89 | + 'amp_img_srcset_invalid_zero_width' => [ |
| 90 | + '<img src="https://example.com/image.jpg" height="100" width="200" srcset="https://example.com/image.jpg 0w">', |
| 91 | + '<img src="https://example.com/image.jpg" height="100" width="200">', |
| 92 | + [ AMP_Tag_And_Attribute_Sanitizer::INVALID_ATTR_VALUE ], |
| 93 | + ], |
| 94 | + |
| 95 | + 'amp_img_srcset_invalid_zero_pixel_density' => [ |
| 96 | + '<img src="https://example.com/image.jpg" height="100" width="200" srcset="https://example.com/image.jpg 0.0x">', |
| 97 | + '<img src="https://example.com/image.jpg" height="100" width="200">', |
| 98 | + [ AMP_Tag_And_Attribute_Sanitizer::INVALID_ATTR_VALUE ], |
| 99 | + ], |
| 100 | + |
| 101 | + 'amp_img_srcset_valid_pixel_density' => [ |
| 102 | + '<img src="https://example.com/image.jpg" height="100" width="200" srcset="https://example.com/image.jpg 5x">', |
| 103 | + null, |
| 104 | + ], |
| 105 | + |
| 106 | + 'amp_img_srcset_valid_float_pixel_density' => [ |
| 107 | + '<img src="https://example.com/image.jpg" height="100" width="200" srcset="https://example.com/image.jpg 5.2x">', |
| 108 | + null, |
| 109 | + ], |
| 110 | + |
| 111 | + 'amp_img_srcset_valid_float_pixel_density_with_leading_zero' => [ |
| 112 | + '<img src="https://example.com/image.jpg" height="100" width="200" srcset="https://example.com/image.jpg 0.002x">', |
| 113 | + null, |
| 114 | + ], |
| 115 | + |
| 116 | + 'amp_img_srcset_invalid_tokens' => [ |
| 117 | + '<img src="https://example.com/image.jpg" height="100" width="200" srcset="bad bad">', |
| 118 | + '<img src="https://example.com/image.jpg" height="100" width="200">', |
| 119 | + [ AMP_Tag_And_Attribute_Sanitizer::INVALID_ATTR_VALUE ], |
| 120 | + ], |
| 121 | + ]; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Test the sanitize() method. |
| 126 | + * |
| 127 | + * @covers ::sanitize() |
| 128 | + * @dataProvider data_sanitize() |
| 129 | + * |
| 130 | + * @param string $source Source. |
| 131 | + * @param string|null $expected Expected. If null, then no change from source. |
| 132 | + * @param array $expected_error_codes Expected error codes. |
| 133 | + */ |
| 134 | + public function test_sanitize( $source, $expected = null, $expected_error_codes = [] ) { |
| 135 | + $error_codes = []; |
| 136 | + if ( null === $expected ) { |
| 137 | + $expected = $source; |
| 138 | + } |
| 139 | + |
| 140 | + $args = [ |
| 141 | + 'use_document_element' => true, |
| 142 | + 'validation_error_callback' => static function( $error ) use ( &$error_codes ) { |
| 143 | + $error_codes[] = $error['code']; |
| 144 | + }, |
| 145 | + ]; |
| 146 | + |
| 147 | + $dom = AMP_DOM_Utils::get_dom_from_content( $source ); |
| 148 | + |
| 149 | + $sanitizer = new AMP_Srcset_Sanitizer( $dom, $args ); |
| 150 | + $sanitizer->sanitize(); |
| 151 | + |
| 152 | + $this->assertEqualSets( $error_codes, $expected_error_codes ); |
| 153 | + |
| 154 | + $content = AMP_DOM_Utils::get_content_from_dom( $dom ); |
| 155 | + |
| 156 | + $this->assertEqualMarkup( $expected, $content ); |
| 157 | + } |
| 158 | +} |
0 commit comments