]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
POST entries/tags with test
authorNicolas Lœuillet <nicolas@loeuillet.org>
Tue, 24 Feb 2015 21:00:24 +0000 (22:00 +0100)
committerNicolas Lœuillet <nicolas@loeuillet.org>
Fri, 6 Mar 2015 19:50:31 +0000 (20:50 +0100)
src/Wallabag/CoreBundle/Controller/WallabagRestController.php
src/Wallabag/CoreBundle/Repository/EntryRepository.php
src/Wallabag/CoreBundle/Tests/Controller/WallabagRestControllerTest.php

index db9cf1be4d3b857ab3c798b6559df001b716e523..e59ad4b7bf9d208f77cb51feda46a620f59deab7 100644 (file)
@@ -213,8 +213,34 @@ class WallabagRestController extends Controller
      *       }
      * )
      */
-    public function postEntriesTagsAction(Entry $entry)
+    public function postEntriesTagsAction(Request $request, Entry $entry)
     {
+        $tags = explode(',', $request->request->get('tags'));
+
+        foreach ($tags as $label) {
+            $tagEntity = $this
+                ->getDoctrine()
+                ->getRepository('WallabagCoreBundle:Tag')
+                ->findOneByLabel($label);
+
+            if (is_null($tagEntity)) {
+                $tagEntity = new Tag();
+                $tagEntity->setLabel($label);
+            }
+
+            // only add the tag on the entry if the relation doesn't exist
+            if (!$entry->getTags()->contains($tagEntity)) {
+                $entry->addTag($tagEntity);
+            }
+        }
+
+        $em = $this->getDoctrine()->getManager();
+        $em->persist($entry);
+        $em->flush();
+
+        $json = $this->get('serializer')->serialize($entry, 'json');
+
+        return new Response($json, 200, array('application/json'));
     }
 
     /**
index f6f60c6fec2cab86ade3867033df0489ed7eff77..10fb9bf7933ca1f986bcce8712d640f5c2a25ef4 100644 (file)
@@ -119,6 +119,11 @@ class EntryRepository extends EntityRepository
             ->getResult();
     }
 
+    /**
+     * Fetch an entry with a tag. Only used for tests.
+     *
+     * @return Entry
+     */
     public function findOneWithTags()
     {
         $qb = $this->createQueryBuilder('e')
index d239005567b41b85eb140cf613dd5f814b1d4cb5..cadbb70b7d8f4a22627765e605d3b6258133f36e 100644 (file)
@@ -176,4 +176,41 @@ class WallabagRestControllerTest extends WallabagTestCase
 
         $this->assertEquals(json_encode($tags), $client->getResponse()->getContent());
     }
+
+    public function testPostTagsOnEntry()
+    {
+        $client = $this->createClient();
+        $client->request('GET', '/api/salts/admin.json');
+        $salt = json_decode($client->getResponse()->getContent());
+        $headers = $this->generateHeaders('admin', 'test', $salt[0]);
+
+        $entry = $client->getContainer()
+            ->get('doctrine.orm.entity_manager')
+            ->getRepository('WallabagCoreBundle:Entry')
+            ->findOneByUser(1);
+
+        if (!$entry) {
+            $this->markTestSkipped('No content found in db.');
+        }
+
+        $newTags = 'tag1,tag2,tag3';
+
+        $client->request('POST', '/api/entries/'.$entry->getId().'/tags', array('tags' => $newTags), array(), $headers);
+
+        $this->assertEquals(200, $client->getResponse()->getStatusCode());
+
+        $entryDB = $client->getContainer()
+            ->get('doctrine.orm.entity_manager')
+            ->getRepository('WallabagCoreBundle:Entry')
+            ->find($entry->getId());
+
+        $tagsInDB = array();
+        foreach ($entryDB->getTags()->toArray() as $tag) {
+            $tagsInDB[$tag->getId()] = $tag->getLabel();
+        }
+
+        foreach (explode(',', $newTags) as $tag) {
+            $this->assertContains($tag, $tagsInDB);
+        }
+    }
 }