From: Thomas Citharel Date: Wed, 10 May 2017 07:38:55 +0000 (+0200) Subject: Merge pull request #3095 from aaa2000/api-error-on-fail-fetch-content X-Git-Tag: 2.2.3~5 X-Git-Url: https://git.immae.eu/?a=commitdiff_plain;h=91ba9a59754b09f0713161567d36c959c2aa5ffe;hp=-c;p=github%2Fwallabag%2Fwallabag.git Merge pull request #3095 from aaa2000/api-error-on-fail-fetch-content Create a new entry via API even when its content can't be retrieved --- 91ba9a59754b09f0713161567d36c959c2aa5ffe diff --combined src/Wallabag/ApiBundle/Controller/EntryRestController.php index c544815e,e9e1aca3..54c1747c --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php @@@ -98,13 -98,12 +98,13 @@@ class EntryRestController extends Walla $tags = $request->query->get('tags', ''); $since = $request->query->get('since', 0); + /** @var \Pagerfanta\Pagerfanta $pager */ $pager = $this->getDoctrine() ->getRepository('WallabagCoreBundle:Entry') ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order, $since, $tags); - $pager->setCurrentPage($page); $pager->setMaxPerPage($perPage); + $pager->setCurrentPage($page); $pagerfantaFactory = new PagerfantaFactory('page', 'perPage'); $paginatedCollection = $pagerfantaFactory->createRepresentation( @@@ -200,10 -199,19 +200,19 @@@ $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)) { diff --combined tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php index 63d70bd9,deafd1fa..0a65f9ce --- a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php +++ b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php @@@ -4,6 -4,7 +4,7 @@@ namespace Tests\Wallabag\ApiBundle\Cont use Tests\Wallabag\ApiBundle\WallabagApiTestCase; use Wallabag\CoreBundle\Entity\Tag; + use Wallabag\CoreBundle\Helper\ContentProxy; class EntryRestControllerTest extends WallabagApiTestCase { @@@ -156,22 -157,6 +157,22 @@@ $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); } + public function testGetEntriesOnPageTwo() + { + $this->client->request('GET', '/api/entries', [ + 'page' => 2, + 'perPage' => 2, + ]); + + $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); + + $content = json_decode($this->client->getResponse()->getContent(), true); + + $this->assertGreaterThanOrEqual(0, $content['total']); + $this->assertEquals(2, $content['page']); + $this->assertEquals(2, $content['limit']); + } + public function testGetStarredEntries() { $this->client->request('GET', '/api/entries', ['starred' => 1, 'sort' => 'updated']); @@@ -375,6 -360,39 +376,39 @@@ $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', [