]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Fix relations export for Entry
authorJeremy Benoist <jeremy.benoist@gmail.com>
Sun, 2 Oct 2016 14:06:42 +0000 (16:06 +0200)
committerJeremy Benoist <jeremy.benoist@gmail.com>
Fri, 7 Oct 2016 05:43:19 +0000 (07:43 +0200)
Tags & Annotations weren’t really well exported.
This is now fixed (+ tests)

src/Wallabag/AnnotationBundle/Entity/Annotation.php
src/Wallabag/CoreBundle/DataFixtures/ORM/LoadEntryData.php
src/Wallabag/CoreBundle/Entity/Entry.php
src/Wallabag/CoreBundle/Helper/EntriesExport.php
tests/Wallabag/CoreBundle/Controller/ExportControllerTest.php

index 90ee7c2d7e2200b484ae3c0d0550c15279390b38..c48d873100e2a38a25fa37e06a04a465ea49f0c1 100644 (file)
@@ -7,6 +7,7 @@ use JMS\Serializer\Annotation\ExclusionPolicy;
 use JMS\Serializer\Annotation\Exclude;
 use JMS\Serializer\Annotation\VirtualProperty;
 use JMS\Serializer\Annotation\SerializedName;
+use JMS\Serializer\Annotation\Groups;
 use Wallabag\UserBundle\Entity\User;
 use Wallabag\CoreBundle\Entity\Entry;
 
@@ -33,6 +34,8 @@ class Annotation
      * @var string
      *
      * @ORM\Column(name="text", type="text")
+     *
+     * @Groups({"entries_for_user", "export_all"})
      */
     private $text;
 
@@ -54,6 +57,8 @@ class Annotation
      * @var string
      *
      * @ORM\Column(name="quote", type="string")
+     *
+     * @Groups({"entries_for_user", "export_all"})
      */
     private $quote;
 
@@ -61,6 +66,8 @@ class Annotation
      * @var array
      *
      * @ORM\Column(name="ranges", type="array")
+     *
+     * @Groups({"entries_for_user", "export_all"})
      */
     private $ranges;
 
index 6c6a331a6c8904adb7b1aa8e843c133bb5657a40..fedad00993f6b3b5ff354f72c74bcea76e3e9c46 100644 (file)
@@ -23,6 +23,9 @@ class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface
         $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);
@@ -96,6 +99,7 @@ class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface
         $entry6->setContent('This is my content /o/');
         $entry6->setArchived(true);
         $entry6->setLanguage('de');
+        $entry6->addTag($this->getReference('bar-tag'));
 
         $manager->persist($entry6);
 
index a4b0d7a82b4c436ed4bc79b6154e88590da36847..f2da3f4def670d5a0e08af123a1617a2cb3fa509 100644 (file)
@@ -196,8 +196,6 @@ class Entry
      *      @ORM\JoinColumn(name="tag_id", referencedColumnName="id")
      *  }
      * )
-     *
-     * @Groups({"entries_for_user", "export_all"})
      */
     private $tags;
 
@@ -541,6 +539,21 @@ class Entry
         return $this->tags;
     }
 
+    /**
+     * @VirtualProperty
+     * @SerializedName("tags")
+     * @Groups({"entries_for_user", "export_all"})
+     */
+    public function getSerializedTags()
+    {
+        $data = [];
+        foreach ($this->tags as $tag) {
+            $data[] = $tag->getLabel();
+        }
+
+        return $data;
+    }
+
     /**
      * @param Tag $tag
      */
index 0c627dcdb6344fa687364d138b2d36bf217ddc89..d1f1e23354480e9d24fe07134ea2b15071f39eba 100644 (file)
@@ -53,10 +53,6 @@ class EntriesExport
 
         $this->entries = $entries;
 
-        foreach ($entries as $entry) {
-            $this->tags[] = $entry->getTags();
-        }
-
         return $this;
     }
 
@@ -159,8 +155,8 @@ class EntriesExport
 
         // set tags as subjects
         foreach ($this->entries as $entry) {
-            foreach ($this->tags as $tag) {
-                $book->setSubject($tag['value']);
+            foreach ($entry->getTags() as $tag) {
+                $book->setSubject($tag->getLabel());
             }
 
             // the reader in Kobo Devices doesn't likes special caracters
@@ -265,8 +261,8 @@ class EntriesExport
          * Adding actual entries
          */
         foreach ($this->entries as $entry) {
-            foreach ($this->tags as $tag) {
-                $pdf->SetKeywords($tag['value']);
+            foreach ($entry->getTags() as $tag) {
+                $pdf->SetKeywords($tag->getLabel());
             }
 
             $pdf->AddPage();
index 47b86117dde2a35c72c6902fecfbf028755f4743..9f35a44877f8904eb3ed497eec664e4059bc73af 100644 (file)
@@ -146,7 +146,9 @@ class ExportControllerTest extends WallabagCoreTestCase
             ->get('doctrine.orm.entity_manager')
             ->getRepository('WallabagCoreBundle:Entry')
             ->createQueryBuilder('e')
+            ->select('e, t')
             ->leftJoin('e.user', 'u')
+            ->leftJoin('e.tags', 't')
             ->where('u.username = :username')->setParameter('username', 'admin')
             ->andWhere('e.isArchived = true')
             ->getQuery()
@@ -169,6 +171,18 @@ class ExportControllerTest extends WallabagCoreTestCase
         // +1 for title line
         $this->assertEquals(count($contentInDB) + 1, count($csv));
         $this->assertEquals('Title;URL;Content;Tags;"MIME Type";Language;"Creation date"', $csv[0]);
+        $this->assertContains($contentInDB[0]['title'], $csv[1]);
+        $this->assertContains($contentInDB[0]['url'], $csv[1]);
+        $this->assertContains($contentInDB[0]['content'], $csv[1]);
+        $this->assertContains($contentInDB[0]['mimetype'], $csv[1]);
+        $this->assertContains($contentInDB[0]['language'], $csv[1]);
+        $this->assertContains($contentInDB[0]['createdAt']->format('d/m/Y h:i:s'), $csv[1]);
+
+        $expectedTag = [];
+        foreach ($contentInDB[0]['tags'] as $tag) {
+            $expectedTag[] = $tag['label'];
+        }
+        $this->assertContains(implode(', ', $expectedTag), $csv[1]);
     }
 
     public function testJsonExport()
@@ -176,29 +190,23 @@ class ExportControllerTest extends WallabagCoreTestCase
         $this->logInAs('admin');
         $client = $this->getClient();
 
-        // to be sure results are the same
         $contentInDB = $client->getContainer()
             ->get('doctrine.orm.entity_manager')
             ->getRepository('WallabagCoreBundle:Entry')
-            ->createQueryBuilder('e')
-            ->leftJoin('e.user', 'u')
-            ->where('u.username = :username')->setParameter('username', 'admin')
-            ->getQuery()
-            ->getArrayResult();
+            ->findOneByUsernameAndNotArchived('admin');
 
         ob_start();
-        $crawler = $client->request('GET', '/export/all.json');
+        $crawler = $client->request('GET', '/export/'.$contentInDB->getId().'.json');
         ob_end_clean();
 
         $this->assertEquals(200, $client->getResponse()->getStatusCode());
 
         $headers = $client->getResponse()->headers;
         $this->assertEquals('application/json', $headers->get('content-type'));
-        $this->assertEquals('attachment; filename="All articles.json"', $headers->get('content-disposition'));
+        $this->assertEquals('attachment; filename="'.$contentInDB->getTitle().'.json"', $headers->get('content-disposition'));
         $this->assertEquals('UTF-8', $headers->get('content-transfer-encoding'));
 
         $content = json_decode($client->getResponse()->getContent(), true);
-        $this->assertEquals(count($contentInDB), count($content));
         $this->assertArrayHasKey('id', $content[0]);
         $this->assertArrayHasKey('title', $content[0]);
         $this->assertArrayHasKey('url', $content[0]);
@@ -212,6 +220,17 @@ class ExportControllerTest extends WallabagCoreTestCase
         $this->assertArrayHasKey('tags', $content[0]);
         $this->assertArrayHasKey('created_at', $content[0]);
         $this->assertArrayHasKey('updated_at', $content[0]);
+
+        $this->assertEquals($contentInDB->isArchived(), $content[0]['is_archived']);
+        $this->assertEquals($contentInDB->isStarred(), $content[0]['is_starred']);
+        $this->assertEquals($contentInDB->getTitle(), $content[0]['title']);
+        $this->assertEquals($contentInDB->getUrl(), $content[0]['url']);
+        $this->assertEquals([['text' => 'This is my annotation /o/', 'quote' => 'content']], $content[0]['annotations']);
+        $this->assertEquals($contentInDB->getMimetype(), $content[0]['mimetype']);
+        $this->assertEquals($contentInDB->getLanguage(), $content[0]['language']);
+        $this->assertEquals($contentInDB->getReadingtime(), $content[0]['reading_time']);
+        $this->assertEquals($contentInDB->getDomainname(), $content[0]['domain_name']);
+        $this->assertEquals(['foo', 'baz'], $content[0]['tags']);
     }
 
     public function testXmlExport()