]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Tests\Wallabag\CoreBundle\Helper; | |
4 | ||
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 | ||
11 | class ContentProxyTest extends \PHPUnit_Framework_TestCase | |
12 | { | |
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>.'; | |
14 | ||
15 | public function testWithBadUrl() | |
16 | { | |
17 | $tagger = $this->getTaggerMock(); | |
18 | $tagger->expects($this->once()) | |
19 | ->method('tag'); | |
20 | ||
21 | $graby = $this->getMockBuilder('Graby\Graby') | |
22 | ->setMethods(['fetchContent']) | |
23 | ->disableOriginalConstructor() | |
24 | ->getMock(); | |
25 | ||
26 | $graby->expects($this->any()) | |
27 | ->method('fetchContent') | |
28 | ->willReturn([ | |
29 | 'html' => false, | |
30 | 'title' => '', | |
31 | 'url' => '', | |
32 | 'content_type' => '', | |
33 | 'language' => '', | |
34 | ]); | |
35 | ||
36 | $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage); | |
37 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://user@:80'); | |
38 | ||
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()); | |
47 | } | |
48 | ||
49 | public function testWithEmptyContent() | |
50 | { | |
51 | $tagger = $this->getTaggerMock(); | |
52 | $tagger->expects($this->once()) | |
53 | ->method('tag'); | |
54 | ||
55 | $graby = $this->getMockBuilder('Graby\Graby') | |
56 | ->setMethods(['fetchContent']) | |
57 | ->disableOriginalConstructor() | |
58 | ->getMock(); | |
59 | ||
60 | $graby->expects($this->any()) | |
61 | ->method('fetchContent') | |
62 | ->willReturn([ | |
63 | 'html' => false, | |
64 | 'title' => '', | |
65 | 'url' => '', | |
66 | 'content_type' => '', | |
67 | 'language' => '', | |
68 | ]); | |
69 | ||
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'); | |
72 | ||
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()); | |
81 | } | |
82 | ||
83 | public function testWithEmptyContentButOG() | |
84 | { | |
85 | $tagger = $this->getTaggerMock(); | |
86 | $tagger->expects($this->once()) | |
87 | ->method('tag'); | |
88 | ||
89 | $graby = $this->getMockBuilder('Graby\Graby') | |
90 | ->setMethods(['fetchContent']) | |
91 | ->disableOriginalConstructor() | |
92 | ->getMock(); | |
93 | ||
94 | $graby->expects($this->any()) | |
95 | ->method('fetchContent') | |
96 | ->willReturn([ | |
97 | 'html' => false, | |
98 | 'title' => '', | |
99 | 'url' => '', | |
100 | 'content_type' => '', | |
101 | 'language' => '', | |
102 | 'status' => '', | |
103 | 'open_graph' => [ | |
104 | 'og_title' => 'my title', | |
105 | 'og_description' => 'desc', | |
106 | ], | |
107 | ]); | |
108 | ||
109 | $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage); | |
110 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://domain.io'); | |
111 | ||
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()); | |
121 | } | |
122 | ||
123 | public function testWithContent() | |
124 | { | |
125 | $tagger = $this->getTaggerMock(); | |
126 | $tagger->expects($this->once()) | |
127 | ->method('tag'); | |
128 | ||
129 | $graby = $this->getMockBuilder('Graby\Graby') | |
130 | ->setMethods(['fetchContent']) | |
131 | ->disableOriginalConstructor() | |
132 | ->getMock(); | |
133 | ||
134 | $graby->expects($this->any()) | |
135 | ->method('fetchContent') | |
136 | ->willReturn([ | |
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', | |
141 | 'language' => 'fr', | |
142 | 'status' => '200', | |
143 | 'open_graph' => [ | |
144 | 'og_title' => 'my OG title', | |
145 | 'og_description' => 'OG desc', | |
146 | 'og_image' => 'http://3.3.3.3/cover.jpg', | |
147 | ], | |
148 | ]); | |
149 | ||
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'); | |
152 | ||
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()); | |
162 | } | |
163 | ||
164 | public function testWithForcedContent() | |
165 | { | |
166 | $tagger = $this->getTaggerMock(); | |
167 | $tagger->expects($this->once()) | |
168 | ->method('tag'); | |
169 | ||
170 | $graby = $this->getMockBuilder('Graby\Graby')->getMock(); | |
171 | ||
172 | $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage); | |
173 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0', [ | |
174 | 'html' => str_repeat('this is my content', 325), | |
175 | 'title' => 'this is my title', | |
176 | 'url' => 'http://1.1.1.1', | |
177 | 'content_type' => 'text/html', | |
178 | 'language' => 'fr', | |
179 | ]); | |
180 | ||
181 | $this->assertEquals('http://1.1.1.1', $entry->getUrl()); | |
182 | $this->assertEquals('this is my title', $entry->getTitle()); | |
183 | $this->assertContains('this is my content', $entry->getContent()); | |
184 | $this->assertEquals('text/html', $entry->getMimetype()); | |
185 | $this->assertEquals('fr', $entry->getLanguage()); | |
186 | $this->assertEquals(4.0, $entry->getReadingTime()); | |
187 | $this->assertEquals('1.1.1.1', $entry->getDomainName()); | |
188 | } | |
189 | ||
190 | public function testTaggerThrowException() | |
191 | { | |
192 | $graby = $this->getMockBuilder('Graby\Graby') | |
193 | ->disableOriginalConstructor() | |
194 | ->getMock(); | |
195 | ||
196 | $tagger = $this->getTaggerMock(); | |
197 | $tagger->expects($this->once()) | |
198 | ->method('tag') | |
199 | ->will($this->throwException(new \Exception())); | |
200 | ||
201 | $tagRepo = $this->getTagRepositoryMock(); | |
202 | $proxy = new ContentProxy($graby, $tagger, $tagRepo, $this->getLogger(), $this->fetchingErrorMessage); | |
203 | ||
204 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0', [ | |
205 | 'html' => str_repeat('this is my content', 325), | |
206 | 'title' => 'this is my title', | |
207 | 'url' => 'http://1.1.1.1', | |
208 | 'content_type' => 'text/html', | |
209 | 'language' => 'fr', | |
210 | ]); | |
211 | ||
212 | $this->assertCount(0, $entry->getTags()); | |
213 | } | |
214 | ||
215 | public function testAssignTagsWithArrayAndExtraSpaces() | |
216 | { | |
217 | $graby = $this->getMockBuilder('Graby\Graby') | |
218 | ->disableOriginalConstructor() | |
219 | ->getMock(); | |
220 | ||
221 | $tagRepo = $this->getTagRepositoryMock(); | |
222 | $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage); | |
223 | ||
224 | $entry = new Entry(new User()); | |
225 | ||
226 | $proxy->assignTagsToEntry($entry, [' tag1', 'tag2 ']); | |
227 | ||
228 | $this->assertCount(2, $entry->getTags()); | |
229 | $this->assertEquals('tag1', $entry->getTags()[0]->getLabel()); | |
230 | $this->assertEquals('tag2', $entry->getTags()[1]->getLabel()); | |
231 | } | |
232 | ||
233 | public function testAssignTagsWithString() | |
234 | { | |
235 | $graby = $this->getMockBuilder('Graby\Graby') | |
236 | ->disableOriginalConstructor() | |
237 | ->getMock(); | |
238 | ||
239 | $tagRepo = $this->getTagRepositoryMock(); | |
240 | $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage); | |
241 | ||
242 | $entry = new Entry(new User()); | |
243 | ||
244 | $proxy->assignTagsToEntry($entry, 'tag1, tag2'); | |
245 | ||
246 | $this->assertCount(2, $entry->getTags()); | |
247 | $this->assertEquals('tag1', $entry->getTags()[0]->getLabel()); | |
248 | $this->assertEquals('tag2', $entry->getTags()[1]->getLabel()); | |
249 | } | |
250 | ||
251 | public function testAssignTagsWithEmptyArray() | |
252 | { | |
253 | $graby = $this->getMockBuilder('Graby\Graby') | |
254 | ->disableOriginalConstructor() | |
255 | ->getMock(); | |
256 | ||
257 | $tagRepo = $this->getTagRepositoryMock(); | |
258 | $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage); | |
259 | ||
260 | $entry = new Entry(new User()); | |
261 | ||
262 | $proxy->assignTagsToEntry($entry, []); | |
263 | ||
264 | $this->assertCount(0, $entry->getTags()); | |
265 | } | |
266 | ||
267 | public function testAssignTagsWithEmptyString() | |
268 | { | |
269 | $graby = $this->getMockBuilder('Graby\Graby') | |
270 | ->disableOriginalConstructor() | |
271 | ->getMock(); | |
272 | ||
273 | $tagRepo = $this->getTagRepositoryMock(); | |
274 | $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage); | |
275 | ||
276 | $entry = new Entry(new User()); | |
277 | ||
278 | $proxy->assignTagsToEntry($entry, ''); | |
279 | ||
280 | $this->assertCount(0, $entry->getTags()); | |
281 | } | |
282 | ||
283 | public function testAssignTagsAlreadyAssigned() | |
284 | { | |
285 | $graby = $this->getMockBuilder('Graby\Graby') | |
286 | ->disableOriginalConstructor() | |
287 | ->getMock(); | |
288 | ||
289 | $tagRepo = $this->getTagRepositoryMock(); | |
290 | $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage); | |
291 | ||
292 | $tagEntity = new Tag(); | |
293 | $tagEntity->setLabel('tag1'); | |
294 | ||
295 | $entry = new Entry(new User()); | |
296 | $entry->addTag($tagEntity); | |
297 | ||
298 | $proxy->assignTagsToEntry($entry, 'tag1, tag2'); | |
299 | ||
300 | $this->assertCount(2, $entry->getTags()); | |
301 | $this->assertEquals('tag1', $entry->getTags()[0]->getLabel()); | |
302 | $this->assertEquals('tag2', $entry->getTags()[1]->getLabel()); | |
303 | } | |
304 | ||
305 | public function testAssignTagsNotFlushed() | |
306 | { | |
307 | $graby = $this->getMockBuilder('Graby\Graby') | |
308 | ->disableOriginalConstructor() | |
309 | ->getMock(); | |
310 | ||
311 | $tagRepo = $this->getTagRepositoryMock(); | |
312 | $tagRepo->expects($this->never()) | |
313 | ->method('__call'); | |
314 | ||
315 | $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage); | |
316 | ||
317 | $tagEntity = new Tag(); | |
318 | $tagEntity->setLabel('tag1'); | |
319 | ||
320 | $entry = new Entry(new User()); | |
321 | ||
322 | $proxy->assignTagsToEntry($entry, 'tag1', [$tagEntity]); | |
323 | ||
324 | $this->assertCount(1, $entry->getTags()); | |
325 | $this->assertEquals('tag1', $entry->getTags()[0]->getLabel()); | |
326 | } | |
327 | ||
328 | private function getTaggerMock() | |
329 | { | |
330 | return $this->getMockBuilder('Wallabag\CoreBundle\Helper\RuleBasedTagger') | |
331 | ->setMethods(['tag']) | |
332 | ->disableOriginalConstructor() | |
333 | ->getMock(); | |
334 | } | |
335 | ||
336 | private function getTagRepositoryMock() | |
337 | { | |
338 | return $this->getMockBuilder('Wallabag\CoreBundle\Repository\TagRepository') | |
339 | ->disableOriginalConstructor() | |
340 | ->getMock(); | |
341 | } | |
342 | ||
343 | private function getLogger() | |
344 | { | |
345 | return new NullLogger(); | |
346 | } | |
347 | } |