]> git.immae.eu Git - github/wallabag/wallabag.git/blame - tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php
Merge pull request #2593 from wallabag/test-clarify
[github/wallabag/wallabag.git] / tests / Wallabag / CoreBundle / Helper / ContentProxyTest.php
CommitLineData
558d9aab
JB
1<?php
2
23634d5d 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
8a493541 11class ContentProxyTest extends \PHPUnit_Framework_TestCase
558d9aab 12{
4d0ec0e7
JB
13 public function testWithBadUrl()
14 {
15 $tagger = $this->getTaggerMock();
16 $tagger->expects($this->once())
17 ->method('tag');
18
19 $graby = $this->getMockBuilder('Graby\Graby')
4094ea47 20 ->setMethods(['fetchContent'])
4d0ec0e7
JB
21 ->disableOriginalConstructor()
22 ->getMock();
23
24 $graby->expects($this->any())
25 ->method('fetchContent')
4094ea47 26 ->willReturn([
4d0ec0e7
JB
27 'html' => false,
28 'title' => '',
29 'url' => '',
30 'content_type' => '',
31 'language' => '',
4094ea47 32 ]);
4d0ec0e7
JB
33
34 $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger());
35 $entry = $proxy->updateEntry(new Entry(new User()), 'http://user@:80');
36
37 $this->assertEquals('http://user@:80', $entry->getUrl());
38 $this->assertEmpty($entry->getTitle());
39 $this->assertEquals('<p>Unable to retrieve readable content.</p>', $entry->getContent());
40 $this->assertEmpty($entry->getPreviewPicture());
41 $this->assertEmpty($entry->getMimetype());
42 $this->assertEmpty($entry->getLanguage());
43 $this->assertEquals(0.0, $entry->getReadingTime());
44 $this->assertEquals(false, $entry->getDomainName());
45 }
46
558d9aab
JB
47 public function testWithEmptyContent()
48 {
f530f7f5
KG
49 $tagger = $this->getTaggerMock();
50 $tagger->expects($this->once())
51 ->method('tag');
52
558d9aab 53 $graby = $this->getMockBuilder('Graby\Graby')
4094ea47 54 ->setMethods(['fetchContent'])
558d9aab
JB
55 ->disableOriginalConstructor()
56 ->getMock();
57
58 $graby->expects($this->any())
59 ->method('fetchContent')
4094ea47 60 ->willReturn([
98f0929f
JB
61 'html' => false,
62 'title' => '',
63 'url' => '',
64 'content_type' => '',
65 'language' => '',
4094ea47 66 ]);
558d9aab 67
c2656f96 68 $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger());
558d9aab
JB
69 $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0');
70
71 $this->assertEquals('http://0.0.0.0', $entry->getUrl());
72 $this->assertEmpty($entry->getTitle());
73 $this->assertEquals('<p>Unable to retrieve readable content.</p>', $entry->getContent());
74 $this->assertEmpty($entry->getPreviewPicture());
75 $this->assertEmpty($entry->getMimetype());
98f0929f 76 $this->assertEmpty($entry->getLanguage());
da3d4998
JB
77 $this->assertEquals(0.0, $entry->getReadingTime());
78 $this->assertEquals('0.0.0.0', $entry->getDomainName());
558d9aab
JB
79 }
80
81 public function testWithEmptyContentButOG()
82 {
f530f7f5
KG
83 $tagger = $this->getTaggerMock();
84 $tagger->expects($this->once())
85 ->method('tag');
86
558d9aab 87 $graby = $this->getMockBuilder('Graby\Graby')
4094ea47 88 ->setMethods(['fetchContent'])
558d9aab
JB
89 ->disableOriginalConstructor()
90 ->getMock();
91
92 $graby->expects($this->any())
93 ->method('fetchContent')
4094ea47 94 ->willReturn([
98f0929f
JB
95 'html' => false,
96 'title' => '',
97 'url' => '',
98 'content_type' => '',
99 'language' => '',
4094ea47 100 'open_graph' => [
98f0929f
JB
101 'og_title' => 'my title',
102 'og_description' => 'desc',
4094ea47
JB
103 ],
104 ]);
558d9aab 105
c2656f96 106 $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger());
da3d4998 107 $entry = $proxy->updateEntry(new Entry(new User()), 'http://domain.io');
558d9aab 108
da3d4998 109 $this->assertEquals('http://domain.io', $entry->getUrl());
558d9aab
JB
110 $this->assertEquals('my title', $entry->getTitle());
111 $this->assertEquals('<p>Unable to retrieve readable content.</p><p><i>But we found a short description: </i></p>desc', $entry->getContent());
112 $this->assertEmpty($entry->getPreviewPicture());
98f0929f 113 $this->assertEmpty($entry->getLanguage());
558d9aab 114 $this->assertEmpty($entry->getMimetype());
da3d4998
JB
115 $this->assertEquals(0.0, $entry->getReadingTime());
116 $this->assertEquals('domain.io', $entry->getDomainName());
558d9aab
JB
117 }
118
119 public function testWithContent()
120 {
f530f7f5
KG
121 $tagger = $this->getTaggerMock();
122 $tagger->expects($this->once())
123 ->method('tag');
124
558d9aab 125 $graby = $this->getMockBuilder('Graby\Graby')
4094ea47 126 ->setMethods(['fetchContent'])
558d9aab
JB
127 ->disableOriginalConstructor()
128 ->getMock();
129
130 $graby->expects($this->any())
131 ->method('fetchContent')
4094ea47 132 ->willReturn([
da3d4998 133 'html' => str_repeat('this is my content', 325),
558d9aab
JB
134 'title' => 'this is my title',
135 'url' => 'http://1.1.1.1',
136 'content_type' => 'text/html',
98f0929f 137 'language' => 'fr',
4094ea47 138 'open_graph' => [
558d9aab
JB
139 'og_title' => 'my OG title',
140 'og_description' => 'OG desc',
f1e29e69 141 'og_image' => 'http://3.3.3.3/cover.jpg',
4094ea47
JB
142 ],
143 ]);
558d9aab 144
c2656f96 145 $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger());
558d9aab
JB
146 $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0');
147
148 $this->assertEquals('http://1.1.1.1', $entry->getUrl());
149 $this->assertEquals('this is my title', $entry->getTitle());
da3d4998 150 $this->assertContains('this is my content', $entry->getContent());
558d9aab
JB
151 $this->assertEquals('http://3.3.3.3/cover.jpg', $entry->getPreviewPicture());
152 $this->assertEquals('text/html', $entry->getMimetype());
98f0929f 153 $this->assertEquals('fr', $entry->getLanguage());
da3d4998
JB
154 $this->assertEquals(4.0, $entry->getReadingTime());
155 $this->assertEquals('1.1.1.1', $entry->getDomainName());
558d9aab 156 }
f530f7f5 157
4d0ec0e7
JB
158 public function testWithForcedContent()
159 {
160 $tagger = $this->getTaggerMock();
161 $tagger->expects($this->once())
162 ->method('tag');
163
164 $graby = $this->getMockBuilder('Graby\Graby')->getMock();
165
166 $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger());
167 $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0', [
168 'html' => str_repeat('this is my content', 325),
169 'title' => 'this is my title',
170 'url' => 'http://1.1.1.1',
171 'content_type' => 'text/html',
172 'language' => 'fr',
173 ]);
174
175 $this->assertEquals('http://1.1.1.1', $entry->getUrl());
176 $this->assertEquals('this is my title', $entry->getTitle());
177 $this->assertContains('this is my content', $entry->getContent());
178 $this->assertEquals('text/html', $entry->getMimetype());
179 $this->assertEquals('fr', $entry->getLanguage());
180 $this->assertEquals(4.0, $entry->getReadingTime());
181 $this->assertEquals('1.1.1.1', $entry->getDomainName());
182 }
183
184 public function testTaggerThrowException()
185 {
186 $graby = $this->getMockBuilder('Graby\Graby')
187 ->disableOriginalConstructor()
188 ->getMock();
189
190 $tagger = $this->getTaggerMock();
191 $tagger->expects($this->once())
192 ->method('tag')
193 ->will($this->throwException(new \Exception()));
194
195 $tagRepo = $this->getTagRepositoryMock();
196 $proxy = new ContentProxy($graby, $tagger, $tagRepo, $this->getLogger());
197
198 $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0', [
199 'html' => str_repeat('this is my content', 325),
200 'title' => 'this is my title',
201 'url' => 'http://1.1.1.1',
202 'content_type' => 'text/html',
203 'language' => 'fr',
204 ]);
205
206 $this->assertCount(0, $entry->getTags());
207 }
208
c2656f96
JB
209 public function testAssignTagsWithArrayAndExtraSpaces()
210 {
211 $graby = $this->getMockBuilder('Graby\Graby')
212 ->disableOriginalConstructor()
213 ->getMock();
214
215 $tagRepo = $this->getTagRepositoryMock();
216 $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger());
217
218 $entry = new Entry(new User());
219
4094ea47 220 $proxy->assignTagsToEntry($entry, [' tag1', 'tag2 ']);
c2656f96
JB
221
222 $this->assertCount(2, $entry->getTags());
223 $this->assertEquals('tag1', $entry->getTags()[0]->getLabel());
224 $this->assertEquals('tag2', $entry->getTags()[1]->getLabel());
225 }
226
227 public function testAssignTagsWithString()
228 {
229 $graby = $this->getMockBuilder('Graby\Graby')
230 ->disableOriginalConstructor()
231 ->getMock();
232
233 $tagRepo = $this->getTagRepositoryMock();
234 $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger());
235
236 $entry = new Entry(new User());
237
238 $proxy->assignTagsToEntry($entry, 'tag1, tag2');
239
240 $this->assertCount(2, $entry->getTags());
241 $this->assertEquals('tag1', $entry->getTags()[0]->getLabel());
242 $this->assertEquals('tag2', $entry->getTags()[1]->getLabel());
243 }
244
245 public function testAssignTagsWithEmptyArray()
246 {
247 $graby = $this->getMockBuilder('Graby\Graby')
248 ->disableOriginalConstructor()
249 ->getMock();
250
251 $tagRepo = $this->getTagRepositoryMock();
252 $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger());
253
254 $entry = new Entry(new User());
255
4094ea47 256 $proxy->assignTagsToEntry($entry, []);
c2656f96
JB
257
258 $this->assertCount(0, $entry->getTags());
259 }
260
261 public function testAssignTagsWithEmptyString()
262 {
263 $graby = $this->getMockBuilder('Graby\Graby')
264 ->disableOriginalConstructor()
265 ->getMock();
266
267 $tagRepo = $this->getTagRepositoryMock();
268 $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger());
269
270 $entry = new Entry(new User());
271
272 $proxy->assignTagsToEntry($entry, '');
273
274 $this->assertCount(0, $entry->getTags());
275 }
276
277 public function testAssignTagsAlreadyAssigned()
278 {
279 $graby = $this->getMockBuilder('Graby\Graby')
280 ->disableOriginalConstructor()
281 ->getMock();
282
283 $tagRepo = $this->getTagRepositoryMock();
284 $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger());
285
286 $tagEntity = new Tag();
287 $tagEntity->setLabel('tag1');
288
289 $entry = new Entry(new User());
290 $entry->addTag($tagEntity);
291
292 $proxy->assignTagsToEntry($entry, 'tag1, tag2');
293
294 $this->assertCount(2, $entry->getTags());
295 $this->assertEquals('tag1', $entry->getTags()[0]->getLabel());
296 $this->assertEquals('tag2', $entry->getTags()[1]->getLabel());
297 }
298
40113585
JB
299 public function testAssignTagsNotFlushed()
300 {
301 $graby = $this->getMockBuilder('Graby\Graby')
302 ->disableOriginalConstructor()
303 ->getMock();
304
305 $tagRepo = $this->getTagRepositoryMock();
306 $tagRepo->expects($this->never())
307 ->method('__call');
308
309 $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger());
310
311 $tagEntity = new Tag();
312 $tagEntity->setLabel('tag1');
313
314 $entry = new Entry(new User());
315
316 $proxy->assignTagsToEntry($entry, 'tag1', [$tagEntity]);
317
318 $this->assertCount(1, $entry->getTags());
319 $this->assertEquals('tag1', $entry->getTags()[0]->getLabel());
320 }
321
f530f7f5
KG
322 private function getTaggerMock()
323 {
324 return $this->getMockBuilder('Wallabag\CoreBundle\Helper\RuleBasedTagger')
4094ea47 325 ->setMethods(['tag'])
f530f7f5
KG
326 ->disableOriginalConstructor()
327 ->getMock();
328 }
1c9cd2a7 329
c2656f96
JB
330 private function getTagRepositoryMock()
331 {
332 return $this->getMockBuilder('Wallabag\CoreBundle\Repository\TagRepository')
333 ->disableOriginalConstructor()
334 ->getMock();
335 }
336
0c5bcd82 337 private function getLogger()
1c9cd2a7 338 {
0c5bcd82 339 return new NullLogger();
1c9cd2a7 340 }
558d9aab 341}