From 8197f08266256f55767666b90be47f585c7a6d28 Mon Sep 17 00:00:00 2001 From: adev Date: Sun, 27 Oct 2019 18:51:32 +0100 Subject: API return an error with empty quote Fix #4137 --- .../AnnotationBundle/Entity/Annotation.php | 1 + .../AnnotationBundle/Form/NewAnnotationType.php | 1 + .../Controller/AnnotationRestController.php | 2 +- .../Controller/AnnotationControllerTest.php | 30 ++++++++++++++++++++++ 4 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/Wallabag/AnnotationBundle/Entity/Annotation.php b/src/Wallabag/AnnotationBundle/Entity/Annotation.php index a180d504..ee7c1931 100644 --- a/src/Wallabag/AnnotationBundle/Entity/Annotation.php +++ b/src/Wallabag/AnnotationBundle/Entity/Annotation.php @@ -60,6 +60,7 @@ class Annotation /** * @var string * + * @Assert\NotNull() * @Assert\Length( * max = 10000, * maxMessage = "validator.quote_length_too_high" diff --git a/src/Wallabag/AnnotationBundle/Form/NewAnnotationType.php b/src/Wallabag/AnnotationBundle/Form/NewAnnotationType.php index c73c3ded..aac6445c 100644 --- a/src/Wallabag/AnnotationBundle/Form/NewAnnotationType.php +++ b/src/Wallabag/AnnotationBundle/Form/NewAnnotationType.php @@ -18,6 +18,7 @@ class NewAnnotationType extends AbstractType ]) ->add('quote', null, [ 'empty_data' => null, + 'trim' => false, ]) ->add('ranges', CollectionType::class, [ 'entry_type' => RangeType::class, diff --git a/src/Wallabag/ApiBundle/Controller/AnnotationRestController.php b/src/Wallabag/ApiBundle/Controller/AnnotationRestController.php index f59431e4..e5b3eb27 100644 --- a/src/Wallabag/ApiBundle/Controller/AnnotationRestController.php +++ b/src/Wallabag/ApiBundle/Controller/AnnotationRestController.php @@ -37,7 +37,7 @@ class AnnotationRestController extends WallabagRestController * @ApiDoc( * requirements={ * {"name"="ranges", "dataType"="array", "requirement"="\w+", "description"="The range array for the annotation"}, - * {"name"="quote", "dataType"="string", "required"=false, "description"="Optional, quote for the annotation"}, + * {"name"="quote", "dataType"="string", "required"=true, "description"="Quote for the annotation"}, * {"name"="text", "dataType"="string", "required"=true, "description"=""}, * } * ) diff --git a/tests/Wallabag/AnnotationBundle/Controller/AnnotationControllerTest.php b/tests/Wallabag/AnnotationBundle/Controller/AnnotationControllerTest.php index 2c46e0a1..74e9ba8f 100644 --- a/tests/Wallabag/AnnotationBundle/Controller/AnnotationControllerTest.php +++ b/tests/Wallabag/AnnotationBundle/Controller/AnnotationControllerTest.php @@ -107,6 +107,36 @@ class AnnotationControllerTest extends WallabagAnnotationTestCase $this->assertSame('my annotation', $annotation->getText()); } + public function testCouldNotSetAnnotationWithoutQuote() + { + $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); + + /** @var Entry $entry */ + $entry = $em + ->getRepository('WallabagCoreBundle:Entry') + ->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); + + $this->assertSame(400, $this->client->getResponse()->getStatusCode()); + + $content = json_decode($this->client->getResponse()->getContent(), true); + + $this->assertCount( + 1, + $content['errors']['children']['quote']['errors'], + 'The quote field should contains an error' + ); + } + /** * @dataProvider dataForEachAnnotations */ -- cgit v1.2.3 From 86c1751186ab71a54e33092cd9fb83c33917f619 Mon Sep 17 00:00:00 2001 From: adev Date: Sun, 24 Nov 2019 10:44:26 +0100 Subject: Optionnal quote because the frontend does not use it --- src/Wallabag/AnnotationBundle/Entity/Annotation.php | 1 - src/Wallabag/AnnotationBundle/Form/NewAnnotationType.php | 2 +- src/Wallabag/ApiBundle/Controller/AnnotationRestController.php | 4 ++-- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Wallabag/AnnotationBundle/Entity/Annotation.php b/src/Wallabag/AnnotationBundle/Entity/Annotation.php index ee7c1931..a180d504 100644 --- a/src/Wallabag/AnnotationBundle/Entity/Annotation.php +++ b/src/Wallabag/AnnotationBundle/Entity/Annotation.php @@ -60,7 +60,6 @@ class Annotation /** * @var string * - * @Assert\NotNull() * @Assert\Length( * max = 10000, * maxMessage = "validator.quote_length_too_high" diff --git a/src/Wallabag/AnnotationBundle/Form/NewAnnotationType.php b/src/Wallabag/AnnotationBundle/Form/NewAnnotationType.php index aac6445c..48bc2c59 100644 --- a/src/Wallabag/AnnotationBundle/Form/NewAnnotationType.php +++ b/src/Wallabag/AnnotationBundle/Form/NewAnnotationType.php @@ -17,7 +17,7 @@ class NewAnnotationType extends AbstractType 'empty_data' => '', ]) ->add('quote', null, [ - 'empty_data' => null, + 'empty_data' => '', 'trim' => false, ]) ->add('ranges', CollectionType::class, [ diff --git a/src/Wallabag/ApiBundle/Controller/AnnotationRestController.php b/src/Wallabag/ApiBundle/Controller/AnnotationRestController.php index e5b3eb27..66693189 100644 --- a/src/Wallabag/ApiBundle/Controller/AnnotationRestController.php +++ b/src/Wallabag/ApiBundle/Controller/AnnotationRestController.php @@ -37,8 +37,8 @@ class AnnotationRestController extends WallabagRestController * @ApiDoc( * requirements={ * {"name"="ranges", "dataType"="array", "requirement"="\w+", "description"="The range array for the annotation"}, - * {"name"="quote", "dataType"="string", "required"=true, "description"="Quote for the annotation"}, - * {"name"="text", "dataType"="string", "required"=true, "description"=""}, + * {"name"="quote", "dataType"="string", "description"="The annotated text"}, + * {"name"="text", "dataType"="string", "required"=true, "description"="Content of annotation"}, * } * ) * -- cgit v1.2.3 From 6a0d49ab7a8ac9e00f9df34b37b6d3e1ab40f2a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Benoist?= Date: Wed, 27 Nov 2019 14:04:11 +0100 Subject: Fix tests --- .../Controller/AnnotationControllerTest.php | 41 ++++++++++++++++++---- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/tests/Wallabag/AnnotationBundle/Controller/AnnotationControllerTest.php b/tests/Wallabag/AnnotationBundle/Controller/AnnotationControllerTest.php index 74e9ba8f..260edd77 100644 --- a/tests/Wallabag/AnnotationBundle/Controller/AnnotationControllerTest.php +++ b/tests/Wallabag/AnnotationBundle/Controller/AnnotationControllerTest.php @@ -107,7 +107,7 @@ class AnnotationControllerTest extends WallabagAnnotationTestCase $this->assertSame('my annotation', $annotation->getText()); } - public function testCouldNotSetAnnotationWithoutQuote() + public function testAllowEmptyQuote() { $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); @@ -126,15 +126,42 @@ class AnnotationControllerTest extends WallabagAnnotationTestCase ]); $this->client->request('POST', '/api/annotations/' . $entry->getId() . '.json', [], [], $headers, $content); - $this->assertSame(400, $this->client->getResponse()->getStatusCode()); + $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 annotation', $content['text']); + $this->assertSame('', $content['quote']); + } + + public function testAllowOmmittedQuote() + { + $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); + + /** @var Entry $entry */ + $entry = $em + ->getRepository('WallabagCoreBundle:Entry') + ->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->assertCount( - 1, - $content['errors']['children']['quote']['errors'], - 'The quote field should contains an error' - ); + $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']); } /** -- cgit v1.2.3