/
test-class-amp-youtube-embed-handler.php
328 lines (293 loc) · 12 KB
/
test-class-amp-youtube-embed-handler.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
<?php
/**
* Tests for AMP_YouTube_Embed_Handler.
*
* @package AMP
* @since 0.7
*/
use AmpProject\AmpWP\Tests\AssertContainsCompatibility;
use AmpProject\AmpWP\Tests\PrivateAccess;
/**
* Tests for AMP_YouTube_Embed_Handler.
*
* @covers AMP_YouTube_Embed_Handler
*/
class Test_AMP_YouTube_Embed_Handler extends WP_UnitTestCase {
use AssertContainsCompatibility;
use PrivateAccess;
protected $youtube_video_id = 'kfVsfOSbJY0';
/**
* Response for YouTube oEmbed request.
*
* @see Test_AMP_YouTube_Embed_Handler::$youtube_video_id
* @var string
*/
protected $youtube_oembed_response = '{"height":281,"type":"video","author_name":"rebecca","thumbnail_url":"https:\/\/i.ytimg.com\/vi\/kfVsfOSbJY0\/hqdefault.jpg","provider_url":"https:\/\/www.youtube.com\/","title":"Rebecca Black - Friday","version":"1.0","width":500,"thumbnail_height":360,"html":"\u003ciframe width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/kfVsfOSbJY0?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen\u003e\u003c\/iframe\u003e","thumbnail_width":500,"provider_name":"YouTube","author_url":"https:\/\/www.youtube.com\/user\/rebecca"}';
/**
* An instance of this embed handler.
*
* @var AMP_YouTube_Embed_Handler.
*/
public $handler;
/**
* Set up each test.
*/
public function setUp() {
parent::setUp();
$this->handler = new AMP_YouTube_Embed_Handler();
add_filter( 'pre_http_request', [ $this, 'mock_http_request' ], 10, 3 );
}
/**
* After a test method runs, reset any state in WordPress the test method might have changed.
*/
public function tearDown() {
remove_filter( 'pre_http_request', [ $this, 'mock_http_request' ] );
parent::tearDown();
}
/**
* Mock HTTP request.
*
* @param mixed $preempt Whether to preempt an HTTP request's return value. Default false.
* @param mixed $r HTTP request arguments.
* @param string $url The request URL.
* @return array Response data.
*/
public function mock_http_request( $preempt, $r, $url ) {
if ( in_array( 'external-http', $_SERVER['argv'], true ) ) {
return $preempt;
}
$host = wp_parse_url( $url, PHP_URL_HOST );
if ( ! in_array( $host, [ 'youtu.be', 'youtube.com', 'www.youtube.com' ], true ) ) {
return $preempt;
}
/**
* The URL http://www.youtube.com/watch?v=kfVsfOSbJY0?hl=en&fs=1&w=425&h=349 is invalid as
* it has multiple query vars. Checking if `?v=kfVsfOSbJY0?hl=en` is in the URL should be a
* sufficient check when that URL is being mocked.
*/
if ( false !== strpos( $url, '%3Fv%3DkfVsfOSbJY0%3Fhl%3Den' ) ) {
return $preempt;
}
unset( $r );
return [
'body' => $this->youtube_oembed_response,
'headers' => [],
'response' => [
'code' => 200,
'message' => 'ok',
],
'cookies' => [],
'http_response' => null,
];
}
/**
* Test video_override().
*
* @covers AMP_YouTube_Embed_Handler::video_override()
*/
public function test_video_override() {
remove_all_filters( 'wp_video_shortcode_override' );
$this->handler->register_embed();
$youtube_id = 'XOY3ZUO6P0k';
$youtube_src = 'https://youtu.be/' . $youtube_id;
$attr_youtube = [
'src' => $youtube_src,
];
$youtube_shortcode = $this->handler->video_override( '', $attr_youtube );
$this->assertStringContains( '<amp-youtube', $youtube_shortcode );
$this->assertStringContains( $youtube_id, $youtube_shortcode );
$vimeo_id = '64086087';
$vimeo_src = 'https://vimeo.com/' . $vimeo_id;
$attr_vimeo = [
'src' => $vimeo_src,
];
$vimeo_shortcode = $this->handler->video_override( '', $attr_vimeo );
$this->assertEquals( '', $vimeo_shortcode );
$daily_motion_id = 'x6bacgf';
$daily_motion_src = 'http://www.dailymotion.com/video/' . $daily_motion_id;
$attr_daily_motion = [
'src' => $daily_motion_src,
];
$daily_motion_shortcode = $this->handler->video_override( '', $attr_daily_motion );
$this->assertEquals( '', $daily_motion_shortcode );
$no_attributes = $this->handler->video_override( '', [] );
$this->assertEquals( '', $no_attributes );
remove_all_filters( 'wp_video_shortcode_override' );
}
public function get_conversion_data() {
$loading_attribute = version_compare( get_bloginfo( 'version' ), '5.4', '>' ) ? 'loading="lazy" ' : '';
return [
'no_embed' => [
'<p>Hello world.</p>',
'<p>Hello world.</p>' . PHP_EOL,
],
'url_simple' => [
'https://www.youtube.com/watch?v=kfVsfOSbJY0' . PHP_EOL,
'<p><amp-youtube data-videoid="kfVsfOSbJY0" layout="responsive" width="500" height="281" title="Rebecca Black - Friday"><a placeholder href="https://www.youtube.com/watch?v=kfVsfOSbJY0"><img ' . $loading_attribute . 'src="https://i.ytimg.com/vi/kfVsfOSbJY0/hqdefault.jpg" layout="fill" object-fit="cover" alt="Rebecca Black - Friday"></img></a></amp-youtube></p>' . PHP_EOL,
'<p><amp-youtube data-videoid="kfVsfOSbJY0" layout="responsive" width="500" height="281"><a placeholder href="https://www.youtube.com/watch?v=kfVsfOSbJY0"><img ' . $loading_attribute . 'src="https://i.ytimg.com/vi/kfVsfOSbJY0/hqdefault.jpg" layout="fill" object-fit="cover"></img></a></amp-youtube></p>' . PHP_EOL,
],
'url_short' => [
'https://youtu.be/kfVsfOSbJY0' . PHP_EOL,
'<p><amp-youtube data-videoid="kfVsfOSbJY0" layout="responsive" width="500" height="281" title="Rebecca Black - Friday"><a placeholder href="https://youtu.be/kfVsfOSbJY0"><img ' . $loading_attribute . 'src="https://i.ytimg.com/vi/kfVsfOSbJY0/hqdefault.jpg" layout="fill" object-fit="cover" alt="Rebecca Black - Friday"></img></a></amp-youtube></p>' . PHP_EOL,
'<p><amp-youtube data-videoid="kfVsfOSbJY0" layout="responsive" width="500" height="281"><a placeholder href="https://youtu.be/kfVsfOSbJY0"><img ' . $loading_attribute . 'src="https://i.ytimg.com/vi/kfVsfOSbJY0/hqdefault.jpg" layout="fill" object-fit="cover"></img></a></amp-youtube></p>' . PHP_EOL,
],
'url_with_querystring' => [
'http://www.youtube.com/watch?v=kfVsfOSbJY0&hl=en&fs=1&w=425&h=349' . PHP_EOL,
'<p><amp-youtube data-videoid="kfVsfOSbJY0" layout="responsive" width="500" height="281" title="Rebecca Black - Friday"><a placeholder href="http://www.youtube.com/watch?v=kfVsfOSbJY0&hl=en&fs=1&w=425&h=349"><img ' . $loading_attribute . 'src="https://i.ytimg.com/vi/kfVsfOSbJY0/hqdefault.jpg" layout="fill" object-fit="cover" alt="Rebecca Black - Friday"></img></a></amp-youtube></p>' . PHP_EOL,
'<p><amp-youtube data-videoid="kfVsfOSbJY0" layout="responsive" width="500" height="281"><a placeholder href="http://www.youtube.com/watch?v=kfVsfOSbJY0&hl=en&fs=1&w=425&h=349"><img ' . $loading_attribute . 'src="https://i.ytimg.com/vi/kfVsfOSbJY0/hqdefault.jpg" layout="fill" object-fit="cover"></img></a></amp-youtube></p>' . PHP_EOL,
],
// Several reports of invalid URLs that have multiple `?` in the URL.
'url_with_querystring_and_extra_?' => [
'http://www.youtube.com/watch?v=kfVsfOSbJY0?hl=en&fs=1&w=425&h=349' . PHP_EOL,
'<p>http://www.youtube.com/watch?v=kfVsfOSbJY0?hl=en&fs=1&w=425&h=349</p>' . PHP_EOL,
],
'embed_url' => [
'https://www.youtube.com/embed/kfVsfOSbJY0' . PHP_EOL,
'<p><amp-youtube data-videoid="kfVsfOSbJY0" layout="responsive" width="500" height="281" title="Rebecca Black - Friday"><a placeholder href="https://youtube.com/watch?v=kfVsfOSbJY0"><img ' . $loading_attribute . 'src="https://i.ytimg.com/vi/kfVsfOSbJY0/hqdefault.jpg" layout="fill" object-fit="cover" alt="Rebecca Black - Friday"></img></a></amp-youtube></p>' . PHP_EOL,
'<p><amp-youtube data-videoid="kfVsfOSbJY0" layout="responsive" width="500" height="281"><a placeholder href="https://youtube.com/watch?v=kfVsfOSbJY0"><img src="https://i.ytimg.com/vi/kfVsfOSbJY0/hqdefault.jpg" layout="fill" object-fit="cover"></img></a></amp-youtube></p>' . PHP_EOL,
],
];
}
/**
* @dataProvider get_conversion_data
*/
public function test__conversion( $source, $expected, $fallback_for_expected = null ) {
$this->handler->register_embed();
$filtered_content = apply_filters( 'the_content', $source );
if (
version_compare( strtok( get_bloginfo( 'version' ), '-' ), '5.2', '<' )
&& null !== $fallback_for_expected
) {
$this->assertEquals( $fallback_for_expected, $filtered_content );
} else {
$this->assertEquals( $expected, $filtered_content );
}
}
/**
* Gets the test data for test_get_video_id_from_url().
*
* @return array The test data.
*/
public function get_video_id_data() {
return [
'basic_url' => [
'https://www.youtube.com/watch?v=XOY3ZUO6P0k',
'XOY3ZUO6P0k',
],
'mobile_url' => [
'https://m.youtube.com/watch?v=XOY3ZUO6P0k',
'XOY3ZUO6P0k',
],
'short_url' => [
'https://youtu.be/XOY3ZUO6P0k',
'XOY3ZUO6P0k',
],
'url_with_underscore' => [
'https://www.youtube.com/watch?v=CMrv_D78oxY',
'CMrv_D78oxY',
],
'short_url_with_underscore' => [
'https://youtu.be/CMrv_D78oxY',
'CMrv_D78oxY',
],
'url_with_hyphen' => [
'https://www.youtube.com/watch?v=xo68-iWaKv8',
'xo68-iWaKv8',
],
'url_with_hyphen_and_query_string' => [
'https://www.youtube.com/watch?v=xo68-iWaKv8&w=800&h=400',
'xo68-iWaKv8',
],
'url_with_hyphen_and_query_string_dimensions_before_id' => [
'https://www.youtube.com/watch?w=800&h=400&v=xo68-iWaKv8',
'xo68-iWaKv8',
],
'embed_url' => [
'http://www.youtube.com/embed/XOY3ZUO6P0k',
'XOY3ZUO6P0k',
],
'embed_url_ending_in_query_param' => [
'http://www.youtube.com/embed/XOY3ZUO6P0k?rel=0',
'XOY3ZUO6P0k',
],
'v_segment_url' => [
'http://youtube.com/v/XOY3ZUO6P0k',
'XOY3ZUO6P0k',
],
'e_segment_url' => [
'http://youtube.com/e/XOY3ZUO6P0k',
'XOY3ZUO6P0k',
],
'vi_segment_url' => [
'http://youtube.com/vi/XOY3ZUO6P0k',
'XOY3ZUO6P0k',
],
'vi_query_param_url' => [
'http://youtube.com/?vi=XOY3ZUO6P0k',
'XOY3ZUO6P0k',
],
'nocookie_url' => [
'//www.youtube-nocookie.com/embed/XOY3ZUO6P0k?rel=0',
'XOY3ZUO6P0k',
],
'account_url' => [
'https://www.youtube.com/account',
false,
],
'account_url_followed_by_segment' => [
'https://www.youtube.com/account/johnsmith',
false,
],
'playlist_url' => [
'https://www.youtube.com/playlist?list=PLCra4VPr-3frJzAd-lVYo3-34wu0Eax_u',
false,
],
'false_because_no_id' => [
'http://youtube.com/?wrong=XOY3ZUO6P0k',
false,
],
];
}
/**
* Tests get_video_id_from_url.
*
* @dataProvider get_video_id_data
* @covers AMP_YouTube_Embed_Handler::get_video_id_from_url()
*
* @param string $url The URL to test.
* @param string|false $expected The expected result.
* @throws ReflectionException If a reflection of the object is not possible.
*/
public function test_get_video_id_from_url( $url, $expected ) {
$this->assertEquals(
$expected,
$this->call_private_method( $this->handler, 'get_video_id_from_url', [ $url ] )
);
}
public function get_scripts_data() {
return [
'not_converted' => [
'<p>Hello World.</p>',
[],
],
'converted' => [
'https://www.youtube.com/watch?v=kfVsfOSbJY0' . PHP_EOL,
[ 'amp-youtube' => true ],
],
];
}
/**
* @dataProvider get_scripts_data
*/
public function test__get_scripts( $source, $expected ) {
$this->handler->register_embed();
$source = apply_filters( 'the_content', $source );
$whitelist_sanitizer = new AMP_Tag_And_Attribute_Sanitizer( AMP_DOM_Utils::get_dom_from_content( $source ) );
$whitelist_sanitizer->sanitize();
$scripts = array_merge(
$this->handler->get_scripts(),
$whitelist_sanitizer->get_scripts()
);
$this->assertEquals( $expected, $scripts );
}
}