Make AbstractPolicy compatible with both object and class as $model (#1977)

This commit is contained in:
Clark Winkelmann 2020-04-19 15:52:59 +02:00 committed by GitHub
parent 738e95f024
commit 7143e13073
3 changed files with 77 additions and 1 deletions

View File

@ -35,7 +35,7 @@ abstract class AbstractPolicy
*/
public function getPermission(GetPermission $event)
{
if (! $event->model instanceof $this->model) {
if (! $event->model instanceof $this->model && $event->model !== $this->model) {
return;
}

View File

@ -0,0 +1,48 @@
<?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\unit\User;
use Flarum\Event\GetPermission;
use Flarum\Tests\unit\TestCase;
use Flarum\User\User;
use Illuminate\Events\Dispatcher;
use Mockery as m;
class AbstractPolicyTest extends TestCase
{
private $policy;
private $dispatcher;
public function setUp()
{
$this->policy = m::mock(UserPolicy::class)->makePartial();
$this->dispatcher = new Dispatcher();
$this->dispatcher->subscribe($this->policy);
User::setEventDispatcher($this->dispatcher);
}
public function test_policy_can_be_called_with_object()
{
$this->policy->shouldReceive('edit')->andReturn(true);
$allowed = $this->dispatcher->until(new GetPermission(new User(), 'edit', new User()));
$this->assertTrue($allowed);
}
public function test_policy_can_be_called_with_class()
{
$this->policy->shouldReceive('create')->andReturn(true);
$allowed = $this->dispatcher->until(new GetPermission(new User(), 'create', User::class));
$this->assertTrue($allowed);
}
}

View File

@ -0,0 +1,28 @@
<?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\unit\User;
use Flarum\User\AbstractPolicy;
use Flarum\User\User;
class UserPolicy extends AbstractPolicy
{
protected $model = User::class;
public function create(User $actor)
{
return true;
}
public function edit(User $actor, User $user)
{
return true;
}
}