2016-02-07 15:52:59 +00:00
|
|
|
<?php
|
|
|
|
|
2016-06-01 19:27:35 +00:00
|
|
|
namespace Tests\AnnotationBundle\Controller;
|
2016-02-07 15:52:59 +00:00
|
|
|
|
2016-06-01 19:27:35 +00:00
|
|
|
use Tests\Wallabag\AnnotationBundle\WallabagAnnotationTestCase;
|
2016-10-09 12:01:28 +00:00
|
|
|
use Wallabag\AnnotationBundle\Entity\Annotation;
|
|
|
|
use Wallabag\CoreBundle\Entity\Entry;
|
2016-02-07 15:52:59 +00:00
|
|
|
|
2016-02-26 12:59:08 +00:00
|
|
|
class AnnotationControllerTest extends WallabagAnnotationTestCase
|
2016-02-07 15:52:59 +00:00
|
|
|
{
|
2016-10-22 10:09:20 +00:00
|
|
|
/**
|
|
|
|
* This data provider allow to tests annotation from the :
|
|
|
|
* - API POV (when user use the api to manage annotations)
|
2016-10-28 08:55:39 +00:00
|
|
|
* - and User POV (when user use the web interface - using javascript - to manage annotations).
|
2016-10-22 10:09:20 +00:00
|
|
|
*/
|
|
|
|
public function dataForEachAnnotations()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
['/api/annotations'],
|
|
|
|
['annotations'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-10-09 12:01:28 +00:00
|
|
|
/**
|
2016-10-14 12:36:08 +00:00
|
|
|
* Test fetching annotations for an entry.
|
2016-10-22 10:09:20 +00:00
|
|
|
*
|
|
|
|
* @dataProvider dataForEachAnnotations
|
2016-10-09 12:01:28 +00:00
|
|
|
*/
|
2016-10-22 10:09:20 +00:00
|
|
|
public function testGetAnnotations($prefixUrl)
|
2016-02-07 15:52:59 +00:00
|
|
|
{
|
2016-10-22 10:09:20 +00:00
|
|
|
$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');
|
2016-02-07 15:52:59 +00:00
|
|
|
|
2016-10-22 10:09:20 +00:00
|
|
|
$em->persist($annotation);
|
|
|
|
$em->flush();
|
|
|
|
|
|
|
|
if ('annotations' === $prefixUrl) {
|
|
|
|
$this->logInAs('admin');
|
2016-02-07 15:52:59 +00:00
|
|
|
}
|
2016-03-11 16:56:41 +00:00
|
|
|
|
2016-10-22 10:09:20 +00:00
|
|
|
$this->client->request('GET', $prefixUrl.'/'.$entry->getId().'.json');
|
2016-02-07 15:52:59 +00:00
|
|
|
$this->assertEquals(200, $this->client->getResponse()->getStatusCode());
|
|
|
|
|
|
|
|
$content = json_decode($this->client->getResponse()->getContent(), true);
|
2016-10-22 10:09:20 +00:00
|
|
|
$this->assertGreaterThanOrEqual(1, $content['total']);
|
2016-02-26 12:59:08 +00:00
|
|
|
$this->assertEquals($annotation->getText(), $content['rows'][0]['text']);
|
2016-10-22 10:09:20 +00:00
|
|
|
|
|
|
|
// 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();
|
2016-02-07 15:52:59 +00:00
|
|
|
}
|
|
|
|
|
2016-10-09 12:01:28 +00:00
|
|
|
/**
|
2016-10-14 12:36:08 +00:00
|
|
|
* Test creating an annotation for an entry.
|
2016-10-22 10:09:20 +00:00
|
|
|
*
|
|
|
|
* @dataProvider dataForEachAnnotations
|
2016-10-09 12:01:28 +00:00
|
|
|
*/
|
2016-10-22 10:09:20 +00:00
|
|
|
public function testSetAnnotation($prefixUrl)
|
2016-02-07 15:52:59 +00:00
|
|
|
{
|
2016-10-22 10:09:20 +00:00
|
|
|
$em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
|
|
|
|
|
|
|
|
if ('annotations' === $prefixUrl) {
|
|
|
|
$this->logInAs('admin');
|
|
|
|
}
|
2016-02-07 15:52:59 +00:00
|
|
|
|
2016-10-09 12:01:28 +00:00
|
|
|
/** @var Entry $entry */
|
2016-10-22 10:09:20 +00:00
|
|
|
$entry = $em
|
2016-02-07 15:52:59 +00:00
|
|
|
->getRepository('WallabagCoreBundle:Entry')
|
2016-03-11 16:56:41 +00:00
|
|
|
->findOneByUsernameAndNotArchived('admin');
|
2016-02-07 15:52:59 +00:00
|
|
|
|
2016-04-12 09:36:01 +00:00
|
|
|
$headers = ['CONTENT_TYPE' => 'application/json'];
|
|
|
|
$content = json_encode([
|
2016-02-26 12:59:08 +00:00
|
|
|
'text' => 'my annotation',
|
2016-02-07 15:52:59 +00:00
|
|
|
'quote' => 'my quote',
|
2016-04-12 09:36:01 +00:00
|
|
|
'ranges' => ['start' => '', 'startOffset' => 24, 'end' => '', 'endOffset' => 31],
|
|
|
|
]);
|
2016-10-22 10:09:20 +00:00
|
|
|
$this->client->request('POST', $prefixUrl.'/'.$entry->getId().'.json', [], [], $headers, $content);
|
2016-02-07 15:52:59 +00:00
|
|
|
|
|
|
|
$this->assertEquals(200, $this->client->getResponse()->getStatusCode());
|
|
|
|
|
2016-03-11 16:56:41 +00:00
|
|
|
$content = json_decode($this->client->getResponse()->getContent(), true);
|
|
|
|
|
|
|
|
$this->assertEquals('Big boss', $content['user']);
|
|
|
|
$this->assertEquals('v1.0', $content['annotator_schema_version']);
|
|
|
|
$this->assertEquals('my annotation', $content['text']);
|
|
|
|
$this->assertEquals('my quote', $content['quote']);
|
|
|
|
|
2016-10-09 12:01:28 +00:00
|
|
|
/** @var Annotation $annotation */
|
2016-02-26 12:59:08 +00:00
|
|
|
$annotation = $this->client->getContainer()
|
2016-02-07 15:52:59 +00:00
|
|
|
->get('doctrine.orm.entity_manager')
|
2016-02-26 12:59:08 +00:00
|
|
|
->getRepository('WallabagAnnotationBundle:Annotation')
|
|
|
|
->findLastAnnotationByPageId($entry->getId(), 1);
|
2016-02-07 15:52:59 +00:00
|
|
|
|
2016-02-26 12:59:08 +00:00
|
|
|
$this->assertEquals('my annotation', $annotation->getText());
|
2016-02-07 15:52:59 +00:00
|
|
|
}
|
|
|
|
|
2016-10-09 12:01:28 +00:00
|
|
|
/**
|
2016-10-14 12:36:08 +00:00
|
|
|
* Test editing an existing annotation.
|
2016-10-22 10:09:20 +00:00
|
|
|
*
|
|
|
|
* @dataProvider dataForEachAnnotations
|
2016-10-09 12:01:28 +00:00
|
|
|
*/
|
2016-10-22 10:09:20 +00:00
|
|
|
public function testEditAnnotation($prefixUrl)
|
2016-02-07 15:52:59 +00:00
|
|
|
{
|
2016-10-22 10:09:20 +00:00
|
|
|
$em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
|
|
|
|
|
|
|
|
$user = $em
|
|
|
|
->getRepository('WallabagUserBundle:User')
|
|
|
|
->findOneByUserName('admin');
|
|
|
|
$entry = $em
|
|
|
|
->getRepository('WallabagCoreBundle:Entry')
|
|
|
|
->findOneByUsernameAndNotArchived('admin');
|
2016-02-07 15:52:59 +00:00
|
|
|
|
2016-10-22 10:09:20 +00:00
|
|
|
$annotation = new Annotation($user);
|
|
|
|
$annotation->setEntry($entry);
|
|
|
|
$annotation->setText('This is my annotation /o/');
|
|
|
|
$annotation->setQuote('my quote');
|
|
|
|
|
|
|
|
$em->persist($annotation);
|
|
|
|
$em->flush();
|
2016-02-07 15:52:59 +00:00
|
|
|
|
2016-04-12 09:36:01 +00:00
|
|
|
$headers = ['CONTENT_TYPE' => 'application/json'];
|
|
|
|
$content = json_encode([
|
2016-02-26 12:59:08 +00:00
|
|
|
'text' => 'a modified annotation',
|
2016-04-12 09:36:01 +00:00
|
|
|
]);
|
2016-10-22 10:09:20 +00:00
|
|
|
$this->client->request('PUT', $prefixUrl.'/'.$annotation->getId().'.json', [], [], $headers, $content);
|
2016-02-07 15:52:59 +00:00
|
|
|
$this->assertEquals(200, $this->client->getResponse()->getStatusCode());
|
|
|
|
|
|
|
|
$content = json_decode($this->client->getResponse()->getContent(), true);
|
|
|
|
|
2016-03-11 16:56:41 +00:00
|
|
|
$this->assertEquals('Big boss', $content['user']);
|
|
|
|
$this->assertEquals('v1.0', $content['annotator_schema_version']);
|
2016-02-26 12:59:08 +00:00
|
|
|
$this->assertEquals('a modified annotation', $content['text']);
|
2016-03-12 09:45:14 +00:00
|
|
|
$this->assertEquals('my quote', $content['quote']);
|
2016-02-07 15:52:59 +00:00
|
|
|
|
2016-10-09 12:01:28 +00:00
|
|
|
/** @var Annotation $annotationUpdated */
|
2016-10-22 10:09:20 +00:00
|
|
|
$annotationUpdated = $em
|
2016-02-26 12:59:08 +00:00
|
|
|
->getRepository('WallabagAnnotationBundle:Annotation')
|
2016-03-11 16:56:41 +00:00
|
|
|
->findOneById($annotation->getId());
|
2016-02-26 12:59:08 +00:00
|
|
|
$this->assertEquals('a modified annotation', $annotationUpdated->getText());
|
2016-10-22 10:09:20 +00:00
|
|
|
|
|
|
|
$em->remove($annotationUpdated);
|
|
|
|
$em->flush();
|
2016-02-07 15:52:59 +00:00
|
|
|
}
|
2016-03-11 16:56:41 +00:00
|
|
|
|
2016-10-09 12:01:28 +00:00
|
|
|
/**
|
2016-10-14 12:36:08 +00:00
|
|
|
* Test deleting an annotation.
|
2016-10-22 10:09:20 +00:00
|
|
|
*
|
|
|
|
* @dataProvider dataForEachAnnotations
|
2016-10-09 12:01:28 +00:00
|
|
|
*/
|
2016-10-22 10:09:20 +00:00
|
|
|
public function testDeleteAnnotation($prefixUrl)
|
2016-03-11 16:56:41 +00:00
|
|
|
{
|
2016-10-22 10:09:20 +00:00
|
|
|
$em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
|
|
|
|
|
|
|
|
$user = $em
|
|
|
|
->getRepository('WallabagUserBundle:User')
|
|
|
|
->findOneByUserName('admin');
|
|
|
|
$entry = $em
|
|
|
|
->getRepository('WallabagCoreBundle:Entry')
|
|
|
|
->findOneByUsernameAndNotArchived('admin');
|
2016-03-11 16:56:41 +00:00
|
|
|
|
2016-10-22 10:09:20 +00:00
|
|
|
$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');
|
|
|
|
}
|
2016-03-11 16:56:41 +00:00
|
|
|
|
2016-04-12 09:36:01 +00:00
|
|
|
$headers = ['CONTENT_TYPE' => 'application/json'];
|
|
|
|
$content = json_encode([
|
2016-03-11 16:56:41 +00:00
|
|
|
'text' => 'a modified annotation',
|
2016-04-12 09:36:01 +00:00
|
|
|
]);
|
2016-10-22 10:09:20 +00:00
|
|
|
$this->client->request('DELETE', $prefixUrl.'/'.$annotation->getId().'.json', [], [], $headers, $content);
|
2016-03-11 16:56:41 +00:00
|
|
|
$this->assertEquals(200, $this->client->getResponse()->getStatusCode());
|
|
|
|
|
|
|
|
$content = json_decode($this->client->getResponse()->getContent(), true);
|
|
|
|
|
2016-10-22 10:09:20 +00:00
|
|
|
$this->assertEquals('This is my annotation /o/', $content['text']);
|
2016-03-11 16:56:41 +00:00
|
|
|
|
2016-10-22 10:09:20 +00:00
|
|
|
$annotationDeleted = $em
|
2016-03-11 16:56:41 +00:00
|
|
|
->getRepository('WallabagAnnotationBundle:Annotation')
|
|
|
|
->findOneById($annotation->getId());
|
|
|
|
|
|
|
|
$this->assertNull($annotationDeleted);
|
|
|
|
}
|
2016-02-07 15:52:59 +00:00
|
|
|
}
|