wallabag/tests/Wallabag/AnnotationBundle/Controller/AnnotationControllerTest.php

293 lines
10 KiB
PHP
Raw Normal View History

<?php
namespace Tests\Wallabag\AnnotationBundle\Controller;
2022-08-28 00:01:46 +00:00
use Doctrine\ORM\EntityManagerInterface;
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;
2022-08-25 19:37:10 +00:00
use Wallabag\UserBundle\Entity\User;
class AnnotationControllerTest extends WallabagAnnotationTestCase
{
/**
* 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).
*/
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.
*
* @dataProvider dataForEachAnnotations
2016-10-09 12:01:28 +00:00
*/
public function testGetAnnotations($prefixUrl)
{
2022-08-28 00:01:46 +00:00
$em = $this->client->getContainer()->get(EntityManagerInterface::class);
$user = $em
2022-08-25 19:37:10 +00:00
->getRepository(User::class)
->findOneByUserName('admin');
$entry = $em
2022-08-25 19:37:10 +00:00
->getRepository(Entry::class)
->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');
}
2017-07-01 07:52:38 +00:00
$this->client->request('GET', $prefixUrl . '/' . $entry->getId() . '.json');
$this->assertSame(200, $this->client->getResponse()->getStatusCode());
$content = json_decode($this->client->getResponse()->getContent(), true);
$this->assertGreaterThanOrEqual(1, $content['total']);
2017-07-01 07:52:38 +00:00
$this->assertSame($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
2022-08-25 19:37:10 +00:00
$annotation = $em->getRepository(Annotation::class)->findAnnotationById($annotation->getId());
$em->remove($annotation);
$em->flush();
}
2016-10-09 12:01:28 +00:00
/**
2016-10-14 12:36:08 +00:00
* Test creating an annotation for an entry.
*
* @dataProvider dataForEachAnnotations
2016-10-09 12:01:28 +00:00
*/
public function testSetAnnotation($prefixUrl)
{
2022-08-28 00:01:46 +00:00
$em = $this->client->getContainer()->get(EntityManagerInterface::class);
if ('annotations' === $prefixUrl) {
$this->logInAs('admin');
}
2016-10-09 12:01:28 +00:00
/** @var Entry $entry */
$entry = $em
2022-08-25 19:37:10 +00:00
->getRepository(Entry::class)
->findOneByUsernameAndNotArchived('admin');
$headers = ['CONTENT_TYPE' => 'application/json'];
$content = json_encode([
'text' => 'my annotation',
'quote' => 'my quote',
'ranges' => [
2017-06-07 21:23:34 +00:00
['start' => '', 'startOffset' => 24, 'end' => '', 'endOffset' => 31],
],
]);
2017-07-01 07:52:38 +00:00
$this->client->request('POST', $prefixUrl . '/' . $entry->getId() . '.json', [], [], $headers, $content);
2017-07-01 07:52:38 +00:00
$this->assertSame(200, $this->client->getResponse()->getStatusCode());
$content = json_decode($this->client->getResponse()->getContent(), true);
2017-07-01 07:52:38 +00:00
$this->assertSame('Big boss', $content['user']);
$this->assertSame('v1.0', $content['annotator_schema_version']);
$this->assertSame('my annotation', $content['text']);
$this->assertSame('my quote', $content['quote']);
2016-10-09 12:01:28 +00:00
/** @var Annotation $annotation */
2018-11-26 21:22:49 +00:00
$annotation = $em
2022-08-25 19:37:10 +00:00
->getRepository(Annotation::class)
->findLastAnnotationByPageId($entry->getId(), 1);
2017-07-01 07:52:38 +00:00
$this->assertSame('my annotation', $annotation->getText());
}
2019-11-27 13:04:11 +00:00
public function testAllowEmptyQuote()
{
2022-08-28 00:01:46 +00:00
$em = $this->client->getContainer()->get(EntityManagerInterface::class);
/** @var Entry $entry */
$entry = $em
2022-08-25 19:37:10 +00:00
->getRepository(Entry::class)
->findOneByUsernameAndNotArchived('admin');
$headers = ['CONTENT_TYPE' => 'application/json'];
$content = json_encode([
'text' => 'my annotation',
'quote' => null,
'ranges' => [
['start' => '', 'startOffset' => 24, 'end' => '', 'endOffset' => 31],
],
]);
$this->client->request('POST', '/api/annotations/' . $entry->getId() . '.json', [], [], $headers, $content);
2019-11-27 13:04:11 +00:00
$this->assertSame(200, $this->client->getResponse()->getStatusCode());
$content = json_decode($this->client->getResponse()->getContent(), true);
2019-11-27 13:04:11 +00:00
$this->assertSame('Big boss', $content['user']);
$this->assertSame('v1.0', $content['annotator_schema_version']);
$this->assertSame('my annotation', $content['text']);
$this->assertSame('', $content['quote']);
}
public function testAllowOmmittedQuote()
{
2022-08-28 00:01:46 +00:00
$em = $this->client->getContainer()->get(EntityManagerInterface::class);
2019-11-27 13:04:11 +00:00
/** @var Entry $entry */
$entry = $em
2022-08-25 19:37:10 +00:00
->getRepository(Entry::class)
2019-11-27 13:04:11 +00:00
->findOneByUsernameAndNotArchived('admin');
$headers = ['CONTENT_TYPE' => 'application/json'];
$content = json_encode([
'text' => 'my new annotation',
'ranges' => [
['start' => '', 'startOffset' => 25, 'end' => '', 'endOffset' => 32],
],
]);
$this->client->request('POST', '/api/annotations/' . $entry->getId() . '.json', [], [], $headers, $content);
$this->assertSame(200, $this->client->getResponse()->getStatusCode());
$content = json_decode($this->client->getResponse()->getContent(), true);
$this->assertSame('Big boss', $content['user']);
$this->assertSame('v1.0', $content['annotator_schema_version']);
$this->assertSame('my new annotation', $content['text']);
$this->assertSame('', $content['quote']);
}
/**
* @dataProvider dataForEachAnnotations
*/
public function testSetAnnotationWithQuoteTooLong($prefixUrl)
{
2022-08-28 00:01:46 +00:00
$em = $this->client->getContainer()->get(EntityManagerInterface::class);
if ('annotations' === $prefixUrl) {
$this->logInAs('admin');
}
/** @var Entry $entry */
$entry = $em
2022-08-25 19:37:10 +00:00
->getRepository(Entry::class)
->findOneByUsernameAndNotArchived('admin');
$longQuote = str_repeat('a', 10001);
$headers = ['CONTENT_TYPE' => 'application/json'];
$content = json_encode([
'text' => 'my annotation',
'quote' => $longQuote,
'ranges' => [
2017-06-07 21:23:34 +00:00
['start' => '', 'startOffset' => 24, 'end' => '', 'endOffset' => 31],
],
]);
2017-07-01 07:52:38 +00:00
$this->client->request('POST', $prefixUrl . '/' . $entry->getId() . '.json', [], [], $headers, $content);
2017-07-01 07:52:38 +00:00
$this->assertSame(400, $this->client->getResponse()->getStatusCode());
}
2016-10-09 12:01:28 +00:00
/**
2016-10-14 12:36:08 +00:00
* Test editing an existing annotation.
*
* @dataProvider dataForEachAnnotations
2016-10-09 12:01:28 +00:00
*/
public function testEditAnnotation($prefixUrl)
{
2022-08-28 00:01:46 +00:00
$em = $this->client->getContainer()->get(EntityManagerInterface::class);
$user = $em
2022-08-25 19:37:10 +00:00
->getRepository(User::class)
->findOneByUserName('admin');
$entry = $em
2022-08-25 19:37:10 +00:00
->getRepository(Entry::class)
->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',
]);
2017-07-01 07:52:38 +00:00
$this->client->request('PUT', $prefixUrl . '/' . $annotation->getId() . '.json', [], [], $headers, $content);
$this->assertSame(200, $this->client->getResponse()->getStatusCode());
$content = json_decode($this->client->getResponse()->getContent(), true);
2017-07-01 07:52:38 +00:00
$this->assertSame('Big boss', $content['user']);
$this->assertSame('v1.0', $content['annotator_schema_version']);
$this->assertSame('a modified annotation', $content['text']);
$this->assertSame('my quote', $content['quote']);
2016-10-09 12:01:28 +00:00
/** @var Annotation $annotationUpdated */
$annotationUpdated = $em
2022-08-25 19:37:10 +00:00
->getRepository(Annotation::class)
->findOneById($annotation->getId());
2017-07-01 07:52:38 +00:00
$this->assertSame('a modified annotation', $annotationUpdated->getText());
$em->remove($annotationUpdated);
$em->flush();
}
2016-10-09 12:01:28 +00:00
/**
2016-10-14 12:36:08 +00:00
* Test deleting an annotation.
*
* @dataProvider dataForEachAnnotations
2016-10-09 12:01:28 +00:00
*/
public function testDeleteAnnotation($prefixUrl)
{
2022-08-28 00:01:46 +00:00
$em = $this->client->getContainer()->get(EntityManagerInterface::class);
$user = $em
2022-08-25 19:37:10 +00:00
->getRepository(User::class)
->findOneByUserName('admin');
$entry = $em
2022-08-25 19:37:10 +00:00
->getRepository(Entry::class)
->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',
]);
2017-07-01 07:52:38 +00:00
$this->client->request('DELETE', $prefixUrl . '/' . $annotation->getId() . '.json', [], [], $headers, $content);
$this->assertSame(200, $this->client->getResponse()->getStatusCode());
$content = json_decode($this->client->getResponse()->getContent(), true);
2017-07-01 07:52:38 +00:00
$this->assertSame('This is my annotation /o/', $content['text']);
$annotationDeleted = $em
2022-08-25 19:37:10 +00:00
->getRepository(Annotation::class)
->findOneById($annotation->getId());
$this->assertNull($annotationDeleted);
}
}