From ef9db0655ae2fdf47766b3b285cc4f070898c33f Mon Sep 17 00:00:00 2001 From: Clark Winkelmann Date: Mon, 1 Nov 2021 21:38:21 +0100 Subject: [PATCH] Fix post policy for PHP 8 (#3145) * Add tests to verify post policy works as intended * Fix "reply" post edit setting not working on PHP 8 Fixes #3144 --- framework/core/src/Post/Access/PostPolicy.php | 2 +- .../tests/integration/post/PostPolicyTest.php | 124 ++++++++++++++++++ 2 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 framework/core/tests/integration/post/PostPolicyTest.php diff --git a/framework/core/src/Post/Access/PostPolicy.php b/framework/core/src/Post/Access/PostPolicy.php index ff2768625..9c5b5e812 100644 --- a/framework/core/src/Post/Access/PostPolicy.php +++ b/framework/core/src/Post/Access/PostPolicy.php @@ -58,7 +58,7 @@ class PostPolicy extends AbstractPolicy if ($allowEditing === '-1' || ($allowEditing === 'reply' && $post->number >= $post->discussion->last_post_number) - || ($post->created_at->diffInMinutes(new Carbon) < $allowEditing)) { + || (is_numeric($allowEditing) && $post->created_at->diffInMinutes(new Carbon) < $allowEditing)) { return $this->allow(); } } diff --git a/framework/core/tests/integration/post/PostPolicyTest.php b/framework/core/tests/integration/post/PostPolicyTest.php new file mode 100644 index 000000000..c750ebcba --- /dev/null +++ b/framework/core/tests/integration/post/PostPolicyTest.php @@ -0,0 +1,124 @@ +prepareDatabase([ + 'discussions' => [ + ['id' => 1, 'title' => 'Editable discussion', 'created_at' => Carbon::parse('2021-11-01 13:00:00')->toDateTimeString(), 'user_id' => 2, 'first_post_id' => 1, 'comment_count' => 2, 'is_private' => 0, 'last_post_number' => 2], + ], + 'posts' => [ + ['id' => 1, 'discussion_id' => 1, 'number' => 1, 'created_at' => Carbon::parse('2021-11-01 13:00:00')->toDateTimeString(), 'user_id' => 2, 'type' => 'comment', 'content' => ''], + ['id' => 2, 'discussion_id' => 1, 'number' => 2, 'created_at' => Carbon::parse('2021-11-01 13:00:03')->toDateTimeString(), 'user_id' => 2, 'type' => 'comment', 'content' => ''], + ], + 'users' => [ + $this->normalUser(), + ] + ]); + } + + protected function tearDown(): void + { + parent::tearDown(); + + Carbon::setTestNow(); + } + + /** + * @test + */ + public function edit_indefinitely() + { + $this->setting('allow_post_editing', '-1'); + $this->app(); + + $user = User::findOrFail(2); + $earlierPost = Post::findOrFail(1); + $lastPost = Post::findOrFail(2); + + // Date close to "now" + Carbon::setTestNow('2021-11-01 13:00:05'); + + $this->assertTrue($user->can('edit', $earlierPost)); + $this->assertTrue($user->can('edit', $lastPost)); + + // Date further into the future + Carbon::setTestNow('2025-01-01 13:00:00'); + + $this->assertTrue($user->can('edit', $earlierPost)); + $this->assertTrue($user->can('edit', $lastPost)); + } + + /** + * @test + */ + public function edit_until_reply() + { + $this->setting('allow_post_editing', 'reply'); + $this->app(); + + $user = User::findOrFail(2); + $earlierPost = Post::findOrFail(1); + $lastPost = Post::findOrFail(2); + + // Date close to "now" + Carbon::setTestNow('2021-11-01 13:00:05'); + + $this->assertFalse($user->can('edit', $earlierPost)); + $this->assertTrue($user->can('edit', $lastPost)); + + // Date further into the future + Carbon::setTestNow('2025-01-01 13:00:00'); + + $this->assertFalse($user->can('edit', $earlierPost)); + $this->assertTrue($user->can('edit', $lastPost)); + } + + /** + * @test + */ + public function edit_10_minutes() + { + $this->setting('allow_post_editing', '10'); + $this->app(); + + $user = User::findOrFail(2); + $earlierPost = Post::findOrFail(1); + $lastPost = Post::findOrFail(2); + + // Date close to "now" + Carbon::setTestNow('2021-11-01 13:00:05'); + + $this->assertTrue($user->can('edit', $earlierPost)); + $this->assertTrue($user->can('edit', $lastPost)); + + // Date further into the future + Carbon::setTestNow('2025-01-01 13:00:00'); + + $this->assertFalse($user->can('edit', $earlierPost)); + $this->assertFalse($user->can('edit', $lastPost)); + } +}