Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 19 additions & 24 deletions lib/Settings/Admin.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2020
*
* @author Josua Hunziker <josh@o23.ch>
*
*
* Based on the work of Ján Stibila <nextcloud@stibila.eu>
*
* @license AGPL-3.0
Expand All @@ -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),
Expand All @@ -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;
}
Expand Down
8 changes: 4 additions & 4 deletions templates/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @copyright Copyright (c) 2020
*
* @author Josua Hunziker <josh@o23.ch>
*
*
* Based on the work of Ján Stibila <nextcloud@stibila.eu>
*
* @license AGPL-3.0
Expand All @@ -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' : '';

?>

Expand Down
38 changes: 22 additions & 16 deletions tests/Unit/AdminSettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -39,7 +41,7 @@ function script($script, $scope)

function style($style, $scope)
{
print ('<STYLE>' . $script . '</STYLE><SCOPE>' . $scope . '</SCOPE>');
print ('<STYLE>' . $style . '</STYLE><SCOPE>' . $scope . '</SCOPE>');
}

function p($string)
Expand All @@ -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()
Expand All @@ -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();

Expand All @@ -119,7 +125,7 @@ public function testForm()
include 'templates/admin.php';
$html = ob_get_contents();
@ob_end_clean();

$this->assertIsString($html);
$this->assertStringContainsString('<p class="auto_groups_settings_section">', $html);
$this->assertStringContainsString('<input name="auto_groups" id="auto_groups" value="autogroup1|autogroup2"', $html);
Expand Down
Loading