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 class ContentProxyTest
extends \PHPUnit_Framework_TestCase
14 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>.';
16 public function testWithBadUrl()
18 $tagger = $this->getTaggerMock();
19 $tagger->expects($this->once())
22 $graby = $this->getMockBuilder('Graby\Graby')
23 ->setMethods(['fetchContent'])
24 ->disableOriginalConstructor()
27 $graby->expects($this->any())
28 ->method('fetchContent')
37 $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage
);
38 $entry = $proxy->updateEntry(new Entry(new User()), 'http://user@:80');
40 $this->assertEquals('http://user@:80', $entry->getUrl());
41 $this->assertEmpty($entry->getTitle());
42 $this->assertEquals($this->fetchingErrorMessage
, $entry->getContent());
43 $this->assertEmpty($entry->getPreviewPicture());
44 $this->assertEmpty($entry->getMimetype());
45 $this->assertEmpty($entry->getLanguage());
46 $this->assertEquals(0.0, $entry->getReadingTime());
47 $this->assertEquals(false, $entry->getDomainName());
50 public function testWithEmptyContent()
52 $tagger = $this->getTaggerMock();
53 $tagger->expects($this->once())
56 $graby = $this->getMockBuilder('Graby\Graby')
57 ->setMethods(['fetchContent'])
58 ->disableOriginalConstructor()
61 $graby->expects($this->any())
62 ->method('fetchContent')
71 $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage
);
72 $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0');
74 $this->assertEquals('http://0.0.0.0', $entry->getUrl());
75 $this->assertEmpty($entry->getTitle());
76 $this->assertEquals($this->fetchingErrorMessage
, $entry->getContent());
77 $this->assertEmpty($entry->getPreviewPicture());
78 $this->assertEmpty($entry->getMimetype());
79 $this->assertEmpty($entry->getLanguage());
80 $this->assertEquals(0.0, $entry->getReadingTime());
81 $this->assertEquals('0.0.0.0', $entry->getDomainName());
84 public function testWithEmptyContentButOG()
86 $tagger = $this->getTaggerMock();
87 $tagger->expects($this->once())
90 $graby = $this->getMockBuilder('Graby\Graby')
91 ->setMethods(['fetchContent'])
92 ->disableOriginalConstructor()
95 $graby->expects($this->any())
96 ->method('fetchContent')
101 'content_type' => '',
105 'og_title' => 'my title',
106 'og_description' => 'desc',
110 $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage
);
111 $entry = $proxy->updateEntry(new Entry(new User()), 'http://domain.io');
113 $this->assertEquals('http://domain.io', $entry->getUrl());
114 $this->assertEquals('my title', $entry->getTitle());
115 $this->assertEquals($this->fetchingErrorMessage
.'<p><i>But we found a short description: </i></p>desc', $entry->getContent());
116 $this->assertEmpty($entry->getPreviewPicture());
117 $this->assertEmpty($entry->getLanguage());
118 $this->assertEmpty($entry->getHttpStatus());
119 $this->assertEmpty($entry->getMimetype());
120 $this->assertEquals(0.0, $entry->getReadingTime());
121 $this->assertEquals('domain.io', $entry->getDomainName());
124 public function testWithContent()
126 $tagger = $this->getTaggerMock();
127 $tagger->expects($this->once())
130 $graby = $this->getMockBuilder('Graby\Graby')
131 ->setMethods(['fetchContent'])
132 ->disableOriginalConstructor()
135 $graby->expects($this->any())
136 ->method('fetchContent')
138 'html' => str_repeat('this is my content', 325),
139 'title' => 'this is my title',
140 'url' => 'http://1.1.1.1',
141 'content_type' => 'text/html',
145 'og_title' => 'my OG title',
146 'og_description' => 'OG desc',
147 'og_image' => 'http://3.3.3.3/cover.jpg',
151 $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage
);
152 $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0');
154 $this->assertEquals('http://1.1.1.1', $entry->getUrl());
155 $this->assertEquals('this is my title', $entry->getTitle());
156 $this->assertContains('this is my content', $entry->getContent());
157 $this->assertEquals('http://3.3.3.3/cover.jpg', $entry->getPreviewPicture());
158 $this->assertEquals('text/html', $entry->getMimetype());
159 $this->assertEquals('fr', $entry->getLanguage());
160 $this->assertEquals('200', $entry->getHttpStatus());
161 $this->assertEquals(4.0, $entry->getReadingTime());
162 $this->assertEquals('1.1.1.1', $entry->getDomainName());
165 public function testWithContentAndNoOgImage()
167 $tagger = $this->getTaggerMock();
168 $tagger->expects($this->once())
171 $graby = $this->getMockBuilder('Graby\Graby')
172 ->setMethods(['fetchContent'])
173 ->disableOriginalConstructor()
176 $graby->expects($this->any())
177 ->method('fetchContent')
179 'html' => str_repeat('this is my content', 325),
180 'title' => 'this is my title',
181 'url' => 'http://1.1.1.1',
182 'content_type' => 'text/html',
186 'og_title' => 'my OG title',
187 'og_description' => 'OG desc',
192 $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage
);
193 $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0');
195 $this->assertEquals('http://1.1.1.1', $entry->getUrl());
196 $this->assertEquals('this is my title', $entry->getTitle());
197 $this->assertContains('this is my content', $entry->getContent());
198 $this->assertNull($entry->getPreviewPicture());
199 $this->assertEquals('text/html', $entry->getMimetype());
200 $this->assertEquals('fr', $entry->getLanguage());
201 $this->assertEquals('200', $entry->getHttpStatus());
202 $this->assertEquals(4.0, $entry->getReadingTime());
203 $this->assertEquals('1.1.1.1', $entry->getDomainName());
206 public function testWithForcedContent()
208 $tagger = $this->getTaggerMock();
209 $tagger->expects($this->once())
212 $graby = $this->getMockBuilder('Graby\Graby')->getMock();
214 $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage
);
215 $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0', [
216 'html' => str_repeat('this is my content', 325),
217 'title' => 'this is my title',
218 'url' => 'http://1.1.1.1',
219 'content_type' => 'text/html',
223 $this->assertEquals('http://1.1.1.1', $entry->getUrl());
224 $this->assertEquals('this is my title', $entry->getTitle());
225 $this->assertContains('this is my content', $entry->getContent());
226 $this->assertEquals('text/html', $entry->getMimetype());
227 $this->assertEquals('fr', $entry->getLanguage());
228 $this->assertEquals(4.0, $entry->getReadingTime());
229 $this->assertEquals('1.1.1.1', $entry->getDomainName());
232 public function testTaggerThrowException()
234 $graby = $this->getMockBuilder('Graby\Graby')
235 ->disableOriginalConstructor()
238 $tagger = $this->getTaggerMock();
239 $tagger->expects($this->once())
241 ->will($this->throwException(new \
Exception()));
243 $proxy = new ContentProxy($graby, $tagger, $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 private function getTaggerMock()
258 return $this->getMockBuilder(RuleBasedTagger
::class)
259 ->setMethods(['tag'])
260 ->disableOriginalConstructor()
264 private function getLogger()
266 return new NullLogger();