diff options
Diffstat (limited to 'tests/Wallabag/CoreBundle/Helper')
-rw-r--r-- | tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php | 129 | ||||
-rw-r--r-- | tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php | 25 |
2 files changed, 142 insertions, 12 deletions
diff --git a/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php b/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php index a3570125..95dd75ba 100644 --- a/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php +++ b/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php | |||
@@ -11,6 +11,9 @@ use Wallabag\CoreBundle\Entity\Tag; | |||
11 | use Wallabag\UserBundle\Entity\User; | 11 | use Wallabag\UserBundle\Entity\User; |
12 | use Wallabag\CoreBundle\Helper\RuleBasedTagger; | 12 | use Wallabag\CoreBundle\Helper\RuleBasedTagger; |
13 | use Graby\Graby; | 13 | use Graby\Graby; |
14 | use Symfony\Component\Validator\Validator\RecursiveValidator; | ||
15 | use Symfony\Component\Validator\ConstraintViolationList; | ||
16 | use Symfony\Component\Validator\ConstraintViolation; | ||
14 | 17 | ||
15 | class ContentProxyTest extends \PHPUnit_Framework_TestCase | 18 | class ContentProxyTest extends \PHPUnit_Framework_TestCase |
16 | { | 19 | { |
@@ -37,7 +40,7 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase | |||
37 | 'language' => '', | 40 | 'language' => '', |
38 | ]); | 41 | ]); |
39 | 42 | ||
40 | $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage); | 43 | $proxy = new ContentProxy($graby, $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); |
41 | $entry = new Entry(new User()); | 44 | $entry = new Entry(new User()); |
42 | $proxy->updateEntry($entry, 'http://user@:80'); | 45 | $proxy->updateEntry($entry, 'http://user@:80'); |
43 | 46 | ||
@@ -72,7 +75,7 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase | |||
72 | 'language' => '', | 75 | 'language' => '', |
73 | ]); | 76 | ]); |
74 | 77 | ||
75 | $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage); | 78 | $proxy = new ContentProxy($graby, $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); |
76 | $entry = new Entry(new User()); | 79 | $entry = new Entry(new User()); |
77 | $proxy->updateEntry($entry, 'http://0.0.0.0'); | 80 | $proxy->updateEntry($entry, 'http://0.0.0.0'); |
78 | 81 | ||
@@ -112,7 +115,7 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase | |||
112 | ], | 115 | ], |
113 | ]); | 116 | ]); |
114 | 117 | ||
115 | $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage); | 118 | $proxy = new ContentProxy($graby, $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); |
116 | $entry = new Entry(new User()); | 119 | $entry = new Entry(new User()); |
117 | $proxy->updateEntry($entry, 'http://domain.io'); | 120 | $proxy->updateEntry($entry, 'http://domain.io'); |
118 | 121 | ||
@@ -154,7 +157,7 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase | |||
154 | ], | 157 | ], |
155 | ]); | 158 | ]); |
156 | 159 | ||
157 | $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage); | 160 | $proxy = new ContentProxy($graby, $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); |
158 | $entry = new Entry(new User()); | 161 | $entry = new Entry(new User()); |
159 | $proxy->updateEntry($entry, 'http://0.0.0.0'); | 162 | $proxy->updateEntry($entry, 'http://0.0.0.0'); |
160 | 163 | ||
@@ -192,18 +195,112 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase | |||
192 | 'open_graph' => [ | 195 | 'open_graph' => [ |
193 | 'og_title' => 'my OG title', | 196 | 'og_title' => 'my OG title', |
194 | 'og_description' => 'OG desc', | 197 | 'og_description' => 'OG desc', |
195 | 'og_image' => false, | 198 | 'og_image' => null, |
196 | ], | 199 | ], |
197 | ]); | 200 | ]); |
198 | 201 | ||
199 | $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage); | 202 | $proxy = new ContentProxy($graby, $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); |
200 | $entry = new Entry(new User()); | 203 | $entry = new Entry(new User()); |
201 | $proxy->updateEntry($entry, 'http://0.0.0.0'); | 204 | $proxy->updateEntry($entry, 'http://0.0.0.0'); |
202 | 205 | ||
203 | $this->assertEquals('http://1.1.1.1', $entry->getUrl()); | 206 | $this->assertEquals('http://1.1.1.1', $entry->getUrl()); |
204 | $this->assertEquals('this is my title', $entry->getTitle()); | 207 | $this->assertEquals('this is my title', $entry->getTitle()); |
205 | $this->assertContains('this is my content', $entry->getContent()); | 208 | $this->assertContains('this is my content', $entry->getContent()); |
206 | $this->assertNull($entry->getPreviewPicture()); | 209 | $this->assertEmpty($entry->getPreviewPicture()); |
210 | $this->assertEquals('text/html', $entry->getMimetype()); | ||
211 | $this->assertEquals('fr', $entry->getLanguage()); | ||
212 | $this->assertEquals('200', $entry->getHttpStatus()); | ||
213 | $this->assertEquals(4.0, $entry->getReadingTime()); | ||
214 | $this->assertEquals('1.1.1.1', $entry->getDomainName()); | ||
215 | } | ||
216 | |||
217 | public function testWithContentAndBadLanguage() | ||
218 | { | ||
219 | $tagger = $this->getTaggerMock(); | ||
220 | $tagger->expects($this->once()) | ||
221 | ->method('tag'); | ||
222 | |||
223 | $validator = $this->getValidator(); | ||
224 | $validator->expects($this->exactly(2)) | ||
225 | ->method('validate') | ||
226 | ->will($this->onConsecutiveCalls( | ||
227 | new ConstraintViolationList([new ConstraintViolation('oops', 'oops', [], 'oops', 'language', 'dontexist')]), | ||
228 | new ConstraintViolationList() | ||
229 | )); | ||
230 | |||
231 | $graby = $this->getMockBuilder('Graby\Graby') | ||
232 | ->setMethods(['fetchContent']) | ||
233 | ->disableOriginalConstructor() | ||
234 | ->getMock(); | ||
235 | |||
236 | $graby->expects($this->any()) | ||
237 | ->method('fetchContent') | ||
238 | ->willReturn([ | ||
239 | 'html' => str_repeat('this is my content', 325), | ||
240 | 'title' => 'this is my title', | ||
241 | 'url' => 'http://1.1.1.1', | ||
242 | 'content_type' => 'text/html', | ||
243 | 'language' => 'dontexist', | ||
244 | 'status' => '200', | ||
245 | ]); | ||
246 | |||
247 | $proxy = new ContentProxy($graby, $tagger, $validator, $this->getLogger(), $this->fetchingErrorMessage); | ||
248 | $entry = new Entry(new User()); | ||
249 | $proxy->updateEntry($entry, 'http://0.0.0.0'); | ||
250 | |||
251 | $this->assertEquals('http://1.1.1.1', $entry->getUrl()); | ||
252 | $this->assertEquals('this is my title', $entry->getTitle()); | ||
253 | $this->assertContains('this is my content', $entry->getContent()); | ||
254 | $this->assertEquals('text/html', $entry->getMimetype()); | ||
255 | $this->assertEmpty($entry->getLanguage()); | ||
256 | $this->assertEquals('200', $entry->getHttpStatus()); | ||
257 | $this->assertEquals(4.0, $entry->getReadingTime()); | ||
258 | $this->assertEquals('1.1.1.1', $entry->getDomainName()); | ||
259 | } | ||
260 | |||
261 | public function testWithContentAndBadOgImage() | ||
262 | { | ||
263 | $tagger = $this->getTaggerMock(); | ||
264 | $tagger->expects($this->once()) | ||
265 | ->method('tag'); | ||
266 | |||
267 | $validator = $this->getValidator(); | ||
268 | $validator->expects($this->exactly(2)) | ||
269 | ->method('validate') | ||
270 | ->will($this->onConsecutiveCalls( | ||
271 | new ConstraintViolationList(), | ||
272 | new ConstraintViolationList([new ConstraintViolation('oops', 'oops', [], 'oops', 'url', 'https://')]) | ||
273 | )); | ||
274 | |||
275 | $graby = $this->getMockBuilder('Graby\Graby') | ||
276 | ->setMethods(['fetchContent']) | ||
277 | ->disableOriginalConstructor() | ||
278 | ->getMock(); | ||
279 | |||
280 | $graby->expects($this->any()) | ||
281 | ->method('fetchContent') | ||
282 | ->willReturn([ | ||
283 | 'html' => str_repeat('this is my content', 325), | ||
284 | 'title' => 'this is my title', | ||
285 | 'url' => 'http://1.1.1.1', | ||
286 | 'content_type' => 'text/html', | ||
287 | 'language' => 'fr', | ||
288 | 'status' => '200', | ||
289 | 'open_graph' => [ | ||
290 | 'og_title' => 'my OG title', | ||
291 | 'og_description' => 'OG desc', | ||
292 | 'og_image' => 'https://', | ||
293 | ], | ||
294 | ]); | ||
295 | |||
296 | $proxy = new ContentProxy($graby, $tagger, $validator, $this->getLogger(), $this->fetchingErrorMessage); | ||
297 | $entry = new Entry(new User()); | ||
298 | $proxy->updateEntry($entry, 'http://0.0.0.0'); | ||
299 | |||
300 | $this->assertEquals('http://1.1.1.1', $entry->getUrl()); | ||
301 | $this->assertEquals('this is my title', $entry->getTitle()); | ||
302 | $this->assertContains('this is my content', $entry->getContent()); | ||
303 | $this->assertEmpty($entry->getPreviewPicture()); | ||
207 | $this->assertEquals('text/html', $entry->getMimetype()); | 304 | $this->assertEquals('text/html', $entry->getMimetype()); |
208 | $this->assertEquals('fr', $entry->getLanguage()); | 305 | $this->assertEquals('fr', $entry->getLanguage()); |
209 | $this->assertEquals('200', $entry->getHttpStatus()); | 306 | $this->assertEquals('200', $entry->getHttpStatus()); |
@@ -217,7 +314,7 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase | |||
217 | $tagger->expects($this->once()) | 314 | $tagger->expects($this->once()) |
218 | ->method('tag'); | 315 | ->method('tag'); |
219 | 316 | ||
220 | $proxy = new ContentProxy((new Graby()), $tagger, $this->getLogger(), $this->fetchingErrorMessage); | 317 | $proxy = new ContentProxy((new Graby()), $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); |
221 | $entry = new Entry(new User()); | 318 | $entry = new Entry(new User()); |
222 | $proxy->updateEntry( | 319 | $proxy->updateEntry( |
223 | $entry, | 320 | $entry, |
@@ -259,7 +356,7 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase | |||
259 | $logHandler = new TestHandler(); | 356 | $logHandler = new TestHandler(); |
260 | $logger = new Logger('test', [$logHandler]); | 357 | $logger = new Logger('test', [$logHandler]); |
261 | 358 | ||
262 | $proxy = new ContentProxy((new Graby()), $tagger, $logger, $this->fetchingErrorMessage); | 359 | $proxy = new ContentProxy((new Graby()), $tagger, $this->getValidator(), $logger, $this->fetchingErrorMessage); |
263 | $entry = new Entry(new User()); | 360 | $entry = new Entry(new User()); |
264 | $proxy->updateEntry( | 361 | $proxy->updateEntry( |
265 | $entry, | 362 | $entry, |
@@ -294,7 +391,7 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase | |||
294 | $handler = new TestHandler(); | 391 | $handler = new TestHandler(); |
295 | $logger->pushHandler($handler); | 392 | $logger->pushHandler($handler); |
296 | 393 | ||
297 | $proxy = new ContentProxy((new Graby()), $tagger, $logger, $this->fetchingErrorMessage); | 394 | $proxy = new ContentProxy((new Graby()), $tagger, $this->getValidator(), $logger, $this->fetchingErrorMessage); |
298 | $entry = new Entry(new User()); | 395 | $entry = new Entry(new User()); |
299 | $proxy->updateEntry( | 396 | $proxy->updateEntry( |
300 | $entry, | 397 | $entry, |
@@ -331,7 +428,7 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase | |||
331 | ->method('tag') | 428 | ->method('tag') |
332 | ->will($this->throwException(new \Exception())); | 429 | ->will($this->throwException(new \Exception())); |
333 | 430 | ||
334 | $proxy = new ContentProxy((new Graby()), $tagger, $this->getLogger(), $this->fetchingErrorMessage); | 431 | $proxy = new ContentProxy((new Graby()), $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); |
335 | $entry = new Entry(new User()); | 432 | $entry = new Entry(new User()); |
336 | $proxy->updateEntry( | 433 | $proxy->updateEntry( |
337 | $entry, | 434 | $entry, |
@@ -371,7 +468,7 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase | |||
371 | $tagger->expects($this->once()) | 468 | $tagger->expects($this->once()) |
372 | ->method('tag'); | 469 | ->method('tag'); |
373 | 470 | ||
374 | $proxy = new ContentProxy((new Graby()), $tagger, $this->getLogger(), $this->fetchingErrorMessage); | 471 | $proxy = new ContentProxy((new Graby()), $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); |
375 | $entry = new Entry(new User()); | 472 | $entry = new Entry(new User()); |
376 | $proxy->updateEntry( | 473 | $proxy->updateEntry( |
377 | $entry, | 474 | $entry, |
@@ -413,4 +510,12 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase | |||
413 | { | 510 | { |
414 | return new NullLogger(); | 511 | return new NullLogger(); |
415 | } | 512 | } |
513 | |||
514 | private function getValidator() | ||
515 | { | ||
516 | return $this->getMockBuilder(RecursiveValidator::class) | ||
517 | ->setMethods(['validate']) | ||
518 | ->disableOriginalConstructor() | ||
519 | ->getMock(); | ||
520 | } | ||
416 | } | 521 | } |
diff --git a/tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php b/tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php index 9125f8dc..c02f9658 100644 --- a/tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php +++ b/tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php | |||
@@ -157,4 +157,29 @@ class DownloadImagesTest extends \PHPUnit_Framework_TestCase | |||
157 | 157 | ||
158 | $this->assertFalse($res, 'Absolute image can not be determined, so it will not be replaced'); | 158 | $this->assertFalse($res, 'Absolute image can not be determined, so it will not be replaced'); |
159 | } | 159 | } |
160 | |||
161 | public function testProcessRealImage() | ||
162 | { | ||
163 | $client = new Client(); | ||
164 | |||
165 | $mock = new Mock([ | ||
166 | new Response(200, ['content-type' => null], Stream::factory(file_get_contents(__DIR__.'/../fixtures/image-no-content-type.jpg'))), | ||
167 | ]); | ||
168 | |||
169 | $client->getEmitter()->attach($mock); | ||
170 | |||
171 | $logHandler = new TestHandler(); | ||
172 | $logger = new Logger('test', array($logHandler)); | ||
173 | |||
174 | $download = new DownloadImages($client, sys_get_temp_dir().'/wallabag_test', 'http://wallabag.io/', $logger); | ||
175 | |||
176 | $res = $download->processSingleImage( | ||
177 | 123, | ||
178 | 'https://cdn.theconversation.com/files/157200/article/width926/gsj2rjp2-1487348607.jpg', | ||
179 | 'https://theconversation.com/conversation-avec-gerald-bronner-ce-nest-pas-la-post-verite-qui-nous-menace-mais-lextension-de-notre-credulite-73089' | ||
180 | ); | ||
181 | |||
182 | $this->assertContains('http://wallabag.io/assets/images/9/b/9b0ead26/', $res, 'Content-Type was empty but data is ok for an image'); | ||
183 | $this->assertContains('DownloadImages: Checking extension (alternative)', $logHandler->getRecords()[3]['message']); | ||
184 | } | ||
160 | } | 185 | } |