forked from ampproject/amp-wp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-amp-img-sanitizer.php
180 lines (147 loc) · 8.75 KB
/
test-amp-img-sanitizer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
class AMP_Img_Sanitizer_Test extends WP_UnitTestCase {
public static function force_remove_extraction_callbacks() {
remove_all_filters( 'amp_extract_image_dimensions_batch' );
}
public function setUp() {
parent::setUp();
add_action( 'amp_extract_image_dimensions_batch_callbacks_registered', array( __CLASS__, 'force_remove_extraction_callbacks' ) );
}
public function get_data() {
return array(
'no_images' => array(
'<p>Lorem Ipsum Demet Delorit.</p>',
'<p>Lorem Ipsum Demet Delorit.</p>',
),
'image_without_src' => array(
'<p><img width="300" height="300" /></p>',
'<p></p>',
),
'image_with_empty_src' => array(
'<p><img src="" width="300" height="300" /></p>',
'<p></p>',
),
'image_with_layout' => array(
'<img src="http://placehold.it/100x100" data-amp-layout="fill" width="100" height="100" />',
'<amp-img src="http://placehold.it/100x100" layout="fill" width="100" height="100" class="amp-wp-enforced-sizes"></amp-img>',
),
'image_with_spaces_only_src' => array(
'<p><img src=" " width="300" height="300" /></p>',
'<p></p>',
),
'image_with_empty_width_and_height' => array(
'<p><img src="http://placehold.it/300x300" width="" height="" /></p>',
'<p><amp-img src="http://placehold.it/300x300" width="600" height="400" class="amp-wp-unknown-size amp-wp-enforced-sizes" layout="intrinsic"></amp-img></p>',
),
'image_with_empty_width' => array(
'<p><img src="http://placehold.it/300x300" width="" height="300" /></p>',
'<p><amp-img src="http://placehold.it/300x300" width="600" height="300" class="amp-wp-unknown-size amp-wp-unknown-width amp-wp-enforced-sizes" layout="intrinsic"></amp-img></p>',
),
'image_with_empty_height' => array(
'<p><img src="http://placehold.it/300x300" width="300" height="" /></p>',
'<p><amp-img src="http://placehold.it/300x300" width="300" height="400" class="amp-wp-unknown-size amp-wp-unknown-height amp-wp-enforced-sizes" layout="intrinsic"></amp-img></p>',
),
'image_with_zero_width' => array(
'<p><img src="http://placehold.it/300x300" width="0" height="300" /></p>',
'<p><amp-img src="http://placehold.it/300x300" width="0" height="300" class="amp-wp-enforced-sizes"></amp-img></p>',
),
'image_with_zero_width_and_height' => array(
'<p><img src="http://placehold.it/300x300" width="0" height="0" /></p>',
'<p><amp-img src="http://placehold.it/300x300" width="0" height="0" class="amp-wp-enforced-sizes"></amp-img></p>',
),
'image_with_decimal_width' => array(
'<p><img src="http://placehold.it/300x300" width="299.5" height="300" /></p>',
'<p><amp-img src="http://placehold.it/300x300" width="299.5" height="300" class="amp-wp-enforced-sizes" layout="intrinsic"></amp-img></p>',
),
'image_with_self_closing_tag' => array(
'<img src="http://placehold.it/350x150" width="350" height="150" alt="Placeholder!" />',
'<amp-img src="http://placehold.it/350x150" width="350" height="150" alt="Placeholder!" class="amp-wp-enforced-sizes" layout="intrinsic"></amp-img>',
),
'image_with_no_end_tag' => array(
'<img src="http://placehold.it/350x150" width="350" height="150" alt="Placeholder!">',
'<amp-img src="http://placehold.it/350x150" width="350" height="150" alt="Placeholder!" class="amp-wp-enforced-sizes" layout="intrinsic"></amp-img>',
),
'image_with_end_tag' => array(
'<img src="http://placehold.it/350x150" width="350" height="150" alt="Placeholder!"></img>',
'<amp-img src="http://placehold.it/350x150" width="350" height="150" alt="Placeholder!" class="amp-wp-enforced-sizes" layout="intrinsic"></amp-img>',
),
'image_with_on_attribute' => array(
'<img src="http://placehold.it/350x150" on="tap:my-lightbox" width="350" height="150" />',
'<amp-img src="http://placehold.it/350x150" on="tap:my-lightbox" width="350" height="150" class="amp-wp-enforced-sizes" layout="intrinsic"></amp-img>',
),
'image_with_blacklisted_attribute' => array(
'<img src="http://placehold.it/350x150" width="350" height="150" style="border: 1px solid red;" />',
'<amp-img src="http://placehold.it/350x150" width="350" height="150" class="amp-wp-enforced-sizes" layout="intrinsic"></amp-img>',
),
'image_with_no_dimensions_is_forced_dimensions' => array(
'<img src="http://placehold.it/350x150" />',
'<amp-img src="http://placehold.it/350x150" width="600" height="400" class="amp-wp-unknown-size amp-wp-enforced-sizes" layout="intrinsic"></amp-img>',
),
'image_with_sizes_attribute_is_overridden' => array(
'<img src="http://placehold.it/350x150" width="350" height="150" />',
'<amp-img src="http://placehold.it/350x150" width="350" height="150" class="amp-wp-enforced-sizes" layout="intrinsic"></amp-img>',
),
'gif_image_conversion' => array(
'<img src="http://placehold.it/350x150.gif" width="350" height="150" alt="Placeholder!" />',
'<amp-anim src="http://placehold.it/350x150.gif" width="350" height="150" alt="Placeholder!" class="amp-wp-enforced-sizes" layout="intrinsic"></amp-anim>',
),
'gif_image_url_with_querystring' => array(
'<img src="http://placehold.it/350x150.gif?foo=bar" width="350" height="150" alt="Placeholder!" />',
'<amp-anim src="http://placehold.it/350x150.gif?foo=bar" width="350" height="150" alt="Placeholder!" class="amp-wp-enforced-sizes" layout="intrinsic"></amp-anim>',
),
'multiple_same_image' => array(
'<img src="http://placehold.it/350x150" width="350" height="150" />
<img src="http://placehold.it/350x150" width="350" height="150" />
<img src="http://placehold.it/350x150" width="350" height="150" />
<img src="http://placehold.it/350x150" width="350" height="150" />
',
'<amp-img src="http://placehold.it/350x150" width="350" height="150" class="amp-wp-enforced-sizes" layout="intrinsic"></amp-img><amp-img src="http://placehold.it/350x150" width="350" height="150" class="amp-wp-enforced-sizes" layout="intrinsic"></amp-img><amp-img src="http://placehold.it/350x150" width="350" height="150" class="amp-wp-enforced-sizes" layout="intrinsic"></amp-img><amp-img src="http://placehold.it/350x150" width="350" height="150" class="amp-wp-enforced-sizes" layout="intrinsic"></amp-img>',
),
'multiple_different_images' => array(
'<img src="http://placehold.it/350x150" width="350" height="150" />
<img src="http://placehold.it/360x160" width="360" height="160" />
<img src="http://placehold.it/370x170" width="370" height="170" />
<img src="http://placehold.it/380x180" width="380" height="180" />',
'<amp-img src="http://placehold.it/350x150" width="350" height="150" class="amp-wp-enforced-sizes" layout="intrinsic"></amp-img><amp-img src="http://placehold.it/360x160" width="360" height="160" class="amp-wp-enforced-sizes" layout="intrinsic"></amp-img><amp-img src="http://placehold.it/370x170" width="370" height="170" class="amp-wp-enforced-sizes" layout="intrinsic"></amp-img><amp-img src="http://placehold.it/380x180" width="380" height="180" class="amp-wp-enforced-sizes" layout="intrinsic"></amp-img>',
),
);
}
/**
* @dataProvider get_data
*/
public function test_converter( $source, $expected ) {
$dom = AMP_DOM_Utils::get_dom_from_content( $source );
$sanitizer = new AMP_Img_Sanitizer( $dom );
$sanitizer->sanitize();
$content = AMP_DOM_Utils::get_content_from_dom( $dom );
$this->assertEquals( $expected, $content );
}
public function test_no_gif_no_image_scripts() {
$source = '<img src="http://placehold.it/350x150.png" width="350" height="150" alt="Placeholder!" />';
$expected = array();
$dom = AMP_DOM_Utils::get_dom_from_content( $source );
$sanitizer = new AMP_Img_Sanitizer( $dom );
$sanitizer->sanitize();
$whitelist_sanitizer = new AMP_Tag_And_Attribute_Sanitizer( $dom );
$whitelist_sanitizer->sanitize();
$scripts = array_merge(
$sanitizer->get_scripts(),
$whitelist_sanitizer->get_scripts()
);
$this->assertEquals( $expected, $scripts );
}
public function test_no_gif_image_scripts() {
$source = '<img src="http://placehold.it/350x150.gif" width="350" height="150" alt="Placeholder!" />';
$expected = array( 'amp-anim' => true );
$dom = AMP_DOM_Utils::get_dom_from_content( $source );
$sanitizer = new AMP_Img_Sanitizer( $dom );
$sanitizer->sanitize();
$whitelist_sanitizer = new AMP_Tag_And_Attribute_Sanitizer( $dom );
$whitelist_sanitizer->sanitize();
$scripts = array_merge(
$sanitizer->get_scripts(),
$whitelist_sanitizer->get_scripts()
);
$this->assertEquals( $expected, $scripts );
}
}