Skip to content

Commit

Permalink
feat(files/user-config): make configs admin-configurable
Browse files Browse the repository at this point in the history
Signed-off-by: Kai Henseler <[email protected]>
  • Loading branch information
bromiesTM committed Dec 12, 2024
1 parent efa9c9d commit 5b5ee91
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 7 deletions.
10 changes: 7 additions & 3 deletions apps/files/lib/Service/UserConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace OCA\Files\Service;

use OCA\Files\AppInfo\Application;
use OCP\AppFramework\Services\IAppConfig;
use OCP\IConfig;
use OCP\IUser;
use OCP\IUserSession;
Expand Down Expand Up @@ -54,6 +55,7 @@ class UserConfig {
public function __construct(
protected IConfig $config,
IUserSession $userSession,
protected IAppConfig $appConfig,
) {
$this->user = $userSession->getUser();
}
Expand Down Expand Up @@ -143,9 +145,11 @@ public function getConfigs(): array {

$userId = $this->user->getUID();
$userConfigs = array_map(function (string $key) use ($userId) {
$value = $this->config->getUserValue($userId, Application::APP_ID, $key, $this->getDefaultConfigValue($key));
// If the default is expected to be a boolean, we need to cast the value
if (is_bool($this->getDefaultConfigValue($key)) && is_string($value)) {
$value = $this->config->getUserValue($userId, Application::APP_ID, $key, null);
if ($value === null) {
$value = $this->appConfig->getAppValueBool($key, $this->getDefaultConfigValue($key));
} else if (is_bool($this->getDefaultConfigValue($key)) && is_string($value)) {
// If the default value is expected to be a boolean, we need to cast the value
return $value === '1';
}
return $value;
Expand Down
66 changes: 62 additions & 4 deletions apps/files/tests/Service/UserConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use OCA\Files\AppInfo\Application;
use OCA\Files\Service\UserConfig;
use OCP\AppFramework\Services\IAppConfig;
use OCP\IConfig;
use OCP\IUser;
use OCP\IUserSession;
Expand All @@ -34,6 +35,9 @@ class UserConfigTest extends \Test\TestCase {
/** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
private $userSessionMock;

/** @var IAppConfig|\PHPUnit\Framework\MockObject\MockObject */
private $appConfigMock;

/**
* @var UserConfig|\PHPUnit\Framework\MockObject\MockObject
*/
Expand All @@ -42,6 +46,7 @@ class UserConfigTest extends \Test\TestCase {
protected function setUp(): void {
parent::setUp();
$this->configMock = $this->createMock(IConfig::class);
$this->appConfigMock = $this->createMock(IAppConfig::class);

$this->userUID = static::getUniqueID('user_id-');
\OC::$server->getUserManager()->createUser($this->userUID, 'test');
Expand All @@ -67,6 +72,7 @@ protected function getUserConfigService(array $methods = []) {
->setConstructorArgs([
$this->configMock,
$this->userSessionMock,
$this->appConfigMock,
])
->setMethods($methods)
->getMock();
Expand Down Expand Up @@ -100,15 +106,15 @@ public function testThrowsExceptionWhenNoUserLoggedInForSetConfig(): void {
$this->expectException(\Exception::class);
$this->expectExceptionMessage('No user logged in');

$userConfig = new UserConfig($this->configMock, $this->userSessionMock);
$userConfig = new UserConfig($this->configMock, $this->userSessionMock, $this->appConfigMock);
$userConfig->setConfig('crop_image_previews', true);
}

public function testThrowsInvalidArgumentExceptionForUnknownConfigKey(): void {
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Unknown config key');

$userConfig = new UserConfig($this->configMock, $this->userSessionMock);
$userConfig = new UserConfig($this->configMock, $this->userSessionMock, $this->appConfigMock);
$userConfig->setConfig('unknown_key', true);
}

Expand All @@ -125,6 +131,14 @@ public static function validBoolConfigValues(): array {
];
}

public function testThrowsInvalidArgumentExceptionForInvalidConfigValue(): void {
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid config value');

$userConfig = new UserConfig($this->configMock, $this->userSessionMock, $this->appConfigMock);
$userConfig->setConfig('crop_image_previews', 'foo');
}

/**
* @dataProvider validBoolConfigValues
*/
Expand All @@ -144,7 +158,13 @@ public function testGetsConfigsWithDefaultValuesSuccessfully(): void {
return $default;
});

$userConfig = new UserConfig($this->configMock, $this->userSessionMock);
// pass the default app settings unchanged
$this->appConfigMock->method('getAppValueBool')
->willReturnCallback(function ($key, $default) {
return $default;
});

$userConfig = new UserConfig($this->configMock, $this->userSessionMock, $this->appConfigMock);
$configs = $userConfig->getConfigs();
$this->assertEquals([
'crop_image_previews' => true,
Expand All @@ -171,7 +191,13 @@ public function testGetsConfigsOverrideWithUserValuesSuccessfully(): void {
return $default;
});

$userConfig = new UserConfig($this->configMock, $this->userSessionMock);
// pass the default app settings unchanged
$this->appConfigMock->method('getAppValueBool')
->willReturnCallback(function ($key, $default) {
return $default;
});

$userConfig = new UserConfig($this->configMock, $this->userSessionMock, $this->appConfigMock);
$configs = $userConfig->getConfigs();
$this->assertEquals([
'crop_image_previews' => false,
Expand All @@ -182,4 +208,36 @@ public function testGetsConfigsOverrideWithUserValuesSuccessfully(): void {
'folder_tree' => true,
], $configs);
}

public function testGetsConfigsOverrideWithAppsValuesSuccessfully(): void {
$this->userSessionMock->method('getUser')->willReturn($this->userMock);

// set all user values to true
$this->configMock->method('getUserValue')
->willReturnCallback(function () {
return true;
});

// emulate override by the app config values
$this->appConfigMock->method('getAppValueBool')
->willReturnCallback(function ($key, $default) {
if ($key === 'crop_image_previews') {
return false;
} elseif ($key === 'show_hidden') {
return false;
}
return $default;
});

$userConfig = new UserConfig($this->configMock, $this->userSessionMock, $this->appConfigMock);
$configs = $userConfig->getConfigs();
$this->assertEquals([
'crop_image_previews' => false,
'show_hidden' => false,
'sort_favorites_first' => true,
'sort_folders_first' => true,
'grid_view' => true,
'folder_tree' => true,
], $configs);
}
}

0 comments on commit 5b5ee91

Please sign in to comment.