]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Add test on /api/annotations 1890/head
authorJeremy Benoist <jeremy.benoist@gmail.com>
Sat, 22 Oct 2016 10:09:20 +0000 (12:09 +0200)
committerJeremy Benoist <jeremy.benoist@gmail.com>
Sat, 22 Oct 2016 10:09:20 +0000 (12:09 +0200)
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

src/Wallabag/AnnotationBundle/Repository/AnnotationRepository.php
src/Wallabag/ApiBundle/Controller/WallabagRestController.php
tests/Wallabag/AnnotationBundle/Controller/AnnotationControllerTest.php

index 5f7da70ecfa4cb1d375fdbc94f68d9a019c2c4ce..8cccffba1366bb51c46f68de2bcec354c2b3a616 100644 (file)
@@ -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()
         ;
     }
 
index 0c709ca099dfb964715338ce96ab413ef4310900..a73d44ca2bc35f79be45dbe004df022b552baa22 100644 (file)
@@ -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,
+        ]);
     }
 
     /**
index 8c23de455b270d7f7c66c8df62733c1e69ff5cd6..cee0b8473dae5c6a766b33ed3e0bd8127493bfc6 100644 (file)
@@ -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());