-
Notifications
You must be signed in to change notification settings - Fork 384
/
test-amp-soundcloud-embed.php
194 lines (172 loc) · 5.58 KB
/
test-amp-soundcloud-embed.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
<?php
/**
* Class AMP_SoundCloud_Embed_Test
*
* @package AMP
*/
/**
* Class AMP_SoundCloud_Embed_Test
*
* @covers AMP_SoundCloud_Embed_Handler
*/
class AMP_SoundCloud_Embed_Test extends WP_UnitTestCase {
/**
* The oEmbed URL.
*
* @var string
*/
protected $oembed_url = 'https://soundcloud.com/jack-villano-villano/mozart-requiem-in-d-minor';
/**
* Response for oEmbed request.
*
* @see AMP_SoundCloud_Embed_Test::$oembed_url
* @var string
*/
protected $oembed_response = '{"version":1.0,"type":"rich","provider_name":"SoundCloud","provider_url":"http://soundcloud.com","height":400,"width":500,"title":"Mozart - Requiem in D minor Complete Full by Jack Villano Villano","description":"mass in D Minor ","thumbnail_url":"http://i1.sndcdn.com/artworks-000046826426-o7i9ki-t500x500.jpg","html":"\u003Ciframe width=\"500\" height=\"400\" scrolling=\"no\" frameborder=\"no\" src=\"https://w.soundcloud.com/player/?visual=true\u0026url=https%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F90097394\u0026show_artwork=true\u0026maxwidth=500\u0026maxheight=750\u0026dnt=1\"\u003E\u003C/iframe\u003E","author_name":"Jack Villano Villano","author_url":"https://soundcloud.com/jack-villano-villano"}';
/**
* Set up.
*
* @global WP_Post $post
*/
public function setUp() {
global $post;
parent::setUp();
/*
* As #34115 in 4.9 a post is not needed for context to run oEmbeds. Prior ot 4.9, the WP_Embed::shortcode()
* method would short-circuit when this is the case:
* https://github.com/WordPress/wordpress-develop/blob/4.8.4/src/wp-includes/class-wp-embed.php#L192-L193
* So on WP<4.9 we set a post global to ensure oEmbeds get processed.
*/
if ( version_compare( strtok( get_bloginfo( 'version' ), '-' ), '4.9', '<' ) ) {
$post = $this->factory()->post->create_and_get();
}
if ( function_exists( 'soundcloud_shortcode' ) ) {
add_shortcode( 'soundcloud', 'soundcloud_shortcode' );
}
add_filter( 'pre_http_request', array( $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', array( $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 ) {
unset( $r );
if ( false !== strpos( $url, 'soundcloud.com' ) ) {
return array(
'body' => $this->oembed_response,
'headers' => array(),
'response' => array(
'code' => 200,
'message' => 'ok',
),
'cookies' => array(),
'http_response' => null,
);
}
return $preempt;
}
/**
* Get conversion data.
*
* @return array
*/
public function get_conversion_data() {
$data = array(
'no_embed' => array(
'<p>Hello world.</p>',
'<p>Hello world.</p>' . PHP_EOL,
),
'url_simple' => array(
$this->oembed_url . PHP_EOL,
'<p><amp-soundcloud data-trackid="90097394" layout="fixed-height" height="200"></amp-soundcloud></p>' . PHP_EOL,
),
);
if ( defined( 'JETPACK__PLUGIN_DIR' ) ) {
require_once JETPACK__PLUGIN_DIR . 'modules/shortcodes/soundcloud.php';
}
if ( function_exists( 'soundcloud_shortcode' ) ) {
$data = array_merge(
$data,
array(
// This is supported by Jetpack.
'shortcode_unnamed_attr_as_url' => array(
'[soundcloud url=https://api.soundcloud.com/tracks/89299804]' . PHP_EOL,
'<amp-soundcloud data-trackid="89299804" layout="fixed-height" height="200"></amp-soundcloud>' . PHP_EOL,
),
// This apparently only works on WordPress.com.
'shortcode_with_id' => array(
'[soundcloud id=89299804]' . PHP_EOL,
'<amp-soundcloud data-trackid="89299804" layout="fixed-height" height="200"></amp-soundcloud>' . PHP_EOL,
),
)
);
}
return $data;
}
/**
* Test conversion.
*
* @covers AMP_SoundCloud_Embed_Handler::filter_embed_oembed_html()
* @covers AMP_SoundCloud_Embed_Handler::shortcode()
* @covers AMP_SoundCloud_Embed_Handler::render()
* @dataProvider get_conversion_data
*
* @param string $source Source.
* @param string $expected Expected.
*/
public function test__conversion( $source, $expected ) {
$embed = new AMP_SoundCloud_Embed_Handler();
$embed->register_embed();
$filtered_content = apply_filters( 'the_content', $source );
$this->assertEquals( $expected, $filtered_content );
}
/**
* Get scripts data.
*
* @return array Scripts data.
*/
public function get_scripts_data() {
return array(
'not_converted' => array(
'<p>Hello World.</p>',
array(),
),
'converted' => array(
$this->oembed_url . PHP_EOL,
array( 'amp-soundcloud' => true ),
),
);
}
/**
* Test get scripts.
*
* @covers AMP_SoundCloud_Embed_Handler::get_scripts()
* @dataProvider get_scripts_data
*
* @param string $source Source.
* @param string $expected Expected.
*/
public function test__get_scripts( $source, $expected ) {
$embed = new AMP_SoundCloud_Embed_Handler();
$embed->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(
$embed->get_scripts(),
$whitelist_sanitizer->get_scripts()
);
$this->assertEquals( $expected, $scripts );
}
}