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')
->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()
;
}

View 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,
]);
}
/**

View file

@ -9,39 +9,74 @@ use Wallabag\CoreBundle\Entity\Entry;
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 */
$annotation = $this->client->getContainer()
->get('doctrine.orm.entity_manager')
->getRepository('WallabagAnnotationBundle:Annotation')
->findOneByUsername('admin');
return [
['/api/annotations'],
['annotations'],
];
}
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', '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');
$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'];
$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');
$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'];
$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());