]>
Commit | Line | Data |
---|---|---|
558d9aab JB |
1 | <?php |
2 | ||
a2c1b94e | 3 | namespace Tests\Wallabag\CoreBundle\Helper; |
558d9aab | 4 | |
f808b016 | 5 | use Graby\Graby; |
d5c2cc54 | 6 | use Monolog\Handler\TestHandler; |
f808b016 | 7 | use Monolog\Logger; |
bd91bd5c | 8 | use PHPUnit\Framework\TestCase; |
f808b016 JB |
9 | use Psr\Log\NullLogger; |
10 | use Symfony\Component\Validator\ConstraintViolation; | |
11 | use Symfony\Component\Validator\ConstraintViolationList; | |
12 | use Symfony\Component\Validator\Validator\RecursiveValidator; | |
c2656f96 JB |
13 | use Wallabag\CoreBundle\Entity\Entry; |
14 | use Wallabag\CoreBundle\Entity\Tag; | |
f808b016 | 15 | use Wallabag\CoreBundle\Helper\ContentProxy; |
6bc6fb1f | 16 | use Wallabag\CoreBundle\Helper\RuleBasedTagger; |
f808b016 | 17 | use Wallabag\UserBundle\Entity\User; |
558d9aab | 18 | |
bd91bd5c | 19 | class ContentProxyTest extends TestCase |
558d9aab | 20 | { |
ac1509a6 | 21 | private $fetchingErrorMessage = 'wallabag can\'t retrieve contents for this article. Please <a href="http://doc.wallabag.org/en/user/errors_during_fetching.html#how-can-i-help-to-fix-that">troubleshoot this issue</a>.'; |
a2c1b94e | 22 | |
4d0ec0e7 JB |
23 | public function testWithBadUrl() |
24 | { | |
25 | $tagger = $this->getTaggerMock(); | |
26 | $tagger->expects($this->once()) | |
27 | ->method('tag'); | |
28 | ||
29 | $graby = $this->getMockBuilder('Graby\Graby') | |
4094ea47 | 30 | ->setMethods(['fetchContent']) |
4d0ec0e7 JB |
31 | ->disableOriginalConstructor() |
32 | ->getMock(); | |
33 | ||
34 | $graby->expects($this->any()) | |
35 | ->method('fetchContent') | |
4094ea47 | 36 | ->willReturn([ |
4d0ec0e7 JB |
37 | 'html' => false, |
38 | 'title' => '', | |
39 | 'url' => '', | |
40 | 'content_type' => '', | |
41 | 'language' => '', | |
4094ea47 | 42 | ]); |
4d0ec0e7 | 43 | |
709e21a3 | 44 | $proxy = new ContentProxy($graby, $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); |
7aba665e JC |
45 | $entry = new Entry(new User()); |
46 | $proxy->updateEntry($entry, 'http://user@:80'); | |
4d0ec0e7 | 47 | |
f808b016 | 48 | $this->assertSame('http://user@:80', $entry->getUrl()); |
4d0ec0e7 | 49 | $this->assertEmpty($entry->getTitle()); |
f808b016 | 50 | $this->assertSame($this->fetchingErrorMessage, $entry->getContent()); |
4d0ec0e7 JB |
51 | $this->assertEmpty($entry->getPreviewPicture()); |
52 | $this->assertEmpty($entry->getMimetype()); | |
53 | $this->assertEmpty($entry->getLanguage()); | |
f808b016 | 54 | $this->assertSame(0.0, $entry->getReadingTime()); |
709e21a3 | 55 | $this->assertNull($entry->getDomainName()); |
4d0ec0e7 JB |
56 | } |
57 | ||
558d9aab JB |
58 | public function testWithEmptyContent() |
59 | { | |
f530f7f5 KG |
60 | $tagger = $this->getTaggerMock(); |
61 | $tagger->expects($this->once()) | |
62 | ->method('tag'); | |
63 | ||
558d9aab | 64 | $graby = $this->getMockBuilder('Graby\Graby') |
4094ea47 | 65 | ->setMethods(['fetchContent']) |
558d9aab JB |
66 | ->disableOriginalConstructor() |
67 | ->getMock(); | |
68 | ||
69 | $graby->expects($this->any()) | |
70 | ->method('fetchContent') | |
4094ea47 | 71 | ->willReturn([ |
98f0929f JB |
72 | 'html' => false, |
73 | 'title' => '', | |
74 | 'url' => '', | |
75 | 'content_type' => '', | |
76 | 'language' => '', | |
4094ea47 | 77 | ]); |
558d9aab | 78 | |
709e21a3 | 79 | $proxy = new ContentProxy($graby, $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); |
7aba665e JC |
80 | $entry = new Entry(new User()); |
81 | $proxy->updateEntry($entry, 'http://0.0.0.0'); | |
558d9aab | 82 | |
f808b016 | 83 | $this->assertSame('http://0.0.0.0', $entry->getUrl()); |
558d9aab | 84 | $this->assertEmpty($entry->getTitle()); |
f808b016 | 85 | $this->assertSame($this->fetchingErrorMessage, $entry->getContent()); |
558d9aab JB |
86 | $this->assertEmpty($entry->getPreviewPicture()); |
87 | $this->assertEmpty($entry->getMimetype()); | |
98f0929f | 88 | $this->assertEmpty($entry->getLanguage()); |
f808b016 JB |
89 | $this->assertSame(0.0, $entry->getReadingTime()); |
90 | $this->assertSame('0.0.0.0', $entry->getDomainName()); | |
558d9aab JB |
91 | } |
92 | ||
93 | public function testWithEmptyContentButOG() | |
94 | { | |
f530f7f5 KG |
95 | $tagger = $this->getTaggerMock(); |
96 | $tagger->expects($this->once()) | |
97 | ->method('tag'); | |
98 | ||
558d9aab | 99 | $graby = $this->getMockBuilder('Graby\Graby') |
4094ea47 | 100 | ->setMethods(['fetchContent']) |
558d9aab JB |
101 | ->disableOriginalConstructor() |
102 | ->getMock(); | |
103 | ||
104 | $graby->expects($this->any()) | |
105 | ->method('fetchContent') | |
4094ea47 | 106 | ->willReturn([ |
98f0929f JB |
107 | 'html' => false, |
108 | 'title' => '', | |
109 | 'url' => '', | |
110 | 'content_type' => '', | |
111 | 'language' => '', | |
10b35097 | 112 | 'status' => '', |
4094ea47 | 113 | 'open_graph' => [ |
98f0929f JB |
114 | 'og_title' => 'my title', |
115 | 'og_description' => 'desc', | |
4094ea47 JB |
116 | ], |
117 | ]); | |
558d9aab | 118 | |
709e21a3 | 119 | $proxy = new ContentProxy($graby, $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); |
7aba665e JC |
120 | $entry = new Entry(new User()); |
121 | $proxy->updateEntry($entry, 'http://domain.io'); | |
558d9aab | 122 | |
f808b016 JB |
123 | $this->assertSame('http://domain.io', $entry->getUrl()); |
124 | $this->assertSame('my title', $entry->getTitle()); | |
125 | $this->assertSame($this->fetchingErrorMessage . '<p><i>But we found a short description: </i></p>desc', $entry->getContent()); | |
558d9aab | 126 | $this->assertEmpty($entry->getPreviewPicture()); |
98f0929f | 127 | $this->assertEmpty($entry->getLanguage()); |
10b35097 | 128 | $this->assertEmpty($entry->getHttpStatus()); |
558d9aab | 129 | $this->assertEmpty($entry->getMimetype()); |
f808b016 JB |
130 | $this->assertSame(0.0, $entry->getReadingTime()); |
131 | $this->assertSame('domain.io', $entry->getDomainName()); | |
558d9aab JB |
132 | } |
133 | ||
134 | public function testWithContent() | |
135 | { | |
f530f7f5 KG |
136 | $tagger = $this->getTaggerMock(); |
137 | $tagger->expects($this->once()) | |
138 | ->method('tag'); | |
139 | ||
558d9aab | 140 | $graby = $this->getMockBuilder('Graby\Graby') |
4094ea47 | 141 | ->setMethods(['fetchContent']) |
558d9aab JB |
142 | ->disableOriginalConstructor() |
143 | ->getMock(); | |
144 | ||
145 | $graby->expects($this->any()) | |
146 | ->method('fetchContent') | |
4094ea47 | 147 | ->willReturn([ |
da3d4998 | 148 | 'html' => str_repeat('this is my content', 325), |
558d9aab JB |
149 | 'title' => 'this is my title', |
150 | 'url' => 'http://1.1.1.1', | |
151 | 'content_type' => 'text/html', | |
98f0929f | 152 | 'language' => 'fr', |
10b35097 | 153 | 'status' => '200', |
4094ea47 | 154 | 'open_graph' => [ |
558d9aab JB |
155 | 'og_title' => 'my OG title', |
156 | 'og_description' => 'OG desc', | |
f1e29e69 | 157 | 'og_image' => 'http://3.3.3.3/cover.jpg', |
4094ea47 JB |
158 | ], |
159 | ]); | |
558d9aab | 160 | |
709e21a3 | 161 | $proxy = new ContentProxy($graby, $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); |
7aba665e JC |
162 | $entry = new Entry(new User()); |
163 | $proxy->updateEntry($entry, 'http://0.0.0.0'); | |
558d9aab | 164 | |
f808b016 JB |
165 | $this->assertSame('http://1.1.1.1', $entry->getUrl()); |
166 | $this->assertSame('this is my title', $entry->getTitle()); | |
da3d4998 | 167 | $this->assertContains('this is my content', $entry->getContent()); |
f808b016 JB |
168 | $this->assertSame('http://3.3.3.3/cover.jpg', $entry->getPreviewPicture()); |
169 | $this->assertSame('text/html', $entry->getMimetype()); | |
170 | $this->assertSame('fr', $entry->getLanguage()); | |
171 | $this->assertSame('200', $entry->getHttpStatus()); | |
172 | $this->assertSame(4.0, $entry->getReadingTime()); | |
173 | $this->assertSame('1.1.1.1', $entry->getDomainName()); | |
558d9aab | 174 | } |
f530f7f5 | 175 | |
3d71d403 JB |
176 | public function testWithContentAndNoOgImage() |
177 | { | |
178 | $tagger = $this->getTaggerMock(); | |
179 | $tagger->expects($this->once()) | |
180 | ->method('tag'); | |
181 | ||
182 | $graby = $this->getMockBuilder('Graby\Graby') | |
183 | ->setMethods(['fetchContent']) | |
184 | ->disableOriginalConstructor() | |
185 | ->getMock(); | |
186 | ||
187 | $graby->expects($this->any()) | |
188 | ->method('fetchContent') | |
189 | ->willReturn([ | |
190 | 'html' => str_repeat('this is my content', 325), | |
191 | 'title' => 'this is my title', | |
192 | 'url' => 'http://1.1.1.1', | |
193 | 'content_type' => 'text/html', | |
194 | 'language' => 'fr', | |
195 | 'status' => '200', | |
196 | 'open_graph' => [ | |
197 | 'og_title' => 'my OG title', | |
198 | 'og_description' => 'OG desc', | |
0d349ea6 | 199 | 'og_image' => null, |
3d71d403 JB |
200 | ], |
201 | ]); | |
202 | ||
709e21a3 | 203 | $proxy = new ContentProxy($graby, $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); |
d5c2cc54 JB |
204 | $entry = new Entry(new User()); |
205 | $proxy->updateEntry($entry, 'http://0.0.0.0'); | |
3d71d403 | 206 | |
f808b016 JB |
207 | $this->assertSame('http://1.1.1.1', $entry->getUrl()); |
208 | $this->assertSame('this is my title', $entry->getTitle()); | |
3d71d403 | 209 | $this->assertContains('this is my content', $entry->getContent()); |
41d45c61 | 210 | $this->assertNull($entry->getPreviewPicture()); |
f808b016 JB |
211 | $this->assertSame('text/html', $entry->getMimetype()); |
212 | $this->assertSame('fr', $entry->getLanguage()); | |
213 | $this->assertSame('200', $entry->getHttpStatus()); | |
214 | $this->assertSame(4.0, $entry->getReadingTime()); | |
215 | $this->assertSame('1.1.1.1', $entry->getDomainName()); | |
0d349ea6 JB |
216 | } |
217 | ||
218 | public function testWithContentAndBadLanguage() | |
219 | { | |
220 | $tagger = $this->getTaggerMock(); | |
221 | $tagger->expects($this->once()) | |
222 | ->method('tag'); | |
223 | ||
5661e8d4 | 224 | $validator = $this->getValidator(false); |
a05b6115 | 225 | $validator->expects($this->once()) |
0d349ea6 | 226 | ->method('validate') |
a05b6115 | 227 | ->willReturn(new ConstraintViolationList([new ConstraintViolation('oops', 'oops', [], 'oops', 'language', 'dontexist')])); |
0d349ea6 JB |
228 | |
229 | $graby = $this->getMockBuilder('Graby\Graby') | |
230 | ->setMethods(['fetchContent']) | |
231 | ->disableOriginalConstructor() | |
232 | ->getMock(); | |
233 | ||
234 | $graby->expects($this->any()) | |
235 | ->method('fetchContent') | |
236 | ->willReturn([ | |
237 | 'html' => str_repeat('this is my content', 325), | |
238 | 'title' => 'this is my title', | |
239 | 'url' => 'http://1.1.1.1', | |
240 | 'content_type' => 'text/html', | |
241 | 'language' => 'dontexist', | |
242 | 'status' => '200', | |
243 | ]); | |
244 | ||
709e21a3 | 245 | $proxy = new ContentProxy($graby, $tagger, $validator, $this->getLogger(), $this->fetchingErrorMessage); |
0d349ea6 JB |
246 | $entry = new Entry(new User()); |
247 | $proxy->updateEntry($entry, 'http://0.0.0.0'); | |
248 | ||
f808b016 JB |
249 | $this->assertSame('http://1.1.1.1', $entry->getUrl()); |
250 | $this->assertSame('this is my title', $entry->getTitle()); | |
0d349ea6 | 251 | $this->assertContains('this is my content', $entry->getContent()); |
f808b016 | 252 | $this->assertSame('text/html', $entry->getMimetype()); |
41d45c61 | 253 | $this->assertNull($entry->getLanguage()); |
f808b016 JB |
254 | $this->assertSame('200', $entry->getHttpStatus()); |
255 | $this->assertSame(4.0, $entry->getReadingTime()); | |
256 | $this->assertSame('1.1.1.1', $entry->getDomainName()); | |
0d349ea6 JB |
257 | } |
258 | ||
259 | public function testWithContentAndBadOgImage() | |
260 | { | |
261 | $tagger = $this->getTaggerMock(); | |
262 | $tagger->expects($this->once()) | |
263 | ->method('tag'); | |
264 | ||
5661e8d4 | 265 | $validator = $this->getValidator(false); |
0d349ea6 JB |
266 | $validator->expects($this->exactly(2)) |
267 | ->method('validate') | |
268 | ->will($this->onConsecutiveCalls( | |
269 | new ConstraintViolationList(), | |
270 | new ConstraintViolationList([new ConstraintViolation('oops', 'oops', [], 'oops', 'url', 'https://')]) | |
271 | )); | |
272 | ||
273 | $graby = $this->getMockBuilder('Graby\Graby') | |
274 | ->setMethods(['fetchContent']) | |
275 | ->disableOriginalConstructor() | |
276 | ->getMock(); | |
277 | ||
278 | $graby->expects($this->any()) | |
279 | ->method('fetchContent') | |
280 | ->willReturn([ | |
281 | 'html' => str_repeat('this is my content', 325), | |
282 | 'title' => 'this is my title', | |
283 | 'url' => 'http://1.1.1.1', | |
284 | 'content_type' => 'text/html', | |
285 | 'language' => 'fr', | |
286 | 'status' => '200', | |
287 | 'open_graph' => [ | |
288 | 'og_title' => 'my OG title', | |
289 | 'og_description' => 'OG desc', | |
290 | 'og_image' => 'https://', | |
291 | ], | |
292 | ]); | |
293 | ||
709e21a3 | 294 | $proxy = new ContentProxy($graby, $tagger, $validator, $this->getLogger(), $this->fetchingErrorMessage); |
0d349ea6 JB |
295 | $entry = new Entry(new User()); |
296 | $proxy->updateEntry($entry, 'http://0.0.0.0'); | |
297 | ||
f808b016 JB |
298 | $this->assertSame('http://1.1.1.1', $entry->getUrl()); |
299 | $this->assertSame('this is my title', $entry->getTitle()); | |
0d349ea6 | 300 | $this->assertContains('this is my content', $entry->getContent()); |
41d45c61 | 301 | $this->assertNull($entry->getPreviewPicture()); |
f808b016 JB |
302 | $this->assertSame('text/html', $entry->getMimetype()); |
303 | $this->assertSame('fr', $entry->getLanguage()); | |
304 | $this->assertSame('200', $entry->getHttpStatus()); | |
305 | $this->assertSame(4.0, $entry->getReadingTime()); | |
306 | $this->assertSame('1.1.1.1', $entry->getDomainName()); | |
3d71d403 JB |
307 | } |
308 | ||
4d0ec0e7 JB |
309 | public function testWithForcedContent() |
310 | { | |
311 | $tagger = $this->getTaggerMock(); | |
312 | $tagger->expects($this->once()) | |
313 | ->method('tag'); | |
314 | ||
709e21a3 | 315 | $proxy = new ContentProxy((new Graby()), $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage, true); |
7aba665e JC |
316 | $entry = new Entry(new User()); |
317 | $proxy->updateEntry( | |
318 | $entry, | |
0d6cfb88 JB |
319 | 'http://0.0.0.0', |
320 | [ | |
321 | 'html' => str_repeat('this is my content', 325), | |
322 | 'title' => 'this is my title', | |
323 | 'url' => 'http://1.1.1.1', | |
324 | 'content_type' => 'text/html', | |
325 | 'language' => 'fr', | |
f0378b4d JB |
326 | 'date' => '1395635872', |
327 | 'authors' => ['Jeremy', 'Nico', 'Thomas'], | |
328 | 'all_headers' => [ | |
329 | 'Cache-Control' => 'no-cache', | |
38a04dee | 330 | ], |
0d6cfb88 JB |
331 | ] |
332 | ); | |
4d0ec0e7 | 333 | |
f808b016 JB |
334 | $this->assertSame('http://1.1.1.1', $entry->getUrl()); |
335 | $this->assertSame('this is my title', $entry->getTitle()); | |
4d0ec0e7 | 336 | $this->assertContains('this is my content', $entry->getContent()); |
f808b016 JB |
337 | $this->assertSame('text/html', $entry->getMimetype()); |
338 | $this->assertSame('fr', $entry->getLanguage()); | |
339 | $this->assertSame(4.0, $entry->getReadingTime()); | |
340 | $this->assertSame('1.1.1.1', $entry->getDomainName()); | |
341 | $this->assertSame('24/03/2014', $entry->getPublishedAt()->format('d/m/Y')); | |
f0378b4d JB |
342 | $this->assertContains('Jeremy', $entry->getPublishedBy()); |
343 | $this->assertContains('Nico', $entry->getPublishedBy()); | |
344 | $this->assertContains('Thomas', $entry->getPublishedBy()); | |
709e21a3 | 345 | $this->assertNotNull($entry->getHeaders(), 'Headers are stored, so value is not null'); |
f0378b4d JB |
346 | $this->assertContains('no-cache', $entry->getHeaders()); |
347 | } | |
348 | ||
349 | public function testWithForcedContentAndDatetime() | |
350 | { | |
351 | $tagger = $this->getTaggerMock(); | |
352 | $tagger->expects($this->once()) | |
353 | ->method('tag'); | |
354 | ||
d5c2cc54 | 355 | $logHandler = new TestHandler(); |
6acadf8e | 356 | $logger = new Logger('test', [$logHandler]); |
d5c2cc54 | 357 | |
709e21a3 | 358 | $proxy = new ContentProxy((new Graby()), $tagger, $this->getValidator(), $logger, $this->fetchingErrorMessage); |
7aba665e | 359 | $entry = new Entry(new User()); |
6acadf8e | 360 | $proxy->updateEntry( |
7aba665e | 361 | $entry, |
6acadf8e | 362 | 'http://1.1.1.1', |
f0378b4d JB |
363 | [ |
364 | 'html' => str_repeat('this is my content', 325), | |
365 | 'title' => 'this is my title', | |
366 | 'url' => 'http://1.1.1.1', | |
367 | 'content_type' => 'text/html', | |
368 | 'language' => 'fr', | |
369 | 'date' => '2016-09-08T11:55:58+0200', | |
370 | ] | |
371 | ); | |
372 | ||
f808b016 JB |
373 | $this->assertSame('http://1.1.1.1', $entry->getUrl()); |
374 | $this->assertSame('this is my title', $entry->getTitle()); | |
f0378b4d | 375 | $this->assertContains('this is my content', $entry->getContent()); |
f808b016 JB |
376 | $this->assertSame('text/html', $entry->getMimetype()); |
377 | $this->assertSame('fr', $entry->getLanguage()); | |
378 | $this->assertSame(4.0, $entry->getReadingTime()); | |
379 | $this->assertSame('1.1.1.1', $entry->getDomainName()); | |
380 | $this->assertSame('08/09/2016', $entry->getPublishedAt()->format('d/m/Y')); | |
f0378b4d JB |
381 | } |
382 | ||
383 | public function testWithForcedContentAndBadDate() | |
384 | { | |
385 | $tagger = $this->getTaggerMock(); | |
386 | $tagger->expects($this->once()) | |
387 | ->method('tag'); | |
388 | ||
389 | $logger = new Logger('foo'); | |
390 | $handler = new TestHandler(); | |
391 | $logger->pushHandler($handler); | |
392 | ||
709e21a3 | 393 | $proxy = new ContentProxy((new Graby()), $tagger, $this->getValidator(), $logger, $this->fetchingErrorMessage); |
7aba665e JC |
394 | $entry = new Entry(new User()); |
395 | $proxy->updateEntry( | |
396 | $entry, | |
6acadf8e | 397 | 'http://1.1.1.1', |
f0378b4d JB |
398 | [ |
399 | 'html' => str_repeat('this is my content', 325), | |
400 | 'title' => 'this is my title', | |
401 | 'url' => 'http://1.1.1.1', | |
402 | 'content_type' => 'text/html', | |
403 | 'language' => 'fr', | |
404 | 'date' => '01 02 2012', | |
405 | ] | |
406 | ); | |
407 | ||
f808b016 JB |
408 | $this->assertSame('http://1.1.1.1', $entry->getUrl()); |
409 | $this->assertSame('this is my title', $entry->getTitle()); | |
f0378b4d | 410 | $this->assertContains('this is my content', $entry->getContent()); |
f808b016 JB |
411 | $this->assertSame('text/html', $entry->getMimetype()); |
412 | $this->assertSame('fr', $entry->getLanguage()); | |
413 | $this->assertSame(4.0, $entry->getReadingTime()); | |
414 | $this->assertSame('1.1.1.1', $entry->getDomainName()); | |
f0378b4d JB |
415 | $this->assertNull($entry->getPublishedAt()); |
416 | ||
417 | $records = $handler->getRecords(); | |
418 | ||
419 | $this->assertCount(1, $records); | |
420 | $this->assertContains('Error while defining date', $records[0]['message']); | |
4d0ec0e7 JB |
421 | } |
422 | ||
423 | public function testTaggerThrowException() | |
424 | { | |
4d0ec0e7 JB |
425 | $tagger = $this->getTaggerMock(); |
426 | $tagger->expects($this->once()) | |
427 | ->method('tag') | |
428 | ->will($this->throwException(new \Exception())); | |
429 | ||
709e21a3 | 430 | $proxy = new ContentProxy((new Graby()), $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); |
7aba665e | 431 | $entry = new Entry(new User()); |
6acadf8e JB |
432 | $proxy->updateEntry( |
433 | $entry, | |
434 | 'http://1.1.1.1', | |
435 | [ | |
436 | 'html' => str_repeat('this is my content', 325), | |
437 | 'title' => 'this is my title', | |
438 | 'url' => 'http://1.1.1.1', | |
439 | 'content_type' => 'text/html', | |
440 | 'language' => 'fr', | |
441 | ] | |
d0e9b3d6 | 442 | ); |
4d0ec0e7 JB |
443 | |
444 | $this->assertCount(0, $entry->getTags()); | |
445 | } | |
446 | ||
74a75f7d JB |
447 | public function dataForCrazyHtml() |
448 | { | |
449 | return [ | |
450 | 'script and comment' => [ | |
451 | '<strong>Script inside:</strong> <!--[if gte IE 4]><script>alert(\'lol\');</script><![endif]--><br />', | |
38a04dee | 452 | 'lol', |
74a75f7d JB |
453 | ], |
454 | 'script' => [ | |
455 | '<strong>Script inside:</strong><script>alert(\'lol\');</script>', | |
38a04dee | 456 | 'script', |
74a75f7d JB |
457 | ], |
458 | ]; | |
459 | } | |
460 | ||
461 | /** | |
462 | * @dataProvider dataForCrazyHtml | |
463 | */ | |
464 | public function testWithCrazyHtmlContent($html, $escapedString) | |
465 | { | |
466 | $tagger = $this->getTaggerMock(); | |
467 | $tagger->expects($this->once()) | |
468 | ->method('tag'); | |
469 | ||
709e21a3 | 470 | $proxy = new ContentProxy((new Graby()), $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); |
6acadf8e JB |
471 | $entry = new Entry(new User()); |
472 | $proxy->updateEntry( | |
473 | $entry, | |
74a75f7d JB |
474 | 'http://1.1.1.1', |
475 | [ | |
476 | 'html' => $html, | |
477 | 'title' => 'this is my title', | |
478 | 'url' => 'http://1.1.1.1', | |
479 | 'content_type' => 'text/html', | |
480 | 'language' => 'fr', | |
481 | 'status' => '200', | |
482 | 'open_graph' => [ | |
483 | 'og_title' => 'my OG title', | |
484 | 'og_description' => 'OG desc', | |
485 | 'og_image' => 'http://3.3.3.3/cover.jpg', | |
486 | ], | |
487 | ] | |
488 | ); | |
489 | ||
f808b016 JB |
490 | $this->assertSame('http://1.1.1.1', $entry->getUrl()); |
491 | $this->assertSame('this is my title', $entry->getTitle()); | |
74a75f7d | 492 | $this->assertNotContains($escapedString, $entry->getContent()); |
f808b016 JB |
493 | $this->assertSame('http://3.3.3.3/cover.jpg', $entry->getPreviewPicture()); |
494 | $this->assertSame('text/html', $entry->getMimetype()); | |
495 | $this->assertSame('fr', $entry->getLanguage()); | |
496 | $this->assertSame('200', $entry->getHttpStatus()); | |
497 | $this->assertSame('1.1.1.1', $entry->getDomainName()); | |
74a75f7d JB |
498 | } |
499 | ||
d0ec2ddd JB |
500 | public function testWithImageAsContent() |
501 | { | |
502 | $tagger = $this->getTaggerMock(); | |
503 | $tagger->expects($this->once()) | |
504 | ->method('tag'); | |
505 | ||
506 | $graby = $this->getMockBuilder('Graby\Graby') | |
507 | ->setMethods(['fetchContent']) | |
508 | ->disableOriginalConstructor() | |
509 | ->getMock(); | |
510 | ||
511 | $graby->expects($this->any()) | |
512 | ->method('fetchContent') | |
513 | ->willReturn([ | |
514 | 'html' => '<p><img src="http://1.1.1.1/image.jpg" /></p>', | |
515 | 'title' => 'this is my title', | |
516 | 'url' => 'http://1.1.1.1/image.jpg', | |
517 | 'content_type' => 'image/jpeg', | |
518 | 'status' => '200', | |
519 | 'open_graph' => [], | |
520 | ]); | |
521 | ||
709e21a3 | 522 | $proxy = new ContentProxy($graby, $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); |
d0ec2ddd JB |
523 | $entry = new Entry(new User()); |
524 | $proxy->updateEntry($entry, 'http://0.0.0.0'); | |
525 | ||
c18a2476 JB |
526 | $this->assertSame('http://1.1.1.1/image.jpg', $entry->getUrl()); |
527 | $this->assertSame('this is my title', $entry->getTitle()); | |
d0ec2ddd JB |
528 | $this->assertContains('http://1.1.1.1/image.jpg', $entry->getContent()); |
529 | $this->assertSame('http://1.1.1.1/image.jpg', $entry->getPreviewPicture()); | |
c18a2476 JB |
530 | $this->assertSame('image/jpeg', $entry->getMimetype()); |
531 | $this->assertSame('200', $entry->getHttpStatus()); | |
532 | $this->assertSame('1.1.1.1', $entry->getDomainName()); | |
d0ec2ddd JB |
533 | } |
534 | ||
f530f7f5 KG |
535 | private function getTaggerMock() |
536 | { | |
6bc6fb1f | 537 | return $this->getMockBuilder(RuleBasedTagger::class) |
4094ea47 | 538 | ->setMethods(['tag']) |
f530f7f5 KG |
539 | ->disableOriginalConstructor() |
540 | ->getMock(); | |
541 | } | |
1c9cd2a7 | 542 | |
0c5bcd82 | 543 | private function getLogger() |
1c9cd2a7 | 544 | { |
0c5bcd82 | 545 | return new NullLogger(); |
1c9cd2a7 | 546 | } |
0d349ea6 | 547 | |
5661e8d4 | 548 | private function getValidator($withDefaultMock = true) |
0d349ea6 | 549 | { |
5661e8d4 | 550 | $mock = $this->getMockBuilder(RecursiveValidator::class) |
0d349ea6 JB |
551 | ->setMethods(['validate']) |
552 | ->disableOriginalConstructor() | |
553 | ->getMock(); | |
5661e8d4 JB |
554 | |
555 | if ($withDefaultMock) { | |
556 | $mock->expects($this->any()) | |
557 | ->method('validate') | |
558 | ->willReturn(new ConstraintViolationList()); | |
559 | } | |
560 | ||
561 | return $mock; | |
0d349ea6 | 562 | } |
558d9aab | 563 | } |