]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Merge pull request #3095 from aaa2000/api-error-on-fail-fetch-content
authorThomas Citharel <tcit@tcit.fr>
Wed, 10 May 2017 07:38:55 +0000 (09:38 +0200)
committerGitHub <noreply@github.com>
Wed, 10 May 2017 07:38:55 +0000 (09:38 +0200)
Create a new entry via API even when its content can't be retrieved

src/Wallabag/ApiBundle/Controller/EntryRestController.php
tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php

index c544815ebc7dee9bb308787940559ee376565af3..54c1747c5a9fb9412f3101892df651b12e34d07d 100644 (file)
@@ -200,10 +200,19 @@ class EntryRestController extends WallabagRestController
         $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($url, $this->getUser()->getId());
 
         if (false === $entry) {
-            $entry = $this->get('wallabag_core.content_proxy')->updateEntry(
-                new Entry($this->getUser()),
-                $url
-            );
+            $entry = new Entry($this->getUser());
+            try {
+                $entry = $this->get('wallabag_core.content_proxy')->updateEntry(
+                    $entry,
+                    $url
+                );
+            } catch (\Exception $e) {
+                $this->get('logger')->error('Error while saving an entry', [
+                    'exception' => $e,
+                    'entry' => $entry,
+                ]);
+                $entry->setUrl($url);
+            }
         }
 
         if (!is_null($title)) {
index 63d70bd9bb3b1c2ef51f59a6e1c05921e0cd4c88..0a65f9cee0628a78a38ab6b2fd7ce259c1383371 100644 (file)
@@ -4,6 +4,7 @@ namespace Tests\Wallabag\ApiBundle\Controller;
 
 use Tests\Wallabag\ApiBundle\WallabagApiTestCase;
 use Wallabag\CoreBundle\Entity\Tag;
+use Wallabag\CoreBundle\Helper\ContentProxy;
 
 class EntryRestControllerTest extends WallabagApiTestCase
 {
@@ -375,6 +376,39 @@ class EntryRestControllerTest extends WallabagApiTestCase
         $this->assertCount(2, $content['tags']);
     }
 
+    public function testPostEntryWhenFetchContentFails()
+    {
+        /** @var \Symfony\Component\DependencyInjection\Container $container */
+        $container = $this->client->getContainer();
+        $contentProxy = $this->getMockBuilder(ContentProxy::class)
+            ->disableOriginalConstructor()
+            ->setMethods(['updateEntry'])
+            ->getMock();
+        $contentProxy->expects($this->any())
+            ->method('updateEntry')
+            ->willThrowException(new \Exception('Test Fetch content fails'));
+        $container->set('wallabag_core.content_proxy', $contentProxy);
+
+        try {
+            $this->client->request('POST', '/api/entries.json', [
+                'url' => 'http://www.example.com/',
+            ]);
+
+            $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
+            $content = json_decode($this->client->getResponse()->getContent(), true);
+            $this->assertGreaterThan(0, $content['id']);
+            $this->assertEquals('http://www.example.com/', $content['url']);
+        } finally {
+            // Remove the created entry to avoid side effects on other tests
+            if (isset($content['id'])) {
+                $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
+                $entry = $em->getReference('WallabagCoreBundle:Entry', $content['id']);
+                $em->remove($entry);
+                $em->flush();
+            }
+        }
+    }
+
     public function testPostArchivedAndStarredEntry()
     {
         $this->client->request('POST', '/api/entries.json', [