diff options
author | Jeremy Benoist <jeremy.benoist@gmail.com> | 2016-07-24 10:55:49 +0200 |
---|---|---|
committer | Jeremy Benoist <jeremy.benoist@gmail.com> | 2016-08-23 07:26:58 +0200 |
commit | a0e1eafc35e1007056555651ace7221d09cd8270 (patch) | |
tree | f565b8d53fbf89e0a2221467fee213fdc3153050 | |
parent | 9bf83f1fb8d588b871a5d12289179de087756d02 (diff) | |
download | wallabag-a0e1eafc35e1007056555651ace7221d09cd8270.tar.gz wallabag-a0e1eafc35e1007056555651ace7221d09cd8270.tar.zst wallabag-a0e1eafc35e1007056555651ace7221d09cd8270.zip |
Add some tests
Also, retrieve tag from the request instead of the query (which will be the same but it's more easy to test).
Moved down `deleteTagAction` because it conflicted with the new action:
api_delete_tag => /api/tags/{tag}.{_format}
api_delete_tags_label => /api/tags/label.{_format}
And finally, throw exception when a tag is not found before removing it.
-rw-r--r-- | src/Wallabag/ApiBundle/Controller/WallabagRestController.php | 58 | ||||
-rw-r--r-- | tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php | 113 |
2 files changed, 145 insertions, 26 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php index 43225149..869fdc56 100644 --- a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php +++ b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php | |||
@@ -334,15 +334,22 @@ class WallabagRestController extends FOSRestController | |||
334 | * | 334 | * |
335 | * @ApiDoc( | 335 | * @ApiDoc( |
336 | * requirements={ | 336 | * requirements={ |
337 | * {"name"="tag", "dataType"="integer", "requirement"="\w+", "description"="The tag"} | 337 | * {"name"="tag", "dataType"="string", "required"=true, "requirement"="\w+", "description"="Tag as a string"} |
338 | * } | 338 | * } |
339 | * ) | 339 | * ) |
340 | * | 340 | * |
341 | * @return Response | 341 | * @return Response |
342 | */ | 342 | */ |
343 | public function deleteTagAction(Tag $tag) | 343 | public function deleteTagLabelAction(Request $request) |
344 | { | 344 | { |
345 | $this->validateAuthentication(); | 345 | $this->validateAuthentication(); |
346 | $label = $request->request->get('tag', ''); | ||
347 | |||
348 | $tag = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($label); | ||
349 | |||
350 | if (empty($tag)) { | ||
351 | throw $this->createNotFoundException('Tag not found'); | ||
352 | } | ||
346 | 353 | ||
347 | $this->getDoctrine() | 354 | $this->getDoctrine() |
348 | ->getRepository('WallabagCoreBundle:Entry') | 355 | ->getRepository('WallabagCoreBundle:Entry') |
@@ -354,60 +361,65 @@ class WallabagRestController extends FOSRestController | |||
354 | } | 361 | } |
355 | 362 | ||
356 | /** | 363 | /** |
357 | * Permanently remove one tag from **every** entry. | 364 | * Permanently remove some tags from **every** entry. |
358 | * | 365 | * |
359 | * @ApiDoc( | 366 | * @ApiDoc( |
360 | * requirements={ | 367 | * requirements={ |
361 | * {"name"="tag", "dataType"="string", "requirement"="\w+", "description"="The tag as a string"} | 368 | * {"name"="tags", "dataType"="string", "required"=true, "format"="tag1,tag2", "description"="Tags as strings (comma splitted)"} |
362 | * } | 369 | * } |
363 | * ) | 370 | * ) |
364 | * | 371 | * |
365 | * @return Response | 372 | * @return Response |
366 | */ | 373 | */ |
367 | public function deleteTagLabelAction(Request $request) | 374 | public function deleteTagsLabelAction(Request $request) |
368 | { | 375 | { |
369 | $this->validateAuthentication(); | 376 | $this->validateAuthentication(); |
370 | $label = $request->query->get('tag', ''); | ||
371 | 377 | ||
372 | $tag = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($label); | 378 | $tagsLabels = $request->request->get('tags', ''); |
379 | |||
380 | $tags = []; | ||
381 | |||
382 | foreach (explode(',', $tagsLabels) as $tagLabel) { | ||
383 | $tagEntity = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($tagLabel); | ||
384 | |||
385 | if (!empty($tagEntity)) { | ||
386 | $tags[] = $tagEntity; | ||
387 | } | ||
388 | } | ||
389 | |||
390 | if (empty($tags)) { | ||
391 | throw $this->createNotFoundException('Tags not found'); | ||
392 | } | ||
393 | |||
373 | $this->getDoctrine() | 394 | $this->getDoctrine() |
374 | ->getRepository('WallabagCoreBundle:Entry') | 395 | ->getRepository('WallabagCoreBundle:Entry') |
375 | ->removeTag($this->getUser()->getId(), $tag); | 396 | ->removeTags($this->getUser()->getId(), $tags); |
376 | 397 | ||
377 | $json = $this->get('serializer')->serialize($tag, 'json'); | 398 | $json = $this->get('serializer')->serialize($tags, 'json'); |
378 | 399 | ||
379 | return $this->renderJsonResponse($json); | 400 | return $this->renderJsonResponse($json); |
380 | } | 401 | } |
381 | 402 | ||
382 | /** | 403 | /** |
383 | * Permanently remove some tags from **every** entry. | 404 | * Permanently remove one tag from **every** entry. |
384 | * | 405 | * |
385 | * @ApiDoc( | 406 | * @ApiDoc( |
386 | * requirements={ | 407 | * requirements={ |
387 | * {"name"="tags", "dataType"="string", "required"=true, "format"="tag1,tag2", "description"="The tags as strings"} | 408 | * {"name"="tag", "dataType"="integer", "requirement"="\w+", "description"="The tag"} |
388 | * } | 409 | * } |
389 | * ) | 410 | * ) |
390 | * | 411 | * |
391 | * @return Response | 412 | * @return Response |
392 | */ | 413 | */ |
393 | public function deleteTagsLabelAction(Request $request) | 414 | public function deleteTagAction(Tag $tag) |
394 | { | 415 | { |
395 | $this->validateAuthentication(); | 416 | $this->validateAuthentication(); |
396 | 417 | ||
397 | $tagsLabels = $request->query->get('tags', ''); | ||
398 | |||
399 | $tags = array(); | ||
400 | |||
401 | foreach (explode(',', $tagsLabels) as $tagLabel) { | ||
402 | $tagEntity = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($tagLabel); | ||
403 | $tags[] = $tagEntity; | ||
404 | } | ||
405 | |||
406 | $this->getDoctrine() | 418 | $this->getDoctrine() |
407 | ->getRepository('WallabagCoreBundle:Entry') | 419 | ->getRepository('WallabagCoreBundle:Entry') |
408 | ->removeTags($this->getUser()->getId(), $tags); | 420 | ->removeTag($this->getUser()->getId(), $tag); |
409 | 421 | ||
410 | $json = $this->get('serializer')->serialize($tags, 'json'); | 422 | $json = $this->get('serializer')->serialize($tag, 'json'); |
411 | 423 | ||
412 | return $this->renderJsonResponse($json); | 424 | return $this->renderJsonResponse($json); |
413 | } | 425 | } |
diff --git a/tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php index 528366af..ee5b2ab7 100644 --- a/tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php +++ b/tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php | |||
@@ -3,6 +3,7 @@ | |||
3 | namespace Tests\Wallabag\ApiBundle\Controller; | 3 | namespace Tests\Wallabag\ApiBundle\Controller; |
4 | 4 | ||
5 | use Tests\Wallabag\ApiBundle\WallabagApiTestCase; | 5 | use Tests\Wallabag\ApiBundle\WallabagApiTestCase; |
6 | use Wallabag\CoreBundle\Entity\Tag; | ||
6 | 7 | ||
7 | class WallabagRestControllerTest extends WallabagApiTestCase | 8 | class WallabagRestControllerTest extends WallabagApiTestCase |
8 | { | 9 | { |
@@ -359,7 +360,7 @@ class WallabagRestControllerTest extends WallabagApiTestCase | |||
359 | $entry = $this->client->getContainer() | 360 | $entry = $this->client->getContainer() |
360 | ->get('doctrine.orm.entity_manager') | 361 | ->get('doctrine.orm.entity_manager') |
361 | ->getRepository('WallabagCoreBundle:Entry') | 362 | ->getRepository('WallabagCoreBundle:Entry') |
362 | ->findOneWithTags(1); | 363 | ->findOneWithTags($this->user->getId()); |
363 | 364 | ||
364 | $entry = $entry[0]; | 365 | $entry = $entry[0]; |
365 | 366 | ||
@@ -421,7 +422,7 @@ class WallabagRestControllerTest extends WallabagApiTestCase | |||
421 | $entry = $this->client->getContainer() | 422 | $entry = $this->client->getContainer() |
422 | ->get('doctrine.orm.entity_manager') | 423 | ->get('doctrine.orm.entity_manager') |
423 | ->getRepository('WallabagCoreBundle:Entry') | 424 | ->getRepository('WallabagCoreBundle:Entry') |
424 | ->findOneWithTags(1); | 425 | ->findOneWithTags($this->user->getId()); |
425 | $entry = $entry[0]; | 426 | $entry = $entry[0]; |
426 | 427 | ||
427 | if (!$entry) { | 428 | if (!$entry) { |
@@ -472,7 +473,7 @@ class WallabagRestControllerTest extends WallabagApiTestCase | |||
472 | $this->assertEquals($tag['label'], $content['label']); | 473 | $this->assertEquals($tag['label'], $content['label']); |
473 | $this->assertEquals($tag['slug'], $content['slug']); | 474 | $this->assertEquals($tag['slug'], $content['slug']); |
474 | 475 | ||
475 | $entries = $entry = $this->client->getContainer() | 476 | $entries = $this->client->getContainer() |
476 | ->get('doctrine.orm.entity_manager') | 477 | ->get('doctrine.orm.entity_manager') |
477 | ->getRepository('WallabagCoreBundle:Entry') | 478 | ->getRepository('WallabagCoreBundle:Entry') |
478 | ->findAllByTagId($this->user->getId(), $tag['id']); | 479 | ->findAllByTagId($this->user->getId(), $tag['id']); |
@@ -480,6 +481,112 @@ class WallabagRestControllerTest extends WallabagApiTestCase | |||
480 | $this->assertCount(0, $entries); | 481 | $this->assertCount(0, $entries); |
481 | } | 482 | } |
482 | 483 | ||
484 | public function testDeleteTagByLabel() | ||
485 | { | ||
486 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); | ||
487 | $entry = $this->client->getContainer() | ||
488 | ->get('doctrine.orm.entity_manager') | ||
489 | ->getRepository('WallabagCoreBundle:Entry') | ||
490 | ->findOneWithTags($this->user->getId()); | ||
491 | |||
492 | $entry = $entry[0]; | ||
493 | |||
494 | $tag = new Tag(); | ||
495 | $tag->setLabel('Awesome tag for test'); | ||
496 | $em->persist($tag); | ||
497 | |||
498 | $entry->addTag($tag); | ||
499 | |||
500 | $em->persist($entry); | ||
501 | $em->flush(); | ||
502 | |||
503 | $this->client->request('DELETE', '/api/tag/label.json', ['tag' => $tag->getLabel()]); | ||
504 | |||
505 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | ||
506 | |||
507 | $content = json_decode($this->client->getResponse()->getContent(), true); | ||
508 | |||
509 | $this->assertArrayHasKey('label', $content); | ||
510 | $this->assertEquals($tag->getLabel(), $content['label']); | ||
511 | $this->assertEquals($tag->getSlug(), $content['slug']); | ||
512 | |||
513 | $entries = $this->client->getContainer() | ||
514 | ->get('doctrine.orm.entity_manager') | ||
515 | ->getRepository('WallabagCoreBundle:Entry') | ||
516 | ->findAllByTagId($this->user->getId(), $tag->getId()); | ||
517 | |||
518 | $this->assertCount(0, $entries); | ||
519 | } | ||
520 | |||
521 | public function testDeleteTagByLabelNotFound() | ||
522 | { | ||
523 | $this->client->request('DELETE', '/api/tag/label.json', ['tag' => 'does not exist']); | ||
524 | |||
525 | $this->assertEquals(404, $this->client->getResponse()->getStatusCode()); | ||
526 | } | ||
527 | |||
528 | public function testDeleteTagsByLabel() | ||
529 | { | ||
530 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); | ||
531 | $entry = $this->client->getContainer() | ||
532 | ->get('doctrine.orm.entity_manager') | ||
533 | ->getRepository('WallabagCoreBundle:Entry') | ||
534 | ->findOneWithTags($this->user->getId()); | ||
535 | |||
536 | $entry = $entry[0]; | ||
537 | |||
538 | $tag = new Tag(); | ||
539 | $tag->setLabel('Awesome tag for tagsLabel'); | ||
540 | $em->persist($tag); | ||
541 | |||
542 | $tag2 = new Tag(); | ||
543 | $tag2->setLabel('Awesome tag for tagsLabel 2'); | ||
544 | $em->persist($tag2); | ||
545 | |||
546 | $entry->addTag($tag); | ||
547 | $entry->addTag($tag2); | ||
548 | |||
549 | $em->persist($entry); | ||
550 | $em->flush(); | ||
551 | |||
552 | $this->client->request('DELETE', '/api/tags/label.json', ['tags' => $tag->getLabel().','.$tag2->getLabel()]); | ||
553 | |||
554 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | ||
555 | |||
556 | $content = json_decode($this->client->getResponse()->getContent(), true); | ||
557 | |||
558 | $this->assertCount(2, $content); | ||
559 | |||
560 | $this->assertArrayHasKey('label', $content[0]); | ||
561 | $this->assertEquals($tag->getLabel(), $content[0]['label']); | ||
562 | $this->assertEquals($tag->getSlug(), $content[0]['slug']); | ||
563 | |||
564 | $this->assertArrayHasKey('label', $content[1]); | ||
565 | $this->assertEquals($tag2->getLabel(), $content[1]['label']); | ||
566 | $this->assertEquals($tag2->getSlug(), $content[1]['slug']); | ||
567 | |||
568 | $entries = $this->client->getContainer() | ||
569 | ->get('doctrine.orm.entity_manager') | ||
570 | ->getRepository('WallabagCoreBundle:Entry') | ||
571 | ->findAllByTagId($this->user->getId(), $tag->getId()); | ||
572 | |||
573 | $this->assertCount(0, $entries); | ||
574 | |||
575 | $entries = $this->client->getContainer() | ||
576 | ->get('doctrine.orm.entity_manager') | ||
577 | ->getRepository('WallabagCoreBundle:Entry') | ||
578 | ->findAllByTagId($this->user->getId(), $tag2->getId()); | ||
579 | |||
580 | $this->assertCount(0, $entries); | ||
581 | } | ||
582 | |||
583 | public function testDeleteTagsByLabelNotFound() | ||
584 | { | ||
585 | $this->client->request('DELETE', '/api/tags/label.json', ['tags' => 'does not exist']); | ||
586 | |||
587 | $this->assertEquals(404, $this->client->getResponse()->getStatusCode()); | ||
588 | } | ||
589 | |||
483 | public function testGetVersion() | 590 | public function testGetVersion() |
484 | { | 591 | { |
485 | $this->client->request('GET', '/api/version'); | 592 | $this->client->request('GET', '/api/version'); |