framework/tests/unit/User/AbstractPolicyTest.php

92 lines
2.0 KiB
PHP
Raw Normal View History

<?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\Testing\unit\TestCase;
use Flarum\User\Access\AbstractPolicy;
use Flarum\User\User;
use Illuminate\Events\Dispatcher;
use Mockery as m;
class AbstractPolicyTest extends TestCase
{
private $policy;
/**
* @inheritDoc
*/
protected function setUp(): void
{
2020-04-19 21:55:10 +08:00
$this->policy = m::mock(CustomUserPolicy::class)->makePartial();
User::setEventDispatcher(new Dispatcher());
}
/**
* @inheritDoc
*/
protected function tearDown(): void
{
m::close();
}
public function test_policy_can_be_called_with_object()
{
$allowed = $this->policy->checkAbility(new User(), 'create', new User());
$this->assertEquals(AbstractPolicy::ALLOW, $allowed);
}
public function test_policy_can_be_called_with_class()
{
$allowed = $this->policy->checkAbility(new User(), 'edit', User::class);
$this->assertEquals(AbstractPolicy::DENY, $allowed);
}
public function test_policy_converts_true_to_ALLOW()
{
$allowed = $this->policy->checkAbility(new User(), 'somethingRandom', User::class);
$this->assertEquals(AbstractPolicy::ALLOW, $allowed);
}
public function test_policy_converts_false_to_DENY()
{
$allowed = $this->policy->checkAbility(new User(), 'somethingElseRandom', User::class);
$this->assertEquals(AbstractPolicy::DENY, $allowed);
}
}
2020-04-19 21:55:10 +08:00
class CustomUserPolicy extends AbstractPolicy
{
protected $model = User::class;
public function create(User $actor)
{
return $this->allow();
}
public function edit(User $actor, $target)
{
return $this->deny();
2020-04-19 21:55:10 +08:00
}
public function somethingRandom(User $actor, $target)
2020-04-19 21:55:10 +08:00
{
return true;
}
public function somethingElseRandom(User $actor, $target)
{
return false;
}
2020-04-19 21:55:10 +08:00
}