Skip to content

Commit 9ab25d9

Browse files
nfebeAndyScherzinger
authored andcommitted
fix(systemtags): Provide initial state for admin restriction setting
The “Restrict tag creation and editing to administrators” switch in the SystemTags admin settings was not reflecting the correct database value on page load. This happened because no initial state was being provided. Fix This change ensures the correct initial state is set by: - Injecting IAppConfig and IInitialState dependencies. - Following the same initial state provisioning pattern used by other admin settings in Nextcloud. Signed-off-by: nfebe <[email protected]>
1 parent 28a0174 commit 9ab25d9

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

apps/systemtags/lib/Settings/Admin.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,28 @@
55
*/
66
namespace OCA\SystemTags\Settings;
77

8+
use OCA\SystemTags\AppInfo\Application;
89
use OCP\AppFramework\Http\TemplateResponse;
10+
use OCP\AppFramework\Services\IInitialState;
11+
use OCP\IAppConfig;
912
use OCP\Settings\ISettings;
1013
use OCP\Util;
1114

1215
class Admin implements ISettings {
1316

17+
public function __construct(
18+
private IAppConfig $appConfig,
19+
private IInitialState $initialStateService,
20+
) {
21+
}
22+
1423
/**
1524
* @return TemplateResponse
1625
*/
1726
public function getForm() {
27+
$restrictSystemTagsCreationToAdmin = $this->appConfig->getValueBool(Application::APP_ID, 'restrict_creation_to_admin', false);
28+
$this->initialStateService->provideInitialState('restrictSystemTagsCreationToAdmin', $restrictSystemTagsCreationToAdmin);
29+
1830
Util::addScript('systemtags', 'admin');
1931
return new TemplateResponse('systemtags', 'admin', [], '');
2032
}

apps/systemtags/tests/Settings/AdminTest.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,54 @@
77

88
use OCA\SystemTags\Settings\Admin;
99
use OCP\AppFramework\Http\TemplateResponse;
10+
use OCP\AppFramework\Services\IInitialState;
11+
use OCP\IAppConfig;
1012
use Test\TestCase;
1113

1214
class AdminTest extends TestCase {
1315
/** @var Admin */
1416
private $admin;
17+
/** @var IAppConfig|\PHPUnit\Framework\MockObject\MockObject */
18+
private $appConfig;
19+
/** @var IInitialState|\PHPUnit\Framework\MockObject\MockObject */
20+
private $initialState;
1521

1622
protected function setUp(): void {
1723
parent::setUp();
1824

19-
$this->admin = new Admin();
25+
$this->appConfig = $this->createMock(IAppConfig::class);
26+
$this->initialState = $this->createMock(IInitialState::class);
27+
28+
$this->admin = new Admin(
29+
$this->appConfig,
30+
$this->initialState
31+
);
2032
}
2133

2234
public function testGetForm(): void {
35+
$this->appConfig->expects($this->once())
36+
->method('getValueBool')
37+
->with('systemtags', 'restrict_creation_to_admin', false)
38+
->willReturn(false);
39+
40+
$this->initialState->expects($this->once())
41+
->method('provideInitialState')
42+
->with('restrictSystemTagsCreationToAdmin', false);
43+
44+
$expected = new TemplateResponse('systemtags', 'admin', [], '');
45+
$this->assertEquals($expected, $this->admin->getForm());
46+
}
47+
48+
public function testGetFormWithRestrictedCreation(): void {
49+
$this->appConfig->expects($this->once())
50+
->method('getValueBool')
51+
->with('systemtags', 'restrict_creation_to_admin', false)
52+
->willReturn(true);
53+
54+
$this->initialState->expects($this->once())
55+
->method('provideInitialState')
56+
->with('restrictSystemTagsCreationToAdmin', true);
57+
2358
$expected = new TemplateResponse('systemtags', 'admin', [], '');
2459
$this->assertEquals($expected, $this->admin->getForm());
2560
}

0 commit comments

Comments
 (0)