1
0
Fork 0
mirror of https://github.com/wallabag/wallabag.git synced 2025-07-22 17:18:37 +00:00

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
This commit is contained in:
Jeremy Benoist 2016-10-22 12:09:20 +02:00
parent 3199ec4702
commit aa4741091f
No known key found for this signature in database
GPG key ID: BCA73962457ACC3C
3 changed files with 121 additions and 56 deletions

View file

@ -50,7 +50,8 @@ class AnnotationRepository extends EntityRepository
{ {
return $this->createQueryBuilder('a') return $this->createQueryBuilder('a')
->andWhere('a.id = :annotationId')->setParameter('annotationId', $annotationId) ->andWhere('a.id = :annotationId')->setParameter('annotationId', $annotationId)
->getQuery()->getSingleResult() ->getQuery()
->getSingleResult()
; ;
} }
@ -67,7 +68,8 @@ class AnnotationRepository extends EntityRepository
return $this->createQueryBuilder('a') return $this->createQueryBuilder('a')
->where('a.entry = :entryId')->setParameter('entryId', $entryId) ->where('a.entry = :entryId')->setParameter('entryId', $entryId)
->andwhere('a.user = :userId')->setParameter('userId', $userId) ->andwhere('a.user = :userId')->setParameter('userId', $userId)
->getQuery()->getResult() ->getQuery()
->getResult()
; ;
} }

View file

@ -536,7 +536,7 @@ class WallabagRestController extends FOSRestController
{ {
$this->validateAuthentication(); $this->validateAuthentication();
return $this->forward('WallabagApiBundle:WallabagRest:getAnnotations', [ return $this->forward('WallabagAnnotationBundle:WallabagAnnotation:getAnnotations', [
'entry' => $entry, 'entry' => $entry,
]); ]);
} }
@ -544,10 +544,6 @@ class WallabagRestController extends FOSRestController
/** /**
* Creates a new annotation. * Creates a new annotation.
* *
* @param Request $request
* @param Entry $entry
*
* @return JsonResponse
* @ApiDoc( * @ApiDoc(
* requirements={ * requirements={
* {"name"="ranges", "dataType"="array", "requirement"="\w+", "description"="The range array for the annotation"}, * {"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"=""}, * {"name"="text", "dataType"="string", "required"=true, "description"=""},
* } * }
* ) * )
*
* @param Request $request
* @param Entry $entry
*
* @return JsonResponse
*/ */
public function postAnnotationAction(Request $request, Entry $entry) public function postAnnotationAction(Request $request, Entry $entry)
{ {
$this->validateAuthentication(); $this->validateAuthentication();
return $this->forward('WallabagApiBundle:WallabagRest:postAnnotation', [ return $this->forward('WallabagAnnotationBundle:WallabagAnnotation:postAnnotation', [
'request' => $request, 'request' => $request,
'entry' => $entry, 'entry' => $entry,
]); ]);
} }
/** /**
@ -586,10 +587,10 @@ class WallabagRestController extends FOSRestController
{ {
$this->validateAuthentication(); $this->validateAuthentication();
return $this->forward('WallabagApiBundle:WallabagRest:putAnnotation', [ return $this->forward('WallabagAnnotationBundle:WallabagAnnotation:putAnnotation', [
'annotation' => $annotation, 'annotation' => $annotation,
'request' => $request, 'request' => $request,
]); ]);
} }
/** /**
@ -611,9 +612,9 @@ class WallabagRestController extends FOSRestController
{ {
$this->validateAuthentication(); $this->validateAuthentication();
return $this->forward('WallabagApiBundle:WallabagRest:deleteAnnotation', [ return $this->forward('WallabagAnnotationBundle:WallabagAnnotation:deleteAnnotation', [
'annotation' => $annotation, 'annotation' => $annotation,
]); ]);
} }
/** /**

View file

@ -9,39 +9,74 @@ use Wallabag\CoreBundle\Entity\Entry;
class AnnotationControllerTest extends WallabagAnnotationTestCase class AnnotationControllerTest extends WallabagAnnotationTestCase
{ {
/** /**
* Test fetching annotations for an entry. * 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 testGetAnnotations() public function dataForEachAnnotations()
{ {
/** @var Annotation $annotation */ return [
$annotation = $this->client->getContainer() ['/api/annotations'],
->get('doctrine.orm.entity_manager') ['annotations'],
->getRepository('WallabagAnnotationBundle:Annotation') ];
->findOneByUsername('admin'); }
if (!$annotation) { /**
$this->markTestSkipped('No content found in db.'); * Test fetching annotations for an entry.
*
* @dataProvider dataForEachAnnotations
*/
public function testGetAnnotations($prefixUrl)
{
$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');
$em->persist($annotation);
$em->flush();
if ('annotations' === $prefixUrl) {
$this->logInAs('admin');
} }
$this->logInAs('admin'); $this->client->request('GET', $prefixUrl.'/'.$entry->getId().'.json');
$this->client->request('GET', 'annotations/'.$annotation->getEntry()->getId().'.json');
$this->assertEquals(200, $this->client->getResponse()->getStatusCode()); $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
$content = json_decode($this->client->getResponse()->getContent(), true); $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']); $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. * 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 */ /** @var Entry $entry */
$entry = $this->client->getContainer() $entry = $em
->get('doctrine.orm.entity_manager')
->getRepository('WallabagCoreBundle:Entry') ->getRepository('WallabagCoreBundle:Entry')
->findOneByUsernameAndNotArchived('admin'); ->findOneByUsernameAndNotArchived('admin');
@ -51,7 +86,7 @@ class AnnotationControllerTest extends WallabagAnnotationTestCase
'quote' => 'my quote', 'quote' => 'my quote',
'ranges' => ['start' => '', 'startOffset' => 24, 'end' => '', 'endOffset' => 31], '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()); $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
@ -73,22 +108,33 @@ class AnnotationControllerTest extends WallabagAnnotationTestCase
/** /**
* Test editing an existing annotation. * Test editing an existing annotation.
*
* @dataProvider dataForEachAnnotations
*/ */
public function testEditAnnotation() public function testEditAnnotation($prefixUrl)
{ {
/** @var Annotation $annotation */ $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
$annotation = $this->client->getContainer()
->get('doctrine.orm.entity_manager')
->getRepository('WallabagAnnotationBundle:Annotation')
->findOneByUsername('admin');
$this->logInAs('admin'); $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('my quote');
$em->persist($annotation);
$em->flush();
$headers = ['CONTENT_TYPE' => 'application/json']; $headers = ['CONTENT_TYPE' => 'application/json'];
$content = json_encode([ $content = json_encode([
'text' => 'a modified annotation', '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()); $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
$content = json_decode($this->client->getResponse()->getContent(), true); $content = json_decode($this->client->getResponse()->getContent(), true);
@ -99,39 +145,55 @@ class AnnotationControllerTest extends WallabagAnnotationTestCase
$this->assertEquals('my quote', $content['quote']); $this->assertEquals('my quote', $content['quote']);
/** @var Annotation $annotationUpdated */ /** @var Annotation $annotationUpdated */
$annotationUpdated = $this->client->getContainer() $annotationUpdated = $em
->get('doctrine.orm.entity_manager')
->getRepository('WallabagAnnotationBundle:Annotation') ->getRepository('WallabagAnnotationBundle:Annotation')
->findOneById($annotation->getId()); ->findOneById($annotation->getId());
$this->assertEquals('a modified annotation', $annotationUpdated->getText()); $this->assertEquals('a modified annotation', $annotationUpdated->getText());
$em->remove($annotationUpdated);
$em->flush();
} }
/** /**
* Test deleting an annotation. * Test deleting an annotation.
*
* @dataProvider dataForEachAnnotations
*/ */
public function testDeleteAnnotation() public function testDeleteAnnotation($prefixUrl)
{ {
/** @var Annotation $annotation */ $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
$annotation = $this->client->getContainer()
->get('doctrine.orm.entity_manager')
->getRepository('WallabagAnnotationBundle:Annotation')
->findOneByUsername('admin');
$this->logInAs('admin'); $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('my quote');
$em->persist($annotation);
$em->flush();
if ('annotations' === $prefixUrl) {
$this->logInAs('admin');
}
$headers = ['CONTENT_TYPE' => 'application/json']; $headers = ['CONTENT_TYPE' => 'application/json'];
$content = json_encode([ $content = json_encode([
'text' => 'a modified annotation', '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()); $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
$content = json_decode($this->client->getResponse()->getContent(), true); $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() $annotationDeleted = $em
->get('doctrine.orm.entity_manager')
->getRepository('WallabagAnnotationBundle:Annotation') ->getRepository('WallabagAnnotationBundle:Annotation')
->findOneById($annotation->getId()); ->findOneById($annotation->getId());