diff options
author | Nicolas LÅ“uillet <nicolas@loeuillet.org> | 2015-02-24 22:00:24 +0100 |
---|---|---|
committer | Nicolas LÅ“uillet <nicolas@loeuillet.org> | 2015-03-06 20:50:31 +0100 |
commit | a36737f4859e3acbddf5cfe90c279ba725a6d88a (patch) | |
tree | b65709740a5e92bc53eeb0e3e050e0df3e78f3bb | |
parent | 46bbd8d321e6a00131f0e6ed96fa6f3d693b3678 (diff) | |
download | wallabag-a36737f4859e3acbddf5cfe90c279ba725a6d88a.tar.gz wallabag-a36737f4859e3acbddf5cfe90c279ba725a6d88a.tar.zst wallabag-a36737f4859e3acbddf5cfe90c279ba725a6d88a.zip |
POST entries/tags with test
3 files changed, 69 insertions, 1 deletions
diff --git a/src/Wallabag/CoreBundle/Controller/WallabagRestController.php b/src/Wallabag/CoreBundle/Controller/WallabagRestController.php index db9cf1be..e59ad4b7 100644 --- a/src/Wallabag/CoreBundle/Controller/WallabagRestController.php +++ b/src/Wallabag/CoreBundle/Controller/WallabagRestController.php | |||
@@ -213,8 +213,34 @@ class WallabagRestController extends Controller | |||
213 | * } | 213 | * } |
214 | * ) | 214 | * ) |
215 | */ | 215 | */ |
216 | public function postEntriesTagsAction(Entry $entry) | 216 | public function postEntriesTagsAction(Request $request, Entry $entry) |
217 | { | 217 | { |
218 | $tags = explode(',', $request->request->get('tags')); | ||
219 | |||
220 | foreach ($tags as $label) { | ||
221 | $tagEntity = $this | ||
222 | ->getDoctrine() | ||
223 | ->getRepository('WallabagCoreBundle:Tag') | ||
224 | ->findOneByLabel($label); | ||
225 | |||
226 | if (is_null($tagEntity)) { | ||
227 | $tagEntity = new Tag(); | ||
228 | $tagEntity->setLabel($label); | ||
229 | } | ||
230 | |||
231 | // only add the tag on the entry if the relation doesn't exist | ||
232 | if (!$entry->getTags()->contains($tagEntity)) { | ||
233 | $entry->addTag($tagEntity); | ||
234 | } | ||
235 | } | ||
236 | |||
237 | $em = $this->getDoctrine()->getManager(); | ||
238 | $em->persist($entry); | ||
239 | $em->flush(); | ||
240 | |||
241 | $json = $this->get('serializer')->serialize($entry, 'json'); | ||
242 | |||
243 | return new Response($json, 200, array('application/json')); | ||
218 | } | 244 | } |
219 | 245 | ||
220 | /** | 246 | /** |
diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index f6f60c6f..10fb9bf7 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php | |||
@@ -119,6 +119,11 @@ class EntryRepository extends EntityRepository | |||
119 | ->getResult(); | 119 | ->getResult(); |
120 | } | 120 | } |
121 | 121 | ||
122 | /** | ||
123 | * Fetch an entry with a tag. Only used for tests. | ||
124 | * | ||
125 | * @return Entry | ||
126 | */ | ||
122 | public function findOneWithTags() | 127 | public function findOneWithTags() |
123 | { | 128 | { |
124 | $qb = $this->createQueryBuilder('e') | 129 | $qb = $this->createQueryBuilder('e') |
diff --git a/src/Wallabag/CoreBundle/Tests/Controller/WallabagRestControllerTest.php b/src/Wallabag/CoreBundle/Tests/Controller/WallabagRestControllerTest.php index d2390055..cadbb70b 100644 --- a/src/Wallabag/CoreBundle/Tests/Controller/WallabagRestControllerTest.php +++ b/src/Wallabag/CoreBundle/Tests/Controller/WallabagRestControllerTest.php | |||
@@ -176,4 +176,41 @@ class WallabagRestControllerTest extends WallabagTestCase | |||
176 | 176 | ||
177 | $this->assertEquals(json_encode($tags), $client->getResponse()->getContent()); | 177 | $this->assertEquals(json_encode($tags), $client->getResponse()->getContent()); |
178 | } | 178 | } |
179 | |||
180 | public function testPostTagsOnEntry() | ||
181 | { | ||
182 | $client = $this->createClient(); | ||
183 | $client->request('GET', '/api/salts/admin.json'); | ||
184 | $salt = json_decode($client->getResponse()->getContent()); | ||
185 | $headers = $this->generateHeaders('admin', 'test', $salt[0]); | ||
186 | |||
187 | $entry = $client->getContainer() | ||
188 | ->get('doctrine.orm.entity_manager') | ||
189 | ->getRepository('WallabagCoreBundle:Entry') | ||
190 | ->findOneByUser(1); | ||
191 | |||
192 | if (!$entry) { | ||
193 | $this->markTestSkipped('No content found in db.'); | ||
194 | } | ||
195 | |||
196 | $newTags = 'tag1,tag2,tag3'; | ||
197 | |||
198 | $client->request('POST', '/api/entries/'.$entry->getId().'/tags', array('tags' => $newTags), array(), $headers); | ||
199 | |||
200 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | ||
201 | |||
202 | $entryDB = $client->getContainer() | ||
203 | ->get('doctrine.orm.entity_manager') | ||
204 | ->getRepository('WallabagCoreBundle:Entry') | ||
205 | ->find($entry->getId()); | ||
206 | |||
207 | $tagsInDB = array(); | ||
208 | foreach ($entryDB->getTags()->toArray() as $tag) { | ||
209 | $tagsInDB[$tag->getId()] = $tag->getLabel(); | ||
210 | } | ||
211 | |||
212 | foreach (explode(',', $newTags) as $tag) { | ||
213 | $this->assertContains($tag, $tagsInDB); | ||
214 | } | ||
215 | } | ||
179 | } | 216 | } |