Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make files settings admin configurable #49758

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
17 changes: 13 additions & 4 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 @@ -115,7 +117,12 @@ public function setConfig(string $key, $value): void {
throw new \InvalidArgumentException('Unknown config key');
}

if (!in_array($value, $this->getAllowedConfigValues($key))) {
$isBoolValue = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
if ($isBoolValue !== null) {
$value = $isBoolValue;
}

if (!in_array($value, $this->getAllowedConfigValues($key), true)) {
throw new \InvalidArgumentException('Invalid config value');
}

Expand All @@ -138,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
243 changes: 243 additions & 0 deletions apps/files/tests/Service/UserConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
<?php

/**
* SPDX-FileCopyrightText: 2024 STRATO AG
* SPDX-License-Identifier: AGPL-3.0-only
*/

namespace OCA\Files\Tests\Service;

use OCA\Files\AppInfo\Application;
use OCA\Files\Service\UserConfig;
use OCP\AppFramework\Services\IAppConfig;
use OCP\IConfig;
use OCP\IUser;
use OCP\IUserSession;

/**
* Class UserConfigTest
*
* @group DB
*
* @package OCA\Files
*/
class UserConfigTest extends \Test\TestCase {
/**
* @var string
*/
private $userUID;

private $userMock;

/** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
private $configMock;

/** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
private $userSessionMock;

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

/**
* @var UserConfig|\PHPUnit\Framework\MockObject\MockObject
*/
private $userConfigService;

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');
\OC_User::setUserId($this->userUID);
\OC_Util::setupFS($this->userUID);
$this->userMock = $this->getUserMock($this->userUID);

$this->userSessionMock = $this->createMock(IUserSession::class);
$this->userSessionMock->expects($this->any())
->method('getUser')
->withAnyParameters()
->willReturn($this->userMock);

$this->userConfigService = $this->getUserConfigService(['addActivity']);
}

/**
* @param array $methods
* @return UserConfig|\PHPUnit\Framework\MockObject\MockObject
*/
protected function getUserConfigService(array $methods = []) {
return $this->getMockBuilder(UserConfig::class)
->setConstructorArgs([
$this->configMock,
$this->userSessionMock,
$this->appConfigMock,
])
->setMethods($methods)
->getMock();
}

/**
* @param string $uid
* @return IUser|MockObject
*/
protected function getUserMock($uid) {
$user = $this->createMock(IUser::class);
$user->expects($this->any())
->method('getUID')
->willReturn($uid);
return $user;
}
protected function tearDown(): void {
\OC_User::setUserId('');
$user = \OC::$server->getUserManager()->get($this->userUID);
if ($user !== null) {
$user->delete();
}
}
public function testThrowsExceptionWhenNoUserLoggedInForSetConfig(): void {
$this->userSessionMock = $this->createMock(IUserSession::class);
$this->userSessionMock->expects($this->any())
->method('getUser')
->withAnyParameters()
->willReturn(null);

$this->expectException(\Exception::class);
$this->expectExceptionMessage('No user logged in');

$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, $this->appConfigMock);
$userConfig->setConfig('unknown_key', true);
}

public static function validBoolConfigValues(): array {
return [
['true', '1'],
['false', '0'],
['1', '1'],
['0', '0'],
['yes', '1'],
['no', '0'],
[true, '1'],
[false, '0'],
];
}

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
*/
public function testSetsConfigWithBooleanValuesSuccessfully($boolValue, $expectedValue): void {
$this->configMock->expects($this->once())
->method('setUserValue')
->with($this->userUID, Application::APP_ID, 'crop_image_previews', $expectedValue);

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

public function testGetsConfigsWithDefaultValuesSuccessfully(): void {
$this->userSessionMock->method('getUser')->willReturn($this->userMock);
$this->configMock->method('getUserValue')
->willReturnCallback(function ($userId, $appId, $key, $default) {
return $default;
});

// 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,
'show_hidden' => false,
'sort_favorites_first' => true,
'sort_folders_first' => true,
'grid_view' => false,
'folder_tree' => true,
], $configs);
}

public function testGetsConfigsOverrideWithUserValuesSuccessfully(): void {
$this->userSessionMock->method('getUser')->willReturn($this->userMock);
$this->configMock->method('getUserValue')
->willReturnCallback(function ($userId, $appId, $key, $default) {

// Override the default values
if ($key === 'crop_image_previews') {
return !$default;
} elseif ($key === 'show_hidden') {
return !$default;
}

return $default;
});

// 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,
'show_hidden' => true,
'sort_favorites_first' => true,
'sort_folders_first' => true,
'grid_view' => false,
'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);
}
}