aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJérémy Benoist <j0k3r@users.noreply.github.com>2017-06-29 11:17:58 +0200
committerGitHub <noreply@github.com>2017-06-29 11:17:58 +0200
commit822c877949aff8ae57677671115f8f4fc69588d5 (patch)
treef7e76618cecab36447c95e7f7d34b135a01fa199
parent42708d1121fef12c84487247b170eb03083d5ffc (diff)
parent331e5b026189ff6433014659f32304b1706015e8 (diff)
downloadwallabag-822c877949aff8ae57677671115f8f4fc69588d5.tar.gz
wallabag-822c877949aff8ae57677671115f8f4fc69588d5.tar.zst
wallabag-822c877949aff8ae57677671115f8f4fc69588d5.zip
Merge pull request #3245 from wallabag/fix-bc
Avoid BC on exists endpoint
-rw-r--r--src/Wallabag/ApiBundle/Controller/EntryRestController.php34
-rw-r--r--tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php43
2 files changed, 69 insertions, 8 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php
index 768c4fdc..c7938633 100644
--- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php
+++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php
@@ -4,6 +4,7 @@ namespace Wallabag\ApiBundle\Controller;
4 4
5use Hateoas\Configuration\Route; 5use Hateoas\Configuration\Route;
6use Hateoas\Representation\Factory\PagerfantaFactory; 6use Hateoas\Representation\Factory\PagerfantaFactory;
7use JMS\Serializer\SerializationContext;
7use Nelmio\ApiDocBundle\Annotation\ApiDoc; 8use Nelmio\ApiDocBundle\Annotation\ApiDoc;
8use Symfony\Component\HttpKernel\Exception\HttpException; 9use Symfony\Component\HttpKernel\Exception\HttpException;
9use Symfony\Component\HttpFoundation\Request; 10use Symfony\Component\HttpFoundation\Request;
@@ -18,9 +19,14 @@ class EntryRestController extends WallabagRestController
18{ 19{
19 /** 20 /**
20 * Check if an entry exist by url. 21 * Check if an entry exist by url.
22 * Return ID if entry(ies) exist (and if you give the return_id parameter).
23 * Otherwise it returns false.
24 *
25 * @todo Remove that `return_id` in the next major release
21 * 26 *
22 * @ApiDoc( 27 * @ApiDoc(
23 * parameters={ 28 * parameters={
29 * {"name"="return_id", "dataType"="string", "required"=false, "format"="1 or 0", "description"="Set 1 if you want to retrieve ID in case entry(ies) exists, 0 by default"},
24 * {"name"="url", "dataType"="string", "required"=true, "format"="An url", "description"="Url to check if it exists"}, 30 * {"name"="url", "dataType"="string", "required"=true, "format"="An url", "description"="Url to check if it exists"},
25 * {"name"="urls", "dataType"="string", "required"=false, "format"="An array of urls (?urls[]=http...&urls[]=http...)", "description"="Urls (as an array) to check if it exists"} 31 * {"name"="urls", "dataType"="string", "required"=false, "format"="An array of urls (?urls[]=http...&urls[]=http...)", "description"="Urls (as an array) to check if it exists"}
26 * } 32 * }
@@ -32,6 +38,7 @@ class EntryRestController extends WallabagRestController
32 { 38 {
33 $this->validateAuthentication(); 39 $this->validateAuthentication();
34 40
41 $returnId = (null === $request->query->get('return_id')) ? false : (bool) $request->query->get('return_id');
35 $urls = $request->query->get('urls', []); 42 $urls = $request->query->get('urls', []);
36 43
37 // handle multiple urls first 44 // handle multiple urls first
@@ -42,7 +49,7 @@ class EntryRestController extends WallabagRestController
42 ->getRepository('WallabagCoreBundle:Entry') 49 ->getRepository('WallabagCoreBundle:Entry')
43 ->findByUrlAndUserId($url, $this->getUser()->getId()); 50 ->findByUrlAndUserId($url, $this->getUser()->getId());
44 51
45 $results[$url] = $res instanceof Entry ? $res->getId() : false; 52 $results[$url] = $this->returnExistInformation($res, $returnId);
46 } 53 }
47 54
48 return $this->sendResponse($results); 55 return $this->sendResponse($results);
@@ -59,7 +66,7 @@ class EntryRestController extends WallabagRestController
59 ->getRepository('WallabagCoreBundle:Entry') 66 ->getRepository('WallabagCoreBundle:Entry')
60 ->findByUrlAndUserId($url, $this->getUser()->getId()); 67 ->findByUrlAndUserId($url, $this->getUser()->getId());
61 68
62 $exists = $res instanceof Entry ? $res->getId() : false; 69 $exists = $this->returnExistInformation($res, $returnId);
63 70
64 return $this->sendResponse(['exists' => $exists]); 71 return $this->sendResponse(['exists' => $exists]);
65 } 72 }
@@ -617,7 +624,11 @@ class EntryRestController extends WallabagRestController
617 */ 624 */
618 private function sendResponse($data) 625 private function sendResponse($data)
619 { 626 {
620 $json = $this->get('serializer')->serialize($data, 'json'); 627 // https://github.com/schmittjoh/JMSSerializerBundle/issues/293
628 $context = new SerializationContext();
629 $context->setSerializeNull(true);
630
631 $json = $this->get('serializer')->serialize($data, 'json', $context);
621 632
622 return (new JsonResponse())->setJson($json); 633 return (new JsonResponse())->setJson($json);
623 } 634 }
@@ -694,4 +705,21 @@ class EntryRestController extends WallabagRestController
694 // entry saved, dispatch event about it! 705 // entry saved, dispatch event about it!
695 $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); 706 $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry));
696 } 707 }
708
709 /**
710 * Return information about the entry if it exist and depending on the id or not.
711 *
712 * @param Entry|null $entry
713 * @param bool $returnId
714 *
715 * @return bool|int
716 */
717 private function returnExistInformation($entry, $returnId)
718 {
719 if ($returnId) {
720 return $entry instanceof Entry ? $entry->getId() : null;
721 }
722
723 return $entry instanceof Entry;
724 }
697} 725}
diff --git a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php
index 067aed2c..c0391d87 100644
--- a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php
+++ b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php
@@ -759,21 +759,54 @@ class EntryRestControllerTest extends WallabagApiTestCase
759 $this->assertEquals(true, $content['is_starred']); 759 $this->assertEquals(true, $content['is_starred']);
760 } 760 }
761 761
762 public function testGetEntriesExists() 762 public function dataForEntriesExistWithUrl()
763 { 763 {
764 $this->client->request('GET', '/api/entries/exists?url=http://0.0.0.0/entry2'); 764 return [
765 'with_id' => [
766 'url' => '/api/entries/exists?url=http://0.0.0.0/entry2&return_id=1',
767 'expectedValue' => 2,
768 ],
769 'without_id' => [
770 'url' => '/api/entries/exists?url=http://0.0.0.0/entry2',
771 'expectedValue' => true,
772 ],
773 ];
774 }
775
776 /**
777 * @dataProvider dataForEntriesExistWithUrl
778 */
779 public function testGetEntriesExists($url, $expectedValue)
780 {
781 $this->client->request('GET', $url);
765 782
766 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 783 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
767 784
768 $content = json_decode($this->client->getResponse()->getContent(), true); 785 $content = json_decode($this->client->getResponse()->getContent(), true);
769 786
770 $this->assertEquals(2, $content['exists']); 787 $this->assertSame($expectedValue, $content['exists']);
771 } 788 }
772 789
773 public function testGetEntriesExistsWithManyUrls() 790 public function testGetEntriesExistsWithManyUrls()
774 { 791 {
775 $url1 = 'http://0.0.0.0/entry2'; 792 $url1 = 'http://0.0.0.0/entry2';
776 $url2 = 'http://0.0.0.0/entry10'; 793 $url2 = 'http://0.0.0.0/entry10';
794 $this->client->request('GET', '/api/entries/exists?urls[]='.$url1.'&urls[]='.$url2.'&return_id=1');
795
796 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
797
798 $content = json_decode($this->client->getResponse()->getContent(), true);
799
800 $this->assertArrayHasKey($url1, $content);
801 $this->assertArrayHasKey($url2, $content);
802 $this->assertSame(2, $content[$url1]);
803 $this->assertNull($content[$url2]);
804 }
805
806 public function testGetEntriesExistsWithManyUrlsReturnBool()
807 {
808 $url1 = 'http://0.0.0.0/entry2';
809 $url2 = 'http://0.0.0.0/entry10';
777 $this->client->request('GET', '/api/entries/exists?urls[]='.$url1.'&urls[]='.$url2); 810 $this->client->request('GET', '/api/entries/exists?urls[]='.$url1.'&urls[]='.$url2);
778 811
779 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 812 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
@@ -782,8 +815,8 @@ class EntryRestControllerTest extends WallabagApiTestCase
782 815
783 $this->assertArrayHasKey($url1, $content); 816 $this->assertArrayHasKey($url1, $content);
784 $this->assertArrayHasKey($url2, $content); 817 $this->assertArrayHasKey($url2, $content);
785 $this->assertEquals(2, $content[$url1]); 818 $this->assertTrue($content[$url1]);
786 $this->assertEquals(false, $content[$url2]); 819 $this->assertFalse($content[$url2]);
787 } 820 }
788 821
789 public function testGetEntriesExistsWhichDoesNotExists() 822 public function testGetEntriesExistsWhichDoesNotExists()