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
This commit is contained in:
Clark Winkelmann 2021-11-01 21:38:21 +01:00 committed by GitHub
parent 9fbfed3695
commit ef9db0655a
2 changed files with 125 additions and 1 deletions

View File

@ -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();
}
}

View File

@ -0,0 +1,124 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Tests\integration\post;
use Carbon\Carbon;
use Flarum\Post\Post;
use Flarum\Testing\integration\RetrievesAuthorizedUsers;
use Flarum\Testing\integration\TestCase;
use Flarum\User\User;
class PostPolicyTest extends TestCase
{
use RetrievesAuthorizedUsers;
/**
* @inheritDoc
*/
protected function setUp(): void
{
parent::setUp();
$this->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' => '<t></t>'],
['id' => 2, 'discussion_id' => 1, 'number' => 2, 'created_at' => Carbon::parse('2021-11-01 13:00:03')->toDateTimeString(), 'user_id' => 2, 'type' => 'comment', 'content' => '<t></t>'],
],
'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));
}
}