Skip to content

Commit 1e95d59

Browse files
committed
updated tests and documentation
Signed-off-by: Jurj-Bogdan <[email protected]>
1 parent 28a24d9 commit 1e95d59

File tree

5 files changed

+303
-12
lines changed

5 files changed

+303
-12
lines changed

docs/book/v3/configuration.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ return [
6363
'route' => 'controller route name',
6464
'actions' => [], //list of actions to apply, or empty array for all actions,
6565
// by default, authorization passes if all permissions are present(AND)
66-
'roles' => [], //list of roles to allow,
66+
'roles' => ['admin'], //list of roles to allow,
6767
],
6868
],
6969
],
@@ -76,14 +76,14 @@ return [
7676
'route' => 'controller route name',
7777
'actions' => [], //list of actions to apply, or empty array for all actions,
7878
// by default, authorization passes if all permissions are present(AND)
79-
'permissions' => [], //list of permissions to allow,
79+
'permissions' => ['authenticated'], //list of permissions to allow,
8080
],
8181
[
8282
'route' => 'controller route name',
8383
'actions' => [], //list of actions to apply, or empty array for all actions,
8484
'permissions' => [
8585
//permission can be defined in this way too, for all permission type guards
86-
'permissions' => [], //list of permissions,
86+
'permissions' => ['authenticated'], //list of permissions,
8787
'condition' => \Dot\Rbac\Guard\GuardInterface::CONDITION_OR,
8888
],
8989
]
@@ -105,11 +105,15 @@ return [
105105
];
106106
```
107107

108+
> It is **strongly recommended** to explicitly define permissions or roles and not leave the values empty, especially if using `GuardInterface::POLICY_DENY`!
109+
108110
## Route Name Placeholders
109111

110112
Route **names** are allowed to contain `*` as placeholders, allowing more compact specifications.
111113
This feature is available for all types of guards.
112114

115+
> Note that route rules are verified in order of their writing, take care of the order when using placeholder routes, as not to overwrite any specific routes!
116+
113117
```php
114118
[
115119
'type' => 'Route',

test/Guard/ControllerGuardTest.php

Lines changed: 84 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Dot\Rbac\Role\RoleServiceInterface;
99
use Laminas\Diactoros\ServerRequest;
1010
use Mezzio\Router\RouteResult;
11+
use PHPUnit\Framework\Attributes\DataProvider;
1112
use PHPUnit\Framework\MockObject\Exception;
1213
use PHPUnit\Framework\TestCase;
1314

@@ -19,24 +20,22 @@ class ControllerGuardTest extends TestCase
1920

2021
protected array $rules = [
2122
[
22-
'route' => 'account',
23-
'actions' => [
23+
'route' => 'account',
24+
'actions' => [
2425
'avatar',
2526
'details',
2627
'changePassword',
2728
'deleteAccount',
2829
'index',
2930
],
30-
'permissions' => ['unauthenticated'],
31-
'roles' => ['*'],
31+
'roles' => ['*'],
3232
],
3333
[
34-
'route' => 'page',
35-
'actions' => [
34+
'route' => 'page',
35+
'actions' => [
3636
'premium-content',
3737
],
38-
'permissions' => ['premium'],
39-
'roles' => [],
38+
'roles' => [],
4039
],
4140
[
4241
'route' => 'invalidRoute',
@@ -165,4 +164,81 @@ public function testIsGranted(): void
165164
$result = $this->subject->isGranted($request);
166165
$this->assertTrue($result);
167166
}
167+
168+
public function rulesProvider(): array
169+
{
170+
return [
171+
'valid-placeholder' => [
172+
[
173+
[
174+
'route' => 'account-*',
175+
'actions' => [],
176+
'roles' => ['*'],
177+
],
178+
[
179+
'route' => 'account-view',
180+
'actions' => [],
181+
'roles' => ['*'],
182+
],
183+
],
184+
'account-view',
185+
true,
186+
],
187+
'invalid-placeholder' => [
188+
[
189+
[
190+
'route' => 'account-*',
191+
'actions' => [],
192+
'roles' => ['*'],
193+
],
194+
],
195+
'different-account-route',
196+
false,
197+
],
198+
'valid-composite-placeholder' => [
199+
[
200+
[
201+
'route' => 'user-*-form',
202+
'actions' => [],
203+
'roles' => ['*'],
204+
],
205+
],
206+
'user-create-form',
207+
true,
208+
],
209+
'invalid-composite-placeholder' => [
210+
[
211+
[
212+
'route' => 'user-*-form',
213+
'actions' => [],
214+
'roles' => ['*'],
215+
],
216+
],
217+
'user-create-avatar',
218+
false,
219+
],
220+
];
221+
}
222+
223+
#[DataProvider('rulesProvider')]
224+
public function testPlaceholderRoutes(array $rules, string $matchedRoute, bool $isValid): void
225+
{
226+
$request = $this->createMock(ServerRequest::class);
227+
$routeResult = $this->createMock(RouteResult::class);
228+
229+
$this->subject->setRules($rules);
230+
231+
$request->expects($this->once())
232+
->method('getAttribute')
233+
->with(RouteResult::class)
234+
->willReturn($routeResult);
235+
$routeResult->expects($this->any())
236+
->method('getMatchedParams')
237+
->willReturn([]);
238+
$routeResult->expects($this->atLeastOnce())
239+
->method('getMatchedRouteName')
240+
->willReturn($matchedRoute);
241+
242+
$this->assertEquals($isValid, $this->subject->isGranted($request));
243+
}
168244
}

test/Guard/ControllerPermissionGuardTest.php

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Dot\Rbac\Guard\Guard\ControllerPermissionGuard;
99
use Laminas\Diactoros\ServerRequest;
1010
use Mezzio\Router\RouteResult;
11+
use PHPUnit\Framework\Attributes\DataProvider;
1112
use PHPUnit\Framework\MockObject\Exception;
1213
use PHPUnit\Framework\TestCase;
1314

@@ -28,7 +29,6 @@ class ControllerPermissionGuardTest extends TestCase
2829
'index',
2930
],
3031
'permissions' => ['*'],
31-
'roles' => ['*'],
3232
],
3333
[
3434
'route' => 'page',
@@ -174,4 +174,81 @@ public function testSetAuthorizationService(): void
174174
$result = $this->subject->getAuthorizationService();
175175
$this->assertInstanceOf(AuthorizationInterface::class, $result);
176176
}
177+
178+
public function rulesProvider(): array
179+
{
180+
return [
181+
'valid-placeholder' => [
182+
[
183+
[
184+
'route' => 'account-*',
185+
'actions' => [],
186+
'permissions' => ['*'],
187+
],
188+
[
189+
'route' => 'account-view',
190+
'actions' => [],
191+
'permissions' => ['*'],
192+
],
193+
],
194+
'account-view',
195+
true,
196+
],
197+
'invalid-placeholder' => [
198+
[
199+
[
200+
'route' => 'account-*',
201+
'actions' => [],
202+
'permissions' => ['*'],
203+
],
204+
],
205+
'different-account-route',
206+
false,
207+
],
208+
'valid-composite-placeholder' => [
209+
[
210+
[
211+
'route' => 'user-*-form',
212+
'actions' => [],
213+
'permissions' => ['*'],
214+
],
215+
],
216+
'user-create-form',
217+
true,
218+
],
219+
'invalid-composite-placeholder' => [
220+
[
221+
[
222+
'route' => 'user-*-form',
223+
'actions' => [],
224+
'permissions' => ['*'],
225+
],
226+
],
227+
'user-create-avatar',
228+
false,
229+
],
230+
];
231+
}
232+
233+
#[DataProvider('rulesProvider')]
234+
public function testPlaceholderRoutes(array $rules, string $matchedRoute, bool $isValid): void
235+
{
236+
$request = $this->createMock(ServerRequest::class);
237+
$routeResult = $this->createMock(RouteResult::class);
238+
239+
$this->subject->setRules($rules);
240+
241+
$request->expects($this->once())
242+
->method('getAttribute')
243+
->with(RouteResult::class)
244+
->willReturn($routeResult);
245+
$routeResult->expects($this->any())
246+
->method('getMatchedParams')
247+
->willReturn([]);
248+
$routeResult->expects($this->atLeastOnce())
249+
->method('getMatchedRouteName')
250+
->willReturn($matchedRoute);
251+
252+
$this->assertEquals($isValid, $this->subject->isGranted($request));
253+
}
177254
}

test/Guard/RouteGuardTest.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Dot\Rbac\Role\RoleServiceInterface;
99
use Laminas\Diactoros\ServerRequest;
1010
use Mezzio\Router\RouteResult;
11+
use PHPUnit\Framework\Attributes\DataProvider;
1112
use PHPUnit\Framework\MockObject\Exception;
1213
use PHPUnit\Framework\TestCase;
1314

@@ -145,4 +146,70 @@ public function testIsGranted(): void
145146
$result = $this->subject->isGranted($request);
146147
$this->assertTrue($result);
147148
}
149+
150+
public function rulesProvider(): array
151+
{
152+
return [
153+
'valid-placeholder' => [
154+
[
155+
'account-*' => [
156+
'view',
157+
'create',
158+
],
159+
'account-view' => [],
160+
],
161+
'account-view',
162+
true,
163+
],
164+
'invalid-placeholder' => [
165+
[
166+
'account-*' => [
167+
'view',
168+
'create',
169+
],
170+
],
171+
'different-account-route',
172+
false,
173+
],
174+
'valid-composite-placeholder' => [
175+
[
176+
'user-*-form' => [
177+
'user-create-form',
178+
'user-edit-form',
179+
],
180+
],
181+
'user-create-form',
182+
true,
183+
],
184+
'invalid-composite-placeholder' => [
185+
[
186+
'user-*-form' => [
187+
'user-create-form',
188+
'user-edit-form',
189+
],
190+
],
191+
'user-create-avatar',
192+
false,
193+
],
194+
];
195+
}
196+
197+
#[DataProvider('rulesProvider')]
198+
public function testPlaceholderRoutes(array $rules, string $matchedRoute, bool $isValid): void
199+
{
200+
$request = $this->createMock(ServerRequest::class);
201+
$routeResult = $this->createMock(RouteResult::class);
202+
203+
$this->subject->setRules($rules);
204+
205+
$request->expects($this->once())
206+
->method('getAttribute')
207+
->with(RouteResult::class)
208+
->willReturn($routeResult);
209+
$routeResult->expects($this->atLeastOnce())
210+
->method('getMatchedRouteName')
211+
->willReturn($matchedRoute);
212+
213+
$this->assertEquals($isValid, $this->subject->isGranted($request));
214+
}
148215
}

0 commit comments

Comments
 (0)