-
Notifications
You must be signed in to change notification settings - Fork 11.2k
/
Copy pathAuthAccessGateTest.php
1120 lines (847 loc) · 30.4 KB
/
AuthAccessGateTest.php
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace Illuminate\Tests\Auth;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Auth\Access\Gate;
use Illuminate\Auth\Access\HandlesAuthorization;
use Illuminate\Auth\Access\Response;
use Illuminate\Container\Container;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use stdClass;
class AuthAccessGateTest extends TestCase
{
public function testBasicClosuresCanBeDefined()
{
$gate = $this->getBasicGate();
$gate->define('foo', function ($user) {
return true;
});
$gate->define('bar', function ($user) {
return false;
});
$this->assertTrue($gate->check('foo'));
$this->assertFalse($gate->check('bar'));
}
public function testBeforeCanTakeAnArrayCallbackAsObject()
{
$gate = new Gate(new Container, function () {
//
});
$gate->before([new AccessGateTestBeforeCallback, 'allowEverything']);
$this->assertTrue($gate->check('anything'));
}
public function testBeforeCanTakeAnArrayCallbackAsObjectStatic()
{
$gate = new Gate(new Container, function () {
//
});
$gate->before([new AccessGateTestBeforeCallback, 'allowEverythingStatically']);
$this->assertTrue($gate->check('anything'));
}
public function testBeforeCanTakeAnArrayCallbackWithStaticMethod()
{
$gate = new Gate(new Container, function () {
//
});
$gate->before([AccessGateTestBeforeCallback::class, 'allowEverythingStatically']);
$this->assertTrue($gate->check('anything'));
}
public function testBeforeCanAllowGuests()
{
$gate = new Gate(new Container, function () {
//
});
$gate->before(function (?stdClass $user) {
return true;
});
$this->assertTrue($gate->check('anything'));
}
public function testAfterCanAllowGuests()
{
$gate = new Gate(new Container, function () {
//
});
$gate->after(function (?stdClass $user) {
return true;
});
$this->assertTrue($gate->check('anything'));
}
public function testClosuresCanAllowGuestUsers()
{
$gate = new Gate(new Container, function () {
//
});
$gate->define('foo', function (?stdClass $user) {
return true;
});
$gate->define('bar', function (stdClass $user) {
return false;
});
$this->assertTrue($gate->check('foo'));
$this->assertFalse($gate->check('bar'));
}
public function testPoliciesCanAllowGuests()
{
unset($_SERVER['__laravel.testBefore']);
$gate = new Gate(new Container, function () {
//
});
$gate->policy(AccessGateTestDummy::class, AccessGateTestPolicyThatAllowsGuests::class);
$this->assertTrue($gate->check('edit', new AccessGateTestDummy));
$this->assertFalse($gate->check('update', new AccessGateTestDummy));
$this->assertTrue($_SERVER['__laravel.testBefore']);
$gate = $this->getBasicGate();
$gate->policy(AccessGateTestDummy::class, AccessGateTestPolicyThatAllowsGuests::class);
$this->assertTrue($gate->check('edit', new AccessGateTestDummy));
$this->assertTrue($gate->check('update', new AccessGateTestDummy));
unset($_SERVER['__laravel.testBefore']);
}
public function testPolicyBeforeNotCalledWithGuestsIfItDoesntAllowThem()
{
$_SERVER['__laravel.testBefore'] = false;
$gate = new Gate(new Container, function () {
//
});
$gate->policy(AccessGateTestDummy::class, AccessGateTestPolicyWithNonGuestBefore::class);
$this->assertTrue($gate->check('edit', new AccessGateTestDummy));
$this->assertFalse($gate->check('update', new AccessGateTestDummy));
$this->assertFalse($_SERVER['__laravel.testBefore']);
unset($_SERVER['__laravel.testBefore']);
}
public function testBeforeAndAfterCallbacksCanAllowGuests()
{
$_SERVER['__laravel.gateBefore'] = false;
$_SERVER['__laravel.gateBefore2'] = false;
$_SERVER['__laravel.gateAfter'] = false;
$_SERVER['__laravel.gateAfter2'] = false;
$gate = new Gate(new Container, function () {
//
});
$gate->before(function (?stdClass $user) {
$_SERVER['__laravel.gateBefore'] = true;
});
$gate->after(function (?stdClass $user) {
$_SERVER['__laravel.gateAfter'] = true;
});
$gate->before(function (stdClass $user) {
$_SERVER['__laravel.gateBefore2'] = true;
});
$gate->after(function (stdClass $user) {
$_SERVER['__laravel.gateAfter2'] = true;
});
$gate->define('foo', function ($user = null) {
return true;
});
$this->assertTrue($gate->check('foo'));
$this->assertTrue($_SERVER['__laravel.gateBefore']);
$this->assertFalse($_SERVER['__laravel.gateBefore2']);
$this->assertTrue($_SERVER['__laravel.gateAfter']);
$this->assertFalse($_SERVER['__laravel.gateAfter2']);
unset($_SERVER['__laravel.gateBefore']);
unset($_SERVER['__laravel.gateBefore2']);
unset($_SERVER['__laravel.gateAfter']);
unset($_SERVER['__laravel.gateAfter2']);
}
public function testResourceGatesCanBeDefined()
{
$gate = $this->getBasicGate();
$gate->resource('test', AccessGateTestResource::class);
$dummy = new AccessGateTestDummy;
$this->assertTrue($gate->check('test.view'));
$this->assertTrue($gate->check('test.create'));
$this->assertTrue($gate->check('test.update', $dummy));
$this->assertTrue($gate->check('test.delete', $dummy));
}
public function testCustomResourceGatesCanBeDefined()
{
$gate = $this->getBasicGate();
$abilities = [
'ability1' => 'foo',
'ability2' => 'bar',
];
$gate->resource('test', AccessGateTestCustomResource::class, $abilities);
$this->assertTrue($gate->check('test.ability1'));
$this->assertTrue($gate->check('test.ability2'));
}
public function testBeforeCallbacksCanOverrideResultIfNecessary()
{
$gate = $this->getBasicGate();
$gate->define('foo', function ($user) {
return true;
});
$gate->before(function ($user, $ability) {
$this->assertSame('foo', $ability);
return false;
});
$this->assertFalse($gate->check('foo'));
}
public function testBeforeCallbacksDontInterruptGateCheckIfNoValueIsReturned()
{
$gate = $this->getBasicGate();
$gate->define('foo', function ($user) {
return true;
});
$gate->before(function () {
//
});
$this->assertTrue($gate->check('foo'));
}
public function testAfterCallbacksAreCalledWithResult()
{
$gate = $this->getBasicGate();
$gate->define('foo', function ($user) {
return true;
});
$gate->define('bar', function ($user) {
return false;
});
$gate->after(function ($user, $ability, $result) {
if ($ability == 'foo') {
$this->assertTrue($result, 'After callback on `foo` should receive true as result');
} elseif ($ability == 'bar') {
$this->assertFalse($result, 'After callback on `bar` should receive false as result');
} else {
$this->assertNull($result, 'After callback on `missing` should receive null as result');
}
});
$this->assertTrue($gate->check('foo'));
$this->assertFalse($gate->check('bar'));
$this->assertFalse($gate->check('missing'));
}
public function testAfterCallbacksCanAllowIfNull()
{
$gate = $this->getBasicGate();
$gate->after(function ($user, $ability, $result) {
return true;
});
$this->assertTrue($gate->allows('null'));
}
public function testAfterCallbacksDoNotOverridePreviousResult()
{
$gate = $this->getBasicGate();
$gate->define('deny', function ($user) {
return false;
});
$gate->define('allow', function ($user) {
return true;
});
$gate->after(function ($user, $ability, $result) {
return ! $result;
});
$this->assertTrue($gate->allows('allow'));
$this->assertTrue($gate->denies('deny'));
}
public function testAfterCallbacksDoNotOverrideEachOther()
{
$gate = $this->getBasicGate();
$gate->after(function ($user, $ability, $result) {
return $ability == 'allow';
});
$gate->after(function ($user, $ability, $result) {
return ! $result;
});
$this->assertTrue($gate->allows('allow'));
$this->assertTrue($gate->denies('deny'));
}
public function testCurrentUserThatIsOnGateAlwaysInjectedIntoClosureCallbacks()
{
$gate = $this->getBasicGate();
$gate->define('foo', function ($user) {
$this->assertEquals(1, $user->id);
return true;
});
$this->assertTrue($gate->check('foo'));
}
public function testASingleArgumentCanBePassedWhenCheckingAbilities()
{
$gate = $this->getBasicGate();
$dummy = new AccessGateTestDummy;
$gate->before(function ($user, $ability, array $arguments) use ($dummy) {
$this->assertCount(1, $arguments);
$this->assertSame($dummy, $arguments[0]);
});
$gate->define('foo', function ($user, $x) use ($dummy) {
$this->assertSame($dummy, $x);
return true;
});
$gate->after(function ($user, $ability, $result, array $arguments) use ($dummy) {
$this->assertCount(1, $arguments);
$this->assertSame($dummy, $arguments[0]);
});
$this->assertTrue($gate->check('foo', $dummy));
}
public function testMultipleArgumentsCanBePassedWhenCheckingAbilities()
{
$gate = $this->getBasicGate();
$dummy1 = new AccessGateTestDummy;
$dummy2 = new AccessGateTestDummy;
$gate->before(function ($user, $ability, array $arguments) use ($dummy1, $dummy2) {
$this->assertCount(2, $arguments);
$this->assertSame([$dummy1, $dummy2], $arguments);
});
$gate->define('foo', function ($user, $x, $y) use ($dummy1, $dummy2) {
$this->assertSame($dummy1, $x);
$this->assertSame($dummy2, $y);
return true;
});
$gate->after(function ($user, $ability, $result, array $arguments) use ($dummy1, $dummy2) {
$this->assertCount(2, $arguments);
$this->assertSame([$dummy1, $dummy2], $arguments);
});
$this->assertTrue($gate->check('foo', [$dummy1, $dummy2]));
}
public function testClassesCanBeDefinedAsCallbacksUsingAtNotation()
{
$gate = $this->getBasicGate();
$gate->define('foo', AccessGateTestClass::class.'@foo');
$this->assertTrue($gate->check('foo'));
}
public function testInvokableClassesCanBeDefined()
{
$gate = $this->getBasicGate();
$gate->define('foo', AccessGateTestInvokableClass::class);
$this->assertTrue($gate->check('foo'));
}
public function testGatesCanBeDefinedUsingAnArrayCallback()
{
$gate = $this->getBasicGate();
$gate->define('foo', [new AccessGateTestStaticClass, 'foo']);
$this->assertTrue($gate->check('foo'));
}
public function testGatesCanBeDefinedUsingAnArrayCallbackWithStaticMethod()
{
$gate = $this->getBasicGate();
$gate->define('foo', [AccessGateTestStaticClass::class, 'foo']);
$this->assertTrue($gate->check('foo'));
}
public function testPolicyClassesCanBeDefinedToHandleChecksForGivenType()
{
$gate = $this->getBasicGate();
$gate->policy(AccessGateTestDummy::class, AccessGateTestPolicy::class);
$this->assertTrue($gate->check('update', new AccessGateTestDummy));
}
public function testPolicyClassesHandleChecksForAllSubtypes()
{
$gate = $this->getBasicGate();
$gate->policy(AccessGateTestDummy::class, AccessGateTestPolicy::class);
$this->assertTrue($gate->check('update', new AccessGateTestSubDummy));
}
public function testPolicyClassesHandleChecksForInterfaces()
{
$gate = $this->getBasicGate();
$gate->policy(AccessGateTestDummyInterface::class, AccessGateTestPolicy::class);
$this->assertTrue($gate->check('update', new AccessGateTestSubDummy));
}
public function testPolicyConvertsDashToCamel()
{
$gate = $this->getBasicGate();
$gate->policy(AccessGateTestDummy::class, AccessGateTestPolicy::class);
$this->assertTrue($gate->check('update-dash', new AccessGateTestDummy));
}
public function testPolicyDefaultToFalseIfMethodDoesNotExistAndGateDoesNotExist()
{
$gate = $this->getBasicGate();
$gate->policy(AccessGateTestDummy::class, AccessGateTestPolicy::class);
$this->assertFalse($gate->check('nonexistent_method', new AccessGateTestDummy));
}
public function testPolicyClassesCanBeDefinedToHandleChecksForGivenClassName()
{
$gate = $this->getBasicGate(true);
$gate->policy(AccessGateTestDummy::class, AccessGateTestPolicy::class);
$this->assertTrue($gate->check('create', [AccessGateTestDummy::class, true]));
}
public function testPoliciesMayHaveBeforeMethodsToOverrideChecks()
{
$gate = $this->getBasicGate();
$gate->policy(AccessGateTestDummy::class, AccessGateTestPolicyWithBefore::class);
$this->assertTrue($gate->check('update', new AccessGateTestDummy));
}
public function testPoliciesAlwaysOverrideClosuresWithSameName()
{
$gate = $this->getBasicGate();
$gate->define('update', function () {
$this->fail();
});
$gate->policy(AccessGateTestDummy::class, AccessGateTestPolicy::class);
$this->assertTrue($gate->check('update', new AccessGateTestDummy));
}
public function testPoliciesDeferToGatesIfMethodDoesNotExist()
{
$gate = $this->getBasicGate();
$gate->define('nonexistent_method', function ($user) {
return true;
});
$gate->policy(AccessGateTestDummy::class, AccessGateTestPolicy::class);
$this->assertTrue($gate->check('nonexistent_method', new AccessGateTestDummy));
}
public function testForUserMethodAttachesANewUserToANewGateInstance()
{
$gate = $this->getBasicGate();
// Assert that the callback receives the new user with ID of 2 instead of ID of 1...
$gate->define('foo', function ($user) {
$this->assertEquals(2, $user->id);
return true;
});
$this->assertTrue($gate->forUser((object) ['id' => 2])->check('foo'));
}
public function testForUserMethodAttachesANewUserToANewGateInstanceWithGuessCallback()
{
$gate = $this->getBasicGate();
$gate->define('foo', function () {
return true;
});
$counter = 0;
$guesserCallback = function () use (&$counter) {
$counter++;
};
$gate->guessPolicyNamesUsing($guesserCallback);
$gate->getPolicyFor('fooClass');
$this->assertEquals(1, $counter);
// now the guesser callback should be present on the new gate as well
$newGate = $gate->forUser((object) ['id' => 1]);
$newGate->getPolicyFor('fooClass');
$this->assertEquals(2, $counter);
$newGate->getPolicyFor('fooClass');
$this->assertEquals(3, $counter);
}
/**
* @dataProvider notCallableDataProvider
*/
public function testDefineSecondParameterShouldBeStringOrCallable($callback)
{
$this->expectException(InvalidArgumentException::class);
$gate = $this->getBasicGate();
$gate->define('foo', $callback);
}
/**
* @return array
*/
public function notCallableDataProvider()
{
return [
[1],
[new stdClass],
[[]],
[1.1],
];
}
public function testAuthorizeThrowsUnauthorizedException()
{
$this->expectException(AuthorizationException::class);
$this->expectExceptionMessage('You are not an admin.');
$this->expectExceptionCode(null);
$gate = $this->getBasicGate();
$gate->policy(AccessGateTestDummy::class, AccessGateTestPolicy::class);
$gate->authorize('create', new AccessGateTestDummy);
}
public function testAuthorizeThrowsUnauthorizedExceptionWithCustomStatusCode()
{
$this->expectException(AuthorizationException::class);
$this->expectExceptionMessage('Not allowed to view as it is not published.');
$this->expectExceptionCode('unpublished');
$gate = $this->getBasicGate();
$gate->policy(AccessGateTestDummy::class, AccessGateTestPolicyWithCode::class);
$gate->authorize('view', new AccessGateTestDummy);
}
public function testAuthorizeWithPolicyThatReturnsDeniedResponseObjectThrowsException()
{
$this->expectException(AuthorizationException::class);
$this->expectExceptionMessage('Not allowed.');
$this->expectExceptionCode('some_code');
$gate = $this->getBasicGate();
$gate->policy(AccessGateTestDummy::class, AccessGateTestPolicyWithDeniedResponseObject::class);
$gate->authorize('create', new AccessGateTestDummy);
}
public function testPolicyThatThrowsAuthorizationExceptionIsCaughtInInspect()
{
$gate = $this->getBasicGate();
$gate->policy(AccessGateTestDummy::class, AccessGateTestPolicyThrowingAuthorizationException::class);
$response = $gate->inspect('create', new AccessGateTestDummy);
$this->assertTrue($response->denied());
$this->assertFalse($response->allowed());
$this->assertSame('Not allowed.', $response->message());
$this->assertSame('some_code', $response->code());
}
public function testAuthorizeReturnsAllowedResponse()
{
$gate = $this->getBasicGate(true);
$gate->policy(AccessGateTestDummy::class, AccessGateTestPolicy::class);
$check = $gate->check('create', new AccessGateTestDummy);
$response = $gate->authorize('create', new AccessGateTestDummy);
$this->assertInstanceOf(Response::class, $response);
$this->assertNull($response->message());
$this->assertTrue($check);
}
public function testResponseReturnsResponseWhenAbilityGranted()
{
$gate = $this->getBasicGate(true);
$gate->policy(AccessGateTestDummy::class, AccessGateTestPolicyWithCode::class);
$response = $gate->inspect('view', new AccessGateTestDummy);
$this->assertInstanceOf(Response::class, $response);
$this->assertNull($response->message());
$this->assertTrue($response->allowed());
$this->assertFalse($response->denied());
$this->assertNull($response->code());
}
public function testResponseReturnsResponseWhenAbilityDenied()
{
$gate = $this->getBasicGate();
$gate->policy(AccessGateTestDummy::class, AccessGateTestPolicyWithCode::class);
$response = $gate->inspect('view', new AccessGateTestDummy);
$this->assertInstanceOf(Response::class, $response);
$this->assertSame('Not allowed to view as it is not published.', $response->message());
$this->assertFalse($response->allowed());
$this->assertTrue($response->denied());
$this->assertEquals($response->code(), 'unpublished');
}
public function testAuthorizeReturnsAnAllowedResponseForATruthyReturn()
{
$gate = $this->getBasicGate();
$gate->policy(AccessGateTestDummy::class, AccessGateTestPolicy::class);
$response = $gate->authorize('update', new AccessGateTestDummy);
$this->assertInstanceOf(Response::class, $response);
$this->assertNull($response->message());
}
protected function getBasicGate($isAdmin = false)
{
return new Gate(new Container, function () use ($isAdmin) {
return (object) ['id' => 1, 'isAdmin' => $isAdmin];
});
}
public function testAnyAbilityCheckPassesIfAllPass()
{
$gate = $this->getBasicGate();
$gate->policy(AccessGateTestDummy::class, AccessGateTestPolicyWithAllPermissions::class);
$this->assertTrue($gate->any(['edit', 'update'], new AccessGateTestDummy));
}
public function testAnyAbilityCheckPassesIfAtLeastOnePasses()
{
$gate = $this->getBasicGate();
$gate->policy(AccessGateTestDummy::class, AccessGateTestPolicyWithMixedPermissions::class);
$this->assertTrue($gate->any(['edit', 'update'], new AccessGateTestDummy));
}
public function testAnyAbilityCheckFailsIfNonePass()
{
$gate = $this->getBasicGate();
$gate->policy(AccessGateTestDummy::class, AccessGateTestPolicyWithNoPermissions::class);
$this->assertFalse($gate->any(['edit', 'update'], new AccessGateTestDummy));
}
public function testNoneAbilityCheckPassesIfAllFail()
{
$gate = $this->getBasicGate();
$gate->policy(AccessGateTestDummy::class, AccessGateTestPolicyWithNoPermissions::class);
$this->assertTrue($gate->none(['edit', 'update'], new AccessGateTestDummy));
}
public function testEveryAbilityCheckPassesIfAllPass()
{
$gate = $this->getBasicGate();
$gate->policy(AccessGateTestDummy::class, AccessGateTestPolicyWithAllPermissions::class);
$this->assertTrue($gate->check(['edit', 'update'], new AccessGateTestDummy));
}
public function testEveryAbilityCheckFailsIfAtLeastOneFails()
{
$gate = $this->getBasicGate();
$gate->policy(AccessGateTestDummy::class, AccessGateTestPolicyWithMixedPermissions::class);
$this->assertFalse($gate->check(['edit', 'update'], new AccessGateTestDummy));
}
public function testEveryAbilityCheckFailsIfNonePass()
{
$gate = $this->getBasicGate();
$gate->policy(AccessGateTestDummy::class, AccessGateTestPolicyWithNoPermissions::class);
$this->assertFalse($gate->check(['edit', 'update'], new AccessGateTestDummy));
}
/**
* @dataProvider hasAbilitiesTestDataProvider
*
* @param array $abilitiesToSet
* @param array|string $abilitiesToCheck
* @param bool $expectedHasValue
*/
public function testHasAbilities($abilitiesToSet, $abilitiesToCheck, $expectedHasValue)
{
$gate = $this->getBasicGate();
$gate->resource('test', AccessGateTestResource::class, $abilitiesToSet);
$this->assertEquals($expectedHasValue, $gate->has($abilitiesToCheck));
}
public function hasAbilitiesTestDataProvider()
{
$abilities = ['foo' => 'foo', 'bar' => 'bar'];
$noAbilities = [];
return [
[$abilities, ['test.foo', 'test.bar'], true],
[$abilities, ['test.bar', 'test.foo'], true],
[$abilities, ['test.bar', 'test.foo', 'test.baz'], false],
[$abilities, ['test.bar'], true],
[$abilities, ['baz'], false],
[$abilities, [''], false],
[$abilities, [], true],
[$abilities, 'test.bar', true],
[$abilities, 'test.foo', true],
[$abilities, '', false],
[$noAbilities, '', false],
[$noAbilities, [], true],
];
}
public function testClassesCanBeDefinedAsCallbacksUsingAtNotationForGuests()
{
$gate = new Gate(new Container, function () {
//
});
$gate->define('foo', AccessGateTestClassForGuest::class.'@foo');
$gate->define('obj_foo', [new AccessGateTestClassForGuest, 'foo']);
$gate->define('static_foo', [AccessGateTestClassForGuest::class, 'staticFoo']);
$gate->define('static_@foo', AccessGateTestClassForGuest::class.'@staticFoo');
$gate->define('bar', AccessGateTestClassForGuest::class.'@bar');
$gate->define('invokable', AccessGateTestGuestInvokableClass::class);
$gate->define('nullable_invokable', AccessGateTestGuestNullableInvokable::class);
$gate->define('absent_invokable', 'someAbsentClass');
AccessGateTestClassForGuest::$calledMethod = '';
$this->assertTrue($gate->check('foo'));
$this->assertSame('foo was called', AccessGateTestClassForGuest::$calledMethod);
$this->assertTrue($gate->check('static_foo'));
$this->assertSame('static foo was invoked', AccessGateTestClassForGuest::$calledMethod);
$this->assertTrue($gate->check('bar'));
$this->assertSame('bar got invoked', AccessGateTestClassForGuest::$calledMethod);
$this->assertTrue($gate->check('static_@foo'));
$this->assertSame('static foo was invoked', AccessGateTestClassForGuest::$calledMethod);
$this->assertTrue($gate->check('invokable'));
$this->assertSame('__invoke was called', AccessGateTestGuestInvokableClass::$calledMethod);
$this->assertTrue($gate->check('nullable_invokable'));
$this->assertSame('Nullable __invoke was called', AccessGateTestGuestNullableInvokable::$calledMethod);
$this->assertFalse($gate->check('absent_invokable'));
}
}
class AccessGateTestClassForGuest
{
public static $calledMethod = null;
public function foo($user = null)
{
static::$calledMethod = 'foo was called';
return true;
}
public static function staticFoo($user = null)
{
static::$calledMethod = 'static foo was invoked';
return true;
}
public function bar(?stdClass $user)
{
static::$calledMethod = 'bar got invoked';
return true;
}
}
class AccessGateTestStaticClass
{
public static function foo($user)
{
return $user->id === 1;
}
}
class AccessGateTestClass
{
public function foo($user)
{
return $user->id === 1;
}
}
class AccessGateTestInvokableClass
{
public function __invoke($user)
{
return $user->id === 1;
}
}
class AccessGateTestGuestInvokableClass
{
public static $calledMethod = null;
public function __invoke($user = null)
{
static::$calledMethod = '__invoke was called';
return true;
}
}
class AccessGateTestGuestNullableInvokable
{
public static $calledMethod = null;
public function __invoke(?stdClass $user)
{
static::$calledMethod = 'Nullable __invoke was called';
return true;
}
}
interface AccessGateTestDummyInterface
{
//
}
class AccessGateTestDummy implements AccessGateTestDummyInterface
{
//
}
class AccessGateTestSubDummy extends AccessGateTestDummy
{
//
}
class AccessGateTestPolicy
{
use HandlesAuthorization;
public function createAny($user, $additional)
{
return $additional;
}
public function create($user)
{
return $user->isAdmin ? $this->allow() : $this->deny('You are not an admin.');
}
public function updateAny($user, AccessGateTestDummy $dummy)
{
return ! $user->isAdmin;
}
public function update($user, AccessGateTestDummy $dummy)
{
return ! $user->isAdmin;
}
public function updateDash($user, AccessGateTestDummy $dummy)
{
return $user instanceof stdClass;
}
}
class AccessGateTestPolicyWithBefore
{
public function before($user, $ability)
{
return true;
}
public function update($user, AccessGateTestDummy $dummy)
{
return false;
}
}
class AccessGateTestResource
{
public function view($user)
{
return true;
}
public function create($user)
{
return true;
}
public function update($user, AccessGateTestDummy $dummy)
{
return true;
}
public function delete($user, AccessGateTestDummy $dummy)
{
return true;
}
}
class AccessGateTestCustomResource
{
public function foo($user)
{
return true;
}
public function bar($user)
{
return true;