Skip to content

Commit

Permalink
attempt a test
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewandante committed Oct 18, 2023
1 parent 7c4534b commit 1dedee0
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 15 deletions.
64 changes: 49 additions & 15 deletions tests/php/Dev/DevAdminControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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'
]
],
]
);
}
Expand All @@ -57,7 +65,33 @@ public function testGoodRegisteredControllerStatus()
$this->assertEquals(true, $this->getAndCheckForError('/dev/x2'));
}

/**
* @dataProvider getLinksPermissionsProvider
*/
public function testGetLinks(string $permission, int $count): void
{
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)
{
Expand Down
39 changes: 39 additions & 0 deletions tests/php/Dev/DevAdminControllerTest/ControllerWithPermissions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace SilverStripe\Dev\Tests\DevAdminControllerTest;

use SilverStripe\Control\Controller;
use SilverStripe\Security\Permission;
use SilverStripe\Security\PermissionProvider;

class ControllerWithPermissions extends Controller implements PermissionProvider
{

const OK_MSG = 'DevAdminControllerTest_Controller1 TEST OK';

private static $url_handlers = [
'' => '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',
];
}
}

0 comments on commit 1dedee0

Please sign in to comment.