From 2297d60f100effc1bf4300488a170a6bed3ae756 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Thu, 20 Oct 2016 22:49:46 +0200 Subject: [PATCH] =?UTF-8?q?If=20reload=20content=20failed,=20don=E2=80=99t?= =?UTF-8?q?=20update=20it?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In case user wants a fresh version of the current one and the website isn’t available, don’t erase it with a boring message saying wallabag wasn’t able to refresh the content. --- app/config/config.yml | 1 + .../CoreBundle/Controller/EntryController.php | 9 ++++ .../DependencyInjection/Configuration.php | 2 + .../WallabagCoreExtension.php | 1 + .../CoreBundle/Resources/config/services.yml | 2 +- .../Controller/EntryControllerTest.php | 42 ++++++++++++++++++- 6 files changed, 55 insertions(+), 2 deletions(-) diff --git a/app/config/config.yml b/app/config/config.yml index 5127c8cf7..a56cbdd9e 100644 --- a/app/config/config.yml +++ b/app/config/config.yml @@ -50,6 +50,7 @@ wallabag_core: rss_limit: 50 reading_speed: 1 cache_lifetime: 10 + fetching_error_message: "wallabag can't retrieve contents for this article. Please report this issue to us." wallabag_user: registration_enabled: "%fosuser_registration%" diff --git a/src/Wallabag/CoreBundle/Controller/EntryController.php b/src/Wallabag/CoreBundle/Controller/EntryController.php index 3b28e6350..97bb3d12f 100644 --- a/src/Wallabag/CoreBundle/Controller/EntryController.php +++ b/src/Wallabag/CoreBundle/Controller/EntryController.php @@ -330,6 +330,15 @@ class EntryController extends Controller $this->updateEntry($entry, 'entry_reloaded'); + // if refreshing entry failed, don't save it + if ($this->getParameter('wallabag_core.fetching_error_message') === $entry->getContent()) { + $bag = $this->get('session')->getFlashBag(); + $bag->clear(); + $bag->add('notice', 'flashes.entry.notice.entry_reloaded_failed'); + + return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()])); + } + $em = $this->getDoctrine()->getManager(); $em->persist($entry); $em->flush(); diff --git a/src/Wallabag/CoreBundle/DependencyInjection/Configuration.php b/src/Wallabag/CoreBundle/DependencyInjection/Configuration.php index d8141eea9..3a3da0246 100644 --- a/src/Wallabag/CoreBundle/DependencyInjection/Configuration.php +++ b/src/Wallabag/CoreBundle/DependencyInjection/Configuration.php @@ -39,6 +39,8 @@ class Configuration implements ConfigurationInterface ->integerNode('cache_lifetime') ->defaultValue(10) ->end() + ->scalarNode('fetching_error_message') + ->end() ->end() ; diff --git a/src/Wallabag/CoreBundle/DependencyInjection/WallabagCoreExtension.php b/src/Wallabag/CoreBundle/DependencyInjection/WallabagCoreExtension.php index 0cbde908d..b4992d545 100644 --- a/src/Wallabag/CoreBundle/DependencyInjection/WallabagCoreExtension.php +++ b/src/Wallabag/CoreBundle/DependencyInjection/WallabagCoreExtension.php @@ -23,6 +23,7 @@ class WallabagCoreExtension extends Extension $container->setParameter('wallabag_core.version', $config['version']); $container->setParameter('wallabag_core.paypal_url', $config['paypal_url']); $container->setParameter('wallabag_core.cache_lifetime', $config['cache_lifetime']); + $container->setParameter('wallabag_core.fetching_error_message', $config['fetching_error_message']); $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); $loader->load('services.yml'); diff --git a/src/Wallabag/CoreBundle/Resources/config/services.yml b/src/Wallabag/CoreBundle/Resources/config/services.yml index a4b727f42..614488a64 100644 --- a/src/Wallabag/CoreBundle/Resources/config/services.yml +++ b/src/Wallabag/CoreBundle/Resources/config/services.yml @@ -40,7 +40,7 @@ services: class: Graby\Graby arguments: - - error_message: "wallabag can't retrieve contents for this article. Please report this issue to us." + error_message: '%wallabag_core.fetching_error_message%' http_client: user_agents: 'lifehacker.com': 'PHP/5.2' diff --git a/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php b/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php index 9b03a5193..c742c6204 100644 --- a/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php +++ b/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php @@ -359,11 +359,51 @@ class EntryControllerTest extends WallabagCoreTestCase $content = $em ->getRepository('WallabagCoreBundle:Entry') - ->findByUrlAndUserId($this->url, $this->getLoggedInUserId()); + ->find($content->getId()); $this->assertNotEmpty($content->getContent()); } + /** + * @depends testPostNewOk + * + * This test will require an internet connection. + */ + public function testReloadWithFetchingFailed() + { + $this->logInAs('admin'); + $client = $this->getClient(); + + $em = $client->getContainer() + ->get('doctrine.orm.entity_manager'); + + $content = $em + ->getRepository('WallabagCoreBundle:Entry') + ->findByUrlAndUserId($this->url, $this->getLoggedInUserId()); + + // put a known failed url + $content->setUrl('http://0.0.0.0/failed.html'); + $em->persist($content); + $em->flush(); + + $client->request('GET', '/reload/'.$content->getId()); + + $this->assertEquals(302, $client->getResponse()->getStatusCode()); + + // force EntityManager to clear previous entity + // otherwise, retrieve the same entity will retrieve change from the previous request :0 + $em->clear(); + $newContent = $em + ->getRepository('WallabagCoreBundle:Entry') + ->find($content->getId()); + + $newContent->setUrl($this->url); + $em->persist($newContent); + $em->flush(); + + $this->assertNotEquals($client->getContainer()->getParameter('wallabag_core.fetching_error_message'), $newContent->getContent()); + } + public function testEdit() { $this->logInAs('admin');