From 2e3a516ec5cd42501dee5331154e4595b4fe431f Mon Sep 17 00:00:00 2001 From: Davide Iadeluca Date: Mon, 7 Oct 2024 18:50:02 +0200 Subject: [PATCH] test(mentions): implement test for post editing with content empty --- .../tests/integration/api/EditPostTest.php | 142 ++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 extensions/mentions/tests/integration/api/EditPostTest.php diff --git a/extensions/mentions/tests/integration/api/EditPostTest.php b/extensions/mentions/tests/integration/api/EditPostTest.php new file mode 100644 index 0000000000..eb00c61240 --- /dev/null +++ b/extensions/mentions/tests/integration/api/EditPostTest.php @@ -0,0 +1,142 @@ +extension('flarum-mentions'); + + $this->prepareDatabase([ + 'discussions' => [ + ['id' => 1, 'title' => '', 'user_id' => 1, 'comment_count' => 1], + ], + 'posts' => [ + ['id' => 1, 'discussion_id' => 1, 'user_id' => 1, 'type' => 'comment', 'content' => '

'], + ] + ]); + + $this->extend( + (new Extend\Event()) + ->listen(\Flarum\Post\Event\Saving::class, function ($event) { + $event->post->content; + }) + ); + } + + /** + * @test + */ + public function cannot_update_post_with_empty_string() + { + $response = $this->send( + $this->request('PATCH', '/api/posts/1', [ + 'authenticatedAs' => 1, + 'json' => [ + 'data' => [ + 'attributes' => [ + 'content' => '', + ], + ], + ], + ]) + ); + + $this->assertEquals(422, $response->getStatusCode()); + + $body = (string) $response->getBody(); + $this->assertJson($body); + $this->assertEquals([ + 'errors' => [ + [ + 'status' => '422', + 'code' => 'validation_error', + 'detail' => 'The content field is required.', + 'source' => ['pointer' => '/data/attributes/content'], + ], + ], + ], json_decode($body, true)); + } + + /** + * @test + */ + public function cannot_update_post_without_content_property() + { + $response = $this->send( + $this->request('PATCH', '/api/posts/1', [ + 'authenticatedAs' => 1, + 'json' => [ + 'data' => [ + 'attributes' => [], + ], + ], + ]) + ); + + $this->assertEquals(422, $response->getStatusCode()); + + $body = (string) $response->getBody(); + $this->assertJson($body); + $this->assertEquals([ + 'errors' => [ + [ + 'status' => '422', + 'code' => 'validation_error', + 'detail' => 'The content field is required.', + 'source' => ['pointer' => '/data/attributes/content'], + ], + ], + ], json_decode($body, true)); + } + + /** + * @test + */ + public function cannot_update_post_with_content_set_to_null() + { + $response = $this->send( + $this->request('PATCH', '/api/posts/1', [ + 'authenticatedAs' => 1, + 'json' => [ + 'data' => [ + 'attributes' => [ + 'content' => null, + ], + ], + ], + ]) + ); + + $this->assertEquals(422, $response->getStatusCode()); + + $body = (string) $response->getBody(); + $this->assertJson($body); + $this->assertEquals([ + 'errors' => [ + [ + 'status' => '422', + 'code' => 'validation_error', + 'detail' => 'The content field is required.', + 'source' => ['pointer' => '/data/attributes/content'], + ], + ], + ], json_decode($body, true)); + } +}