]> git.immae.eu Git - github/wallabag/wallabag.git/blame - tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php
Merge pull request #3145 from wallabag/fix-so
[github/wallabag/wallabag.git] / tests / Wallabag / CoreBundle / Helper / ContentProxyTest.php
CommitLineData
558d9aab
JB
1<?php
2
a2c1b94e 3namespace Tests\Wallabag\CoreBundle\Helper;
558d9aab 4
0c5bcd82 5use Psr\Log\NullLogger;
558d9aab 6use Wallabag\CoreBundle\Helper\ContentProxy;
c2656f96
JB
7use Wallabag\CoreBundle\Entity\Entry;
8use Wallabag\CoreBundle\Entity\Tag;
619cc453 9use Wallabag\UserBundle\Entity\User;
558d9aab 10
a2c1b94e 11class ContentProxyTest extends \PHPUnit_Framework_TestCase
558d9aab 12{
a2c1b94e
JC
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
4d0ec0e7
JB
15 public function testWithBadUrl()
16 {
17 $tagger = $this->getTaggerMock();
18 $tagger->expects($this->once())
19 ->method('tag');
20
21 $graby = $this->getMockBuilder('Graby\Graby')
4094ea47 22 ->setMethods(['fetchContent'])
4d0ec0e7
JB
23 ->disableOriginalConstructor()
24 ->getMock();
25
26 $graby->expects($this->any())
27 ->method('fetchContent')
4094ea47 28 ->willReturn([
4d0ec0e7
JB
29 'html' => false,
30 'title' => '',
31 'url' => '',
32 'content_type' => '',
33 'language' => '',
4094ea47 34 ]);
4d0ec0e7 35
fc2b7bda 36 $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage);
4d0ec0e7
JB
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());
fc2b7bda 41 $this->assertEquals($this->fetchingErrorMessage, $entry->getContent());
4d0ec0e7
JB
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
558d9aab
JB
49 public function testWithEmptyContent()
50 {
f530f7f5
KG
51 $tagger = $this->getTaggerMock();
52 $tagger->expects($this->once())
53 ->method('tag');
54
558d9aab 55 $graby = $this->getMockBuilder('Graby\Graby')
4094ea47 56 ->setMethods(['fetchContent'])
558d9aab
JB
57 ->disableOriginalConstructor()
58 ->getMock();
59
60 $graby->expects($this->any())
61 ->method('fetchContent')
4094ea47 62 ->willReturn([
98f0929f
JB
63 'html' => false,
64 'title' => '',
65 'url' => '',
66 'content_type' => '',
67 'language' => '',
4094ea47 68 ]);
558d9aab 69
fc2b7bda 70 $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage);
558d9aab
JB
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());
fc2b7bda 75 $this->assertEquals($this->fetchingErrorMessage, $entry->getContent());
558d9aab
JB
76 $this->assertEmpty($entry->getPreviewPicture());
77 $this->assertEmpty($entry->getMimetype());
98f0929f 78 $this->assertEmpty($entry->getLanguage());
da3d4998
JB
79 $this->assertEquals(0.0, $entry->getReadingTime());
80 $this->assertEquals('0.0.0.0', $entry->getDomainName());
558d9aab
JB
81 }
82
83 public function testWithEmptyContentButOG()
84 {
f530f7f5
KG
85 $tagger = $this->getTaggerMock();
86 $tagger->expects($this->once())
87 ->method('tag');
88
558d9aab 89 $graby = $this->getMockBuilder('Graby\Graby')
4094ea47 90 ->setMethods(['fetchContent'])
558d9aab
JB
91 ->disableOriginalConstructor()
92 ->getMock();
93
94 $graby->expects($this->any())
95 ->method('fetchContent')
4094ea47 96 ->willReturn([
98f0929f
JB
97 'html' => false,
98 'title' => '',
99 'url' => '',
100 'content_type' => '',
101 'language' => '',
10b35097 102 'status' => '',
4094ea47 103 'open_graph' => [
98f0929f
JB
104 'og_title' => 'my title',
105 'og_description' => 'desc',
4094ea47
JB
106 ],
107 ]);
558d9aab 108
fc2b7bda 109 $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage);
da3d4998 110 $entry = $proxy->updateEntry(new Entry(new User()), 'http://domain.io');
558d9aab 111
da3d4998 112 $this->assertEquals('http://domain.io', $entry->getUrl());
558d9aab 113 $this->assertEquals('my title', $entry->getTitle());
d09fe4d2 114 $this->assertEquals($this->fetchingErrorMessage.'<p><i>But we found a short description: </i></p>desc', $entry->getContent());
558d9aab 115 $this->assertEmpty($entry->getPreviewPicture());
98f0929f 116 $this->assertEmpty($entry->getLanguage());
10b35097 117 $this->assertEmpty($entry->getHttpStatus());
558d9aab 118 $this->assertEmpty($entry->getMimetype());
da3d4998
JB
119 $this->assertEquals(0.0, $entry->getReadingTime());
120 $this->assertEquals('domain.io', $entry->getDomainName());
558d9aab
JB
121 }
122
123 public function testWithContent()
124 {
f530f7f5
KG
125 $tagger = $this->getTaggerMock();
126 $tagger->expects($this->once())
127 ->method('tag');
128
558d9aab 129 $graby = $this->getMockBuilder('Graby\Graby')
4094ea47 130 ->setMethods(['fetchContent'])
558d9aab
JB
131 ->disableOriginalConstructor()
132 ->getMock();
133
134 $graby->expects($this->any())
135 ->method('fetchContent')
4094ea47 136 ->willReturn([
da3d4998 137 'html' => str_repeat('this is my content', 325),
558d9aab
JB
138 'title' => 'this is my title',
139 'url' => 'http://1.1.1.1',
140 'content_type' => 'text/html',
98f0929f 141 'language' => 'fr',
10b35097 142 'status' => '200',
4094ea47 143 'open_graph' => [
558d9aab
JB
144 'og_title' => 'my OG title',
145 'og_description' => 'OG desc',
f1e29e69 146 'og_image' => 'http://3.3.3.3/cover.jpg',
4094ea47
JB
147 ],
148 ]);
558d9aab 149
fc2b7bda 150 $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage);
558d9aab
JB
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());
da3d4998 155 $this->assertContains('this is my content', $entry->getContent());
558d9aab
JB
156 $this->assertEquals('http://3.3.3.3/cover.jpg', $entry->getPreviewPicture());
157 $this->assertEquals('text/html', $entry->getMimetype());
98f0929f 158 $this->assertEquals('fr', $entry->getLanguage());
10b35097 159 $this->assertEquals('200', $entry->getHttpStatus());
da3d4998
JB
160 $this->assertEquals(4.0, $entry->getReadingTime());
161 $this->assertEquals('1.1.1.1', $entry->getDomainName());
558d9aab 162 }
f530f7f5 163
3d71d403
JB
164 public function testWithContentAndNoOgImage()
165 {
166 $tagger = $this->getTaggerMock();
167 $tagger->expects($this->once())
168 ->method('tag');
169
170 $graby = $this->getMockBuilder('Graby\Graby')
171 ->setMethods(['fetchContent'])
172 ->disableOriginalConstructor()
173 ->getMock();
174
175 $graby->expects($this->any())
176 ->method('fetchContent')
177 ->willReturn([
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',
182 'language' => 'fr',
183 'status' => '200',
184 'open_graph' => [
185 'og_title' => 'my OG title',
186 'og_description' => 'OG desc',
187 'og_image' => false,
188 ],
189 ]);
190
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');
193
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());
203 }
204
4d0ec0e7
JB
205 public function testWithForcedContent()
206 {
207 $tagger = $this->getTaggerMock();
208 $tagger->expects($this->once())
209 ->method('tag');
210
211 $graby = $this->getMockBuilder('Graby\Graby')->getMock();
212
fc2b7bda 213 $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage);
4d0ec0e7
JB
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',
219 'language' => 'fr',
220 ]);
221
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());
229 }
230
231 public function testTaggerThrowException()
232 {
233 $graby = $this->getMockBuilder('Graby\Graby')
234 ->disableOriginalConstructor()
235 ->getMock();
236
237 $tagger = $this->getTaggerMock();
238 $tagger->expects($this->once())
239 ->method('tag')
240 ->will($this->throwException(new \Exception()));
241
242 $tagRepo = $this->getTagRepositoryMock();
fc2b7bda 243 $proxy = new ContentProxy($graby, $tagger, $tagRepo, $this->getLogger(), $this->fetchingErrorMessage);
4d0ec0e7
JB
244
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',
250 'language' => 'fr',
251 ]);
252
253 $this->assertCount(0, $entry->getTags());
254 }
255
c2656f96
JB
256 public function testAssignTagsWithArrayAndExtraSpaces()
257 {
258 $graby = $this->getMockBuilder('Graby\Graby')
259 ->disableOriginalConstructor()
260 ->getMock();
261
262 $tagRepo = $this->getTagRepositoryMock();
fc2b7bda 263 $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage);
c2656f96
JB
264
265 $entry = new Entry(new User());
266
4094ea47 267 $proxy->assignTagsToEntry($entry, [' tag1', 'tag2 ']);
c2656f96
JB
268
269 $this->assertCount(2, $entry->getTags());
270 $this->assertEquals('tag1', $entry->getTags()[0]->getLabel());
271 $this->assertEquals('tag2', $entry->getTags()[1]->getLabel());
272 }
273
274 public function testAssignTagsWithString()
275 {
276 $graby = $this->getMockBuilder('Graby\Graby')
277 ->disableOriginalConstructor()
278 ->getMock();
279
280 $tagRepo = $this->getTagRepositoryMock();
fc2b7bda 281 $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage);
c2656f96
JB
282
283 $entry = new Entry(new User());
284
285 $proxy->assignTagsToEntry($entry, 'tag1, tag2');
286
287 $this->assertCount(2, $entry->getTags());
288 $this->assertEquals('tag1', $entry->getTags()[0]->getLabel());
289 $this->assertEquals('tag2', $entry->getTags()[1]->getLabel());
290 }
291
292 public function testAssignTagsWithEmptyArray()
293 {
294 $graby = $this->getMockBuilder('Graby\Graby')
295 ->disableOriginalConstructor()
296 ->getMock();
297
298 $tagRepo = $this->getTagRepositoryMock();
fc2b7bda 299 $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage);
c2656f96
JB
300
301 $entry = new Entry(new User());
302
4094ea47 303 $proxy->assignTagsToEntry($entry, []);
c2656f96
JB
304
305 $this->assertCount(0, $entry->getTags());
306 }
307
308 public function testAssignTagsWithEmptyString()
309 {
310 $graby = $this->getMockBuilder('Graby\Graby')
311 ->disableOriginalConstructor()
312 ->getMock();
313
314 $tagRepo = $this->getTagRepositoryMock();
fc2b7bda 315 $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage);
c2656f96
JB
316
317 $entry = new Entry(new User());
318
319 $proxy->assignTagsToEntry($entry, '');
320
321 $this->assertCount(0, $entry->getTags());
322 }
323
324 public function testAssignTagsAlreadyAssigned()
325 {
326 $graby = $this->getMockBuilder('Graby\Graby')
327 ->disableOriginalConstructor()
328 ->getMock();
329
330 $tagRepo = $this->getTagRepositoryMock();
fc2b7bda 331 $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage);
c2656f96
JB
332
333 $tagEntity = new Tag();
334 $tagEntity->setLabel('tag1');
335
336 $entry = new Entry(new User());
337 $entry->addTag($tagEntity);
338
339 $proxy->assignTagsToEntry($entry, 'tag1, tag2');
340
341 $this->assertCount(2, $entry->getTags());
342 $this->assertEquals('tag1', $entry->getTags()[0]->getLabel());
343 $this->assertEquals('tag2', $entry->getTags()[1]->getLabel());
344 }
345
40113585
JB
346 public function testAssignTagsNotFlushed()
347 {
348 $graby = $this->getMockBuilder('Graby\Graby')
349 ->disableOriginalConstructor()
350 ->getMock();
351
352 $tagRepo = $this->getTagRepositoryMock();
353 $tagRepo->expects($this->never())
354 ->method('__call');
355
fc2b7bda 356 $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage);
40113585
JB
357
358 $tagEntity = new Tag();
359 $tagEntity->setLabel('tag1');
360
361 $entry = new Entry(new User());
362
363 $proxy->assignTagsToEntry($entry, 'tag1', [$tagEntity]);
364
365 $this->assertCount(1, $entry->getTags());
366 $this->assertEquals('tag1', $entry->getTags()[0]->getLabel());
367 }
368
f530f7f5
KG
369 private function getTaggerMock()
370 {
371 return $this->getMockBuilder('Wallabag\CoreBundle\Helper\RuleBasedTagger')
4094ea47 372 ->setMethods(['tag'])
f530f7f5
KG
373 ->disableOriginalConstructor()
374 ->getMock();
375 }
1c9cd2a7 376
c2656f96
JB
377 private function getTagRepositoryMock()
378 {
379 return $this->getMockBuilder('Wallabag\CoreBundle\Repository\TagRepository')
380 ->disableOriginalConstructor()
381 ->getMock();
382 }
383
0c5bcd82 384 private function getLogger()
1c9cd2a7 385 {
0c5bcd82 386 return new NullLogger();
1c9cd2a7 387 }
558d9aab 388}