]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Merge pull request #3831 from wallabag/fix/api-bad-client-id
authorJérémy Benoist <j0k3r@users.noreply.github.com>
Thu, 10 Jan 2019 16:03:03 +0000 (17:03 +0100)
committerGitHub <noreply@github.com>
Thu, 10 Jan 2019 16:03:03 +0000 (17:03 +0100)
Cast client id to avoid PG error

27 files changed:
src/Wallabag/ApiBundle/Controller/TagRestController.php
src/Wallabag/CoreBundle/DataFixtures/ORM/LoadEntryData.php
src/Wallabag/CoreBundle/DataFixtures/ORM/LoadTagData.php
src/Wallabag/CoreBundle/Helper/EntriesExport.php
src/Wallabag/CoreBundle/Repository/TagRepository.php
src/Wallabag/CoreBundle/Resources/views/themes/baggy/Entry/entries.html.twig
src/Wallabag/CoreBundle/Resources/views/themes/material/Entry/entries.html.twig
src/Wallabag/CoreBundle/Tools/Utils.php
src/Wallabag/ImportBundle/Consumer/AbstractConsumer.php
src/Wallabag/ImportBundle/Import/AbstractImport.php
src/Wallabag/ImportBundle/Import/BrowserImport.php
src/Wallabag/ImportBundle/Import/ChromeImport.php
src/Wallabag/ImportBundle/Import/FirefoxImport.php
src/Wallabag/ImportBundle/Import/InstapaperImport.php
src/Wallabag/ImportBundle/Import/PinboardImport.php
src/Wallabag/ImportBundle/Import/PocketImport.php
src/Wallabag/ImportBundle/Import/ReadabilityImport.php
src/Wallabag/ImportBundle/Import/WallabagImport.php
tests/Wallabag/ApiBundle/Controller/TagRestControllerTest.php
tests/Wallabag/CoreBundle/Tools/UtilsTest.php
tests/Wallabag/CoreBundle/Tools/samples/README [new file with mode: 0644]
tests/Wallabag/CoreBundle/Tools/samples/chinese.txt [new file with mode: 0644]
tests/Wallabag/CoreBundle/Tools/samples/cyrillic.txt
tests/Wallabag/CoreBundle/Tools/samples/greek.txt
tests/Wallabag/CoreBundle/Tools/samples/japanese.txt [new file with mode: 0644]
tests/Wallabag/CoreBundle/Tools/samples/korean.txt [new file with mode: 0644]
tests/Wallabag/CoreBundle/Tools/samples/latin.txt

index c6d6df6a91423718f5006cbace9f569eca6e7a7c..f3498f55a83f17bab2c881a75ea6918115dd1125 100644 (file)
@@ -46,12 +46,14 @@ class TagRestController extends WallabagRestController
         $this->validateAuthentication();
         $label = $request->get('tag', '');
 
-        $tag = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($label);
+        $tags = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findByLabelsAndUser([$label], $this->getUser()->getId());
 
-        if (empty($tag)) {
+        if (empty($tags)) {
             throw $this->createNotFoundException('Tag not found');
         }
 
+        $tag = $tags[0];
+
         $this->getDoctrine()
             ->getRepository('WallabagCoreBundle:Entry')
             ->removeTag($this->getUser()->getId(), $tag);
@@ -80,15 +82,7 @@ class TagRestController extends WallabagRestController
 
         $tagsLabels = $request->get('tags', '');
 
-        $tags = [];
-
-        foreach (explode(',', $tagsLabels) as $tagLabel) {
-            $tagEntity = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($tagLabel);
-
-            if (!empty($tagEntity)) {
-                $tags[] = $tagEntity;
-            }
-        }
+        $tags = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findByLabelsAndUser(explode(',', $tagsLabels), $this->getUser()->getId());
 
         if (empty($tags)) {
             throw $this->createNotFoundException('Tags not found');
@@ -120,6 +114,12 @@ class TagRestController extends WallabagRestController
     {
         $this->validateAuthentication();
 
+        $tagFromDb = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findByLabelsAndUser([$tag->getLabel()], $this->getUser()->getId());
+
+        if (empty($tagFromDb)) {
+            throw $this->createNotFoundException('Tag not found');
+        }
+
         $this->getDoctrine()
             ->getRepository('WallabagCoreBundle:Entry')
             ->removeTag($this->getUser()->getId(), $tag);
index 0e1510a29bf450f7c8e9e6067fb7c6ee2af84012..8e7a1d2af9503eef8543d22722bf9292528626ff 100644 (file)
@@ -14,97 +14,112 @@ class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface
      */
     public function load(ObjectManager $manager)
     {
-        $entry1 = new Entry($this->getReference('admin-user'));
-        $entry1->setUrl('http://0.0.0.0/entry1');
-        $entry1->setReadingTime(11);
-        $entry1->setDomainName('domain.io');
-        $entry1->setMimetype('text/html');
-        $entry1->setTitle('test title entry1');
-        $entry1->setContent('This is my content /o/');
-        $entry1->setLanguage('en');
-
-        $entry1->addTag($this->getReference('foo-tag'));
-        $entry1->addTag($this->getReference('baz-tag'));
-
-        $manager->persist($entry1);
-
-        $this->addReference('entry1', $entry1);
-
-        $entry2 = new Entry($this->getReference('admin-user'));
-        $entry2->setUrl('http://0.0.0.0/entry2');
-        $entry2->setReadingTime(1);
-        $entry2->setDomainName('domain.io');
-        $entry2->setMimetype('text/html');
-        $entry2->setTitle('test title entry2');
-        $entry2->setContent('This is my content /o/');
-        $entry2->setOriginUrl('ftp://oneftp.tld');
-        $entry2->setLanguage('fr');
-
-        $manager->persist($entry2);
-
-        $this->addReference('entry2', $entry2);
-
-        $entry3 = new Entry($this->getReference('bob-user'));
-        $entry3->setUrl('http://0.0.0.0/entry3');
-        $entry3->setReadingTime(1);
-        $entry3->setDomainName('domain.io');
-        $entry3->setMimetype('text/html');
-        $entry3->setTitle('test title entry3');
-        $entry3->setContent('This is my content /o/');
-        $entry3->setLanguage('en');
-
-        $entry3->addTag($this->getReference('foo-tag'));
-        $entry3->addTag($this->getReference('bar-tag'));
-
-        $manager->persist($entry3);
-
-        $this->addReference('entry3', $entry3);
-
-        $entry4 = new Entry($this->getReference('admin-user'));
-        $entry4->setUrl('http://0.0.0.0/entry4');
-        $entry4->setReadingTime(12);
-        $entry4->setDomainName('domain.io');
-        $entry4->setMimetype('text/html');
-        $entry4->setTitle('test title entry4');
-        $entry4->setContent('This is my content /o/');
-        $entry4->setLanguage('en');
-
-        $entry4->addTag($this->getReference('foo-tag'));
-        $entry4->addTag($this->getReference('bar-tag'));
-
-        $manager->persist($entry4);
-
-        $this->addReference('entry4', $entry4);
-
-        $entry5 = new Entry($this->getReference('admin-user'));
-        $entry5->setUrl('http://0.0.0.0/entry5');
-        $entry5->setReadingTime(12);
-        $entry5->setDomainName('domain.io');
-        $entry5->setMimetype('text/html');
-        $entry5->setTitle('test title entry5');
-        $entry5->setContent('This is my content /o/');
-        $entry5->setStarred(true);
-        $entry5->setLanguage('fr');
-        $entry5->setPreviewPicture('http://0.0.0.0/image.jpg');
-
-        $manager->persist($entry5);
-
-        $this->addReference('entry5', $entry5);
-
-        $entry6 = new Entry($this->getReference('admin-user'));
-        $entry6->setUrl('http://0.0.0.0/entry6');
-        $entry6->setReadingTime(12);
-        $entry6->setDomainName('domain.io');
-        $entry6->setMimetype('text/html');
-        $entry6->setTitle('test title entry6');
-        $entry6->setContent('This is my content /o/');
-        $entry6->setArchived(true);
-        $entry6->setLanguage('de');
-        $entry6->addTag($this->getReference('bar-tag'));
-
-        $manager->persist($entry6);
-
-        $this->addReference('entry6', $entry6);
+        $entries = [
+            'entry1' => [
+                'user' => 'admin-user',
+                'url' => 'http://0.0.0.0/entry1',
+                'reading_time' => 11,
+                'domain' => 'domain.io',
+                'mime' => 'text/html',
+                'title' => 'test title entry1',
+                'content' => 'This is my content /o/',
+                'language' => 'en',
+                'tags' => ['foo-tag', 'baz-tag'],
+            ],
+            'entry2' => [
+                'user' => 'admin-user',
+                'url' => 'http://0.0.0.0/entry2',
+                'reading_time' => 1,
+                'domain' => 'domain.io',
+                'mime' => 'text/html',
+                'title' => 'test title entry2',
+                'content' => 'This is my content /o/',
+                'origin' => 'ftp://oneftp.tld',
+                'language' => 'fr',
+            ],
+            'entry3' => [
+                'user' => 'bob-user',
+                'url' => 'http://0.0.0.0/entry3',
+                'reading_time' => 1,
+                'domain' => 'domain.io',
+                'mime' => 'text/html',
+                'title' => 'test title entry3',
+                'content' => 'This is my content /o/',
+                'language' => 'en',
+                'tags' => ['foo-tag', 'bar-tag', 'bob-tag'],
+            ],
+            'entry4' => [
+                'user' => 'admin-user',
+                'url' => 'http://0.0.0.0/entry4',
+                'reading_time' => 12,
+                'domain' => 'domain.io',
+                'mime' => 'text/html',
+                'title' => 'test title entry4',
+                'content' => 'This is my content /o/',
+                'language' => 'en',
+                'tags' => ['foo-tag', 'bar-tag'],
+            ],
+            'entry5' => [
+                'user' => 'admin-user',
+                'url' => 'http://0.0.0.0/entry5',
+                'reading_time' => 12,
+                'domain' => 'domain.io',
+                'mime' => 'text/html',
+                'title' => 'test title entry5',
+                'content' => 'This is my content /o/',
+                'language' => 'fr',
+                'starred' => true,
+                'preview' => 'http://0.0.0.0/image.jpg',
+            ],
+            'entry6' => [
+                'user' => 'admin-user',
+                'url' => 'http://0.0.0.0/entry6',
+                'reading_time' => 12,
+                'domain' => 'domain.io',
+                'mime' => 'text/html',
+                'title' => 'test title entry6',
+                'content' => 'This is my content /o/',
+                'language' => 'de',
+                'archived' => true,
+                'tags' => ['bar-tag'],
+            ],
+        ];
+
+        foreach ($entries as $reference => $item) {
+            $entry = new Entry($this->getReference($item['user']));
+            $entry->setUrl($item['url']);
+            $entry->setReadingTime($item['reading_time']);
+            $entry->setDomainName($item['domain']);
+            $entry->setMimetype($item['mime']);
+            $entry->setTitle($item['title']);
+            $entry->setContent($item['content']);
+            $entry->setLanguage($item['language']);
+
+            if (isset($item['tags'])) {
+                foreach ($item['tags'] as $tag) {
+                    $entry->addTag($this->getReference($tag));
+                }
+            }
+
+            if (isset($item['origin'])) {
+                $entry->setOriginUrl($item['origin']);
+            }
+
+            if (isset($item['starred'])) {
+                $entry->setStarred($item['starred']);
+            }
+
+            if (isset($item['archived'])) {
+                $entry->setArchived($item['archived']);
+            }
+
+            if (isset($item['preview'])) {
+                $entry->setPreviewPicture($item['preview']);
+            }
+
+            $manager->persist($entry);
+            $this->addReference($reference, $entry);
+        }
 
         $manager->flush();
     }
index 0ecfd18b55ddab8c00f1616586f78a9f576af5c3..485445c1b99d6061721a217e85d195f6814a9481 100644 (file)
@@ -14,33 +14,22 @@ class LoadTagData extends AbstractFixture implements OrderedFixtureInterface
      */
     public function load(ObjectManager $manager)
     {
-        $tag1 = new Tag();
-        $tag1->setLabel('foo bar');
-
-        $manager->persist($tag1);
-
-        $this->addReference('foo-bar-tag', $tag1);
-
-        $tag2 = new Tag();
-        $tag2->setLabel('bar');
-
-        $manager->persist($tag2);
-
-        $this->addReference('bar-tag', $tag2);
-
-        $tag3 = new Tag();
-        $tag3->setLabel('baz');
-
-        $manager->persist($tag3);
-
-        $this->addReference('baz-tag', $tag3);
-
-        $tag4 = new Tag();
-        $tag4->setLabel('foo');
-
-        $manager->persist($tag4);
-
-        $this->addReference('foo-tag', $tag4);
+        $tags = [
+            'foo-bar-tag' => 'foo bar', //tag used for EntryControllerTest
+            'bar-tag' => 'bar',
+            'baz-tag' => 'baz', // tag used for ExportControllerTest
+            'foo-tag' => 'foo',
+            'bob-tag' => 'bob', // tag used for TagRestControllerTest
+        ];
+
+        foreach ($tags as $reference => $label) {
+            $tag = new Tag();
+            $tag->setLabel($label);
+
+            $manager->persist($tag);
+
+            $this->addReference($reference, $tag);
+        }
 
         $manager->flush();
     }
index cbf1037bff60d53715be00325f0da4fa15e0f918..6082f6b9c47f444a6c5abad61dd167032d8f815b 100644 (file)
@@ -150,8 +150,6 @@ class EntriesExport
          */
 
         $book->setTitle($this->title);
-        // Could also be the ISBN number, prefered for published books, or a UUID.
-        $book->setIdentifier($this->title, EPub::IDENTIFIER_URI);
         // Not needed, but included for the example, Language is mandatory, but EPub defaults to "en". Use RFC3066 Language codes, such as "en", "da", "fr" etc.
         $book->setLanguage($this->language);
         $book->setDescription('Some articles saved on my wallabag');
@@ -174,6 +172,8 @@ class EntriesExport
             $book->setCoverImage('Cover.png', file_get_contents($this->logoPath), 'image/png');
         }
 
+        $entryIds = [];
+
         /*
          * Adding actual entries
          */
@@ -192,8 +192,14 @@ class EntriesExport
             $book->addChapter('Title', 'Title.html', $titlepage, true, EPub::EXTERNAL_REF_ADD);
             $chapter = $content_start . $entry->getContent() . $bookEnd;
             $book->addChapter($entry->getTitle(), htmlspecialchars($filename) . '.html', $chapter, true, EPub::EXTERNAL_REF_ADD);
+
+            $entryIds[] = $entry->getId();
         }
 
+        // Could also be the ISBN number, prefered for published books, or a UUID.
+        $hash = sha1(sprintf('%s:%s', $this->wallabagUrl, implode(',', $entryIds)));
+        $book->setIdentifier(sprintf('urn:wallabag:%s', $hash), EPub::IDENTIFIER_URI);
+
         $book->buildTOC();
 
         return Response::create(
index 3ae9d4141161c4443eb98f9bc6533695558e6143..8464a6a55004c27cce23c5a2283af01dd56c1a74 100644 (file)
@@ -3,6 +3,7 @@
 namespace Wallabag\CoreBundle\Repository;
 
 use Doctrine\ORM\EntityRepository;
+use Doctrine\ORM\QueryBuilder;
 use Wallabag\CoreBundle\Entity\Tag;
 
 class TagRepository extends EntityRepository
@@ -45,12 +46,8 @@ class TagRepository extends EntityRepository
      */
     public function findAllTags($userId)
     {
-        $ids = $this->createQueryBuilder('t')
+        $ids = $this->getQueryBuilderByUser($userId)
             ->select('t.id')
-            ->leftJoin('t.entries', 'e')
-            ->where('e.user = :userId')->setParameter('userId', $userId)
-            ->groupBy('t.id')
-            ->orderBy('t.slug')
             ->getQuery()
             ->getArrayResult();
 
@@ -71,18 +68,30 @@ class TagRepository extends EntityRepository
      */
     public function findAllFlatTagsWithNbEntries($userId)
     {
-        return $this->createQueryBuilder('t')
+        return $this->getQueryBuilderByUser($userId)
             ->select('t.id, t.label, t.slug, count(e.id) as nbEntries')
             ->distinct(true)
-            ->leftJoin('t.entries', 'e')
-            ->where('e.user = :userId')
-            ->groupBy('t.id')
-            ->orderBy('t.slug')
-            ->setParameter('userId', $userId)
             ->getQuery()
             ->getArrayResult();
     }
 
+    public function findByLabelsAndUser($labels, $userId)
+    {
+        $qb = $this->getQueryBuilderByUser($userId)
+            ->select('t.id');
+
+        $ids = $qb->andWhere($qb->expr()->in('t.label', $labels))
+            ->getQuery()
+            ->getArrayResult();
+
+        $tags = [];
+        foreach ($ids as $id) {
+            $tags[] = $this->find($id);
+        }
+
+        return $tags;
+    }
+
     /**
      * Used only in test case to get a tag for our entry.
      *
@@ -101,13 +110,9 @@ class TagRepository extends EntityRepository
 
     public function findForArchivedArticlesByUser($userId)
     {
-        $ids = $this->createQueryBuilder('t')
+        $ids = $this->getQueryBuilderByUser($userId)
             ->select('t.id')
-            ->leftJoin('t.entries', 'e')
-            ->where('e.user = :userId')->setParameter('userId', $userId)
             ->andWhere('e.isArchived = true')
-            ->groupBy('t.id')
-            ->orderBy('t.slug')
             ->getQuery()
             ->getArrayResult();
 
@@ -118,4 +123,20 @@ class TagRepository extends EntityRepository
 
         return $tags;
     }
+
+    /**
+     * Retrieve a sorted list of tags used by a user.
+     *
+     * @param int $userId
+     *
+     * @return QueryBuilder
+     */
+    private function getQueryBuilderByUser($userId)
+    {
+        return $this->createQueryBuilder('t')
+            ->leftJoin('t.entries', 'e')
+            ->where('e.user = :userId')->setParameter('userId', $userId)
+            ->groupBy('t.id')
+            ->orderBy('t.slug');
+    }
 }
index cfc6644b816d9f7f7c394cb9ec99f66fdc8e2925..832112be73a87fbf1859862739f7de4932f2f6ba 100644 (file)
@@ -99,8 +99,8 @@
             {% if craue_setting('export_epub') %}<li class="bold"><a class="waves-effect" href="{{ path('export_entries', { 'category': currentRoute, 'format': 'epub', 'tag' : currentTag }) }}">EPUB</a></li>{% endif %}
             {% if craue_setting('export_mobi') %}<li class="bold"><a class="waves-effect" href="{{ path('export_entries', { 'category': currentRoute, 'format': 'mobi', 'tag' : currentTag }) }}">MOBI</a></li>{% endif %}
             {% if craue_setting('export_pdf') %}<li class="bold"><a class="waves-effect" href="{{ path('export_entries', { 'category': currentRoute, 'format': 'pdf', 'tag' : currentTag }) }}">PDF</a></li>{% endif %}
-            {% if craue_setting('export_csv') %}<li class="bold"><a class="waves-effect" href="{{ path('export_entries', { 'category': currentRoute, 'format': 'json', 'tag' : currentTag }) }}">JSON</a></li>{% endif %}
-            {% if craue_setting('export_json') %}<li class="bold"><a class="waves-effect" href="{{ path('export_entries', { 'category': currentRoute, 'format': 'csv', 'tag' : currentTag }) }}">CSV</a></li>{% endif %}
+            {% if craue_setting('export_json') %}<li class="bold"><a class="waves-effect" href="{{ path('export_entries', { 'category': currentRoute, 'format': 'json', 'tag' : currentTag }) }}">JSON</a></li>{% endif %}
+            {% if craue_setting('export_csv') %}<li class="bold"><a class="waves-effect" href="{{ path('export_entries', { 'category': currentRoute, 'format': 'csv', 'tag' : currentTag }) }}">CSV</a></li>{% endif %}
             {% if craue_setting('export_txt') %}<li class="bold"><a class="waves-effect" href="{{ path('export_entries', { 'category': currentRoute, 'format': 'txt', 'tag' : currentTag }) }}">TXT</a></li>{% endif %}
             {% if craue_setting('export_xml') %}<li class="bold"><a class="waves-effect" href="{{ path('export_entries', { 'category': currentRoute, 'format': 'xml', 'tag' : currentTag }) }}">XML</a></li>{% endif %}
         </ul>
index a137f3c34895954001c1cbb15f1c492630223b8f..742dd330b4591e50b7da4c54858171de285f22d7 100644 (file)
@@ -68,8 +68,8 @@
             {% if craue_setting('export_epub') %}<li class="bold"><a class="waves-effect" href="{{ path('export_entries', { 'category': currentRoute, 'format': 'epub', 'tag' : currentTag }) }}">EPUB</a></li>{% endif %}
             {% if craue_setting('export_mobi') %}<li class="bold"><a class="waves-effect" href="{{ path('export_entries', { 'category': currentRoute, 'format': 'mobi', 'tag' : currentTag }) }}">MOBI</a></li>{% endif %}
             {% if craue_setting('export_pdf') %}<li class="bold"><a class="waves-effect" href="{{ path('export_entries', { 'category': currentRoute, 'format': 'pdf', 'tag' : currentTag }) }}">PDF</a></li>{% endif %}
-            {% if craue_setting('export_csv') %}<li class="bold"><a class="waves-effect" href="{{ path('export_entries', { 'category': currentRoute, 'format': 'json', 'tag' : currentTag }) }}">JSON</a></li>{% endif %}
-            {% if craue_setting('export_json') %}<li class="bold"><a class="waves-effect" href="{{ path('export_entries', { 'category': currentRoute, 'format': 'csv', 'tag' : currentTag }) }}">CSV</a></li>{% endif %}
+            {% if craue_setting('export_json') %}<li class="bold"><a class="waves-effect" href="{{ path('export_entries', { 'category': currentRoute, 'format': 'json', 'tag' : currentTag }) }}">JSON</a></li>{% endif %}
+            {% if craue_setting('export_csv') %}<li class="bold"><a class="waves-effect" href="{{ path('export_entries', { 'category': currentRoute, 'format': 'csv', 'tag' : currentTag }) }}">CSV</a></li>{% endif %}
             {% if craue_setting('export_txt') %}<li class="bold"><a class="waves-effect" href="{{ path('export_entries', { 'category': currentRoute, 'format': 'txt', 'tag' : currentTag }) }}">TXT</a></li>{% endif %}
             {% if craue_setting('export_xml') %}<li class="bold"><a class="waves-effect" href="{{ path('export_entries', { 'category': currentRoute, 'format': 'xml', 'tag' : currentTag }) }}">XML</a></li>{% endif %}
         </ul>
index 46bb1dc5c82441cd8277f00dc121eec84298697e..e56e251e5ab31aeda4a2451912017e27f8b1c486 100644 (file)
@@ -20,15 +20,14 @@ class Utils
     }
 
     /**
-     * For a given text, we calculate reading time for an article
-     * based on 200 words per minute.
+     * For a given text, we calculate reading time for an article based on 200 words per minute.
      *
-     * @param $text
+     * @param string $text
      *
      * @return float
      */
     public static function getReadingTime($text)
     {
-        return floor(\count(preg_split('~[^\p{L}\p{N}\']+~u', strip_tags($text))) / 200);
+        return floor(\count(preg_split('~([^\p{L}\p{N}\']+|(\p{Han}|\p{Hiragana}|\p{Katakana}|\p{Hangul}){1,2})~u', strip_tags($text))) / 200);
     }
 }
index b035f5cc5e83604cf2890dfa035a433a99478b49..e4bfbdf033807fca182b99343d6455d6361e451a 100644 (file)
@@ -52,6 +52,13 @@ abstract class AbstractConsumer
 
         $this->import->setUser($user);
 
+        if (false === $this->import->validateEntry($storedEntry)) {
+            $this->logger->warning('Entry is invalid', ['entry' => $storedEntry]);
+
+            // return true to skip message
+            return true;
+        }
+
         $entry = $this->import->parseEntry($storedEntry);
 
         if (null === $entry) {
index 58a234f46bd357dedf7abd8c14b9cc381b33df37..d39d71b6caf1e3b7def2c0b352b4ceeccdef0840 100644 (file)
@@ -118,6 +118,15 @@ abstract class AbstractImport implements ImportInterface
      */
     abstract public function parseEntry(array $importedEntry);
 
+    /**
+     * Validate that an entry is valid (like has some required keys, etc.).
+     *
+     * @param array $importedEntry
+     *
+     * @return bool
+     */
+    abstract public function validateEntry(array $importedEntry);
+
     /**
      * Fetch content from the ContentProxy (using graby).
      * If it fails return the given entry to be saved in all case (to avoid user to loose the content).
@@ -141,9 +150,9 @@ abstract class AbstractImport implements ImportInterface
     /**
      * Parse and insert all given entries.
      *
-     * @param $entries
+     * @param array $entries
      */
-    protected function parseEntries($entries)
+    protected function parseEntries(array $entries)
     {
         $i = 1;
         $entryToBeFlushed = [];
@@ -153,6 +162,10 @@ abstract class AbstractImport implements ImportInterface
                 $importedEntry = $this->setEntryAsRead($importedEntry);
             }
 
+            if (false === $this->validateEntry($importedEntry)) {
+                continue;
+            }
+
             $entry = $this->parseEntry($importedEntry);
 
             if (null === $entry) {
index 225f1791f5bfe6b6fdc04bba4a2d0abf9cc7c600..4678ae0c5133cfc79e0e18f95a242cab6cea7f5e 100644 (file)
@@ -149,9 +149,9 @@ abstract class BrowserImport extends AbstractImport
     /**
      * Parse and insert all given entries.
      *
-     * @param $entries
+     * @param array $entries
      */
-    protected function parseEntries($entries)
+    protected function parseEntries(array $entries)
     {
         $i = 1;
         $entryToBeFlushed = [];
index 09183abe47cbe321ed8067de34a9f816a6fde97c..eccee69869a1197c59bdf3e500953a4c6867d424 100644 (file)
@@ -30,6 +30,18 @@ class ChromeImport extends BrowserImport
         return 'import.chrome.description';
     }
 
+    /**
+     * {@inheritdoc}
+     */
+    public function validateEntry(array $importedEntry)
+    {
+        if (empty($importedEntry['url'])) {
+            return false;
+        }
+
+        return true;
+    }
+
     /**
      * {@inheritdoc}
      */
index 73269fe164289a11d794a0c5bde7e31a18d2340f..8999e3f3932240accb4096b519af13a31230bdbc 100644 (file)
@@ -30,6 +30,18 @@ class FirefoxImport extends BrowserImport
         return 'import.firefox.description';
     }
 
+    /**
+     * {@inheritdoc}
+     */
+    public function validateEntry(array $importedEntry)
+    {
+        if (empty($importedEntry['uri'])) {
+            return false;
+        }
+
+        return true;
+    }
+
     /**
      * {@inheritdoc}
      */
index e4f0970c0c3f41a531a9d548c46fdc175946e92c..5a18c7c0c6c82c0426ba8d2b436f6beed79b55bc 100644 (file)
@@ -105,6 +105,18 @@ class InstapaperImport extends AbstractImport
         return true;
     }
 
+    /**
+     * {@inheritdoc}
+     */
+    public function validateEntry(array $importedEntry)
+    {
+        if (empty($importedEntry['url'])) {
+            return false;
+        }
+
+        return true;
+    }
+
     /**
      * {@inheritdoc}
      */
index 110b046422ae3c25a7f5be34f59c12e4d11938b2..995d1f2ca99f7ddb0ab330f89fcbb3bb5ff59cf5 100644 (file)
@@ -80,6 +80,18 @@ class PinboardImport extends AbstractImport
         return true;
     }
 
+    /**
+     * {@inheritdoc}
+     */
+    public function validateEntry(array $importedEntry)
+    {
+        if (empty($importedEntry['href'])) {
+            return false;
+        }
+
+        return true;
+    }
+
     /**
      * {@inheritdoc}
      */
index c1b35b7ef028a0e261d9a072e867246eca4816eb..d364338996a652c83684d4d44ce39d960d77364d 100644 (file)
@@ -168,6 +168,18 @@ class PocketImport extends AbstractImport
         $this->client = $client;
     }
 
+    /**
+     * {@inheritdoc}
+     */
+    public function validateEntry(array $importedEntry)
+    {
+        if (empty($importedEntry['resolved_url']) && empty($importedEntry['given_url'])) {
+            return false;
+        }
+
+        return true;
+    }
+
     /**
      * {@inheritdoc}
      *
index 002b27f46b5b2ace08be9f89ad402672496a9c8a..a5f3798e0277f05b4167c6318be47a9cb5d603b7 100644 (file)
@@ -80,6 +80,18 @@ class ReadabilityImport extends AbstractImport
         return true;
     }
 
+    /**
+     * {@inheritdoc}
+     */
+    public function validateEntry(array $importedEntry)
+    {
+        if (empty($importedEntry['article__url'])) {
+            return false;
+        }
+
+        return true;
+    }
+
     /**
      * {@inheritdoc}
      */
index c64ccd64d196095ce8b9e3f913c4f856075f0fa3..350d06001f12202f6c6678b01b5f48da19773ace 100644 (file)
@@ -86,6 +86,18 @@ abstract class WallabagImport extends AbstractImport
         return $this;
     }
 
+    /**
+     * {@inheritdoc}
+     */
+    public function validateEntry(array $importedEntry)
+    {
+        if (empty($importedEntry['url'])) {
+            return false;
+        }
+
+        return true;
+    }
+
     /**
      * {@inheritdoc}
      */
index 430e548d204078178b42bbdc957b9e6ee3ab1ebc..9daa94cd5346bf0a99d8cc73e0366666d830f749 100644 (file)
@@ -7,6 +7,8 @@ use Wallabag\CoreBundle\Entity\Tag;
 
 class TagRestControllerTest extends WallabagApiTestCase
 {
+    private $otherUserTagLabel = 'bob';
+
     public function testGetUserTags()
     {
         $this->client->request('GET', '/api/tags.json');
@@ -19,17 +21,33 @@ class TagRestControllerTest extends WallabagApiTestCase
         $this->assertArrayHasKey('id', $content[0]);
         $this->assertArrayHasKey('label', $content[0]);
 
+        $tagLabels = array_map(function ($i) {
+            return $i['label'];
+        }, $content);
+
+        $this->assertNotContains($this->otherUserTagLabel, $tagLabels, 'There is a possible tag leak');
+
         return end($content);
     }
 
     public function testDeleteUserTag()
     {
+        $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
+        $entry = $this->client->getContainer()
+            ->get('doctrine.orm.entity_manager')
+            ->getRepository('WallabagCoreBundle:Entry')
+            ->findOneWithTags($this->user->getId());
+
+        $entry = $entry[0];
+
         $tagLabel = 'tagtest';
         $tag = new Tag();
         $tag->setLabel($tagLabel);
-
-        $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
         $em->persist($tag);
+
+        $entry->addTag($tag);
+
+        $em->persist($entry);
         $em->flush();
         $em->clear();
 
@@ -53,6 +71,16 @@ class TagRestControllerTest extends WallabagApiTestCase
         $this->assertNull($tag, $tagLabel . ' was removed because it begun an orphan tag');
     }
 
+    public function testDeleteOtherUserTag()
+    {
+        $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
+        $tag = $em->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($this->otherUserTagLabel);
+
+        $this->client->request('DELETE', '/api/tags/' . $tag->getId() . '.json');
+
+        $this->assertSame(404, $this->client->getResponse()->getStatusCode());
+    }
+
     public function dataForDeletingTagByLabel()
     {
         return [
@@ -112,6 +140,13 @@ class TagRestControllerTest extends WallabagApiTestCase
         $this->assertSame(404, $this->client->getResponse()->getStatusCode());
     }
 
+    public function testDeleteTagByLabelOtherUser()
+    {
+        $this->client->request('DELETE', '/api/tag/label.json', ['tag' => $this->otherUserTagLabel]);
+
+        $this->assertSame(404, $this->client->getResponse()->getStatusCode());
+    }
+
     /**
      * @dataProvider dataForDeletingTagByLabel
      */
@@ -180,4 +215,11 @@ class TagRestControllerTest extends WallabagApiTestCase
 
         $this->assertSame(404, $this->client->getResponse()->getStatusCode());
     }
+
+    public function testDeleteTagsByLabelOtherUser()
+    {
+        $this->client->request('DELETE', '/api/tags/label.json', ['tags' => $this->otherUserTagLabel]);
+
+        $this->assertSame(404, $this->client->getResponse()->getStatusCode());
+    }
 }
index 952d076d1b7c0f7a208fd561fb75a6d4da44ae11..c6ed74f07e35574e3432ab96ebd63f38ff107e23 100644 (file)
@@ -11,9 +11,9 @@ class UtilsTest extends TestCase
     /**
      * @dataProvider examples
      */
-    public function testCorrectWordsCountForDifferentLanguages($text, $expectedCount)
+    public function testCorrectWordsCountForDifferentLanguages($filename, $text, $expectedCount)
     {
-        static::assertSame((float) $expectedCount, Utils::getReadingTime($text));
+        static::assertSame((float) $expectedCount, Utils::getReadingTime($text), 'Reading time for: ' . $filename);
     }
 
     public function examples()
@@ -21,7 +21,17 @@ class UtilsTest extends TestCase
         $examples = [];
         $finder = (new Finder())->in(__DIR__ . '/samples');
         foreach ($finder->getIterator() as $file) {
-            $examples[] = [$file->getContents(), 1];
+            preg_match('/-----CONTENT-----\s*(.*?)\s*-----READING_TIME-----\s*(.*)/sx', $file->getContents(), $match);
+
+            if (3 !== \count($match)) {
+                throw new \Exception('Sample file "' . $file->getRelativePathname() . '" as wrong definition, see README.');
+            }
+
+            $examples[] = [
+                $file->getRelativePathname(),
+                $match[1], // content
+                $match[2], // reading time
+            ];
         }
 
         return $examples;
diff --git a/tests/Wallabag/CoreBundle/Tools/samples/README b/tests/Wallabag/CoreBundle/Tools/samples/README
new file mode 100644 (file)
index 0000000..e8f946c
--- /dev/null
@@ -0,0 +1,5 @@
+Defined language sample should use the following structure:
+
+-----CONTENT-----
+
+-----READING_TIME-----
diff --git a/tests/Wallabag/CoreBundle/Tools/samples/chinese.txt b/tests/Wallabag/CoreBundle/Tools/samples/chinese.txt
new file mode 100644 (file)
index 0000000..864603c
--- /dev/null
@@ -0,0 +1,10 @@
+-----CONTENT-----
+职然问讲念谷月挂大报住本読能录要褐込。料士纸木陈与兴组静终図问有。今観深车相环学俳健越増职県県多券报。雪月批导掲稿家缝城间真中崩図人连。前担写治芸面毎作似水州稿注球戦頃。済方宮安目垣強入料会先呼略。計定設負財作覧経己員事田事球岡示差学。最院書模婚金回禁朝船教任分禁検理慮宿。
+
+変送调指式真気交现上様女限宅复。禁业稿者普视想来木残止者済断式安。万致相领鉄再改界逮由竹式元最台変。済问活助库脳部风政京転说区変。文図化仙政常地里芸上褒前読望误记温政信土。惑育候当人万部逮重申結標番業望般。断瀬後社天打日資交献秀世覧第。補当編里身社記利件部夜中心掲大。
+
+时大栗夜测署市要纯京挙化済负品。天最场情算掲放故手茨指岛然渡活民年。第纯交一特问明室试賛际者建。论铜所常縄一広気特秋提公茶可満编旅相変権。
+
+兵线済来先决模入供定树希逮技鉄多连写塩。着刊禁浩歩人仕设谢争关周徒今高。十育幕桂球门载任快毎社洋着道育纸格幻末。关机高害通方纳狱社州要北相持中表。郎市真提里过何连地更重都山割周。
+-----READING_TIME-----
+1
index 7b904da409a1be3d2550189b6212cc4e359039a0..90906d04a79f2eb47ac5a10d6d23cdcb0ada45ef 100644 (file)
@@ -1,7 +1,10 @@
+-----CONTENT-----
 Лорем ипсум долор сит амет, ех цум иллуд деленит, пер регионе фацилис те. Еи мел видит саепе интеллегам, яуас маиестатис цонституам яуо ат, цивибус реформиданс нецесситатибус ид яуи. Импетус тациматес пертинах ад еум. Усу еу легере бландит.
 
 Ан меа тритани иуварет, иллум сцаевола легендос ат меа, дебитис импедит нусяуам ест ад. Не маиорум молестие цотидиеяуе вис. Иисяуе цонцлудатуряуе меи еу, татион цонсецтетуер еи про. Либер риденс ид хас, ид цонсул сенсерит пертинациа меа. Фацер молестиае цомпрехенсам ад еум, ин хис апеириан вивендум. Яуи аудире епицуреи иудицабит ат, веро хабео вертерем ад иус. Бонорум плацерат ин вис, сеа но оцурререт принципес интерессет, хас ет дицерет диспутандо.
 
 Яуо цу цлита оцурререт. Сонет менандри ин сеа. Еум те нонумы вертерем. Вирис еяуидем фацилиси ет вим, делицата интеллегат иус ин. Ид дицат суммо витае вел, алияуип делецтус те дуо, цу вих хинц дуис видиссе. Нец цу фацилис урбанитас, алиа инсоленс ассуеверит при ут.
 
-Яуаеяуе абхорреант инцоррупте не сеа, еу еирмод ерудити вих. Вел оптион тритани цоррумпит те. Поссе сусципит губергрен ут мел, ет еос ириуре менандри еффициенди. Те сале нулла цонсецтетуер сеа, меа не прима алиенум еффициантур. При ет воцибус реформиданс, темпор албуциус сед ан. Еи утрояуе волумус иус, атяуи цонгуе но меи.
\ No newline at end of file
+Яуаеяуе абхорреант инцоррупте не сеа, еу еирмод ерудити вих. Вел оптион тритани цоррумпит те. Поссе сусципит губергрен ут мел, ет еос ириуре менандри еффициенди. Те сале нулла цонсецтетуер сеа, меа не прима алиенум еффициантур. При ет воцибус реформиданс, темпор албуциус сед ан. Еи утрояуе волумус иус, атяуи цонгуе но меи.
+-----READING_TIME-----
+1
index 59f15b8bc3484169833634cf26214a89288b4a4f..f8ade0d7e220d8797b97b3df7feba7358ffb5740 100644 (file)
@@ -1,3 +1,4 @@
+-----CONTENT-----
 Λορεμ ιπσθμ δολορ σιτ αμετ, ηασ νο θταμθρ qθαεqθε ρεπρεηενδθντ. Ναμ λατινε προμπτα qθαερενδθμ ιδ. Νεc ει φαcερ cονcλθδατθρqθε, vολθπτθα vολθπταρια εφφιcιενδι αδ προ, νε σεα ασσεντιορ δεφινιεβασ. Μεα αγαμ ειθσ δολορε ετ, ηισ ει cορπορα περφεcτο. Vιξ cιβο δελενιτ νε, jθστο ριδενσ οπορτερε σεδ ιδ.
 
 Ηισ νισλ ιθvαρετ γθβεργρεν εξ. Εθμ ιμπεδιτ δετραξιτ ινιμιcθσ ατ, αλια βλανδιτ δθο εα, μεα ιλλθδ επιcθρι cονσετετθρ αδ. Ιλλθδ γραεcε δελενιτι ηισ νο. Νεc ιδ ριδενσ εθισμοδ περιcθλισ, vισ αδ λαβοραμθσ περσεcθτι. Ιθσ εα λθπτατθμ αλιqθανδο δισπθτανδο.
@@ -6,4 +7,6 @@
 
 Cθ σεδ αλβθcιθσ ποστθλαντ. Vιξ ιδ ηομερο περcιπιτ cονcεπταμ. Ιν vιμ λιβρισ vιδερερ, εξ vισ αλιι ερρορ. Vιξ λοβορτισ ασσεντιορ cοντεντιονεσ τε, νε ηασ δεcορε περcιπιτθρ. Εστ εξ δισπθτατιονι δεφινιτιονεμ, qθοδ πηαεδρθμ προ εθ, εξ ηασ ιντεγρε ελιγενδι cονσεcτετθερ.
 
-Ιθσ μολλισ ειρμοδ νο, vιξ νοστρθμ cονσετετθρ ει. Ιθδιcο vερτερεμ λθcιλιθσ qθι τε, νε προμπτα θτροqθε αccομμοδαρε περ. Φαcετε μανδαμθσ ηασ εξ, λιβερ δεβετ εθμ εξ, vιξ ιδ διcερετ σιγνιφερθμqθε. Εθ vιξ vοcεντ.
\ No newline at end of file
+Ιθσ μολλισ ειρμοδ νο, vιξ νοστρθμ cονσετετθρ ει. Ιθδιcο vερτερεμ λθcιλιθσ qθι τε, νε προμπτα θτροqθε αccομμοδαρε περ. Φαcετε μανδαμθσ ηασ εξ, λιβερ δεβετ εθμ εξ, vιξ ιδ διcερετ σιγνιφερθμqθε. Εθ vιξ vοcεντ.
+-----READING_TIME-----
+1
diff --git a/tests/Wallabag/CoreBundle/Tools/samples/japanese.txt b/tests/Wallabag/CoreBundle/Tools/samples/japanese.txt
new file mode 100644 (file)
index 0000000..013a8d7
--- /dev/null
@@ -0,0 +1,10 @@
+-----CONTENT-----
+聞7配なク時初かきぴ触整ヨ国鴨覧女ミ将増3部ゅ見荷や言企まげやラ千第ロル企族リた期寄け。戦ト理載コミチヒ芸面だ会入テヒロソ一期ナトヒ試鮮せお天出並ぞる体森ヘツノ決市ね地各ナク強町ず前目とまなを活直オ携握湯りよ。
+
+流ムワ作大禁ヒフ断日ヱ断千ね消諸もとぐろ中勧リ配年リ文7茅ろへりめ辺渡フ三負安ぼ国撮ライム以逃めじット州67棋うきゃ。催キケ者乗フヒソツ染64崎ク捉示よぴふら道世へび属品おく西捕ニレ交重イフ式買散ル展五めづっイ鎧属ざごび数開キハツ聞続表クシタ補球ソウ禁源託ひれも。
+
+季手ッがふ挙思メ勢1使すけねげ日熱争らあふか位義エコ望桑安く決管ーひ広間キヱ皇北ょはこ養山ミ放見負さぞて故携訃畑港ひわン。著支にふみ意豊ラだ球監トクユ馬惨が抱審リヒ労厚ゅぽひ継貸ミノ果疑文キヤ闘府兼ユカシト多不っあ財責エ速訴径猶げすぽ。
+
+了摘見いぶころ会料へゆぱ法利コツハリ統財千りイ伝年りぜ提社ロ片追ごー合作イカシニ感山よち真器敗香レれさ。視シ探大イ令69真ケトヱ便都ケホワナ境号ヱカオハ一助む関念ろんび幼脚要だ客投ヱハイ針教ヒノウラ階担うスりね袖陸ょげけ同講ノ料全ヤ催宮補ゆ徳就画圧愛め。
+-----READING_TIME-----
+1
diff --git a/tests/Wallabag/CoreBundle/Tools/samples/korean.txt b/tests/Wallabag/CoreBundle/Tools/samples/korean.txt
new file mode 100644 (file)
index 0000000..e3ef2af
--- /dev/null
@@ -0,0 +1,10 @@
+-----CONTENT-----
+국군은 국가의 안전보장과 국토방위의 신성한 의무를 수행함을 사명으로 하며, 대통령이 임시회의 집회를 요구할 때에는 기간과 집회요구의 이유를 명시하여야 한다. 정당의 목적이나 활동이 민주적 기본질서에 위배될 때에는 정부는 헌법재판소에 그 해산을 제소할 수 있고. 감사위원은 원장의 제청으로 대통령이 임명하고.
+
+대한민국의 주권은 국민에게 있고, 국회는 국민의 보통·평등·직접·비밀선거에 의하여 선출된 국회의원으로 구성한다. 국가는 농업 및 어업을 보호·육성하기 위하여 농·어촌종합개발과 그 지원등 필요한 계획을 수립·시행하여야 한다. 대통령의 임기연장 또는 중임변경을 위한 헌법개정은 그 헌법개정 제안 당시의 대통령에 대하여는 효력이 없다.
+
+국회가 재적의원 과반수의 찬성으로 계엄의 해제를 요구한 때에는 대통령은 이를 해제하여야 한다, 선거에 관한 경비는 법률이 정하는 경우를 제외하고는 정당 또는 후보자에게 부담시킬 수 없다. 그 정치적 중립성은 준수된다. 헌법개정안은 국회가 의결한 후 30일 이내에 국민투표에 붙여 국회의원선거권자 과반수의 투표와 투표자 과반수의 찬성을 얻어야 한다.
+
+내부규율과 사무처리에 관한 규칙을 제정할 수 있다. 대통령에 대한 탄핵소추는 국회재적의원 과반수의 발의와 국회재적의원 3분의 2 이상의 찬성이 있어야 한다. 대통령은 국가의 원수이며. 대통령이 궐위된 때 또는 대통령 당선자가 사망하거나 판결 기타의 사유로 그 자격을 상실한 때에는 60일 이내에 후임자를 선거한다.
+-----READING_TIME-----
+2
index 605cc40ea570062058e546c831a77da14dd81eef..27988597067fdfdbadcb638eb0e90e222db7c33c 100644 (file)
@@ -1,3 +1,4 @@
+-----CONTENT-----
 Lorem ipsum dolor sit amet, pro vivendo oporteat pertinacia ei. Vim fabellas molestiae cu, vel nibh legimus ea, in qui atomorum democritum. Ius ne agam soluta ignota, his sale aperiri complectitur te, omnis volumus accusam an eos. Ut mentitum appetere mel, minim temporibus eloquentiam sea ea.
 
 Tation nominati pro ad. Pri eros eloquentiam reformidans ea, et liber epicurei erroribus pro, pri patrioque repudiandae et. Cetero perfecto at eam. Eros hendrerit constituto vix at, brute aperiri adolescens pro eu. Vix lucilius consulatu ei, ullum tantas munere vel in, regione feugiat eligendi at eam.
@@ -6,4 +7,6 @@ Eam an lucilius iracundia, audire diceret facilisi his in, ex paulo pertinacia p
 
 Nec ut quod probo eligendi, cu dico iriure aperiam vis. Augue causae abhorreant per ut, iriure repudiandae no nam, exerci equidem deleniti nam te. Et duo saperet debitis adipiscing, quo odio audiam no, ex iudico delenit propriae duo. Eu eum eros abhorreant, an tractatos expetendis est.
 
-Vix.
\ No newline at end of file
+Vix.
+-----READING_TIME-----
+1