X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=tests%2FWallabag%2FAnnotationBundle%2FController%2FAnnotationControllerTest.php;h=964744684ef07c8e7ae52e8b4a274e04a82ddb19;hb=f808b01692a835673f328d7221ba8c212caa9b61;hp=70849f74129000ca787a0a414396feb478516f81;hpb=e408d7e895e784271a55c3a200666034db0af80a;p=github%2Fwallabag%2Fwallabag.git diff --git a/tests/Wallabag/AnnotationBundle/Controller/AnnotationControllerTest.php b/tests/Wallabag/AnnotationBundle/Controller/AnnotationControllerTest.php index 70849f74..96474468 100644 --- a/tests/Wallabag/AnnotationBundle/Controller/AnnotationControllerTest.php +++ b/tests/Wallabag/AnnotationBundle/Controller/AnnotationControllerTest.php @@ -3,35 +3,80 @@ namespace Tests\AnnotationBundle\Controller; use Tests\Wallabag\AnnotationBundle\WallabagAnnotationTestCase; +use Wallabag\AnnotationBundle\Entity\Annotation; +use Wallabag\CoreBundle\Entity\Entry; class AnnotationControllerTest extends WallabagAnnotationTestCase { - public function testGetAnnotations() + /** + * 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() { - $annotation = $this->client->getContainer() - ->get('doctrine.orm.entity_manager') - ->getRepository('WallabagAnnotationBundle:Annotation') - ->findOneByUsername('admin'); + return [ + ['/api/annotations'], + ['annotations'], + ]; + } + + /** + * Test fetching annotations for an entry. + * + * @dataProvider dataForEachAnnotations + */ + public function testGetAnnotations($prefixUrl) + { + $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); - if (!$annotation) { - $this->markTestSkipped('No content found in db.'); + $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'); - $crawler = $this->client->request('GET', 'annotations/'.$annotation->getEntry()->getId().'.json'); - $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); + $this->client->request('GET', $prefixUrl . '/' . $entry->getId() . '.json'); + $this->assertSame(200, $this->client->getResponse()->getStatusCode()); $content = json_decode($this->client->getResponse()->getContent(), true); - $this->assertEquals(1, $content['total']); - $this->assertEquals($annotation->getText(), $content['rows'][0]['text']); + $this->assertGreaterThanOrEqual(1, $content['total']); + $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 + $annotation = $em->getRepository('WallabagAnnotationBundle:Annotation')->findAnnotationById($annotation->getId()); + $em->remove($annotation); + $em->flush(); } - public function testSetAnnotation() + /** + * Test creating an annotation for an entry. + * + * @dataProvider dataForEachAnnotations + */ + public function testSetAnnotation($prefixUrl) { - $this->logInAs('admin'); + $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); - $entry = $this->client->getContainer() - ->get('doctrine.orm.entity_manager') + if ('annotations' === $prefixUrl) { + $this->logInAs('admin'); + } + + /** @var Entry $entry */ + $entry = $em ->getRepository('WallabagCoreBundle:Entry') ->findOneByUsernameAndNotArchived('admin'); @@ -39,79 +84,148 @@ class AnnotationControllerTest extends WallabagAnnotationTestCase $content = json_encode([ 'text' => 'my annotation', 'quote' => 'my quote', - 'ranges' => ['start' => '', 'startOffset' => 24, 'end' => '', 'endOffset' => 31], + 'ranges' => [ + ['start' => '', 'startOffset' => 24, 'end' => '', 'endOffset' => 31], + ], ]); - $crawler = $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->assertSame(200, $this->client->getResponse()->getStatusCode()); $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']); + $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']); + /** @var Annotation $annotation */ $annotation = $this->client->getContainer() ->get('doctrine.orm.entity_manager') ->getRepository('WallabagAnnotationBundle:Annotation') ->findLastAnnotationByPageId($entry->getId(), 1); - $this->assertEquals('my annotation', $annotation->getText()); + $this->assertSame('my annotation', $annotation->getText()); } - public function testEditAnnotation() + /** + * @dataProvider dataForEachAnnotations + */ + public function testSetAnnotationWithQuoteTooLong($prefixUrl) { - $annotation = $this->client->getContainer() - ->get('doctrine.orm.entity_manager') - ->getRepository('WallabagAnnotationBundle:Annotation') - ->findOneByUsername('admin'); + $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); + + if ('annotations' === $prefixUrl) { + $this->logInAs('admin'); + } + + /** @var Entry $entry */ + $entry = $em + ->getRepository('WallabagCoreBundle:Entry') + ->findOneByUsernameAndNotArchived('admin'); + + $longQuote = str_repeat('a', 10001); + $headers = ['CONTENT_TYPE' => 'application/json']; + $content = json_encode([ + 'text' => 'my annotation', + 'quote' => $longQuote, + 'ranges' => [ + ['start' => '', 'startOffset' => 24, 'end' => '', 'endOffset' => 31], + ], + ]); + $this->client->request('POST', $prefixUrl . '/' . $entry->getId() . '.json', [], [], $headers, $content); + + $this->assertSame(400, $this->client->getResponse()->getStatusCode()); + } + + /** + * Test editing an existing annotation. + * + * @dataProvider dataForEachAnnotations + */ + public function testEditAnnotation($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('my quote'); - $this->logInAs('admin'); + $em->persist($annotation); + $em->flush(); $headers = ['CONTENT_TYPE' => 'application/json']; $content = json_encode([ 'text' => 'a modified annotation', ]); - $crawler = $this->client->request('PUT', 'annotations/'.$annotation->getId().'.json', [], [], $headers, $content); - $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); + $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); - $this->assertEquals('Big boss', $content['user']); - $this->assertEquals('v1.0', $content['annotator_schema_version']); - $this->assertEquals('a modified annotation', $content['text']); - $this->assertEquals('my quote', $content['quote']); + $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']); - $annotationUpdated = $this->client->getContainer() - ->get('doctrine.orm.entity_manager') + /** @var Annotation $annotationUpdated */ + $annotationUpdated = $em ->getRepository('WallabagAnnotationBundle:Annotation') ->findOneById($annotation->getId()); - $this->assertEquals('a modified annotation', $annotationUpdated->getText()); + $this->assertSame('a modified annotation', $annotationUpdated->getText()); + + $em->remove($annotationUpdated); + $em->flush(); } - public function testDeleteAnnotation() + /** + * Test deleting an annotation. + * + * @dataProvider dataForEachAnnotations + */ + public function testDeleteAnnotation($prefixUrl) { - $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('my quote'); - $this->logInAs('admin'); + $em->persist($annotation); + $em->flush(); + + if ('annotations' === $prefixUrl) { + $this->logInAs('admin'); + } $headers = ['CONTENT_TYPE' => 'application/json']; $content = json_encode([ 'text' => 'a modified annotation', ]); - $crawler = $this->client->request('DELETE', 'annotations/'.$annotation->getId().'.json', [], [], $headers, $content); - $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); + $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); - $this->assertEquals('a modified annotation', $content['text']); + $this->assertSame('This is my annotation /o/', $content['text']); - $annotationDeleted = $this->client->getContainer() - ->get('doctrine.orm.entity_manager') + $annotationDeleted = $em ->getRepository('WallabagAnnotationBundle:Annotation') ->findOneById($annotation->getId());