]> git.immae.eu Git - github/wallabag/wallabag.git/blob - tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php
Fix ContentProxy tests
[github/wallabag/wallabag.git] / tests / Wallabag / CoreBundle / Helper / ContentProxyTest.php
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 use Tests\Wallabag\CoreBundle\CoreKernelTestCase;
11
12 class ContentProxyTest extends CoreKernelTestCase
13 {
14 public function testWithBadUrl()
15 {
16 $tagger = $this->getTaggerMock();
17 $tagger->expects($this->once())
18 ->method('tag');
19
20 $graby = $this->getMockBuilder('Graby\Graby')
21 ->setMethods(['fetchContent'])
22 ->disableOriginalConstructor()
23 ->getMock();
24
25 $graby->expects($this->any())
26 ->method('fetchContent')
27 ->willReturn([
28 'html' => false,
29 'title' => '',
30 'url' => '',
31 'content_type' => '',
32 'language' => '',
33 ]);
34
35 $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage);
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());
40 $this->assertEquals($this->fetchingErrorMessage, $entry->getContent());
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
48 public function testWithEmptyContent()
49 {
50 $tagger = $this->getTaggerMock();
51 $tagger->expects($this->once())
52 ->method('tag');
53
54 $graby = $this->getMockBuilder('Graby\Graby')
55 ->setMethods(['fetchContent'])
56 ->disableOriginalConstructor()
57 ->getMock();
58
59 $graby->expects($this->any())
60 ->method('fetchContent')
61 ->willReturn([
62 'html' => false,
63 'title' => '',
64 'url' => '',
65 'content_type' => '',
66 'language' => '',
67 ]);
68
69 $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage);
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());
74 $this->assertEquals($this->fetchingErrorMessage, $entry->getContent());
75 $this->assertEmpty($entry->getPreviewPicture());
76 $this->assertEmpty($entry->getMimetype());
77 $this->assertEmpty($entry->getLanguage());
78 $this->assertEquals(0.0, $entry->getReadingTime());
79 $this->assertEquals('0.0.0.0', $entry->getDomainName());
80 }
81
82 public function testWithEmptyContentButOG()
83 {
84 $tagger = $this->getTaggerMock();
85 $tagger->expects($this->once())
86 ->method('tag');
87
88 $graby = $this->getMockBuilder('Graby\Graby')
89 ->setMethods(['fetchContent'])
90 ->disableOriginalConstructor()
91 ->getMock();
92
93 $graby->expects($this->any())
94 ->method('fetchContent')
95 ->willReturn([
96 'html' => false,
97 'title' => '',
98 'url' => '',
99 'content_type' => '',
100 'language' => '',
101 'status' => '',
102 'open_graph' => [
103 'og_title' => 'my title',
104 'og_description' => 'desc',
105 ],
106 ]);
107
108 $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage);
109 $entry = $proxy->updateEntry(new Entry(new User()), 'http://domain.io');
110
111 $this->assertEquals('http://domain.io', $entry->getUrl());
112 $this->assertEquals('my title', $entry->getTitle());
113 $this->assertEquals($this->fetchingErrorMessage . '<p><i>But we found a short description: </i></p>desc', $entry->getContent());
114 $this->assertEmpty($entry->getPreviewPicture());
115 $this->assertEmpty($entry->getLanguage());
116 $this->assertEmpty($entry->getHttpStatus());
117 $this->assertEmpty($entry->getMimetype());
118 $this->assertEquals(0.0, $entry->getReadingTime());
119 $this->assertEquals('domain.io', $entry->getDomainName());
120 }
121
122 public function testWithContent()
123 {
124 $tagger = $this->getTaggerMock();
125 $tagger->expects($this->once())
126 ->method('tag');
127
128 $graby = $this->getMockBuilder('Graby\Graby')
129 ->setMethods(['fetchContent'])
130 ->disableOriginalConstructor()
131 ->getMock();
132
133 $graby->expects($this->any())
134 ->method('fetchContent')
135 ->willReturn([
136 'html' => str_repeat('this is my content', 325),
137 'title' => 'this is my title',
138 'url' => 'http://1.1.1.1',
139 'content_type' => 'text/html',
140 'language' => 'fr',
141 'status' => '200',
142 'open_graph' => [
143 'og_title' => 'my OG title',
144 'og_description' => 'OG desc',
145 'og_image' => 'http://3.3.3.3/cover.jpg',
146 ],
147 ]);
148
149 $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage);
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());
154 $this->assertContains('this is my content', $entry->getContent());
155 $this->assertEquals('http://3.3.3.3/cover.jpg', $entry->getPreviewPicture());
156 $this->assertEquals('text/html', $entry->getMimetype());
157 $this->assertEquals('fr', $entry->getLanguage());
158 $this->assertEquals('200', $entry->getHttpStatus());
159 $this->assertEquals(4.0, $entry->getReadingTime());
160 $this->assertEquals('1.1.1.1', $entry->getDomainName());
161 }
162
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
171 $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage);
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();
201 $proxy = new ContentProxy($graby, $tagger, $tagRepo, $this->getLogger(), $this->fetchingErrorMessage);
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
214 public function testAssignTagsWithArrayAndExtraSpaces()
215 {
216 $graby = $this->getMockBuilder('Graby\Graby')
217 ->disableOriginalConstructor()
218 ->getMock();
219
220 $tagRepo = $this->getTagRepositoryMock();
221 $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage);
222
223 $entry = new Entry(new User());
224
225 $proxy->assignTagsToEntry($entry, [' tag1', 'tag2 ']);
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();
239 $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage);
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();
257 $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage);
258
259 $entry = new Entry(new User());
260
261 $proxy->assignTagsToEntry($entry, []);
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();
273 $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage);
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();
289 $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage);
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
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
314 $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage);
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
327 private function getTaggerMock()
328 {
329 return $this->getMockBuilder('Wallabag\CoreBundle\Helper\RuleBasedTagger')
330 ->setMethods(['tag'])
331 ->disableOriginalConstructor()
332 ->getMock();
333 }
334
335 private function getTagRepositoryMock()
336 {
337 return $this->getMockBuilder('Wallabag\CoreBundle\Repository\TagRepository')
338 ->disableOriginalConstructor()
339 ->getMock();
340 }
341
342 private function getLogger()
343 {
344 return new NullLogger();
345 }
346 }