diff options
7 files changed, 398 insertions, 23 deletions
diff --git a/app/DoctrineMigrations/Version20190401105353.php b/app/DoctrineMigrations/Version20190401105353.php new file mode 100644 index 00000000..d27962db --- /dev/null +++ b/app/DoctrineMigrations/Version20190401105353.php | |||
@@ -0,0 +1,42 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Application\Migrations; | ||
4 | |||
5 | use Doctrine\DBAL\Schema\Schema; | ||
6 | use Wallabag\CoreBundle\Doctrine\WallabagMigration; | ||
7 | |||
8 | /** | ||
9 | * Add hashed_url in entry. | ||
10 | */ | ||
11 | class Version20190401105353 extends WallabagMigration | ||
12 | { | ||
13 | /** | ||
14 | * @param Schema $schema | ||
15 | */ | ||
16 | public function up(Schema $schema) | ||
17 | { | ||
18 | $entryTable = $schema->getTable($this->getTable('entry')); | ||
19 | |||
20 | $this->skipIf($entryTable->hasColumn('hashed_url'), 'It seems that you already played this migration.'); | ||
21 | |||
22 | $entryTable->addColumn('hashed_url', 'text', [ | ||
23 | 'length' => 40, | ||
24 | 'notnull' => false, | ||
25 | ]); | ||
26 | |||
27 | $entryTable->addIndex(['user_id', 'hashed_url'], 'hashed_url_user_id', [], ['lengths' => [null, 40]]); | ||
28 | } | ||
29 | |||
30 | /** | ||
31 | * @param Schema $schema | ||
32 | */ | ||
33 | public function down(Schema $schema) | ||
34 | { | ||
35 | $entryTable = $schema->getTable($this->getTable('entry')); | ||
36 | |||
37 | $this->skipIf(!$entryTable->hasColumn('hashed_url'), 'It seems that you already played this migration.'); | ||
38 | |||
39 | $entryTable->dropIndex('hashed_url_user_id'); | ||
40 | $entryTable->dropColumn('hashed_url'); | ||
41 | } | ||
42 | } | ||
diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index 5c850091..06520af9 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php | |||
@@ -27,8 +27,10 @@ class EntryRestController extends WallabagRestController | |||
27 | * @ApiDoc( | 27 | * @ApiDoc( |
28 | * 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"}, | 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"}, |
30 | * {"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"="DEPRECATED, use hashed_url instead"}, |
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"} | 31 | * {"name"="urls", "dataType"="string", "required"=false, "format"="An array of urls (?urls[]=http...&urls[]=http...)", "description"="DEPRECATED, use hashed_urls instead"}, |
32 | * {"name"="hashed_url", "dataType"="string", "required"=false, "format"="A hashed url", "description"="Hashed url using SHA1 to check if it exists"}, | ||
33 | * {"name"="hashed_urls", "dataType"="string", "required"=false, "format"="An array of hashed urls (?hashed_urls[]=xxx...&hashed_urls[]=xxx...)", "description"="An array of hashed urls using SHA1 to check if they exist"} | ||
32 | * } | 34 | * } |
33 | * ) | 35 | * ) |
34 | * | 36 | * |
@@ -37,17 +39,30 @@ class EntryRestController extends WallabagRestController | |||
37 | public function getEntriesExistsAction(Request $request) | 39 | public function getEntriesExistsAction(Request $request) |
38 | { | 40 | { |
39 | $this->validateAuthentication(); | 41 | $this->validateAuthentication(); |
42 | $repo = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entry'); | ||
40 | 43 | ||
41 | $returnId = (null === $request->query->get('return_id')) ? false : (bool) $request->query->get('return_id'); | 44 | $returnId = (null === $request->query->get('return_id')) ? false : (bool) $request->query->get('return_id'); |
45 | |||
42 | $urls = $request->query->get('urls', []); | 46 | $urls = $request->query->get('urls', []); |
47 | $hashedUrls = $request->query->get('hashed_urls', []); | ||
43 | 48 | ||
44 | // handle multiple urls first | 49 | // handle multiple urls first |
50 | if (!empty($hashedUrls)) { | ||
51 | $results = []; | ||
52 | foreach ($hashedUrls as $hashedUrl) { | ||
53 | $res = $repo->findByHashedUrlAndUserId($hashedUrl, $this->getUser()->getId()); | ||
54 | |||
55 | $results[$hashedUrl] = $this->returnExistInformation($res, $returnId); | ||
56 | } | ||
57 | |||
58 | return $this->sendResponse($results); | ||
59 | } | ||
60 | |||
61 | // @deprecated, to be remove in 3.0 | ||
45 | if (!empty($urls)) { | 62 | if (!empty($urls)) { |
46 | $results = []; | 63 | $results = []; |
47 | foreach ($urls as $url) { | 64 | foreach ($urls as $url) { |
48 | $res = $this->getDoctrine() | 65 | $res = $repo->findByUrlAndUserId($url, $this->getUser()->getId()); |
49 | ->getRepository('WallabagCoreBundle:Entry') | ||
50 | ->findByUrlAndUserId($url, $this->getUser()->getId()); | ||
51 | 66 | ||
52 | $results[$url] = $this->returnExistInformation($res, $returnId); | 67 | $results[$url] = $this->returnExistInformation($res, $returnId); |
53 | } | 68 | } |
@@ -57,18 +72,21 @@ class EntryRestController extends WallabagRestController | |||
57 | 72 | ||
58 | // let's see if it is a simple url? | 73 | // let's see if it is a simple url? |
59 | $url = $request->query->get('url', ''); | 74 | $url = $request->query->get('url', ''); |
75 | $hashedUrl = $request->query->get('hashed_url', ''); | ||
60 | 76 | ||
61 | if (empty($url)) { | 77 | if (empty($url) && empty($hashedUrl)) { |
62 | throw $this->createAccessDeniedException('URL is empty?, logged user id: ' . $this->getUser()->getId()); | 78 | throw $this->createAccessDeniedException('URL is empty?, logged user id: ' . $this->getUser()->getId()); |
63 | } | 79 | } |
64 | 80 | ||
65 | $res = $this->getDoctrine() | 81 | $method = 'findByUrlAndUserId'; |
66 | ->getRepository('WallabagCoreBundle:Entry') | 82 | if (!empty($hashedUrl)) { |
67 | ->findByUrlAndUserId($url, $this->getUser()->getId()); | 83 | $method = 'findByHashedUrlAndUserId'; |
84 | $url = $hashedUrl; | ||
85 | } | ||
68 | 86 | ||
69 | $exists = $this->returnExistInformation($res, $returnId); | 87 | $res = $repo->$method($url, $this->getUser()->getId()); |
70 | 88 | ||
71 | return $this->sendResponse(['exists' => $exists]); | 89 | return $this->sendResponse(['exists' => $this->returnExistInformation($res, $returnId)]); |
72 | } | 90 | } |
73 | 91 | ||
74 | /** | 92 | /** |
diff --git a/src/Wallabag/CoreBundle/Command/GenerateUrlHashesCommand.php b/src/Wallabag/CoreBundle/Command/GenerateUrlHashesCommand.php new file mode 100644 index 00000000..45bd8c5f --- /dev/null +++ b/src/Wallabag/CoreBundle/Command/GenerateUrlHashesCommand.php | |||
@@ -0,0 +1,98 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\CoreBundle\Command; | ||
4 | |||
5 | use Doctrine\ORM\NoResultException; | ||
6 | use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; | ||
7 | use Symfony\Component\Console\Input\InputArgument; | ||
8 | use Symfony\Component\Console\Input\InputInterface; | ||
9 | use Symfony\Component\Console\Output\OutputInterface; | ||
10 | use Wallabag\UserBundle\Entity\User; | ||
11 | |||
12 | class GenerateUrlHashesCommand extends ContainerAwareCommand | ||
13 | { | ||
14 | /** @var OutputInterface */ | ||
15 | protected $output; | ||
16 | |||
17 | protected function configure() | ||
18 | { | ||
19 | $this | ||
20 | ->setName('wallabag:generate-hashed-urls') | ||
21 | ->setDescription('Generates hashed urls for each entry') | ||
22 | ->setHelp('This command helps you to generates hashes of the url of each entry, to check through API if an URL is already saved') | ||
23 | ->addArgument('username', InputArgument::OPTIONAL, 'User to process entries'); | ||
24 | } | ||
25 | |||
26 | protected function execute(InputInterface $input, OutputInterface $output) | ||
27 | { | ||
28 | $this->output = $output; | ||
29 | |||
30 | $username = (string) $input->getArgument('username'); | ||
31 | |||
32 | if ($username) { | ||
33 | try { | ||
34 | $user = $this->getUser($username); | ||
35 | $this->generateHashedUrls($user); | ||
36 | } catch (NoResultException $e) { | ||
37 | $output->writeln(sprintf('<error>User "%s" not found.</error>', $username)); | ||
38 | |||
39 | return 1; | ||
40 | } | ||
41 | } else { | ||
42 | $users = $this->getDoctrine()->getRepository('WallabagUserBundle:User')->findAll(); | ||
43 | |||
44 | $output->writeln(sprintf('Generating hashed urls for "%d" users', \count($users))); | ||
45 | |||
46 | foreach ($users as $user) { | ||
47 | $output->writeln(sprintf('Processing user: %s', $user->getUsername())); | ||
48 | $this->generateHashedUrls($user); | ||
49 | } | ||
50 | $output->writeln('Finished generated hashed urls'); | ||
51 | } | ||
52 | |||
53 | return 0; | ||
54 | } | ||
55 | |||
56 | /** | ||
57 | * @param User $user | ||
58 | */ | ||
59 | private function generateHashedUrls(User $user) | ||
60 | { | ||
61 | $em = $this->getContainer()->get('doctrine.orm.entity_manager'); | ||
62 | $repo = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entry'); | ||
63 | |||
64 | $entries = $repo->findByUser($user->getId()); | ||
65 | |||
66 | $i = 1; | ||
67 | foreach ($entries as $entry) { | ||
68 | $entry->setHashedUrl(hash('sha1', $entry->getUrl())); | ||
69 | $em->persist($entry); | ||
70 | |||
71 | if (0 === ($i % 20)) { | ||
72 | $em->flush(); | ||
73 | } | ||
74 | ++$i; | ||
75 | } | ||
76 | |||
77 | $em->flush(); | ||
78 | |||
79 | $this->output->writeln(sprintf('Generated hashed urls for user: %s', $user->getUserName())); | ||
80 | } | ||
81 | |||
82 | /** | ||
83 | * Fetches a user from its username. | ||
84 | * | ||
85 | * @param string $username | ||
86 | * | ||
87 | * @return \Wallabag\UserBundle\Entity\User | ||
88 | */ | ||
89 | private function getUser($username) | ||
90 | { | ||
91 | return $this->getDoctrine()->getRepository('WallabagUserBundle:User')->findOneByUserName($username); | ||
92 | } | ||
93 | |||
94 | private function getDoctrine() | ||
95 | { | ||
96 | return $this->getContainer()->get('doctrine'); | ||
97 | } | ||
98 | } | ||
diff --git a/src/Wallabag/CoreBundle/Entity/Entry.php b/src/Wallabag/CoreBundle/Entity/Entry.php index b3cfdc4a..c3fb87d2 100644 --- a/src/Wallabag/CoreBundle/Entity/Entry.php +++ b/src/Wallabag/CoreBundle/Entity/Entry.php | |||
@@ -25,7 +25,8 @@ use Wallabag\UserBundle\Entity\User; | |||
25 | * options={"collate"="utf8mb4_unicode_ci", "charset"="utf8mb4"}, | 25 | * options={"collate"="utf8mb4_unicode_ci", "charset"="utf8mb4"}, |
26 | * indexes={ | 26 | * indexes={ |
27 | * @ORM\Index(name="created_at", columns={"created_at"}), | 27 | * @ORM\Index(name="created_at", columns={"created_at"}), |
28 | * @ORM\Index(name="uid", columns={"uid"}) | 28 | * @ORM\Index(name="uid", columns={"uid"}), |
29 | * @ORM\Index(name="hashed_url_user_id", columns={"user_id", "hashed_url"}, options={"lengths"={null, 40}}) | ||
29 | * } | 30 | * } |
30 | * ) | 31 | * ) |
31 | * @ORM\HasLifecycleCallbacks() | 32 | * @ORM\HasLifecycleCallbacks() |
@@ -76,6 +77,13 @@ class Entry | |||
76 | private $url; | 77 | private $url; |
77 | 78 | ||
78 | /** | 79 | /** |
80 | * @var string | ||
81 | * | ||
82 | * @ORM\Column(name="hashed_url", type="string", length=40, nullable=true) | ||
83 | */ | ||
84 | private $hashedUrl; | ||
85 | |||
86 | /** | ||
79 | * @var bool | 87 | * @var bool |
80 | * | 88 | * |
81 | * @Exclude | 89 | * @Exclude |
@@ -316,6 +324,7 @@ class Entry | |||
316 | public function setUrl($url) | 324 | public function setUrl($url) |
317 | { | 325 | { |
318 | $this->url = $url; | 326 | $this->url = $url; |
327 | $this->hashedUrl = hash('sha1', $url); | ||
319 | 328 | ||
320 | return $this; | 329 | return $this; |
321 | } | 330 | } |
@@ -911,4 +920,24 @@ class Entry | |||
911 | { | 920 | { |
912 | return $this->originUrl; | 921 | return $this->originUrl; |
913 | } | 922 | } |
923 | |||
924 | /** | ||
925 | * @return string | ||
926 | */ | ||
927 | public function getHashedUrl() | ||
928 | { | ||
929 | return $this->hashedUrl; | ||
930 | } | ||
931 | |||
932 | /** | ||
933 | * @param mixed $hashedUrl | ||
934 | * | ||
935 | * @return Entry | ||
936 | */ | ||
937 | public function setHashedUrl($hashedUrl) | ||
938 | { | ||
939 | $this->hashedUrl = $hashedUrl; | ||
940 | |||
941 | return $this; | ||
942 | } | ||
914 | } | 943 | } |
diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index 45366623..f5089729 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php | |||
@@ -347,6 +347,30 @@ class EntryRepository extends EntityRepository | |||
347 | } | 347 | } |
348 | 348 | ||
349 | /** | 349 | /** |
350 | * Find an entry by its hashed url and its owner. | ||
351 | * If it exists, return the entry otherwise return false. | ||
352 | * | ||
353 | * @param string $hashedUrl Url hashed using sha1 | ||
354 | * @param int $userId | ||
355 | * | ||
356 | * @return Entry|bool | ||
357 | */ | ||
358 | public function findByHashedUrlAndUserId($hashedUrl, $userId) | ||
359 | { | ||
360 | $res = $this->createQueryBuilder('e') | ||
361 | ->where('e.hashedUrl = :hashed_url')->setParameter('hashed_url', $hashedUrl) | ||
362 | ->andWhere('e.user = :user_id')->setParameter('user_id', $userId) | ||
363 | ->getQuery() | ||
364 | ->getResult(); | ||
365 | |||
366 | if (\count($res)) { | ||
367 | return current($res); | ||
368 | } | ||
369 | |||
370 | return false; | ||
371 | } | ||
372 | |||
373 | /** | ||
350 | * Count all entries for a user. | 374 | * Count all entries for a user. |
351 | * | 375 | * |
352 | * @param int $userId | 376 | * @param int $userId |
diff --git a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php index 2151f587..8cc12ed3 100644 --- a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php +++ b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php | |||
@@ -971,33 +971,49 @@ class EntryRestControllerTest extends WallabagApiTestCase | |||
971 | $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \DateTime($content['starred_at']))->getTimestamp()); | 971 | $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \DateTime($content['starred_at']))->getTimestamp()); |
972 | } | 972 | } |
973 | 973 | ||
974 | public function testGetEntriesExistsWithReturnId() | 974 | public function dataForEntriesExistWithUrl() |
975 | { | 975 | { |
976 | $this->client->request('GET', '/api/entries/exists?url=http://0.0.0.0/entry2&return_id=1'); | 976 | $url = hash('sha1', 'http://0.0.0.0/entry2'); |
977 | 977 | ||
978 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | 978 | return [ |
979 | 979 | 'with_id' => [ | |
980 | $content = json_decode($this->client->getResponse()->getContent(), true); | 980 | 'url' => '/api/entries/exists?url=http://0.0.0.0/entry2&return_id=1', |
981 | 981 | 'expectedValue' => 2, | |
982 | // it returns a database id, we don't know it, so we only check it's greater than the lowest possible value | 982 | ], |
983 | $this->assertGreaterThan(1, $content['exists']); | 983 | 'without_id' => [ |
984 | 'url' => '/api/entries/exists?url=http://0.0.0.0/entry2', | ||
985 | 'expectedValue' => true, | ||
986 | ], | ||
987 | 'hashed_url_with_id' => [ | ||
988 | 'url' => '/api/entries/exists?hashed_url=' . $url . '&return_id=1', | ||
989 | 'expectedValue' => 2, | ||
990 | ], | ||
991 | 'hashed_url_without_id' => [ | ||
992 | 'url' => '/api/entries/exists?hashed_url=' . $url . '', | ||
993 | 'expectedValue' => true, | ||
994 | ], | ||
995 | ]; | ||
984 | } | 996 | } |
985 | 997 | ||
986 | public function testGetEntriesExistsWithoutReturnId() | 998 | /** |
999 | * @dataProvider dataForEntriesExistWithUrl | ||
1000 | */ | ||
1001 | public function testGetEntriesExists($url, $expectedValue) | ||
987 | { | 1002 | { |
988 | $this->client->request('GET', '/api/entries/exists?url=http://0.0.0.0/entry2'); | 1003 | $this->client->request('GET', $url); |
989 | 1004 | ||
990 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | 1005 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
991 | 1006 | ||
992 | $content = json_decode($this->client->getResponse()->getContent(), true); | 1007 | $content = json_decode($this->client->getResponse()->getContent(), true); |
993 | 1008 | ||
994 | $this->assertTrue($content['exists']); | 1009 | $this->assertSame($expectedValue, $content['exists']); |
995 | } | 1010 | } |
996 | 1011 | ||
997 | public function testGetEntriesExistsWithManyUrls() | 1012 | public function testGetEntriesExistsWithManyUrls() |
998 | { | 1013 | { |
999 | $url1 = 'http://0.0.0.0/entry2'; | 1014 | $url1 = 'http://0.0.0.0/entry2'; |
1000 | $url2 = 'http://0.0.0.0/entry10'; | 1015 | $url2 = 'http://0.0.0.0/entry10'; |
1016 | |||
1001 | $this->client->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2 . '&return_id=1'); | 1017 | $this->client->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2 . '&return_id=1'); |
1002 | 1018 | ||
1003 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | 1019 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
@@ -1027,6 +1043,38 @@ class EntryRestControllerTest extends WallabagApiTestCase | |||
1027 | $this->assertFalse($content[$url2]); | 1043 | $this->assertFalse($content[$url2]); |
1028 | } | 1044 | } |
1029 | 1045 | ||
1046 | public function testGetEntriesExistsWithManyUrlsHashed() | ||
1047 | { | ||
1048 | $url1 = 'http://0.0.0.0/entry2'; | ||
1049 | $url2 = 'http://0.0.0.0/entry10'; | ||
1050 | $this->client->request('GET', '/api/entries/exists?hashed_urls[]=' . hash('sha1', $url1) . '&hashed_urls[]=' . hash('sha1', $url2) . '&return_id=1'); | ||
1051 | |||
1052 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | ||
1053 | |||
1054 | $content = json_decode($this->client->getResponse()->getContent(), true); | ||
1055 | |||
1056 | $this->assertArrayHasKey(hash('sha1', $url1), $content); | ||
1057 | $this->assertArrayHasKey(hash('sha1', $url2), $content); | ||
1058 | $this->assertSame(2, $content[hash('sha1', $url1)]); | ||
1059 | $this->assertNull($content[hash('sha1', $url2)]); | ||
1060 | } | ||
1061 | |||
1062 | public function testGetEntriesExistsWithManyUrlsHashedReturnBool() | ||
1063 | { | ||
1064 | $url1 = 'http://0.0.0.0/entry2'; | ||
1065 | $url2 = 'http://0.0.0.0/entry10'; | ||
1066 | $this->client->request('GET', '/api/entries/exists?hashed_urls[]=' . hash('sha1', $url1) . '&hashed_urls[]=' . hash('sha1', $url2)); | ||
1067 | |||
1068 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | ||
1069 | |||
1070 | $content = json_decode($this->client->getResponse()->getContent(), true); | ||
1071 | |||
1072 | $this->assertArrayHasKey(hash('sha1', $url1), $content); | ||
1073 | $this->assertArrayHasKey(hash('sha1', $url2), $content); | ||
1074 | $this->assertTrue($content[hash('sha1', $url1)]); | ||
1075 | $this->assertFalse($content[hash('sha1', $url2)]); | ||
1076 | } | ||
1077 | |||
1030 | public function testGetEntriesExistsWhichDoesNotExists() | 1078 | public function testGetEntriesExistsWhichDoesNotExists() |
1031 | { | 1079 | { |
1032 | $this->client->request('GET', '/api/entries/exists?url=http://google.com/entry2'); | 1080 | $this->client->request('GET', '/api/entries/exists?url=http://google.com/entry2'); |
@@ -1038,6 +1086,17 @@ class EntryRestControllerTest extends WallabagApiTestCase | |||
1038 | $this->assertFalse($content['exists']); | 1086 | $this->assertFalse($content['exists']); |
1039 | } | 1087 | } |
1040 | 1088 | ||
1089 | public function testGetEntriesExistsWhichDoesNotExistsWithHashedUrl() | ||
1090 | { | ||
1091 | $this->client->request('GET', '/api/entries/exists?hashed_url=' . hash('sha1', 'http://google.com/entry2')); | ||
1092 | |||
1093 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | ||
1094 | |||
1095 | $content = json_decode($this->client->getResponse()->getContent(), true); | ||
1096 | |||
1097 | $this->assertFalse($content['exists']); | ||
1098 | } | ||
1099 | |||
1041 | public function testGetEntriesExistsWithNoUrl() | 1100 | public function testGetEntriesExistsWithNoUrl() |
1042 | { | 1101 | { |
1043 | $this->client->request('GET', '/api/entries/exists?url='); | 1102 | $this->client->request('GET', '/api/entries/exists?url='); |
@@ -1045,6 +1104,13 @@ class EntryRestControllerTest extends WallabagApiTestCase | |||
1045 | $this->assertSame(403, $this->client->getResponse()->getStatusCode()); | 1104 | $this->assertSame(403, $this->client->getResponse()->getStatusCode()); |
1046 | } | 1105 | } |
1047 | 1106 | ||
1107 | public function testGetEntriesExistsWithNoHashedUrl() | ||
1108 | { | ||
1109 | $this->client->request('GET', '/api/entries/exists?hashed_url='); | ||
1110 | |||
1111 | $this->assertSame(403, $this->client->getResponse()->getStatusCode()); | ||
1112 | } | ||
1113 | |||
1048 | public function testReloadEntryErrorWhileFetching() | 1114 | public function testReloadEntryErrorWhileFetching() |
1049 | { | 1115 | { |
1050 | $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager') | 1116 | $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager') |
diff --git a/tests/Wallabag/CoreBundle/Command/GenerateUrlHashesCommandTest.php b/tests/Wallabag/CoreBundle/Command/GenerateUrlHashesCommandTest.php new file mode 100644 index 00000000..17eed210 --- /dev/null +++ b/tests/Wallabag/CoreBundle/Command/GenerateUrlHashesCommandTest.php | |||
@@ -0,0 +1,98 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Tests\Wallabag\CoreBundle\Command; | ||
4 | |||
5 | use Symfony\Bundle\FrameworkBundle\Console\Application; | ||
6 | use Symfony\Component\Console\Tester\CommandTester; | ||
7 | use Tests\Wallabag\CoreBundle\WallabagCoreTestCase; | ||
8 | use Wallabag\CoreBundle\Command\GenerateUrlHashesCommand; | ||
9 | use Wallabag\CoreBundle\Entity\Entry; | ||
10 | |||
11 | class GenerateUrlHashesCommandTest extends WallabagCoreTestCase | ||
12 | { | ||
13 | public function testRunGenerateUrlHashesCommand() | ||
14 | { | ||
15 | $application = new Application($this->getClient()->getKernel()); | ||
16 | $application->add(new GenerateUrlHashesCommand()); | ||
17 | |||
18 | $command = $application->find('wallabag:generate-hashed-urls'); | ||
19 | |||
20 | $tester = new CommandTester($command); | ||
21 | $tester->execute([ | ||
22 | 'command' => $command->getName(), | ||
23 | ]); | ||
24 | |||
25 | $this->assertContains('Generating hashed urls for "3" users', $tester->getDisplay()); | ||
26 | $this->assertContains('Finished generated hashed urls', $tester->getDisplay()); | ||
27 | } | ||
28 | |||
29 | public function testRunGenerateUrlHashesCommandWithBadUsername() | ||
30 | { | ||
31 | $application = new Application($this->getClient()->getKernel()); | ||
32 | $application->add(new GenerateUrlHashesCommand()); | ||
33 | |||
34 | $command = $application->find('wallabag:generate-hashed-urls'); | ||
35 | |||
36 | $tester = new CommandTester($command); | ||
37 | $tester->execute([ | ||
38 | 'command' => $command->getName(), | ||
39 | 'username' => 'unknown', | ||
40 | ]); | ||
41 | |||
42 | $this->assertContains('User "unknown" not found', $tester->getDisplay()); | ||
43 | } | ||
44 | |||
45 | public function testRunGenerateUrlHashesCommandForUser() | ||
46 | { | ||
47 | $application = new Application($this->getClient()->getKernel()); | ||
48 | $application->add(new GenerateUrlHashesCommand()); | ||
49 | |||
50 | $command = $application->find('wallabag:generate-hashed-urls'); | ||
51 | |||
52 | $tester = new CommandTester($command); | ||
53 | $tester->execute([ | ||
54 | 'command' => $command->getName(), | ||
55 | 'username' => 'admin', | ||
56 | ]); | ||
57 | |||
58 | $this->assertContains('Generated hashed urls for user: admin', $tester->getDisplay()); | ||
59 | } | ||
60 | |||
61 | public function testGenerateUrls() | ||
62 | { | ||
63 | $url = 'http://www.lemonde.fr/sport/visuel/2017/05/05/rondelle-prison-blanchissage-comprendre-le-hockey-sur-glace_5122587_3242.html'; | ||
64 | $client = $this->getClient(); | ||
65 | $em = $client->getContainer()->get('doctrine.orm.entity_manager'); | ||
66 | |||
67 | $this->logInAs('admin'); | ||
68 | |||
69 | $user = $em->getRepository('WallabagUserBundle:User')->findOneById($this->getLoggedInUserId()); | ||
70 | |||
71 | $entry1 = new Entry($user); | ||
72 | $entry1->setUrl($url); | ||
73 | |||
74 | $em->persist($entry1); | ||
75 | $em->flush(); | ||
76 | |||
77 | $application = new Application($this->getClient()->getKernel()); | ||
78 | $application->add(new GenerateUrlHashesCommand()); | ||
79 | |||
80 | $command = $application->find('wallabag:generate-hashed-urls'); | ||
81 | |||
82 | $tester = new CommandTester($command); | ||
83 | $tester->execute([ | ||
84 | 'command' => $command->getName(), | ||
85 | 'username' => 'admin', | ||
86 | ]); | ||
87 | |||
88 | $this->assertContains('Generated hashed urls for user: admin', $tester->getDisplay()); | ||
89 | |||
90 | $entry = $em->getRepository('WallabagCoreBundle:Entry')->findOneByUrl($url); | ||
91 | |||
92 | $this->assertSame($entry->getHashedUrl(), hash('sha1', $url)); | ||
93 | |||
94 | $query = $em->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.url = :url'); | ||
95 | $query->setParameter('url', $url); | ||
96 | $query->execute(); | ||
97 | } | ||
98 | } | ||