1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
<?php
namespace Illuminate\Tests\Foundation;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Auth\Access\Gate;
use Illuminate\Auth\Access\Response;
use Illuminate\Container\Container;
use Illuminate\Contracts\Auth\Access\Gate as GateContract;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use PHPUnit\Framework\TestCase;
class FoundationAuthorizesRequestsTraitTest extends TestCase
{
protected function tearDown(): void
{
Container::setInstance(null);
}
public function testBasicGateCheck()
{
unset($_SERVER['_test.authorizes.trait']);
$gate = $this->getBasicGate();
$gate->define('baz', function () {
$_SERVER['_test.authorizes.trait'] = true;
return true;
});
$response = (new FoundationTestAuthorizeTraitClass)->authorize('baz');
$this->assertInstanceOf(Response::class, $response);
$this->assertTrue($_SERVER['_test.authorizes.trait']);
}
public function testExceptionIsThrownIfGateCheckFails()
{
$this->expectException(AuthorizationException::class);
$this->expectExceptionMessage('This action is unauthorized.');
$gate = $this->getBasicGate();
$gate->define('baz', function () {
return false;
});
(new FoundationTestAuthorizeTraitClass)->authorize('baz');
}
public function testPoliciesMayBeCalled()
{
unset($_SERVER['_test.authorizes.trait.policy']);
$gate = $this->getBasicGate();
$gate->policy(FoundationAuthorizesRequestTestClass::class, FoundationAuthorizesRequestTestPolicy::class);
$response = (new FoundationTestAuthorizeTraitClass)->authorize('update', new FoundationAuthorizesRequestTestClass);
$this->assertInstanceOf(Response::class, $response);
$this->assertTrue($_SERVER['_test.authorizes.trait.policy']);
}
public function testPolicyMethodMayBeGuessedPassingModelInstance()
{
unset($_SERVER['_test.authorizes.trait.policy']);
$gate = $this->getBasicGate();
$gate->policy(FoundationAuthorizesRequestTestClass::class, FoundationAuthorizesRequestTestPolicy::class);
$response = (new FoundationTestAuthorizeTraitClass)->authorize(new FoundationAuthorizesRequestTestClass);
$this->assertInstanceOf(Response::class, $response);
$this->assertTrue($_SERVER['_test.authorizes.trait.policy']);
}
public function testPolicyMethodMayBeGuessedPassingClassName()
{
unset($_SERVER['_test.authorizes.trait.policy']);
$gate = $this->getBasicGate();
$gate->policy('\\'.FoundationAuthorizesRequestTestClass::class, FoundationAuthorizesRequestTestPolicy::class);
$response = (new FoundationTestAuthorizeTraitClass)->authorize('\\'.FoundationAuthorizesRequestTestClass::class);
$this->assertInstanceOf(Response::class, $response);
$this->assertTrue($_SERVER['_test.authorizes.trait.policy']);
}
public function testPolicyMethodMayBeGuessedAndNormalized()
{
unset($_SERVER['_test.authorizes.trait.policy']);
$gate = $this->getBasicGate();
$gate->policy(FoundationAuthorizesRequestTestClass::class, FoundationAuthorizesRequestTestPolicy::class);
(new FoundationTestAuthorizeTraitClass)->store(new FoundationAuthorizesRequestTestClass);
$this->assertTrue($_SERVER['_test.authorizes.trait.policy']);
}
public function getBasicGate()
{
$container = Container::setInstance(new Container);
$gate = new Gate($container, function () {
return (object) ['id' => 1];
});
$container->instance(GateContract::class, $gate);
return $gate;
}
}
class FoundationAuthorizesRequestTestClass
{
//
}
class FoundationAuthorizesRequestTestPolicy
{
public function create()
{
$_SERVER['_test.authorizes.trait.policy'] = true;
return true;
}
public function update()
{
$_SERVER['_test.authorizes.trait.policy'] = true;
return true;
}
public function testPolicyMethodMayBeGuessedPassingModelInstance()
{
$_SERVER['_test.authorizes.trait.policy'] = true;
return true;
}
public function testPolicyMethodMayBeGuessedPassingClassName()
{
$_SERVER['_test.authorizes.trait.policy'] = true;
return true;
}
}
class FoundationTestAuthorizeTraitClass
{
use AuthorizesRequests;
public function store($object)
{
$this->authorize($object);
}
}
|