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
;
11 class ContentProxyTest
extends \PHPUnit_Framework_TestCase
13 private $fetchingErrorMessage = 'wallabag can\'t retrieve contents for this article. Please <a href="http://doc.wallabag.org/en/master/user/errors_during_fetching.html#how-can-i-help-to-fix-that">troubleshoot this issue</a>.';
15 public function testWithBadUrl()
17 $tagger = $this->getTaggerMock();
18 $tagger->expects($this->once())
21 $graby = $this->getMockBuilder('Graby\Graby')
22 ->setMethods(['fetchContent'])
23 ->disableOriginalConstructor()
26 $graby->expects($this->any())
27 ->method('fetchContent')
36 $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage
);
37 $entry = $proxy->updateEntry(new Entry(new User()), 'http://user@:80');
39 $this->assertEquals('http://user@:80', $entry->getUrl());
40 $this->assertEmpty($entry->getTitle());
41 $this->assertEquals($this->fetchingErrorMessage
, $entry->getContent());
42 $this->assertEmpty($entry->getPreviewPicture());
43 $this->assertEmpty($entry->getMimetype());
44 $this->assertEmpty($entry->getLanguage());
45 $this->assertEquals(0.0, $entry->getReadingTime());
46 $this->assertEquals(false, $entry->getDomainName());
49 public function testWithEmptyContent()
51 $tagger = $this->getTaggerMock();
52 $tagger->expects($this->once())
55 $graby = $this->getMockBuilder('Graby\Graby')
56 ->setMethods(['fetchContent'])
57 ->disableOriginalConstructor()
60 $graby->expects($this->any())
61 ->method('fetchContent')
70 $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage
);
71 $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0');
73 $this->assertEquals('http://0.0.0.0', $entry->getUrl());
74 $this->assertEmpty($entry->getTitle());
75 $this->assertEquals($this->fetchingErrorMessage
, $entry->getContent());
76 $this->assertEmpty($entry->getPreviewPicture());
77 $this->assertEmpty($entry->getMimetype());
78 $this->assertEmpty($entry->getLanguage());
79 $this->assertEquals(0.0, $entry->getReadingTime());
80 $this->assertEquals('0.0.0.0', $entry->getDomainName());
83 public function testWithEmptyContentButOG()
85 $tagger = $this->getTaggerMock();
86 $tagger->expects($this->once())
89 $graby = $this->getMockBuilder('Graby\Graby')
90 ->setMethods(['fetchContent'])
91 ->disableOriginalConstructor()
94 $graby->expects($this->any())
95 ->method('fetchContent')
100 'content_type' => '',
104 'og_title' => 'my title',
105 'og_description' => 'desc',
109 $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage
);
110 $entry = $proxy->updateEntry(new Entry(new User()), 'http://domain.io');
112 $this->assertEquals('http://domain.io', $entry->getUrl());
113 $this->assertEquals('my title', $entry->getTitle());
114 $this->assertEquals($this->fetchingErrorMessage
. '<p><i>But we found a short description: </i></p>desc', $entry->getContent());
115 $this->assertEmpty($entry->getPreviewPicture());
116 $this->assertEmpty($entry->getLanguage());
117 $this->assertEmpty($entry->getHttpStatus());
118 $this->assertEmpty($entry->getMimetype());
119 $this->assertEquals(0.0, $entry->getReadingTime());
120 $this->assertEquals('domain.io', $entry->getDomainName());
123 public function testWithContent()
125 $tagger = $this->getTaggerMock();
126 $tagger->expects($this->once())
129 $graby = $this->getMockBuilder('Graby\Graby')
130 ->setMethods(['fetchContent'])
131 ->disableOriginalConstructor()
134 $graby->expects($this->any())
135 ->method('fetchContent')
137 'html' => str_repeat('this is my content', 325),
138 'title' => 'this is my title',
139 'url' => 'http://1.1.1.1',
140 'content_type' => 'text/html',
144 'og_title' => 'my OG title',
145 'og_description' => 'OG desc',
146 'og_image' => 'http://3.3.3.3/cover.jpg',
150 $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage
);
151 $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0');
153 $this->assertEquals('http://1.1.1.1', $entry->getUrl());
154 $this->assertEquals('this is my title', $entry->getTitle());
155 $this->assertContains('this is my content', $entry->getContent());
156 $this->assertEquals('http://3.3.3.3/cover.jpg', $entry->getPreviewPicture());
157 $this->assertEquals('text/html', $entry->getMimetype());
158 $this->assertEquals('fr', $entry->getLanguage());
159 $this->assertEquals('200', $entry->getHttpStatus());
160 $this->assertEquals(4.0, $entry->getReadingTime());
161 $this->assertEquals('1.1.1.1', $entry->getDomainName());
164 public function testWithContentAndNoOgImage()
166 $tagger = $this->getTaggerMock();
167 $tagger->expects($this->once())
170 $graby = $this->getMockBuilder('Graby\Graby')
171 ->setMethods(['fetchContent'])
172 ->disableOriginalConstructor()
175 $graby->expects($this->any())
176 ->method('fetchContent')
178 'html' => str_repeat('this is my content', 325),
179 'title' => 'this is my title',
180 'url' => 'http://1.1.1.1',
181 'content_type' => 'text/html',
185 'og_title' => 'my OG title',
186 'og_description' => 'OG desc',
191 $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage
);
192 $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0');
194 $this->assertEquals('http://1.1.1.1', $entry->getUrl());
195 $this->assertEquals('this is my title', $entry->getTitle());
196 $this->assertContains('this is my content', $entry->getContent());
197 $this->assertNull($entry->getPreviewPicture());
198 $this->assertEquals('text/html', $entry->getMimetype());
199 $this->assertEquals('fr', $entry->getLanguage());
200 $this->assertEquals('200', $entry->getHttpStatus());
201 $this->assertEquals(4.0, $entry->getReadingTime());
202 $this->assertEquals('1.1.1.1', $entry->getDomainName());
205 public function testWithForcedContent()
207 $tagger = $this->getTaggerMock();
208 $tagger->expects($this->once())
211 $graby = $this->getMockBuilder('Graby\Graby')->getMock();
213 $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage
);
214 $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0', [
215 'html' => str_repeat('this is my content', 325),
216 'title' => 'this is my title',
217 'url' => 'http://1.1.1.1',
218 'content_type' => 'text/html',
222 $this->assertEquals('http://1.1.1.1', $entry->getUrl());
223 $this->assertEquals('this is my title', $entry->getTitle());
224 $this->assertContains('this is my content', $entry->getContent());
225 $this->assertEquals('text/html', $entry->getMimetype());
226 $this->assertEquals('fr', $entry->getLanguage());
227 $this->assertEquals(4.0, $entry->getReadingTime());
228 $this->assertEquals('1.1.1.1', $entry->getDomainName());
231 public function testTaggerThrowException()
233 $graby = $this->getMockBuilder('Graby\Graby')
234 ->disableOriginalConstructor()
237 $tagger = $this->getTaggerMock();
238 $tagger->expects($this->once())
240 ->will($this->throwException(new \
Exception()));
242 $tagRepo = $this->getTagRepositoryMock();
243 $proxy = new ContentProxy($graby, $tagger, $tagRepo, $this->getLogger(), $this->fetchingErrorMessage
);
245 $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0', [
246 'html' => str_repeat('this is my content', 325),
247 'title' => 'this is my title',
248 'url' => 'http://1.1.1.1',
249 'content_type' => 'text/html',
253 $this->assertCount(0, $entry->getTags());
256 public function testAssignTagsWithArrayAndExtraSpaces()
258 $graby = $this->getMockBuilder('Graby\Graby')
259 ->disableOriginalConstructor()
262 $tagRepo = $this->getTagRepositoryMock();
263 $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage
);
265 $entry = new Entry(new User());
267 $proxy->assignTagsToEntry($entry, [' tag1', 'tag2 ']);
269 $this->assertCount(2, $entry->getTags());
270 $this->assertEquals('tag1', $entry->getTags()[0]->getLabel());
271 $this->assertEquals('tag2', $entry->getTags()[1]->getLabel());
274 public function testAssignTagsWithString()
276 $graby = $this->getMockBuilder('Graby\Graby')
277 ->disableOriginalConstructor()
280 $tagRepo = $this->getTagRepositoryMock();
281 $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage
);
283 $entry = new Entry(new User());
285 $proxy->assignTagsToEntry($entry, 'tag1, tag2');
287 $this->assertCount(2, $entry->getTags());
288 $this->assertEquals('tag1', $entry->getTags()[0]->getLabel());
289 $this->assertEquals('tag2', $entry->getTags()[1]->getLabel());
292 public function testAssignTagsWithEmptyArray()
294 $graby = $this->getMockBuilder('Graby\Graby')
295 ->disableOriginalConstructor()
298 $tagRepo = $this->getTagRepositoryMock();
299 $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage
);
301 $entry = new Entry(new User());
303 $proxy->assignTagsToEntry($entry, []);
305 $this->assertCount(0, $entry->getTags());
308 public function testAssignTagsWithEmptyString()
310 $graby = $this->getMockBuilder('Graby\Graby')
311 ->disableOriginalConstructor()
314 $tagRepo = $this->getTagRepositoryMock();
315 $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage
);
317 $entry = new Entry(new User());
319 $proxy->assignTagsToEntry($entry, '');
321 $this->assertCount(0, $entry->getTags());
324 public function testAssignTagsAlreadyAssigned()
326 $graby = $this->getMockBuilder('Graby\Graby')
327 ->disableOriginalConstructor()
330 $tagRepo = $this->getTagRepositoryMock();
331 $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage
);
333 $tagEntity = new Tag();
334 $tagEntity->setLabel('tag1');
336 $entry = new Entry(new User());
337 $entry->addTag($tagEntity);
339 $proxy->assignTagsToEntry($entry, 'tag1, tag2');
341 $this->assertCount(2, $entry->getTags());
342 $this->assertEquals('tag1', $entry->getTags()[0]->getLabel());
343 $this->assertEquals('tag2', $entry->getTags()[1]->getLabel());
346 public function testAssignTagsNotFlushed()
348 $graby = $this->getMockBuilder('Graby\Graby')
349 ->disableOriginalConstructor()
352 $tagRepo = $this->getTagRepositoryMock();
353 $tagRepo->expects($this->never())
356 $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage
);
358 $tagEntity = new Tag();
359 $tagEntity->setLabel('tag1');
361 $entry = new Entry(new User());
363 $proxy->assignTagsToEntry($entry, 'tag1', [$tagEntity]);
365 $this->assertCount(1, $entry->getTags());
366 $this->assertEquals('tag1', $entry->getTags()[0]->getLabel());
369 private function getTaggerMock()
371 return $this->getMockBuilder('Wallabag\CoreBundle\Helper\RuleBasedTagger')
372 ->setMethods(['tag'])
373 ->disableOriginalConstructor()
377 private function getTagRepositoryMock()
379 return $this->getMockBuilder('Wallabag\CoreBundle\Repository\TagRepository')
380 ->disableOriginalConstructor()
384 private function getLogger()
386 return new NullLogger();