From: Jeremy Benoist Date: Sat, 22 Oct 2016 10:09:20 +0000 (+0200) Subject: Add test on /api/annotations X-Git-Tag: 2.2.0~3^2~85^2 X-Git-Url: https://git.immae.eu/?a=commitdiff_plain;h=aa4741091f3f2cc7e4a7ef061a04678597ddeca8;p=github%2Fwallabag%2Fwallabag.git Add test on /api/annotations Fix controller forward in WallabagRestController. Update PHPDoc so it is sorted the same way as others one Duplicate all annotations test to use both api & normal way Also, make annotation tests independent to each other --- diff --git a/src/Wallabag/AnnotationBundle/Repository/AnnotationRepository.php b/src/Wallabag/AnnotationBundle/Repository/AnnotationRepository.php index 5f7da70e..8cccffba 100644 --- a/src/Wallabag/AnnotationBundle/Repository/AnnotationRepository.php +++ b/src/Wallabag/AnnotationBundle/Repository/AnnotationRepository.php @@ -50,7 +50,8 @@ class AnnotationRepository extends EntityRepository { return $this->createQueryBuilder('a') ->andWhere('a.id = :annotationId')->setParameter('annotationId', $annotationId) - ->getQuery()->getSingleResult() + ->getQuery() + ->getSingleResult() ; } @@ -67,7 +68,8 @@ class AnnotationRepository extends EntityRepository return $this->createQueryBuilder('a') ->where('a.entry = :entryId')->setParameter('entryId', $entryId) ->andwhere('a.user = :userId')->setParameter('userId', $userId) - ->getQuery()->getResult() + ->getQuery() + ->getResult() ; } diff --git a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php index 0c709ca0..a73d44ca 100644 --- a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php +++ b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php @@ -536,7 +536,7 @@ class WallabagRestController extends FOSRestController { $this->validateAuthentication(); - return $this->forward('WallabagApiBundle:WallabagRest:getAnnotations', [ + return $this->forward('WallabagAnnotationBundle:WallabagAnnotation:getAnnotations', [ 'entry' => $entry, ]); } @@ -544,10 +544,6 @@ class WallabagRestController extends FOSRestController /** * Creates a new annotation. * - * @param Request $request - * @param Entry $entry - * - * @return JsonResponse * @ApiDoc( * requirements={ * {"name"="ranges", "dataType"="array", "requirement"="\w+", "description"="The range array for the annotation"}, @@ -555,15 +551,20 @@ class WallabagRestController extends FOSRestController * {"name"="text", "dataType"="string", "required"=true, "description"=""}, * } * ) + * + * @param Request $request + * @param Entry $entry + * + * @return JsonResponse */ public function postAnnotationAction(Request $request, Entry $entry) { $this->validateAuthentication(); - return $this->forward('WallabagApiBundle:WallabagRest:postAnnotation', [ - 'request' => $request, - 'entry' => $entry, - ]); + return $this->forward('WallabagAnnotationBundle:WallabagAnnotation:postAnnotation', [ + 'request' => $request, + 'entry' => $entry, + ]); } /** @@ -586,10 +587,10 @@ class WallabagRestController extends FOSRestController { $this->validateAuthentication(); - return $this->forward('WallabagApiBundle:WallabagRest:putAnnotation', [ - 'annotation' => $annotation, - 'request' => $request, - ]); + return $this->forward('WallabagAnnotationBundle:WallabagAnnotation:putAnnotation', [ + 'annotation' => $annotation, + 'request' => $request, + ]); } /** @@ -611,9 +612,9 @@ class WallabagRestController extends FOSRestController { $this->validateAuthentication(); - return $this->forward('WallabagApiBundle:WallabagRest:deleteAnnotation', [ - 'annotation' => $annotation, - ]); + return $this->forward('WallabagAnnotationBundle:WallabagAnnotation:deleteAnnotation', [ + 'annotation' => $annotation, + ]); } /** diff --git a/tests/Wallabag/AnnotationBundle/Controller/AnnotationControllerTest.php b/tests/Wallabag/AnnotationBundle/Controller/AnnotationControllerTest.php index 8c23de45..cee0b847 100644 --- a/tests/Wallabag/AnnotationBundle/Controller/AnnotationControllerTest.php +++ b/tests/Wallabag/AnnotationBundle/Controller/AnnotationControllerTest.php @@ -8,40 +8,75 @@ use Wallabag\CoreBundle\Entity\Entry; class AnnotationControllerTest extends WallabagAnnotationTestCase { + /** + * This data provider allow to tests annotation from the : + * - API POV (when user use the api to manage annotations) + * - and User POV (when user use the web interface - using javascript - to manage annotations) + */ + public function dataForEachAnnotations() + { + return [ + ['/api/annotations'], + ['annotations'], + ]; + } + /** * Test fetching annotations for an entry. + * + * @dataProvider dataForEachAnnotations */ - public function testGetAnnotations() + public function testGetAnnotations($prefixUrl) { - /** @var Annotation $annotation */ - $annotation = $this->client->getContainer() - ->get('doctrine.orm.entity_manager') - ->getRepository('WallabagAnnotationBundle:Annotation') - ->findOneByUsername('admin'); + $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); + + $user = $em + ->getRepository('WallabagUserBundle:User') + ->findOneByUserName('admin'); + $entry = $em + ->getRepository('WallabagCoreBundle:Entry') + ->findOneByUsernameAndNotArchived('admin'); + + $annotation = new Annotation($user); + $annotation->setEntry($entry); + $annotation->setText('This is my annotation /o/'); + $annotation->setQuote('content'); - if (!$annotation) { - $this->markTestSkipped('No content found in db.'); + $em->persist($annotation); + $em->flush(); + + if ('annotations' === $prefixUrl) { + $this->logInAs('admin'); } - $this->logInAs('admin'); - $this->client->request('GET', 'annotations/'.$annotation->getEntry()->getId().'.json'); + $this->client->request('GET', $prefixUrl.'/'.$entry->getId().'.json'); $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); $content = json_decode($this->client->getResponse()->getContent(), true); - $this->assertEquals(1, $content['total']); + $this->assertGreaterThanOrEqual(1, $content['total']); $this->assertEquals($annotation->getText(), $content['rows'][0]['text']); + + // we need to re-fetch the annotation becase after the flush, it has been "detached" from the entity manager + $annotation = $em->getRepository('WallabagAnnotationBundle:Annotation')->findAnnotationById($annotation->getId()); + $em->remove($annotation); + $em->flush(); } /** * Test creating an annotation for an entry. + * + * @dataProvider dataForEachAnnotations */ - public function testSetAnnotation() + public function testSetAnnotation($prefixUrl) { - $this->logInAs('admin'); + $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); + + if ('annotations' === $prefixUrl) { + $this->logInAs('admin'); + } /** @var Entry $entry */ - $entry = $this->client->getContainer() - ->get('doctrine.orm.entity_manager') + $entry = $em ->getRepository('WallabagCoreBundle:Entry') ->findOneByUsernameAndNotArchived('admin'); @@ -51,7 +86,7 @@ class AnnotationControllerTest extends WallabagAnnotationTestCase 'quote' => 'my quote', 'ranges' => ['start' => '', 'startOffset' => 24, 'end' => '', 'endOffset' => 31], ]); - $this->client->request('POST', 'annotations/'.$entry->getId().'.json', [], [], $headers, $content); + $this->client->request('POST', $prefixUrl.'/'.$entry->getId().'.json', [], [], $headers, $content); $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); @@ -73,22 +108,33 @@ class AnnotationControllerTest extends WallabagAnnotationTestCase /** * Test editing an existing annotation. + * + * @dataProvider dataForEachAnnotations */ - public function testEditAnnotation() + public function testEditAnnotation($prefixUrl) { - /** @var Annotation $annotation */ - $annotation = $this->client->getContainer() - ->get('doctrine.orm.entity_manager') - ->getRepository('WallabagAnnotationBundle:Annotation') - ->findOneByUsername('admin'); + $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); + + $user = $em + ->getRepository('WallabagUserBundle:User') + ->findOneByUserName('admin'); + $entry = $em + ->getRepository('WallabagCoreBundle:Entry') + ->findOneByUsernameAndNotArchived('admin'); - $this->logInAs('admin'); + $annotation = new Annotation($user); + $annotation->setEntry($entry); + $annotation->setText('This is my annotation /o/'); + $annotation->setQuote('my quote'); + + $em->persist($annotation); + $em->flush(); $headers = ['CONTENT_TYPE' => 'application/json']; $content = json_encode([ 'text' => 'a modified annotation', ]); - $this->client->request('PUT', 'annotations/'.$annotation->getId().'.json', [], [], $headers, $content); + $this->client->request('PUT', $prefixUrl.'/'.$annotation->getId().'.json', [], [], $headers, $content); $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); $content = json_decode($this->client->getResponse()->getContent(), true); @@ -99,39 +145,55 @@ class AnnotationControllerTest extends WallabagAnnotationTestCase $this->assertEquals('my quote', $content['quote']); /** @var Annotation $annotationUpdated */ - $annotationUpdated = $this->client->getContainer() - ->get('doctrine.orm.entity_manager') + $annotationUpdated = $em ->getRepository('WallabagAnnotationBundle:Annotation') ->findOneById($annotation->getId()); $this->assertEquals('a modified annotation', $annotationUpdated->getText()); + + $em->remove($annotationUpdated); + $em->flush(); } /** * Test deleting an annotation. + * + * @dataProvider dataForEachAnnotations */ - public function testDeleteAnnotation() + public function testDeleteAnnotation($prefixUrl) { - /** @var Annotation $annotation */ - $annotation = $this->client->getContainer() - ->get('doctrine.orm.entity_manager') - ->getRepository('WallabagAnnotationBundle:Annotation') - ->findOneByUsername('admin'); + $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); + + $user = $em + ->getRepository('WallabagUserBundle:User') + ->findOneByUserName('admin'); + $entry = $em + ->getRepository('WallabagCoreBundle:Entry') + ->findOneByUsernameAndNotArchived('admin'); - $this->logInAs('admin'); + $annotation = new Annotation($user); + $annotation->setEntry($entry); + $annotation->setText('This is my annotation /o/'); + $annotation->setQuote('my quote'); + + $em->persist($annotation); + $em->flush(); + + if ('annotations' === $prefixUrl) { + $this->logInAs('admin'); + } $headers = ['CONTENT_TYPE' => 'application/json']; $content = json_encode([ 'text' => 'a modified annotation', ]); - $this->client->request('DELETE', 'annotations/'.$annotation->getId().'.json', [], [], $headers, $content); + $this->client->request('DELETE', $prefixUrl.'/'.$annotation->getId().'.json', [], [], $headers, $content); $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); $content = json_decode($this->client->getResponse()->getContent(), true); - $this->assertEquals('a modified annotation', $content['text']); + $this->assertEquals('This is my annotation /o/', $content['text']); - $annotationDeleted = $this->client->getContainer() - ->get('doctrine.orm.entity_manager') + $annotationDeleted = $em ->getRepository('WallabagAnnotationBundle:Annotation') ->findOneById($annotation->getId());