3 namespace Tests\Wallabag\CoreBundle\Helper
;
5 use Psr\Log\NullLogger
;
6 use Wallabag\CoreBundle\Helper\ContentProxy
;
7 use Wallabag\CoreBundle\Entity\Entry
;
8 use Wallabag\CoreBundle\Entity\Tag
;
9 use Wallabag\UserBundle\Entity\User
;
10 use Wallabag\CoreBundle\Helper\RuleBasedTagger
;
12 use Monolog\Handler\TestHandler
;
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 = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0');
202 $this->assertEquals('http://1.1.1.1', $entry->getUrl());
203 $this->assertEquals('this is my title', $entry->getTitle());
204 $this->assertContains('this is my content', $entry->getContent());
205 $this->assertNull($entry->getPreviewPicture());
206 $this->assertEquals('text/html', $entry->getMimetype());
207 $this->assertEquals('fr', $entry->getLanguage());
208 $this->assertEquals('200', $entry->getHttpStatus());
209 $this->assertEquals(4.0, $entry->getReadingTime());
210 $this->assertEquals('1.1.1.1', $entry->getDomainName());
213 public function testWithForcedContent()
215 $tagger = $this->getTaggerMock();
216 $tagger->expects($this->once())
219 $proxy = new ContentProxy((new Graby()), $tagger, $this->getLogger(), $this->fetchingErrorMessage
);
220 $entry = new Entry(new User());
225 'html' => str_repeat('this is my content', 325),
226 'title' => 'this is my title',
227 'url' => 'http://1.1.1.1',
228 'content_type' => 'text/html',
230 'date' => '1395635872',
231 'authors' => ['Jeremy', 'Nico', 'Thomas'],
233 'Cache-Control' => 'no-cache',
238 $this->assertEquals('http://1.1.1.1', $entry->getUrl());
239 $this->assertEquals('this is my title', $entry->getTitle());
240 $this->assertContains('this is my content', $entry->getContent());
241 $this->assertEquals('text/html', $entry->getMimetype());
242 $this->assertEquals('fr', $entry->getLanguage());
243 $this->assertEquals(4.0, $entry->getReadingTime());
244 $this->assertEquals('1.1.1.1', $entry->getDomainName());
245 $this->assertEquals('24/03/2014', $entry->getPublishedAt()->format('d/m/Y'));
246 $this->assertContains('Jeremy', $entry->getPublishedBy());
247 $this->assertContains('Nico', $entry->getPublishedBy());
248 $this->assertContains('Thomas', $entry->getPublishedBy());
249 $this->assertContains('no-cache', $entry->getHeaders());
252 public function testWithForcedContentAndDatetime()
254 $tagger = $this->getTaggerMock();
255 $tagger->expects($this->once())
258 $proxy = new ContentProxy((new Graby()), $tagger, $this->getLogger(), $this->fetchingErrorMessage
);
259 $entry = new Entry(new User());
264 'html' => str_repeat('this is my content', 325),
265 'title' => 'this is my title',
266 'url' => 'http://1.1.1.1',
267 'content_type' => 'text/html',
269 'date' => '2016-09-08T11:55:58+0200',
273 $this->assertEquals('http://1.1.1.1', $entry->getUrl());
274 $this->assertEquals('this is my title', $entry->getTitle());
275 $this->assertContains('this is my content', $entry->getContent());
276 $this->assertEquals('text/html', $entry->getMimetype());
277 $this->assertEquals('fr', $entry->getLanguage());
278 $this->assertEquals(4.0, $entry->getReadingTime());
279 $this->assertEquals('1.1.1.1', $entry->getDomainName());
280 $this->assertEquals('08/09/2016', $entry->getPublishedAt()->format('d/m/Y'));
283 public function testWithForcedContentAndBadDate()
285 $tagger = $this->getTaggerMock();
286 $tagger->expects($this->once())
289 $logger = new Logger('foo');
290 $handler = new TestHandler();
291 $logger->pushHandler($handler);
293 $proxy = new ContentProxy((new Graby()), $tagger, $logger, $this->fetchingErrorMessage
);
294 $entry = new Entry(new User());
299 'html' => str_repeat('this is my content', 325),
300 'title' => 'this is my title',
301 'url' => 'http://1.1.1.1',
302 'content_type' => 'text/html',
304 'date' => '01 02 2012',
308 $this->assertEquals('http://1.1.1.1', $entry->getUrl());
309 $this->assertEquals('this is my title', $entry->getTitle());
310 $this->assertContains('this is my content', $entry->getContent());
311 $this->assertEquals('text/html', $entry->getMimetype());
312 $this->assertEquals('fr', $entry->getLanguage());
313 $this->assertEquals(4.0, $entry->getReadingTime());
314 $this->assertEquals('1.1.1.1', $entry->getDomainName());
315 $this->assertNull($entry->getPublishedAt());
317 $records = $handler->getRecords();
319 $this->assertCount(1, $records);
320 $this->assertContains('Error while defining date', $records[0]['message']);
323 public function testTaggerThrowException()
325 $graby = $this->getMockBuilder('Graby\Graby')
326 ->disableOriginalConstructor()
329 $tagger = $this->getTaggerMock();
330 $tagger->expects($this->once())
332 ->will($this->throwException(new \
Exception()));
334 $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage
);
336 $entry = new Entry(new User());
337 $proxy->updateEntry($entry, 'http://0.0.0.0', [
338 'html' => str_repeat('this is my content', 325),
339 'title' => 'this is my title',
340 'url' => 'http://1.1.1.1',
341 'content_type' => 'text/html',
345 $this->assertCount(0, $entry->getTags());
348 public function dataForCrazyHtml()
351 'script and comment' => [
352 '<strong>Script inside:</strong> <!--[if gte IE 4]><script>alert(\'lol\');</script><![endif]--><br />',
356 '<strong>Script inside:</strong><script>alert(\'lol\');</script>',
363 * @dataProvider dataForCrazyHtml
365 public function testWithCrazyHtmlContent($html, $escapedString)
367 $tagger = $this->getTaggerMock();
368 $tagger->expects($this->once())
371 $proxy = new ContentProxy((new Graby()), $tagger, $this->getLogger(), $this->fetchingErrorMessage
);
372 $entry = $proxy->updateEntry(
373 new Entry(new User()),
377 'title' => 'this is my title',
378 'url' => 'http://1.1.1.1',
379 'content_type' => 'text/html',
383 'og_title' => 'my OG title',
384 'og_description' => 'OG desc',
385 'og_image' => 'http://3.3.3.3/cover.jpg',
390 $this->assertEquals('http://1.1.1.1', $entry->getUrl());
391 $this->assertEquals('this is my title', $entry->getTitle());
392 $this->assertNotContains($escapedString, $entry->getContent());
393 $this->assertEquals('http://3.3.3.3/cover.jpg', $entry->getPreviewPicture());
394 $this->assertEquals('text/html', $entry->getMimetype());
395 $this->assertEquals('fr', $entry->getLanguage());
396 $this->assertEquals('200', $entry->getHttpStatus());
397 $this->assertEquals('1.1.1.1', $entry->getDomainName());
400 private function getTaggerMock()
402 return $this->getMockBuilder(RuleBasedTagger
::class)
403 ->setMethods(['tag'])
404 ->disableOriginalConstructor()
408 private function getLogger()
410 return new NullLogger();