diff --git a/lib/Settings/Admin.php b/lib/Settings/Admin.php index 74eb839..14cbf5e 100644 --- a/lib/Settings/Admin.php +++ b/lib/Settings/Admin.php @@ -1,9 +1,12 @@ - * + * * Based on the work of Ján Stibila * * @license AGPL-3.0 @@ -25,27 +28,25 @@ namespace OCA\AutoGroups\Settings; use OCP\AppFramework\Http\TemplateResponse; -use OCP\IConfig; +use OCP\AppFramework\Services\IAppConfig; use OCP\Settings\ISettings; class Admin implements ISettings { - - /** @var IConfig */ - private $config; - - public function __construct(IConfig $config) + public function __construct( + private readonly IAppConfig $appConfig, + ) { - $this->config = $config; } - public function getForm() + #[\Override] + public function getForm(): TemplateResponse { - $autoGroups = json_decode($this->config->getAppValue("auto_groups", "auto_groups", '[]')); - $overrideGroups = json_decode($this->config->getAppValue("auto_groups", "override_groups", '[]')); - $creationHook = $this->config->getAppValue("auto_groups", "creation_hook", 'true'); - $modificationHook = $this->config->getAppValue("auto_groups", "modification_hook", 'true'); - $loginHook = $this->config->getAppValue("auto_groups", "login_hook", 'false'); + $autoGroups = $this->appConfig->getAppValueArray("auto_groups"); + $overrideGroups = $this->appConfig->getAppValueArray("override_groups"); + $creationHook = $this->appConfig->getAppValueBool("creation_hook", true); + $modificationHook = $this->appConfig->getAppValueBool("modification_hook", true); + $loginHook = $this->appConfig->getAppValueBool("login_hook"); $parameters = [ 'auto_groups' => implode('|', $autoGroups), @@ -58,20 +59,14 @@ public function getForm() return new TemplateResponse('auto_groups', 'admin', $parameters); } - /** - * @return string the section ID - */ - public function getSection() + #[\Override] + public function getSection(): string { return 'additional'; } - /** - * @return int whether the form should be rather on the top or bottom of - * the admin section. The forms are arranged in ascending order of the - * priority values. It is required to return a value between 0 and 100. - */ - public function getPriority() + #[\Override] + public function getPriority(): int { return 100; } diff --git a/templates/admin.php b/templates/admin.php index 495b217..30caf36 100644 --- a/templates/admin.php +++ b/templates/admin.php @@ -4,7 +4,7 @@ * @copyright Copyright (c) 2020 * * @author Josua Hunziker - * + * * Based on the work of Ján Stibila * * @license AGPL-3.0 @@ -26,9 +26,9 @@ script('auto_groups', 'admin'); style('auto_groups', 'admin'); -$creation_hook_checked = filter_var($_['creation_hook'], FILTER_VALIDATE_BOOLEAN) ? 'checked' : ''; -$modification_hook_checked = filter_var($_['modification_hook'], FILTER_VALIDATE_BOOLEAN) ? 'checked' : ''; -$login_hook_checked = filter_var($_['login_hook'], FILTER_VALIDATE_BOOLEAN) ? 'checked' : ''; +$creation_hook_checked = $_['creation_hook'] ? 'checked' : ''; +$modification_hook_checked = $_['modification_hook'] ? 'checked' : ''; +$login_hook_checked = $_['login_hook'] ? 'checked' : ''; ?> diff --git a/tests/Unit/AdminSettingsTest.php b/tests/Unit/AdminSettingsTest.php index 62a40f8..0d4e65e 100644 --- a/tests/Unit/AdminSettingsTest.php +++ b/tests/Unit/AdminSettingsTest.php @@ -25,10 +25,12 @@ namespace OCA\AutoGroups\Tests\Unit; use OCP\AppFramework\Http\TemplateResponse; +use OCP\AppFramework\Services\IAppConfig; use OCP\IConfig; use OCA\AutoGroups\Settings\Admin; +use PHPUnit\Framework\MockObject\MockObject; use Test\TestCase; // Mock Functions @@ -39,7 +41,7 @@ function script($script, $scope) function style($style, $scope) { - print ('' . $scope . ''); + print ('' . $scope . ''); } function p($string) @@ -58,15 +60,15 @@ public function t($string) // The actual test class class AdminSettingsTest extends TestCase { - private $config; - private $adminSettings; + private IAppConfig&MockObject $appConfig; + private Admin $adminSettings; protected function setUp(): void { parent::setUp(); - $this->config = $this->createMock(IConfig::class); + $this->appConfig = $this->createMock(IAppConfig::class); - $this->adminSettings = new Admin($this->config); + $this->adminSettings = new Admin($this->appConfig); } public function testSection() @@ -84,16 +86,20 @@ public function testPriority() public function testForm() { // getForm() must read all five config values and pass them to the template as parameters - $this->config->expects($this->exactly(5)) - ->method('getAppValue') - ->withConsecutive( - ['auto_groups', 'auto_groups', '[]'], - ['auto_groups', 'override_groups', '[]'], - ['auto_groups', 'creation_hook', 'true'], - ['auto_groups', 'modification_hook', 'true'], - ['auto_groups', 'login_hook', 'false'] - ) - ->willReturnOnConsecutiveCalls(json_encode(['auto1', 'auto2']), json_encode(['override1', 'override2']), true, true); + $this->appConfig->expects($this->exactly(2)) + ->method('getAppValueArray') + ->willReturnMap([ + ['auto_groups', ['auto1', 'auto2']], + ['override_groups', ['override1', 'override2']], + ]); + + $this->appConfig->expects($this->exactly(3)) + ->method('getAppValueBool') + ->willReturnMap([ + ['creation_hook', true, true], + ['modification_hook', true, true], + ['login_hook', false] + ]); $response = $this->adminSettings->getForm(); @@ -119,7 +125,7 @@ public function testForm() include 'templates/admin.php'; $html = ob_get_contents(); @ob_end_clean(); - + $this->assertIsString($html); $this->assertStringContainsString('

', $html); $this->assertStringContainsString('