]> git.immae.eu Git - github/wallabag/wallabag.git/blame - tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php
Merge pull request #2600 from wallabag/install-assets
[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' => '',
10b35097 100 'status' => '',
4094ea47 101 'open_graph' => [
98f0929f
JB
102 'og_title' => 'my title',
103 'og_description' => 'desc',
4094ea47
JB
104 ],
105 ]);
558d9aab 106
c2656f96 107 $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger());
da3d4998 108 $entry = $proxy->updateEntry(new Entry(new User()), 'http://domain.io');
558d9aab 109
da3d4998 110 $this->assertEquals('http://domain.io', $entry->getUrl());
558d9aab
JB
111 $this->assertEquals('my title', $entry->getTitle());
112 $this->assertEquals('<p>Unable to retrieve readable content.</p><p><i>But we found a short description: </i></p>desc', $entry->getContent());
113 $this->assertEmpty($entry->getPreviewPicture());
98f0929f 114 $this->assertEmpty($entry->getLanguage());
10b35097 115 $this->assertEmpty($entry->getHttpStatus());
558d9aab 116 $this->assertEmpty($entry->getMimetype());
da3d4998
JB
117 $this->assertEquals(0.0, $entry->getReadingTime());
118 $this->assertEquals('domain.io', $entry->getDomainName());
558d9aab
JB
119 }
120
121 public function testWithContent()
122 {
f530f7f5
KG
123 $tagger = $this->getTaggerMock();
124 $tagger->expects($this->once())
125 ->method('tag');
126
558d9aab 127 $graby = $this->getMockBuilder('Graby\Graby')
4094ea47 128 ->setMethods(['fetchContent'])
558d9aab
JB
129 ->disableOriginalConstructor()
130 ->getMock();
131
132 $graby->expects($this->any())
133 ->method('fetchContent')
4094ea47 134 ->willReturn([
da3d4998 135 'html' => str_repeat('this is my content', 325),
558d9aab
JB
136 'title' => 'this is my title',
137 'url' => 'http://1.1.1.1',
138 'content_type' => 'text/html',
98f0929f 139 'language' => 'fr',
10b35097 140 'status' => '200',
4094ea47 141 'open_graph' => [
558d9aab
JB
142 'og_title' => 'my OG title',
143 'og_description' => 'OG desc',
f1e29e69 144 'og_image' => 'http://3.3.3.3/cover.jpg',
4094ea47
JB
145 ],
146 ]);
558d9aab 147
c2656f96 148 $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger());
558d9aab
JB
149 $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0');
150
151 $this->assertEquals('http://1.1.1.1', $entry->getUrl());
152 $this->assertEquals('this is my title', $entry->getTitle());
da3d4998 153 $this->assertContains('this is my content', $entry->getContent());
558d9aab
JB
154 $this->assertEquals('http://3.3.3.3/cover.jpg', $entry->getPreviewPicture());
155 $this->assertEquals('text/html', $entry->getMimetype());
98f0929f 156 $this->assertEquals('fr', $entry->getLanguage());
10b35097 157 $this->assertEquals('200', $entry->getHttpStatus());
da3d4998
JB
158 $this->assertEquals(4.0, $entry->getReadingTime());
159 $this->assertEquals('1.1.1.1', $entry->getDomainName());
558d9aab 160 }
f530f7f5 161
4d0ec0e7
JB
162 public function testWithForcedContent()
163 {
164 $tagger = $this->getTaggerMock();
165 $tagger->expects($this->once())
166 ->method('tag');
167
168 $graby = $this->getMockBuilder('Graby\Graby')->getMock();
169
170 $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger());
171 $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0', [
172 'html' => str_repeat('this is my content', 325),
173 'title' => 'this is my title',
174 'url' => 'http://1.1.1.1',
175 'content_type' => 'text/html',
176 'language' => 'fr',
177 ]);
178
179 $this->assertEquals('http://1.1.1.1', $entry->getUrl());
180 $this->assertEquals('this is my title', $entry->getTitle());
181 $this->assertContains('this is my content', $entry->getContent());
182 $this->assertEquals('text/html', $entry->getMimetype());
183 $this->assertEquals('fr', $entry->getLanguage());
184 $this->assertEquals(4.0, $entry->getReadingTime());
185 $this->assertEquals('1.1.1.1', $entry->getDomainName());
186 }
187
188 public function testTaggerThrowException()
189 {
190 $graby = $this->getMockBuilder('Graby\Graby')
191 ->disableOriginalConstructor()
192 ->getMock();
193
194 $tagger = $this->getTaggerMock();
195 $tagger->expects($this->once())
196 ->method('tag')
197 ->will($this->throwException(new \Exception()));
198
199 $tagRepo = $this->getTagRepositoryMock();
200 $proxy = new ContentProxy($graby, $tagger, $tagRepo, $this->getLogger());
201
202 $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0', [
203 'html' => str_repeat('this is my content', 325),
204 'title' => 'this is my title',
205 'url' => 'http://1.1.1.1',
206 'content_type' => 'text/html',
207 'language' => 'fr',
208 ]);
209
210 $this->assertCount(0, $entry->getTags());
211 }
212
c2656f96
JB
213 public function testAssignTagsWithArrayAndExtraSpaces()
214 {
215 $graby = $this->getMockBuilder('Graby\Graby')
216 ->disableOriginalConstructor()
217 ->getMock();
218
219 $tagRepo = $this->getTagRepositoryMock();
220 $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger());
221
222 $entry = new Entry(new User());
223
4094ea47 224 $proxy->assignTagsToEntry($entry, [' tag1', 'tag2 ']);
c2656f96
JB
225
226 $this->assertCount(2, $entry->getTags());
227 $this->assertEquals('tag1', $entry->getTags()[0]->getLabel());
228 $this->assertEquals('tag2', $entry->getTags()[1]->getLabel());
229 }
230
231 public function testAssignTagsWithString()
232 {
233 $graby = $this->getMockBuilder('Graby\Graby')
234 ->disableOriginalConstructor()
235 ->getMock();
236
237 $tagRepo = $this->getTagRepositoryMock();
238 $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger());
239
240 $entry = new Entry(new User());
241
242 $proxy->assignTagsToEntry($entry, 'tag1, tag2');
243
244 $this->assertCount(2, $entry->getTags());
245 $this->assertEquals('tag1', $entry->getTags()[0]->getLabel());
246 $this->assertEquals('tag2', $entry->getTags()[1]->getLabel());
247 }
248
249 public function testAssignTagsWithEmptyArray()
250 {
251 $graby = $this->getMockBuilder('Graby\Graby')
252 ->disableOriginalConstructor()
253 ->getMock();
254
255 $tagRepo = $this->getTagRepositoryMock();
256 $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger());
257
258 $entry = new Entry(new User());
259
4094ea47 260 $proxy->assignTagsToEntry($entry, []);
c2656f96
JB
261
262 $this->assertCount(0, $entry->getTags());
263 }
264
265 public function testAssignTagsWithEmptyString()
266 {
267 $graby = $this->getMockBuilder('Graby\Graby')
268 ->disableOriginalConstructor()
269 ->getMock();
270
271 $tagRepo = $this->getTagRepositoryMock();
272 $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger());
273
274 $entry = new Entry(new User());
275
276 $proxy->assignTagsToEntry($entry, '');
277
278 $this->assertCount(0, $entry->getTags());
279 }
280
281 public function testAssignTagsAlreadyAssigned()
282 {
283 $graby = $this->getMockBuilder('Graby\Graby')
284 ->disableOriginalConstructor()
285 ->getMock();
286
287 $tagRepo = $this->getTagRepositoryMock();
288 $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger());
289
290 $tagEntity = new Tag();
291 $tagEntity->setLabel('tag1');
292
293 $entry = new Entry(new User());
294 $entry->addTag($tagEntity);
295
296 $proxy->assignTagsToEntry($entry, 'tag1, tag2');
297
298 $this->assertCount(2, $entry->getTags());
299 $this->assertEquals('tag1', $entry->getTags()[0]->getLabel());
300 $this->assertEquals('tag2', $entry->getTags()[1]->getLabel());
301 }
302
40113585
JB
303 public function testAssignTagsNotFlushed()
304 {
305 $graby = $this->getMockBuilder('Graby\Graby')
306 ->disableOriginalConstructor()
307 ->getMock();
308
309 $tagRepo = $this->getTagRepositoryMock();
310 $tagRepo->expects($this->never())
311 ->method('__call');
312
313 $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger());
314
315 $tagEntity = new Tag();
316 $tagEntity->setLabel('tag1');
317
318 $entry = new Entry(new User());
319
320 $proxy->assignTagsToEntry($entry, 'tag1', [$tagEntity]);
321
322 $this->assertCount(1, $entry->getTags());
323 $this->assertEquals('tag1', $entry->getTags()[0]->getLabel());
324 }
325
f530f7f5
KG
326 private function getTaggerMock()
327 {
328 return $this->getMockBuilder('Wallabag\CoreBundle\Helper\RuleBasedTagger')
4094ea47 329 ->setMethods(['tag'])
f530f7f5
KG
330 ->disableOriginalConstructor()
331 ->getMock();
332 }
1c9cd2a7 333
c2656f96
JB
334 private function getTagRepositoryMock()
335 {
336 return $this->getMockBuilder('Wallabag\CoreBundle\Repository\TagRepository')
337 ->disableOriginalConstructor()
338 ->getMock();
339 }
340
0c5bcd82 341 private function getLogger()
1c9cd2a7 342 {
0c5bcd82 343 return new NullLogger();
1c9cd2a7 344 }
558d9aab 345}