]>
Commit | Line | Data |
---|---|---|
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 | ||
11 | class ContentProxyTest extends \PHPUnit_Framework_TestCase | |
12 | { | |
13 | public function testWithBadUrl() | |
14 | { | |
15 | $tagger = $this->getTaggerMock(); | |
16 | $tagger->expects($this->once()) | |
17 | ->method('tag'); | |
18 | ||
19 | $graby = $this->getMockBuilder('Graby\Graby') | |
20 | ->setMethods(['fetchContent']) | |
21 | ->disableOriginalConstructor() | |
22 | ->getMock(); | |
23 | ||
24 | $graby->expects($this->any()) | |
25 | ->method('fetchContent') | |
26 | ->willReturn([ | |
27 | 'html' => false, | |
28 | 'title' => '', | |
29 | 'url' => '', | |
30 | 'content_type' => '', | |
31 | 'language' => '', | |
32 | ]); | |
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 | ||
47 | public function testWithEmptyContent() | |
48 | { | |
49 | $tagger = $this->getTaggerMock(); | |
50 | $tagger->expects($this->once()) | |
51 | ->method('tag'); | |
52 | ||
53 | $graby = $this->getMockBuilder('Graby\Graby') | |
54 | ->setMethods(['fetchContent']) | |
55 | ->disableOriginalConstructor() | |
56 | ->getMock(); | |
57 | ||
58 | $graby->expects($this->any()) | |
59 | ->method('fetchContent') | |
60 | ->willReturn([ | |
61 | 'html' => false, | |
62 | 'title' => '', | |
63 | 'url' => '', | |
64 | 'content_type' => '', | |
65 | 'language' => '', | |
66 | ]); | |
67 | ||
68 | $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger()); | |
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()); | |
76 | $this->assertEmpty($entry->getLanguage()); | |
77 | $this->assertEquals(0.0, $entry->getReadingTime()); | |
78 | $this->assertEquals('0.0.0.0', $entry->getDomainName()); | |
79 | } | |
80 | ||
81 | public function testWithEmptyContentButOG() | |
82 | { | |
83 | $tagger = $this->getTaggerMock(); | |
84 | $tagger->expects($this->once()) | |
85 | ->method('tag'); | |
86 | ||
87 | $graby = $this->getMockBuilder('Graby\Graby') | |
88 | ->setMethods(['fetchContent']) | |
89 | ->disableOriginalConstructor() | |
90 | ->getMock(); | |
91 | ||
92 | $graby->expects($this->any()) | |
93 | ->method('fetchContent') | |
94 | ->willReturn([ | |
95 | 'html' => false, | |
96 | 'title' => '', | |
97 | 'url' => '', | |
98 | 'content_type' => '', | |
99 | 'language' => '', | |
100 | 'open_graph' => [ | |
101 | 'og_title' => 'my title', | |
102 | 'og_description' => 'desc', | |
103 | ], | |
104 | ]); | |
105 | ||
106 | $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger()); | |
107 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://domain.io'); | |
108 | ||
109 | $this->assertEquals('http://domain.io', $entry->getUrl()); | |
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()); | |
113 | $this->assertEmpty($entry->getLanguage()); | |
114 | $this->assertEmpty($entry->getMimetype()); | |
115 | $this->assertEquals(0.0, $entry->getReadingTime()); | |
116 | $this->assertEquals('domain.io', $entry->getDomainName()); | |
117 | } | |
118 | ||
119 | public function testWithContent() | |
120 | { | |
121 | $tagger = $this->getTaggerMock(); | |
122 | $tagger->expects($this->once()) | |
123 | ->method('tag'); | |
124 | ||
125 | $graby = $this->getMockBuilder('Graby\Graby') | |
126 | ->setMethods(['fetchContent']) | |
127 | ->disableOriginalConstructor() | |
128 | ->getMock(); | |
129 | ||
130 | $graby->expects($this->any()) | |
131 | ->method('fetchContent') | |
132 | ->willReturn([ | |
133 | 'html' => str_repeat('this is my content', 325), | |
134 | 'title' => 'this is my title', | |
135 | 'url' => 'http://1.1.1.1', | |
136 | 'content_type' => 'text/html', | |
137 | 'language' => 'fr', | |
138 | 'open_graph' => [ | |
139 | 'og_title' => 'my OG title', | |
140 | 'og_description' => 'OG desc', | |
141 | 'og_image' => 'http://3.3.3.3/cover.jpg', | |
142 | ], | |
143 | ]); | |
144 | ||
145 | $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger()); | |
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()); | |
150 | $this->assertContains('this is my content', $entry->getContent()); | |
151 | $this->assertEquals('http://3.3.3.3/cover.jpg', $entry->getPreviewPicture()); | |
152 | $this->assertEquals('text/html', $entry->getMimetype()); | |
153 | $this->assertEquals('fr', $entry->getLanguage()); | |
154 | $this->assertEquals(4.0, $entry->getReadingTime()); | |
155 | $this->assertEquals('1.1.1.1', $entry->getDomainName()); | |
156 | } | |
157 | ||
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 | ||
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 | ||
220 | $proxy->assignTagsToEntry($entry, [' tag1', 'tag2 ']); | |
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 | ||
256 | $proxy->assignTagsToEntry($entry, []); | |
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 | ||
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 | ||
322 | private function getTaggerMock() | |
323 | { | |
324 | return $this->getMockBuilder('Wallabag\CoreBundle\Helper\RuleBasedTagger') | |
325 | ->setMethods(['tag']) | |
326 | ->disableOriginalConstructor() | |
327 | ->getMock(); | |
328 | } | |
329 | ||
330 | private function getTagRepositoryMock() | |
331 | { | |
332 | return $this->getMockBuilder('Wallabag\CoreBundle\Repository\TagRepository') | |
333 | ->disableOriginalConstructor() | |
334 | ->getMock(); | |
335 | } | |
336 | ||
337 | private function getLogger() | |
338 | { | |
339 | return new NullLogger(); | |
340 | } | |
341 | } |