3 namespace Tests\Wallabag\CoreBundle\Helper
;
5 use Psr\Log\NullLogger
;
7 use Monolog\Handler\TestHandler
;
8 use Wallabag\CoreBundle\Helper\ContentProxy
;
9 use Wallabag\CoreBundle\Entity\Entry
;
10 use Wallabag\CoreBundle\Entity\Tag
;
11 use Wallabag\UserBundle\Entity\User
;
12 use Wallabag\CoreBundle\Helper\RuleBasedTagger
;
15 class ContentProxyTest
extends \PHPUnit_Framework_TestCase
17 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>.';
19 public function testWithBadUrl()
21 $tagger = $this->getTaggerMock();
22 $tagger->expects($this->once())
25 $graby = $this->getMockBuilder('Graby\Graby')
26 ->setMethods(['fetchContent'])
27 ->disableOriginalConstructor()
30 $graby->expects($this->any())
31 ->method('fetchContent')
40 $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage
);
41 $entry = new Entry(new User());
42 $proxy->updateEntry($entry, 'http://user@:80');
44 $this->assertEquals('http://user@:80', $entry->getUrl());
45 $this->assertEmpty($entry->getTitle());
46 $this->assertEquals($this->fetchingErrorMessage
, $entry->getContent());
47 $this->assertEmpty($entry->getPreviewPicture());
48 $this->assertEmpty($entry->getMimetype());
49 $this->assertEmpty($entry->getLanguage());
50 $this->assertEquals(0.0, $entry->getReadingTime());
51 $this->assertEquals(false, $entry->getDomainName());
54 public function testWithEmptyContent()
56 $tagger = $this->getTaggerMock();
57 $tagger->expects($this->once())
60 $graby = $this->getMockBuilder('Graby\Graby')
61 ->setMethods(['fetchContent'])
62 ->disableOriginalConstructor()
65 $graby->expects($this->any())
66 ->method('fetchContent')
75 $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage
);
76 $entry = new Entry(new User());
77 $proxy->updateEntry($entry, 'http://0.0.0.0');
79 $this->assertEquals('http://0.0.0.0', $entry->getUrl());
80 $this->assertEmpty($entry->getTitle());
81 $this->assertEquals($this->fetchingErrorMessage
, $entry->getContent());
82 $this->assertEmpty($entry->getPreviewPicture());
83 $this->assertEmpty($entry->getMimetype());
84 $this->assertEmpty($entry->getLanguage());
85 $this->assertEquals(0.0, $entry->getReadingTime());
86 $this->assertEquals('0.0.0.0', $entry->getDomainName());
89 public function testWithEmptyContentButOG()
91 $tagger = $this->getTaggerMock();
92 $tagger->expects($this->once())
95 $graby = $this->getMockBuilder('Graby\Graby')
96 ->setMethods(['fetchContent'])
97 ->disableOriginalConstructor()
100 $graby->expects($this->any())
101 ->method('fetchContent')
106 'content_type' => '',
110 'og_title' => 'my title',
111 'og_description' => 'desc',
115 $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage
);
116 $entry = new Entry(new User());
117 $proxy->updateEntry($entry, 'http://domain.io');
119 $this->assertEquals('http://domain.io', $entry->getUrl());
120 $this->assertEquals('my title', $entry->getTitle());
121 $this->assertEquals($this->fetchingErrorMessage
.'<p><i>But we found a short description: </i></p>desc', $entry->getContent());
122 $this->assertEmpty($entry->getPreviewPicture());
123 $this->assertEmpty($entry->getLanguage());
124 $this->assertEmpty($entry->getHttpStatus());
125 $this->assertEmpty($entry->getMimetype());
126 $this->assertEquals(0.0, $entry->getReadingTime());
127 $this->assertEquals('domain.io', $entry->getDomainName());
130 public function testWithContent()
132 $tagger = $this->getTaggerMock();
133 $tagger->expects($this->once())
136 $graby = $this->getMockBuilder('Graby\Graby')
137 ->setMethods(['fetchContent'])
138 ->disableOriginalConstructor()
141 $graby->expects($this->any())
142 ->method('fetchContent')
144 'html' => str_repeat('this is my content', 325),
145 'title' => 'this is my title',
146 'url' => 'http://1.1.1.1',
147 'content_type' => 'text/html',
151 'og_title' => 'my OG title',
152 'og_description' => 'OG desc',
153 'og_image' => 'http://3.3.3.3/cover.jpg',
157 $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage
);
158 $entry = new Entry(new User());
159 $proxy->updateEntry($entry, 'http://0.0.0.0');
161 $this->assertEquals('http://1.1.1.1', $entry->getUrl());
162 $this->assertEquals('this is my title', $entry->getTitle());
163 $this->assertContains('this is my content', $entry->getContent());
164 $this->assertEquals('http://3.3.3.3/cover.jpg', $entry->getPreviewPicture());
165 $this->assertEquals('text/html', $entry->getMimetype());
166 $this->assertEquals('fr', $entry->getLanguage());
167 $this->assertEquals('200', $entry->getHttpStatus());
168 $this->assertEquals(4.0, $entry->getReadingTime());
169 $this->assertEquals('1.1.1.1', $entry->getDomainName());
172 public function testWithContentAndNoOgImage()
174 $tagger = $this->getTaggerMock();
175 $tagger->expects($this->once())
178 $graby = $this->getMockBuilder('Graby\Graby')
179 ->setMethods(['fetchContent'])
180 ->disableOriginalConstructor()
183 $graby->expects($this->any())
184 ->method('fetchContent')
186 'html' => str_repeat('this is my content', 325),
187 'title' => 'this is my title',
188 'url' => 'http://1.1.1.1',
189 'content_type' => 'text/html',
193 'og_title' => 'my OG title',
194 'og_description' => 'OG desc',
199 $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage
);
200 $entry = new Entry(new User());
201 $proxy->updateEntry($entry, 'http://0.0.0.0');
203 $this->assertEquals('http://1.1.1.1', $entry->getUrl());
204 $this->assertEquals('this is my title', $entry->getTitle());
205 $this->assertContains('this is my content', $entry->getContent());
206 $this->assertNull($entry->getPreviewPicture());
207 $this->assertEquals('text/html', $entry->getMimetype());
208 $this->assertEquals('fr', $entry->getLanguage());
209 $this->assertEquals('200', $entry->getHttpStatus());
210 $this->assertEquals(4.0, $entry->getReadingTime());
211 $this->assertEquals('1.1.1.1', $entry->getDomainName());
214 public function testWithForcedContent()
216 $tagger = $this->getTaggerMock();
217 $tagger->expects($this->once())
220 $proxy = new ContentProxy((new Graby()), $tagger, $this->getLogger(), $this->fetchingErrorMessage
);
221 $entry = new Entry(new User());
226 'html' => str_repeat('this is my content', 325),
227 'title' => 'this is my title',
228 'url' => 'http://1.1.1.1',
229 'content_type' => 'text/html',
231 'date' => '1395635872',
232 'authors' => ['Jeremy', 'Nico', 'Thomas'],
234 'Cache-Control' => 'no-cache',
239 $this->assertEquals('http://1.1.1.1', $entry->getUrl());
240 $this->assertEquals('this is my title', $entry->getTitle());
241 $this->assertContains('this is my content', $entry->getContent());
242 $this->assertEquals('text/html', $entry->getMimetype());
243 $this->assertEquals('fr', $entry->getLanguage());
244 $this->assertEquals(4.0, $entry->getReadingTime());
245 $this->assertEquals('1.1.1.1', $entry->getDomainName());
246 $this->assertEquals('24/03/2014', $entry->getPublishedAt()->format('d/m/Y'));
247 $this->assertContains('Jeremy', $entry->getPublishedBy());
248 $this->assertContains('Nico', $entry->getPublishedBy());
249 $this->assertContains('Thomas', $entry->getPublishedBy());
250 $this->assertContains('no-cache', $entry->getHeaders());
253 public function testWithForcedContentAndDatetime()
255 $tagger = $this->getTaggerMock();
256 $tagger->expects($this->once())
259 $logHandler = new TestHandler();
260 $logger = new Logger('test', [$logHandler]);
262 $proxy = new ContentProxy((new Graby()), $tagger, $logger, $this->fetchingErrorMessage
);
263 $entry = new Entry(new User());
268 'html' => str_repeat('this is my content', 325),
269 'title' => 'this is my title',
270 'url' => 'http://1.1.1.1',
271 'content_type' => 'text/html',
273 'date' => '2016-09-08T11:55:58+0200',
277 $this->assertEquals('http://1.1.1.1', $entry->getUrl());
278 $this->assertEquals('this is my title', $entry->getTitle());
279 $this->assertContains('this is my content', $entry->getContent());
280 $this->assertEquals('text/html', $entry->getMimetype());
281 $this->assertEquals('fr', $entry->getLanguage());
282 $this->assertEquals(4.0, $entry->getReadingTime());
283 $this->assertEquals('1.1.1.1', $entry->getDomainName());
284 $this->assertEquals('08/09/2016', $entry->getPublishedAt()->format('d/m/Y'));
287 public function testWithForcedContentAndBadDate()
289 $tagger = $this->getTaggerMock();
290 $tagger->expects($this->once())
293 $logger = new Logger('foo');
294 $handler = new TestHandler();
295 $logger->pushHandler($handler);
297 $proxy = new ContentProxy((new Graby()), $tagger, $logger, $this->fetchingErrorMessage
);
298 $entry = new Entry(new User());
303 'html' => str_repeat('this is my content', 325),
304 'title' => 'this is my title',
305 'url' => 'http://1.1.1.1',
306 'content_type' => 'text/html',
308 'date' => '01 02 2012',
312 $this->assertEquals('http://1.1.1.1', $entry->getUrl());
313 $this->assertEquals('this is my title', $entry->getTitle());
314 $this->assertContains('this is my content', $entry->getContent());
315 $this->assertEquals('text/html', $entry->getMimetype());
316 $this->assertEquals('fr', $entry->getLanguage());
317 $this->assertEquals(4.0, $entry->getReadingTime());
318 $this->assertEquals('1.1.1.1', $entry->getDomainName());
319 $this->assertNull($entry->getPublishedAt());
321 $records = $handler->getRecords();
323 $this->assertCount(1, $records);
324 $this->assertContains('Error while defining date', $records[0]['message']);
327 public function testTaggerThrowException()
329 $tagger = $this->getTaggerMock();
330 $tagger->expects($this->once())
332 ->will($this->throwException(new \
Exception()));
334 $proxy = new ContentProxy((new Graby()), $tagger, $this->getLogger(), $this->fetchingErrorMessage
);
335 $entry = new Entry(new User());
340 'html' => str_repeat('this is my content', 325),
341 'title' => 'this is my title',
342 'url' => 'http://1.1.1.1',
343 'content_type' => 'text/html',
348 $this->assertCount(0, $entry->getTags());
351 public function dataForCrazyHtml()
354 'script and comment' => [
355 '<strong>Script inside:</strong> <!--[if gte IE 4]><script>alert(\'lol\');</script><![endif]--><br />',
359 '<strong>Script inside:</strong><script>alert(\'lol\');</script>',
366 * @dataProvider dataForCrazyHtml
368 public function testWithCrazyHtmlContent($html, $escapedString)
370 $tagger = $this->getTaggerMock();
371 $tagger->expects($this->once())
374 $proxy = new ContentProxy((new Graby()), $tagger, $this->getLogger(), $this->fetchingErrorMessage
);
375 $entry = new Entry(new User());
381 'title' => 'this is my title',
382 'url' => 'http://1.1.1.1',
383 'content_type' => 'text/html',
387 'og_title' => 'my OG title',
388 'og_description' => 'OG desc',
389 'og_image' => 'http://3.3.3.3/cover.jpg',
394 $this->assertEquals('http://1.1.1.1', $entry->getUrl());
395 $this->assertEquals('this is my title', $entry->getTitle());
396 $this->assertNotContains($escapedString, $entry->getContent());
397 $this->assertEquals('http://3.3.3.3/cover.jpg', $entry->getPreviewPicture());
398 $this->assertEquals('text/html', $entry->getMimetype());
399 $this->assertEquals('fr', $entry->getLanguage());
400 $this->assertEquals('200', $entry->getHttpStatus());
401 $this->assertEquals('1.1.1.1', $entry->getDomainName());
404 private function getTaggerMock()
406 return $this->getMockBuilder(RuleBasedTagger
::class)
407 ->setMethods(['tag'])
408 ->disableOriginalConstructor()
412 private function getLogger()
414 return new NullLogger();