forked from ampproject/amp-wp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-class-amp-base-sanitizer.php
247 lines (221 loc) · 5.93 KB
/
test-class-amp-base-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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<?php
/**
* Class AMP_Base_Sanitizer_Test
*
* @package AMP
*/
/**
* Test AMP_Base_Sanitizer_Test
*
* @covers AMP_Base_Sanitizer
*/
class AMP_Base_Sanitizer_Test extends WP_UnitTestCase {
/**
* Gets data for test_set_layout().
*
* @return array
*/
public function get_data() {
return array(
'both_dimensions_included' => array(
array(
'width' => 100,
'height' => 100,
'layout' => 'responsive',
),
array(
'width' => 100,
'height' => 100,
'layout' => 'responsive',
),
),
'both_dimensions_missing' => array(
array(),
array(
'height' => 400,
'layout' => 'fixed-height',
),
),
'both_dimensions_empty' => array(
array(
'width' => '',
'height' => '',
),
array(
'height' => 400,
'layout' => 'fixed-height',
),
),
'no_width' => array(
array(
'height' => 100,
),
array(
'height' => 100,
'layout' => 'fixed-height',
),
),
'no_height' => array(
array(
'width' => 200,
),
array(
'height' => 400,
'layout' => 'fixed-height',
),
),
'no_layout_specified' => array(
array(
'width' => 100,
'height' => 100,
),
array(
'width' => 100,
'height' => 100,
),
),
);
}
/**
* Test AMP_Base_Sanitizer::set_layout().
*
* @dataProvider get_data
* @param array $source_attributes Source Attrs.
* @param array $expected_attributes Expected Attrs.
* @param array $args Args.
* @covers AMP_Base_Sanitizer::enforce_fixed_height()
*/
public function test_set_layout( $source_attributes, $expected_attributes, $args = array() ) {
$sanitizer = new AMP_Test_Stub_Sanitizer( new DOMDocument(), $args );
$returned_attributes = $sanitizer->set_layout( $source_attributes );
$this->assertEquals( $expected_attributes, $returned_attributes );
}
/**
* Get sanitize_dimension data.
*
* @return array Data.
*/
public function get_sanitize_dimension_data() {
return array(
'empty' => array(
array( '', 'width' ),
'',
),
'empty_space' => array(
array( ' ', 'width' ),
'',
),
'int' => array(
array( 123, 'width' ),
123,
),
'int_as_string' => array(
array( '123', 'width' ),
123,
),
'with_px' => array(
array( '567px', 'width' ),
567,
),
'100%_width__with_max' => array(
array( '100%', 'width' ),
600,
array( 'content_max_width' => 600 ),
),
'100%_width__no_max' => array(
array( '100%', 'width' ),
'',
),
'50%_width__with_max' => array(
array( '50%', 'width' ),
300,
array( 'content_max_width' => 600 ),
),
'%_height' => array(
array( '100%', 'height' ),
'',
),
'non_int' => array(
array( 'abcd', 'width' ),
'',
),
);
}
/**
* Test AMP_Base_Sanitizer::sanitize_dimension().
*
* @param array $source_params Source Attrs.
* @param array $expected_value Expected Attrs.
* @param array $args Args.
* @dataProvider get_sanitize_dimension_data
* @covers AMP_Base_Sanitizer::sanitize_dimension()
*/
public function test_sanitize_dimension( $source_params, $expected_value, $args = array() ) {
$sanitizer = new AMP_Test_Stub_Sanitizer( new DOMDocument(), $args );
list( $value, $dimension ) = $source_params;
$actual_value = $sanitizer->sanitize_dimension( $value, $dimension );
$this->assertEquals( $expected_value, $actual_value );
}
/**
* Tests remove_child.
*
* @covers AMP_Base_Sanitizer::remove_invalid_child()
*/
public function test_remove_child() {
AMP_Validation_Utils::reset_validation_results();
$parent_tag_name = 'div';
$dom_document = new DOMDocument( '1.0', 'utf-8' );
$parent = $dom_document->createElement( $parent_tag_name );
$child = $dom_document->createElement( 'h1' );
$parent->appendChild( $child );
$this->assertEquals( $child, $parent->firstChild );
$sanitizer = new AMP_Iframe_Sanitizer(
$dom_document, array(
'validation_error_callback' => 'AMP_Validation_Utils::add_validation_error',
)
);
$sanitizer->remove_invalid_child( $child );
$this->assertEquals( null, $parent->firstChild );
$this->assertCount( 1, AMP_Validation_Utils::$validation_errors );
$this->assertEquals( $child->nodeName, AMP_Validation_Utils::$validation_errors[0]['node_name'] );
$parent->appendChild( $child );
$this->assertEquals( $child, $parent->firstChild );
$sanitizer->remove_invalid_child( $child );
$this->assertEquals( null, $parent->firstChild );
$this->assertEquals( null, $child->parentNode );
AMP_Validation_Utils::$validation_errors = null;
}
/**
* Tests remove_child.
*
* @covers AMP_Base_Sanitizer::remove_invalid_child()
*/
public function test_remove_attribute() {
AMP_Validation_Utils::reset_validation_results();
$video_name = 'amp-video';
$attribute = 'onload';
$dom_document = new DOMDocument( '1.0', 'utf-8' );
$video = $dom_document->createElement( $video_name );
$video->setAttribute( $attribute, 'someFunction()' );
$attr_node = $video->getAttributeNode( $attribute );
$args = array(
'validation_error_callback' => 'AMP_Validation_Utils::add_validation_error',
);
$sanitizer = new AMP_Video_Sanitizer( $dom_document, $args );
$sanitizer->remove_invalid_attribute( $video, $attribute );
$this->assertEquals( null, $video->getAttribute( $attribute ) );
$this->assertEquals(
array(
'code' => AMP_Validation_Utils::INVALID_ATTRIBUTE_CODE,
'node_name' => $attr_node->nodeName,
'parent_name' => $video->nodeName,
'sources' => array(),
'element_attributes' => array(
'onload' => 'someFunction()',
),
),
AMP_Validation_Utils::$validation_errors[0]
);
AMP_Validation_Utils::reset_validation_results();
}
}