]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Fixing tests
authorJeremy Benoist <jeremy.benoist@gmail.com>
Sun, 30 Oct 2016 10:27:09 +0000 (11:27 +0100)
committerJeremy Benoist <jeremy.benoist@gmail.com>
Sun, 30 Oct 2016 10:27:09 +0000 (11:27 +0100)
src/Wallabag/CoreBundle/Event/Subscriber/DownloadImagesSubscriber.php
src/Wallabag/CoreBundle/Helper/ContentProxy.php
src/Wallabag/CoreBundle/Helper/DownloadImages.php
tests/Wallabag/CoreBundle/Event/Listener/LocaleListenerTest.php [moved from tests/Wallabag/CoreBundle/EventListener/LocaleListenerTest.php with 96% similarity]
tests/Wallabag/CoreBundle/Event/Listener/UserLocaleListenerTest.php [moved from tests/Wallabag/CoreBundle/EventListener/UserLocaleListenerTest.php with 93% similarity]
tests/Wallabag/CoreBundle/Event/Subscriber/TablePrefixSubscriberTest.php [moved from tests/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriberTest.php with 97% similarity]
tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php

index 654edf31078184e1e13e46dba4b4de85a966e596..09f8e9119767d5f9870ff76091e42a4a9b6c7d2b 100644 (file)
@@ -49,22 +49,23 @@ class DownloadImagesSubscriber implements EventSubscriber
             return;
         }
 
-        $em = $args->getEntityManager();
+        $config = new $this->configClass();
+        $config->setEntityManager($args->getEntityManager());
 
         // field content has been updated
         if ($args->hasChangedField('content')) {
-            $html = $this->downloadImages($em, $entity);
+            $html = $this->downloadImages($config, $entity);
 
-            if (null !== $html) {
+            if (false !== $html) {
                 $args->setNewValue('content', $html);
             }
         }
 
         // field preview picture has been updated
         if ($args->hasChangedField('previewPicture')) {
-            $previewPicture = $this->downloadPreviewImage($em, $entity);
+            $previewPicture = $this->downloadPreviewImage($config, $entity);
 
-            if (null !== $previewPicture) {
+            if (false !== $previewPicture) {
                 $entity->setPreviewPicture($previewPicture);
             }
         }
@@ -88,17 +89,25 @@ class DownloadImagesSubscriber implements EventSubscriber
 
         // update all images inside the html
         $html = $this->downloadImages($config, $entity);
-        if (null !== $html) {
+        if (false !== $html) {
             $entity->setContent($html);
         }
 
         // update preview picture
         $previewPicture = $this->downloadPreviewImage($config, $entity);
-        if (null !== $previewPicture) {
+        if (false !== $previewPicture) {
             $entity->setPreviewPicture($previewPicture);
         }
     }
 
+    /**
+     * Download all images from the html.
+     *
+     * @param Config $config
+     * @param Entry  $entry
+     *
+     * @return string|false False in case of async
+     */
     public function downloadImages(Config $config, Entry $entry)
     {
         // if ($config->get('download_images_with_rabbitmq')) {
@@ -113,6 +122,14 @@ class DownloadImagesSubscriber implements EventSubscriber
         );
     }
 
+    /**
+     * Download the preview picture.
+     *
+     * @param Config $config
+     * @param Entry  $entry
+     *
+     * @return string|false False in case of async
+     */
     public function downloadPreviewImage(Config $config, Entry $entry)
     {
         // if ($config->get('download_images_with_rabbitmq')) {
index d90d3dc8caf1d5938e0c808ebd5fa5089c0528ba..1986ab334f2e6d9dd520a8a8adabc977a45246c1 100644 (file)
@@ -65,6 +65,7 @@ class ContentProxy
 
         $entry->setUrl($content['url'] ?: $url);
         $entry->setTitle($title);
+        $entry->setContent($html);
 
         $entry->setLanguage($content['language']);
         $entry->setMimetype($content['content_type']);
@@ -75,8 +76,6 @@ class ContentProxy
             $entry->setDomainName($domainName);
         }
 
-        $entry->setContent($html);
-
         if (isset($content['open_graph']['og_image'])) {
             $entry->setPreviewPicture($content['open_graph']['og_image']);
         }
index 426cbe4812702d1f98eea306e83838b536b12155..004bb277515b1a436a761526a687c886b5f04082 100644 (file)
@@ -91,20 +91,23 @@ class DownloadImages
         // build image path
         $absolutePath = $this->getAbsoluteLink($url, $imagePath);
         if (false === $absolutePath) {
-            $this->logger->log('debug', 'Can not determine the absolute path for that image, skipping.');
+            $this->logger->log('error', 'Can not determine the absolute path for that image, skipping.');
 
             return false;
         }
 
-        $res = $this->client->get(
-            $absolutePath,
-            ['exceptions' => false]
-        );
+        try {
+            $res = $this->client->get($absolutePath);
+        } catch (\Exception $e) {
+            $this->logger->log('error', 'Can not retrieve image, skipping.', ['exception' => $e]);
+
+            return false;
+        }
 
         $ext = $this->mimeGuesser->guess($res->getHeader('content-type'));
         $this->logger->log('debug', 'Checking extension', ['ext' => $ext, 'header' => $res->getHeader('content-type')]);
-        if (!in_array($ext, ['jpeg', 'jpg', 'gif', 'png'])) {
-            $this->logger->log('debug', 'Processed image with not allowed extension. Skipping '.$imagePath);
+        if (!in_array($ext, ['jpeg', 'jpg', 'gif', 'png'], true)) {
+            $this->logger->log('error', 'Processed image with not allowed extension. Skipping '.$imagePath);
 
             return false;
         }
@@ -117,7 +120,7 @@ class DownloadImages
             $im = false;
         }
 
-        if ($im === false) {
+        if (false === $im) {
             $this->logger->log('error', 'Error while regenerating image', ['path' => $localPath]);
 
             return false;
@@ -193,6 +196,8 @@ class DownloadImages
             return $absolute->get_uri();
         }
 
+        $this->logger->log('error', 'Can not make an absolute link', ['base' => $base, 'url' => $url]);
+
         return false;
     }
 }
similarity index 96%
rename from tests/Wallabag/CoreBundle/EventListener/LocaleListenerTest.php
rename to tests/Wallabag/CoreBundle/Event/Listener/LocaleListenerTest.php
index 078bb69aed905d75847a87525785a649cfa724aa..84a54d3abfcb8ec7b682b3c52ecf06159306ef32 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 
-namespace Tests\Wallabag\CoreBundle\EventListener;
+namespace Tests\Wallabag\CoreBundle\Event\Listener;
 
 use Symfony\Component\EventDispatcher\EventDispatcher;
 use Symfony\Component\HttpFoundation\Request;
@@ -9,7 +9,7 @@ use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
 use Symfony\Component\HttpKernel\Event\GetResponseEvent;
 use Symfony\Component\HttpKernel\HttpKernelInterface;
 use Symfony\Component\HttpKernel\KernelEvents;
-use Wallabag\CoreBundle\EventListener\LocaleListener;
+use Wallabag\CoreBundle\Event\Listener\LocaleListener;
 
 class LocaleListenerTest extends \PHPUnit_Framework_TestCase
 {
similarity index 93%
rename from tests/Wallabag/CoreBundle/EventListener/UserLocaleListenerTest.php
rename to tests/Wallabag/CoreBundle/Event/Listener/UserLocaleListenerTest.php
index e9ac7c1dfcdbec8448b81e8222c8ced62b8093d2..45aecc63102327f653fdfac0b23dcb341dc4400a 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 
-namespace Tests\Wallabag\CoreBundle\EventListener;
+namespace Tests\Wallabag\CoreBundle\Event\Listener;
 
 use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\HttpFoundation\Session\Session;
@@ -8,7 +8,7 @@ use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
 use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
 use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
 use Wallabag\CoreBundle\Entity\Config;
-use Wallabag\CoreBundle\EventListener\UserLocaleListener;
+use Wallabag\CoreBundle\Event\Listener\UserLocaleListener;
 use Wallabag\UserBundle\Entity\User;
 
 class UserLocaleListenerTest extends \PHPUnit_Framework_TestCase
similarity index 97%
rename from tests/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriberTest.php
rename to tests/Wallabag/CoreBundle/Event/Subscriber/TablePrefixSubscriberTest.php
index 4ae76703320b78838c71c35a6cf27a70e4fe28db..b8cd0fad732d9c361a3081d05e417d5097950ff1 100644 (file)
@@ -1,11 +1,11 @@
 <?php
 
-namespace Tests\Wallabag\CoreBundle\Subscriber;
+namespace Tests\Wallabag\CoreBundle\Event\Subscriber;
 
 use Doctrine\Common\EventManager;
 use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
 use Doctrine\ORM\Mapping\ClassMetadata;
-use Wallabag\CoreBundle\Subscriber\TablePrefixSubscriber;
+use Wallabag\CoreBundle\Event\Subscriber\TablePrefixSubscriber;
 
 class TablePrefixSubscriberTest extends \PHPUnit_Framework_TestCase
 {
index 0273693ec65605f2bfb038d83fe953f788dab7e1..e000d6817b55e8c6792ac2034589910605835bfe 100644 (file)
@@ -83,6 +83,25 @@ class DownloadImagesTest extends \PHPUnit_Framework_TestCase
         $this->assertContains('/assets/images/4/2/4258f71e/ebe60399.'.$extension, $res);
     }
 
+    public function testProcessSingleImageWithBadUrl()
+    {
+        $client = new Client();
+
+        $mock = new Mock([
+            new Response(404, []),
+        ]);
+
+        $client->getEmitter()->attach($mock);
+
+        $logHandler = new TestHandler();
+        $logger = new Logger('test', array($logHandler));
+
+        $download = new DownloadImages($client, sys_get_temp_dir().'/wallabag_test', $logger);
+        $res = $download->processSingleImage('T9qgcHc.jpg', 'http://imgur.com/gallery/WxtWY');
+
+        $this->assertFalse($res, 'Image can not be found, so it will not be replaced');
+    }
+
     public function testProcessSingleImageWithBadImage()
     {
         $client = new Client();