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