mirror of
https://github.com/flarum/framework.git
synced 2024-11-25 07:38:01 +08:00
fix: Until reply
renaming permission of discussions broken in php 8 (#3243)
* test: `until reply` rename discussion ability * fix: `Until reply` renaming of discussions broken in php 8
This commit is contained in:
parent
9b9db24871
commit
fbb1d95557
|
@ -55,7 +55,7 @@ class DiscussionPolicy extends AbstractPolicy
|
|||
|
||||
if ($allowRenaming === '-1'
|
||||
|| ($allowRenaming === 'reply' && $discussion->participant_count <= 1)
|
||||
|| ($discussion->created_at->diffInMinutes() < $allowRenaming)) {
|
||||
|| (is_numeric($allowRenaming) && $discussion->created_at->diffInMinutes() < $allowRenaming)) {
|
||||
return $this->allow();
|
||||
}
|
||||
}
|
||||
|
|
128
framework/core/tests/integration/policy/DiscussionPolicyTest.php
Normal file
128
framework/core/tests/integration/policy/DiscussionPolicyTest.php
Normal file
|
@ -0,0 +1,128 @@
|
|||
<?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\policy;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Flarum\Bus\Dispatcher;
|
||||
use Flarum\Discussion\Discussion;
|
||||
use Flarum\Foundation\DispatchEventsTrait;
|
||||
use Flarum\Post\Command\PostReply;
|
||||
use Flarum\Testing\integration\RetrievesAuthorizedUsers;
|
||||
use Flarum\Testing\integration\TestCase;
|
||||
use Flarum\User\User;
|
||||
|
||||
class DiscussionPolicyTest extends TestCase
|
||||
{
|
||||
use RetrievesAuthorizedUsers;
|
||||
use DispatchEventsTrait;
|
||||
|
||||
/**
|
||||
* @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' => 1, 'post_number_index' => 1, 'participant_count' => 1],
|
||||
['id' => 2, 'title' => 'Editable discussion', 'created_at' => Carbon::parse('2021-11-01 13:00:00')->toDateTimeString(), 'user_id' => 2, 'first_post_id' => 2, 'comment_count' => 2, 'is_private' => 0, 'last_post_number' => 2, 'participant_count' => 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' => 2, 'number' => 1, 'created_at' => Carbon::parse('2021-11-01 13:00:03')->toDateTimeString(), 'user_id' => 2, 'type' => 'comment', 'content' => '<t></t>'],
|
||||
['id' => 3, 'discussion_id' => 2, 'number' => 2, 'created_at' => Carbon::parse('2021-11-01 13:00:03')->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => '<t></t>'],
|
||||
],
|
||||
'users' => [
|
||||
$this->normalUser(),
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
parent::tearDown();
|
||||
|
||||
Carbon::setTestNow();
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function rename_indefinitely()
|
||||
{
|
||||
$this->setting('allow_renaming', '-1');
|
||||
$this->app();
|
||||
|
||||
$user = User::findOrFail(2);
|
||||
$discussion = Discussion::findOrFail(1);
|
||||
|
||||
// Date close to "now"
|
||||
Carbon::setTestNow('2021-11-01 13:00:05');
|
||||
|
||||
$this->assertTrue($user->can('rename', $discussion));
|
||||
|
||||
// Date further into the future
|
||||
Carbon::setTestNow('2025-01-01 13:00:00');
|
||||
|
||||
$this->assertTrue($user->can('rename', $discussion));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function rename_until_reply()
|
||||
{
|
||||
$this->setting('allow_renaming', 'reply');
|
||||
$this->app();
|
||||
|
||||
$user = User::findOrFail(2);
|
||||
$discussion = Discussion::findOrFail(1);
|
||||
$discussionWithReply = Discussion::findOrFail(2);
|
||||
|
||||
// Date close to "now"
|
||||
Carbon::setTestNow('2021-11-01 13:00:05');
|
||||
|
||||
$this->assertTrue($user->can('rename', $discussion));
|
||||
$this->assertFalse($user->can('rename', $discussionWithReply));
|
||||
|
||||
$this->app()->getContainer()->make(Dispatcher::class)->dispatch(
|
||||
new PostReply(1, User::findOrFail(1), ['attributes' => ['content' => 'test']], null)
|
||||
);
|
||||
|
||||
// Date further into the future
|
||||
Carbon::setTestNow('2025-01-01 13:00:00');
|
||||
|
||||
$this->assertFalse($user->can('rename', $discussion->fresh()));
|
||||
$this->assertFalse($user->can('rename', $discussionWithReply));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function rename_10_minutes()
|
||||
{
|
||||
$this->setting('allow_renaming', '10');
|
||||
$this->app();
|
||||
|
||||
$user = User::findOrFail(2);
|
||||
$discussion = Discussion::findOrFail(1);
|
||||
|
||||
// Date close to "now"
|
||||
Carbon::setTestNow('2021-11-01 13:00:05');
|
||||
|
||||
$this->assertTrue($user->can('rename', $discussion));
|
||||
|
||||
// Date further into the future
|
||||
Carbon::setTestNow('2025-01-01 13:00:00');
|
||||
|
||||
$this->assertFalse($user->can('rename', $discussion));
|
||||
}
|
||||
}
|
|
@ -7,7 +7,7 @@
|
|||
* LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Tests\integration\post;
|
||||
namespace Flarum\Tests\integration\policy;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Flarum\Post\Post;
|
Loading…
Reference in New Issue
Block a user