diff --git a/tests/php/Dev/DevAdminControllerTest.php b/tests/php/Dev/DevAdminControllerTest.php index 60e93fed0a5..3af5b8c08df 100644 --- a/tests/php/Dev/DevAdminControllerTest.php +++ b/tests/php/Dev/DevAdminControllerTest.php @@ -2,11 +2,13 @@ namespace SilverStripe\Dev\Tests; +use Exception; +use ReflectionMethod; +use SilverStripe\Control\Director; use SilverStripe\Dev\DevelopmentAdmin; use SilverStripe\Dev\FunctionalTest; -use SilverStripe\Control\Director; -use Exception; use SilverStripe\Dev\Tests\DevAdminControllerTest\Controller1; +use SilverStripe\Dev\Tests\DevAdminControllerTest\ControllerWithPermissions; /** * Note: the running of this test is handled by the thing it's testing (DevelopmentAdmin controller). @@ -21,19 +23,25 @@ protected function setUp(): void DevelopmentAdmin::config()->merge( 'registered_controllers', [ - 'x1' => [ - 'controller' => Controller1::class, - 'links' => [ - 'x1' => 'x1 link description', - 'x1/y1' => 'x1/y1 link description' - ] - ], - 'x2' => [ - 'controller' => 'DevAdminControllerTest_Controller2', // intentionally not a class that exists - 'links' => [ - 'x2' => 'x2 link description' - ] - ] + 'x1' => [ + 'controller' => Controller1::class, + 'links' => [ + 'x1' => 'x1 link description', + 'x1/y1' => 'x1/y1 link description' + ] + ], + 'x2' => [ + 'controller' => 'DevAdminControllerTest_Controller2', // intentionally not a class that exists + 'links' => [ + 'x2' => 'x2 link description' + ] + ], + 'x3' => [ + 'controller' => ControllerWithPermissions::class, + 'links' => [ + 'x3' => 'x3 link description' + ] + ], ] ); } @@ -57,7 +65,34 @@ public function testGoodRegisteredControllerStatus() $this->assertEquals(true, $this->getAndCheckForError('/dev/x2')); } + /** + * @dataProvider getLinksPermissionsProvider + */ + public function testGetLinks(string $permission, int $count): void + { + DevelopmentAdmin::config()->remove('allow_all_cli'); + DevelopmentAdmin::config()->set('allow_all_cli', false); + $this->logInWithPermission($permission); + $controller = new DevelopmentAdmin(); + $method = new ReflectionMethod($controller, 'getLinks'); + $method->setAccessible(true); + + $this->assertCount( + $count, + $method->invoke($controller), + 'Incorrect amount of links shown for permission level' + ); + } + protected function getLinksPermissionsProvider() : array + { + return [ + ['ADMIN', 8], + ['ALL_DEV_ADMIN', 8], + ['DEV_ADMIN_TEST_PERMISSION', 1], + ['NOTHING', 0], + ]; + } protected function getCapture($url) { diff --git a/tests/php/Dev/DevAdminControllerTest/ControllerWithPermissions.php b/tests/php/Dev/DevAdminControllerTest/ControllerWithPermissions.php new file mode 100644 index 00000000000..7cbd2d276ce --- /dev/null +++ b/tests/php/Dev/DevAdminControllerTest/ControllerWithPermissions.php @@ -0,0 +1,39 @@ + 'index', + ]; + + private static $allowed_actions = [ + 'index', + ]; + + + public function index() + { + echo self::OK_MSG; + } + + public function canInit() + { + return Permission::check('DEV_ADMIN_TEST_PERMISSION'); + } + + public function providePermissions() + { + return [ + 'DEV_ADMIN_TEST_PERMISSION' => 'Dev admin test permission', + ]; + } +}