|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\View\Tests\Embed; |
|
4
|
|
|
|
|
5
|
|
|
use Embed\Extractor; |
|
6
|
|
|
use Embed\Http\Crawler; |
|
7
|
|
|
use Embed\Http\Url; |
|
|
|
|
|
|
8
|
|
|
use Psr\Http\Message\RequestInterface; |
|
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
10
|
|
|
use Psr\Http\Message\StreamInterface; |
|
11
|
|
|
use Psr\Http\Message\UriInterface; |
|
12
|
|
|
use SilverStripe\Core\Injector\Injector; |
|
13
|
|
|
use SilverStripe\Dev\SapphireTest; |
|
14
|
|
|
use SilverStripe\View\Embed\EmbedContainer; |
|
15
|
|
|
|
|
16
|
|
|
class EmbedContainerTest extends SapphireTest |
|
17
|
|
|
{ |
|
18
|
|
|
// this is used to facilitate spaghetti code via mocks |
|
19
|
|
|
// it's set to false when $crawlerMock::createRequest() is called |
|
20
|
|
|
public $firstRequest = true; |
|
21
|
|
|
|
|
22
|
|
|
public function testGetEmbed() |
|
23
|
|
|
{ |
|
24
|
|
|
$url = 'https://www.youtube.com/watch?v=iRXJXaLV0n4'; |
|
25
|
|
|
$crawlerMock = $this->createMock(Crawler::class); |
|
26
|
|
|
$crawlerMock->method('getResponseUri')->willReturn($this->mockUri()); |
|
27
|
|
|
$crawlerMock->method('createUri')->willReturn($this->mockUri()); |
|
28
|
|
|
$crawlerMock->method('sendRequest')->willReturn($this->mockResponse()); |
|
29
|
|
|
$crawlerMock->method('createRequest')->willReturn($this->mockRequest()); |
|
30
|
|
|
|
|
31
|
|
|
$embed = new EmbedContainer($url); |
|
32
|
|
|
$embed->setCrawler($crawlerMock); |
|
33
|
|
|
$this->assertEmpty($embed->getOptions()); |
|
34
|
|
|
|
|
35
|
|
|
$embed->setOptions(['foo' => 'bar']); |
|
36
|
|
|
|
|
37
|
|
|
$extractor = $embed->getExtractor(); |
|
38
|
|
|
$this->assertInstanceOf(Extractor::class, $extractor); |
|
39
|
|
|
$this->assertSame('Try to stay SERIOUS -The most popular CAT videos', $extractor->title); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
private function mockResponse(): ResponseInterface |
|
43
|
|
|
{ |
|
44
|
|
|
$c = new class($this) implements ResponseInterface |
|
45
|
|
|
{ |
|
46
|
|
|
private $unitTestInstance; |
|
47
|
|
|
public function __construct(EmbedContainerTest $unitTestInstance) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->unitTestInstance = $unitTestInstance; |
|
50
|
|
|
} |
|
51
|
|
|
public function getStatusCode() |
|
52
|
|
|
{ |
|
53
|
|
|
return 200; |
|
54
|
|
|
} |
|
55
|
|
|
public function getBody() |
|
56
|
|
|
{ |
|
57
|
|
|
// first request is to the video HTML to get to find the oembed link |
|
58
|
|
|
// second request is to the oembed endpoint to fetch JSON |
|
59
|
|
|
if ($this->unitTestInstance->firstRequest) { |
|
60
|
|
|
return '<html><link rel="alternate" type="application/json+oembed" href="https://www.youtube.com/oembed?format=json&url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DiRXJXaLV0n4" title="Try to stay SERIOUS -The most popular CAT videos"></html>'; |
|
61
|
|
|
} else { |
|
62
|
|
|
return json_encode([ |
|
63
|
|
|
'author_url' => 'https://www.youtube.com/channel/UCR2KG2dK1tAkwZZjm7rAiSg', |
|
64
|
|
|
'thumbnail_width' => 480, |
|
65
|
|
|
'title' => 'Try to stay SERIOUS -The most popular CAT videos', |
|
66
|
|
|
'width' => 480, |
|
67
|
|
|
'provider_name' => 'YouTube', |
|
68
|
|
|
'author_name' => 'Tiger Funnies', |
|
69
|
|
|
'height' => 270, |
|
70
|
|
|
'version' => '1.0', |
|
71
|
|
|
'type' => 'video', |
|
72
|
|
|
// phpcs:ignore |
|
73
|
|
|
'html' => '<iframe width="480" height="270" src="https://www.youtube.com/embed/iRXJXaLV0n4?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>', |
|
74
|
|
|
'provider_url' => 'https://www.youtube.com/', |
|
75
|
|
|
'thumbnail_height' => 360, |
|
76
|
|
|
'thumbnail_url' => 'https://i.ytimg.com/vi/iRXJXaLV0n4/hqdefault.jpg', |
|
77
|
|
|
]); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
public function withStatus($code, $reasonPhrase = '') |
|
81
|
|
|
{ |
|
82
|
|
|
return $this; |
|
83
|
|
|
} |
|
84
|
|
|
public function getReasonPhrase() |
|
85
|
|
|
{ |
|
86
|
|
|
} |
|
87
|
|
|
public function getProtocolVersion() |
|
88
|
|
|
{ |
|
89
|
|
|
} |
|
90
|
|
|
public function withProtocolVersion($version) |
|
91
|
|
|
{ |
|
92
|
|
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
public function getHeaders() |
|
95
|
|
|
{ |
|
96
|
|
|
} |
|
97
|
|
|
public function hasHeader($name) |
|
98
|
|
|
{ |
|
99
|
|
|
} |
|
100
|
|
|
public function getHeader($name) |
|
101
|
|
|
{ |
|
102
|
|
|
} |
|
103
|
|
|
public function getHeaderLine($name) |
|
104
|
|
|
{ |
|
105
|
|
|
} |
|
106
|
|
|
public function withHeader($name, $value) |
|
107
|
|
|
{ |
|
108
|
|
|
return $this; |
|
109
|
|
|
} |
|
110
|
|
|
public function withAddedHeader($name, $value) |
|
111
|
|
|
{ |
|
112
|
|
|
return $this; |
|
113
|
|
|
} |
|
114
|
|
|
public function withoutHeader($name) |
|
115
|
|
|
{ |
|
116
|
|
|
return $this; |
|
117
|
|
|
} |
|
118
|
|
|
public function withBody(StreamInterface $body) |
|
119
|
|
|
{ |
|
120
|
|
|
return $this; |
|
121
|
|
|
} |
|
122
|
|
|
}; |
|
123
|
|
|
return new $c($this); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function mockUri(): UriInterface |
|
127
|
|
|
{ |
|
128
|
|
|
$c = new class implements UriInterface |
|
129
|
|
|
{ |
|
130
|
|
|
public function getScheme() |
|
131
|
|
|
{ |
|
132
|
|
|
return 'https'; |
|
133
|
|
|
} |
|
134
|
|
|
public function getHost() |
|
135
|
|
|
{ |
|
136
|
|
|
return 'youtube.com'; |
|
137
|
|
|
} |
|
138
|
|
|
public function getPath() |
|
139
|
|
|
{ |
|
140
|
|
|
return '/watch'; |
|
141
|
|
|
} |
|
142
|
|
|
public function getQuery() |
|
143
|
|
|
{ |
|
144
|
|
|
return 'v=iRXJXaLV0n4'; |
|
145
|
|
|
} |
|
146
|
|
|
public function getPort() |
|
147
|
|
|
{ |
|
148
|
|
|
return 443; |
|
149
|
|
|
} |
|
150
|
|
|
public function getAuthority() |
|
151
|
|
|
{ |
|
152
|
|
|
} |
|
153
|
|
|
public function getUserInfo() |
|
154
|
|
|
{ |
|
155
|
|
|
} |
|
156
|
|
|
public function getFragment() |
|
157
|
|
|
{ |
|
158
|
|
|
} |
|
159
|
|
|
public function withPath($path) |
|
160
|
|
|
{ |
|
161
|
|
|
return $this; |
|
162
|
|
|
} |
|
163
|
|
|
public function withScheme($scheme) |
|
164
|
|
|
{ |
|
165
|
|
|
return $this; |
|
166
|
|
|
} |
|
167
|
|
|
public function withUserInfo($user, $password = null) |
|
168
|
|
|
{ |
|
169
|
|
|
return $this; |
|
170
|
|
|
} |
|
171
|
|
|
public function withHost($host) |
|
172
|
|
|
{ |
|
173
|
|
|
return $this; |
|
174
|
|
|
} |
|
175
|
|
|
public function withPort($port) |
|
176
|
|
|
{ |
|
177
|
|
|
return $this; |
|
178
|
|
|
} |
|
179
|
|
|
public function withQuery($query) |
|
180
|
|
|
{ |
|
181
|
|
|
return $this; |
|
182
|
|
|
} |
|
183
|
|
|
public function withFragment($fragment) |
|
184
|
|
|
{ |
|
185
|
|
|
return $this; |
|
186
|
|
|
} |
|
187
|
|
|
public function __toString() |
|
188
|
|
|
{ |
|
189
|
|
|
return ''; |
|
190
|
|
|
} |
|
191
|
|
|
}; |
|
192
|
|
|
return new $c(); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
private function mockRequest(): RequestInterface |
|
196
|
|
|
{ |
|
197
|
|
|
$c = new class($this) implements RequestInterface |
|
198
|
|
|
{ |
|
199
|
|
|
private $unitTestInstance; |
|
200
|
|
|
public function __construct(EmbedContainerTest $unitTestInstance) |
|
201
|
|
|
{ |
|
202
|
|
|
$this->unitTestInstance = $unitTestInstance; |
|
203
|
|
|
} |
|
204
|
|
|
public function getRequestTarget() |
|
205
|
|
|
{ |
|
206
|
|
|
} |
|
207
|
|
|
public function withRequestTarget($requestTarget) |
|
208
|
|
|
{ |
|
209
|
|
|
return $this; |
|
210
|
|
|
} |
|
211
|
|
|
public function getMethod() |
|
212
|
|
|
{ |
|
213
|
|
|
} |
|
214
|
|
|
public function withMethod($method) |
|
215
|
|
|
{ |
|
216
|
|
|
} |
|
217
|
|
|
public function getUri() |
|
218
|
|
|
{ |
|
219
|
|
|
$this->unitTestInstance->firstRequest = false; |
|
220
|
|
|
return $this->unitTestInstance->mockUri(); |
|
221
|
|
|
} |
|
222
|
|
|
public function withUri(UriInterface $uri, $preserveHost = false) |
|
223
|
|
|
{ |
|
224
|
|
|
return $this; |
|
225
|
|
|
} |
|
226
|
|
|
public function getProtocolVersion() |
|
227
|
|
|
{ |
|
228
|
|
|
} |
|
229
|
|
|
public function withProtocolVersion($version) |
|
230
|
|
|
{ |
|
231
|
|
|
return $this; |
|
232
|
|
|
} |
|
233
|
|
|
public function getHeaders() |
|
234
|
|
|
{ |
|
235
|
|
|
} |
|
236
|
|
|
public function hasHeader($name) |
|
237
|
|
|
{ |
|
238
|
|
|
} |
|
239
|
|
|
public function getHeader($name) |
|
240
|
|
|
{ |
|
241
|
|
|
} |
|
242
|
|
|
public function getHeaderLine($name) |
|
243
|
|
|
{ |
|
244
|
|
|
} |
|
245
|
|
|
public function withHeader($name, $value) |
|
246
|
|
|
{ |
|
247
|
|
|
return $this; |
|
248
|
|
|
} |
|
249
|
|
|
public function withAddedHeader($name, $value) |
|
250
|
|
|
{ |
|
251
|
|
|
return $this; |
|
252
|
|
|
} |
|
253
|
|
|
public function withoutHeader($name) |
|
254
|
|
|
{ |
|
255
|
|
|
return $this; |
|
256
|
|
|
} |
|
257
|
|
|
public function getBody() |
|
258
|
|
|
{ |
|
259
|
|
|
} |
|
260
|
|
|
public function withBody(StreamInterface $body) |
|
261
|
|
|
{ |
|
262
|
|
|
return $this; |
|
263
|
|
|
} |
|
264
|
|
|
}; |
|
265
|
|
|
return new $c($this); |
|
266
|
|
|
} |
|
267
|
|
|
} |
|
268
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths