From c579ce2306297674c56376a2ab5c8ba66a272253 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Mon, 1 Apr 2019 14:34:20 +0200 Subject: Some cleanup Also, do not run the hashed_url migration into a Doctrine migration --- app/DoctrineMigrations/Version20190401105353.php | 5 ----- .../ApiBundle/Controller/EntryRestController.php | 1 - .../CoreBundle/Command/GenerateUrlHashesCommand.php | 8 ++------ src/Wallabag/CoreBundle/Repository/EntryRepository.php | 6 +++--- .../ApiBundle/Controller/EntryRestControllerTest.php | 18 ++++++++++++++++++ 5 files changed, 23 insertions(+), 15 deletions(-) diff --git a/app/DoctrineMigrations/Version20190401105353.php b/app/DoctrineMigrations/Version20190401105353.php index c1c22053..94ebb1ab 100644 --- a/app/DoctrineMigrations/Version20190401105353.php +++ b/app/DoctrineMigrations/Version20190401105353.php @@ -24,11 +24,6 @@ class Version20190401105353 extends WallabagMigration 'notnull' => false, ]); - // sqlite doesn't have the MD5 function by default - if ('sqlite' !== $this->connection->getDatabasePlatform()->getName()) { - $this->addSql('UPDATE ' . $this->getTable('entry') . ' SET hashed_url = MD5(url)'); - } - $entryTable->addIndex(['user_id', 'hashed_url'], 'hashed_url_user_id'); } diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index 0ecf1a0e..ad43b1d4 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php @@ -52,7 +52,6 @@ class EntryRestController extends WallabagRestController foreach ($hashedUrls as $hashedUrl) { $res = $repo->findByHashedUrlAndUserId($hashedUrl, $this->getUser()->getId()); - // $results[$url] = $this->returnExistInformation($res, $returnId); $results[$hashedUrl] = $this->returnExistInformation($res, $returnId); } diff --git a/src/Wallabag/CoreBundle/Command/GenerateUrlHashesCommand.php b/src/Wallabag/CoreBundle/Command/GenerateUrlHashesCommand.php index 685e1672..45bd8c5f 100644 --- a/src/Wallabag/CoreBundle/Command/GenerateUrlHashesCommand.php +++ b/src/Wallabag/CoreBundle/Command/GenerateUrlHashesCommand.php @@ -20,18 +20,14 @@ class GenerateUrlHashesCommand extends ContainerAwareCommand ->setName('wallabag:generate-hashed-urls') ->setDescription('Generates hashed urls for each entry') ->setHelp('This command helps you to generates hashes of the url of each entry, to check through API if an URL is already saved') - ->addArgument( - 'username', - InputArgument::OPTIONAL, - 'User to process entries' - ); + ->addArgument('username', InputArgument::OPTIONAL, 'User to process entries'); } protected function execute(InputInterface $input, OutputInterface $output) { $this->output = $output; - $username = $input->getArgument('username'); + $username = (string) $input->getArgument('username'); if ($username) { try { diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index 0c175abb..f5089729 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php @@ -350,15 +350,15 @@ class EntryRepository extends EntityRepository * Find an entry by its hashed url and its owner. * If it exists, return the entry otherwise return false. * - * @param $hashedUrl - * @param $userId + * @param string $hashedUrl Url hashed using sha1 + * @param int $userId * * @return Entry|bool */ public function findByHashedUrlAndUserId($hashedUrl, $userId) { $res = $this->createQueryBuilder('e') - ->where('e.hashedUrl = :hashed_url')->setParameter('hashed_url', urldecode($hashedUrl)) + ->where('e.hashedUrl = :hashed_url')->setParameter('hashed_url', $hashedUrl) ->andWhere('e.user = :user_id')->setParameter('user_id', $userId) ->getQuery() ->getResult(); diff --git a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php index 34de8ec8..8cc12ed3 100644 --- a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php +++ b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php @@ -1076,6 +1076,17 @@ class EntryRestControllerTest extends WallabagApiTestCase } public function testGetEntriesExistsWhichDoesNotExists() + { + $this->client->request('GET', '/api/entries/exists?url=http://google.com/entry2'); + + $this->assertSame(200, $this->client->getResponse()->getStatusCode()); + + $content = json_decode($this->client->getResponse()->getContent(), true); + + $this->assertFalse($content['exists']); + } + + public function testGetEntriesExistsWhichDoesNotExistsWithHashedUrl() { $this->client->request('GET', '/api/entries/exists?hashed_url=' . hash('sha1', 'http://google.com/entry2')); @@ -1087,6 +1098,13 @@ class EntryRestControllerTest extends WallabagApiTestCase } public function testGetEntriesExistsWithNoUrl() + { + $this->client->request('GET', '/api/entries/exists?url='); + + $this->assertSame(403, $this->client->getResponse()->getStatusCode()); + } + + public function testGetEntriesExistsWithNoHashedUrl() { $this->client->request('GET', '/api/entries/exists?hashed_url='); -- cgit v1.2.3