|
6 | 6 | */
|
7 | 7 |
|
8 | 8 | use AmpProject\AmpWP\Tests\TestCase;
|
| 9 | +use AmpProject\AmpWP\Tests\Helpers\MarkupComparison; |
9 | 10 |
|
10 | 11 | /**
|
11 | 12 | * Class AMP_Native_Img_Attributes_Sanitizer_Test
|
|
15 | 16 | */
|
16 | 17 | class AMP_Native_Img_Attributes_Sanitizer_Test extends TestCase {
|
17 | 18 |
|
| 19 | + use MarkupComparison; |
| 20 | + |
| 21 | + public function get_data_to_test_sanitize() { |
| 22 | + $amp_carousel_source = '<amp-carousel width="600" height="400" type="slides" layout="responsive" lightbox=""><figure class="slide"><img src="http://example.com/img.png" width="600" height="400" layout="fill" object-fit="cover"></figure></amp-carousel>'; |
| 23 | + $amp_carousel_sanitized = '<amp-carousel width="600" height="400" type="slides" layout="responsive" lightbox=""><figure class="slide"><img src="http://example.com/img.png" width="600" height="400" style="position:absolute;left:0;right:0;top:0;bottom:0;width:100%;height:100%;object-fit:cover"></figure></amp-carousel>'; |
| 24 | + |
| 25 | + return [ |
| 26 | + 'disabled' => [ |
| 27 | + false, |
| 28 | + $amp_carousel_source, |
| 29 | + null, // Same as above. |
| 30 | + ], |
| 31 | + 'carousel_with_img' => [ |
| 32 | + true, |
| 33 | + $amp_carousel_source, |
| 34 | + $amp_carousel_sanitized, |
| 35 | + ], |
| 36 | + 'amp_img' => [ |
| 37 | + true, |
| 38 | + '<amp-img src="https://example.com/img.png" style="border: solid 1px red;" layout="fill" object-fit="cover"></amp-img>', |
| 39 | + null, // Same as above. |
| 40 | + ], |
| 41 | + 'img_with_layout_fill' => [ |
| 42 | + true, |
| 43 | + '<img src="https://example.com/img.png" style="border: solid 1px red" layout="fill">', |
| 44 | + '<img src="https://example.com/img.png" style="border: solid 1px red;position:absolute;left:0;right:0;top:0;bottom:0;width:100%;height:100%">', |
| 45 | + ], |
| 46 | + 'img_with_layout_nodisplay' => [ |
| 47 | + true, |
| 48 | + '<img src="https://example.com/img.png" style="border: solid 1px red;" layout="nodisplay">', |
| 49 | + null, // Same as above. |
| 50 | + ], |
| 51 | + 'img_with_object_fit_cover' => [ |
| 52 | + true, |
| 53 | + '<img src="https://example.com/img.png" style="border: solid 1px red;" object-fit="cover">', |
| 54 | + '<img src="https://example.com/img.png" style="border: solid 1px red;object-fit:cover">', |
| 55 | + ], |
| 56 | + 'img_with_object_fit_contain' => [ |
| 57 | + true, |
| 58 | + '<img src="https://example.com/img.png" style="border: solid 1px red;" object-fit="contain">', |
| 59 | + '<img src="https://example.com/img.png" style="border: solid 1px red;object-fit:contain">', |
| 60 | + ], |
| 61 | + 'img_with_object_position' => [ |
| 62 | + true, |
| 63 | + '<img src="https://example.com/img.png" style="border: solid 1px red;" object-position="top">', |
| 64 | + '<img src="https://example.com/img.png" style="border: solid 1px red;object-position:top">', |
| 65 | + ], |
| 66 | + ]; |
| 67 | + } |
| 68 | + |
18 | 69 | /**
|
19 | 70 | * Test an native img tag has not layout or object-fit attributes.
|
20 | 71 | *
|
21 | 72 | * `layout` and `object-fit` will be replaced with a style attribute.
|
22 | 73 | *
|
23 |
| - * @covers \AMP_Native_Img_Attributes_Sanitizer::sanitize() |
| 74 | + * @dataProvider get_data_to_test_sanitize |
| 75 | + * @covers ::sanitize() |
24 | 76 | */
|
25 |
| - public function test_native_img_tag_has_not_layout_or_object_fit_attrs() { |
26 |
| - $source = '<amp-carousel width="600" height="400" type="slides" layout="responsive" lightbox=""><figure class="slide"><img src="http://example.com/img.png" width="600" height="400" layout="fill" object-fit="cover"></figure></amp-carousel>'; |
27 |
| - $expected = '<amp-carousel width="600" height="400" type="slides" layout="responsive" lightbox=""><figure class="slide"><img src="http://example.com/img.png" width="600" height="400" style="position:absolute; left:0; right:0; top:0; bottom: 0; width:100%; height:100%; object-fit:cover;"></figure></amp-carousel>'; |
| 77 | + public function test_sanitize( $native_img_used, $source, $expected ) { |
| 78 | + if ( null === $expected ) { |
| 79 | + $expected = $source; |
| 80 | + } |
28 | 81 |
|
29 | 82 | $dom = AMP_DOM_Utils::get_dom_from_content( $source );
|
30 | 83 | $sanitizer = new AMP_Native_Img_Attributes_Sanitizer(
|
31 | 84 | $dom,
|
32 |
| - [ |
33 |
| - 'native_img_used' => true, |
34 |
| - 'carousel_required' => true, |
35 |
| - ] |
| 85 | + compact( 'native_img_used' ) |
36 | 86 | );
|
37 | 87 | $sanitizer->sanitize();
|
38 | 88 |
|
39 |
| - $sanitizer = new AMP_Tag_And_Attribute_Sanitizer( $dom ); |
40 |
| - $sanitizer->sanitize(); |
41 | 89 | $content = AMP_DOM_Utils::get_content_from_dom( $dom );
|
42 |
| - $this->assertEquals( $expected, $content ); |
| 90 | + $this->assertEqualMarkup( $expected, $content ); |
43 | 91 | }
|
44 | 92 | }
|
0 commit comments