From d1495dd0a456f0e35a09fb68679ee51f8d17bfe4 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Sun, 30 Oct 2016 21:30:45 +0100 Subject: [PATCH] Ability to enable/disable downloading images MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This will speed up the test suite because it won’t download everything when we add new entry… Add a custom test with downloading image enabled --- .../CoreBundle/Command/InstallCommand.php | 15 +++++++ .../DataFixtures/ORM/LoadSettingData.php | 15 +++++++ .../Subscriber/DownloadImagesSubscriber.php | 8 ++++ .../Controller/EntryControllerTest.php | 40 +++++++++++++++++++ 4 files changed, 78 insertions(+) diff --git a/src/Wallabag/CoreBundle/Command/InstallCommand.php b/src/Wallabag/CoreBundle/Command/InstallCommand.php index 277f8524..aedccfe4 100644 --- a/src/Wallabag/CoreBundle/Command/InstallCommand.php +++ b/src/Wallabag/CoreBundle/Command/InstallCommand.php @@ -398,6 +398,21 @@ class InstallCommand extends ContainerAwareCommand 'value' => 'wallabag', 'section' => 'misc', ], + [ + 'name' => 'download_images_enabled', + 'value' => '0', + 'section' => 'image', + ], + [ + 'name' => 'download_images_with_rabbitmq', + 'value' => '0', + 'section' => 'image', + ], + [ + 'name' => 'download_images_with_redis', + 'value' => '0', + 'section' => 'image', + ], ]; foreach ($settings as $setting) { diff --git a/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadSettingData.php b/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadSettingData.php index 12f66c19..70a7a4ac 100644 --- a/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadSettingData.php +++ b/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadSettingData.php @@ -140,6 +140,21 @@ class LoadSettingData extends AbstractFixture implements OrderedFixtureInterface 'value' => 'wallabag', 'section' => 'misc', ], + [ + 'name' => 'download_images_enabled', + 'value' => '0', + 'section' => 'image', + ], + [ + 'name' => 'download_images_with_rabbitmq', + 'value' => '0', + 'section' => 'image', + ], + [ + 'name' => 'download_images_with_redis', + 'value' => '0', + 'section' => 'image', + ], ]; foreach ($settings as $setting) { diff --git a/src/Wallabag/CoreBundle/Event/Subscriber/DownloadImagesSubscriber.php b/src/Wallabag/CoreBundle/Event/Subscriber/DownloadImagesSubscriber.php index 0792653e..3f2d460c 100644 --- a/src/Wallabag/CoreBundle/Event/Subscriber/DownloadImagesSubscriber.php +++ b/src/Wallabag/CoreBundle/Event/Subscriber/DownloadImagesSubscriber.php @@ -52,6 +52,10 @@ class DownloadImagesSubscriber implements EventSubscriber $config = new $this->configClass(); $config->setEntityManager($args->getEntityManager()); + if (!$config->get('download_images_enabled')) { + return; + } + // field content has been updated if ($args->hasChangedField('content')) { $html = $this->downloadImages($config, $entity); @@ -87,6 +91,10 @@ class DownloadImagesSubscriber implements EventSubscriber $config = new $this->configClass(); $config->setEntityManager($args->getEntityManager()); + if (!$config->get('download_images_enabled')) { + return; + } + // update all images inside the html $html = $this->downloadImages($config, $entity); if (false !== $html) { diff --git a/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php b/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php index 05113650..514e9d89 100644 --- a/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php +++ b/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php @@ -836,4 +836,44 @@ class EntryControllerTest extends WallabagCoreTestCase $client->request('GET', '/share/'.$content->getUuid()); $this->assertEquals(404, $client->getResponse()->getStatusCode()); } + + public function testNewEntryWithDownloadImagesEnabled() + { + $this->logInAs('admin'); + $client = $this->getClient(); + + $url = 'http://www.20minutes.fr/montpellier/1952003-20161030-video-car-tombe-panne-rugbymen-perpignan-improvisent-melee-route'; + $client->getContainer()->get('craue_config')->set('download_images_enabled', 1); + + $crawler = $client->request('GET', '/new'); + + $this->assertEquals(200, $client->getResponse()->getStatusCode()); + + $form = $crawler->filter('form[name=entry]')->form(); + + $data = [ + 'entry[url]' => $url, + ]; + + $client->submit($form, $data); + + $this->assertEquals(302, $client->getResponse()->getStatusCode()); + + $em = $client->getContainer() + ->get('doctrine.orm.entity_manager'); + + $entry = $em + ->getRepository('WallabagCoreBundle:Entry') + ->findByUrlAndUserId($url, $this->getLoggedInUserId()); + + $this->assertInstanceOf('Wallabag\CoreBundle\Entity\Entry', $entry); + $this->assertEquals($url, $entry->getUrl()); + $this->assertContains('Perpignan', $entry->getTitle()); + $this->assertContains('assets/images/8/e/8ec9229a/d9bc0fcd.jpeg', $entry->getContent()); + + $em->remove($entry); + $em->flush(); + + $client->getContainer()->get('craue_config')->set('download_images_enabled', 0); + } } -- 2.41.0