Completed
Push — master ( 87022e...50cc34 )
by
unknown
30:02
created
apps/settings/lib/Controller/CommonSettingsTrait.php 1 patch
Indentation   +158 added lines, -158 removed lines patch added patch discarded remove patch
@@ -29,162 +29,162 @@
 block discarded – undo
29 29
  */
30 30
 trait CommonSettingsTrait {
31 31
 
32
-	/** @var ISettingsManager */
33
-	private $settingsManager;
34
-
35
-	/** @var INavigationManager */
36
-	private $navigationManager;
37
-
38
-	/** @var IUserSession */
39
-	private $userSession;
40
-
41
-	/** @var IGroupManager */
42
-	private $groupManager;
43
-
44
-	/** @var ISubAdmin */
45
-	private $subAdmin;
46
-
47
-	private IDeclarativeManager $declarativeSettingsManager;
48
-
49
-	/** @var IInitialState */
50
-	private $initialState;
51
-
52
-	/**
53
-	 * @param IIconSection[][] $sections
54
-	 * @psalm-param 'admin'|'personal' $type
55
-	 * @return list<array{id: string, name: string, active: bool, icon: string}>
56
-	 */
57
-	protected function formatSections(array $sections, string $currentSection, string $type, string $currentType): array {
58
-		$templateParameters = [];
59
-		foreach ($sections as $prioritizedSections) {
60
-			foreach ($prioritizedSections as $section) {
61
-				if ($type === 'admin') {
62
-					$settings = $this->settingsManager->getAllowedAdminSettings($section->getID(), $this->userSession->getUser());
63
-				} elseif ($type === 'personal') {
64
-					$settings = $this->settingsManager->getPersonalSettings($section->getID());
65
-				}
66
-
67
-				/** @psalm-suppress PossiblyNullArgument */
68
-				$declarativeFormIDs = $this->declarativeSettingsManager->getFormIDs($this->userSession->getUser(), $type, $section->getID());
69
-
70
-				if (empty($settings) && empty($declarativeFormIDs)) {
71
-					continue;
72
-				}
73
-
74
-				$icon = $section->getIcon();
75
-
76
-				$active = $section->getID() === $currentSection
77
-					&& $type === $currentType;
78
-
79
-				$templateParameters[] = [
80
-					'id' => $section->getID(),
81
-					'name' => $section->getName(),
82
-					'active' => $active,
83
-					'icon' => $icon,
84
-				];
85
-			}
86
-		}
87
-		return $templateParameters;
88
-	}
89
-
90
-	/**
91
-	 * @return list<array{id: string, name: string, active: bool, icon: string}>
92
-	 */
93
-	protected function formatPersonalSections(string $currentType, string $currentSection): array {
94
-		$sections = $this->settingsManager->getPersonalSections();
95
-		return $this->formatSections($sections, $currentSection, 'personal', $currentType);
96
-	}
97
-
98
-	/**
99
-	 * @return list<array{id: string, name: string, active: bool, icon: string}>
100
-	 */
101
-	protected function formatAdminSections(string $currentType, string $currentSection): array {
102
-		$sections = $this->settingsManager->getAdminSections();
103
-		return $this->formatSections($sections, $currentSection, 'admin', $currentType);
104
-	}
105
-
106
-	/**
107
-	 * @param list<ISettings> $settings
108
-	 * @param list<DeclarativeSettingsFormSchemaWithValues> $declarativeSettings
109
-	 * @return array{content: string}
110
-	 */
111
-	private function formatSettings(array $settings, array $declarativeSettings): array {
112
-		$settings = array_merge($settings, $declarativeSettings);
113
-
114
-		usort($settings, function ($first, $second) {
115
-			$priorityOne = $first instanceof ISettings ? $first->getPriority() : $first['priority'];
116
-			$priorityTwo = $second instanceof ISettings ? $second->getPriority() : $second['priority'];
117
-			return $priorityOne - $priorityTwo;
118
-		});
119
-
120
-		$html = '';
121
-		foreach ($settings as $setting) {
122
-			if ($setting instanceof ISettings) {
123
-				$form = $setting->getForm();
124
-				$html .= $form->renderAs('')->render();
125
-			} else {
126
-				$html .= '<div id="' . $setting['app'] . '_' . $setting['id'] . '"></div>';
127
-			}
128
-		}
129
-		return ['content' => $html];
130
-	}
131
-
132
-	/**
133
-	 * @psalm-param 'admin'|'personal' $type
134
-	 */
135
-	private function getIndexResponse(string $type, string $section): TemplateResponse {
136
-		$user = $this->userSession->getUser();
137
-		assert($user !== null, 'No user logged in for settings');
138
-
139
-		$this->declarativeSettingsManager->loadSchemas();
140
-		$declarativeSettings = $this->declarativeSettingsManager->getFormsWithValues($user, $type, $section);
141
-
142
-		foreach ($declarativeSettings as &$form) {
143
-			foreach ($form['fields'] as &$field) {
144
-				if (isset($field['sensitive']) && $field['sensitive'] === true && !empty($field['value'])) {
145
-					$field['value'] = 'dummySecret';
146
-				}
147
-			}
148
-		}
149
-
150
-		if ($type === 'personal') {
151
-			$settings = array_values($this->settingsManager->getPersonalSettings($section));
152
-			if ($section === 'theming') {
153
-				$this->navigationManager->setActiveEntry('accessibility_settings');
154
-			} else {
155
-				$this->navigationManager->setActiveEntry('settings');
156
-			}
157
-		} elseif ($type === 'admin') {
158
-			$settings = array_values($this->settingsManager->getAllowedAdminSettings($section, $user));
159
-			if (empty($settings) && empty($declarativeSettings)) {
160
-				throw new NotAdminException('Logged in user does not have permission to access these settings.');
161
-			}
162
-			$this->navigationManager->setActiveEntry('admin_settings');
163
-		} else {
164
-			throw new InvalidArgumentException('$type must be either "admin" or "personal"');
165
-		}
166
-
167
-		if (!empty($declarativeSettings)) {
168
-			Util::addScript(Application::APP_ID, 'declarative-settings-forms');
169
-			$this->initialState->provideInitialState('declarative-settings-forms', $declarativeSettings);
170
-		}
171
-
172
-		$this->initialState->provideInitialState('sections', [
173
-			'personal' => $this->formatPersonalSections($type, $section),
174
-			'admin' => $this->formatAdminSections($type, $section),
175
-		]);
176
-
177
-		$settings = array_merge(...$settings);
178
-		$templateParams = $this->formatSettings($settings, $declarativeSettings);
179
-
180
-		$activeSection = $this->settingsManager->getSection($type, $section);
181
-		if ($activeSection) {
182
-			$templateParams['pageTitle'] = $activeSection->getName();
183
-			$templateParams['activeSectionId'] = $activeSection->getID();
184
-			$templateParams['activeSectionType'] = $type;
185
-		}
186
-
187
-		Util::addScript(Application::APP_ID, 'main', prepend: true);
188
-		return new TemplateResponse('settings', 'settings/frame', $templateParams);
189
-	}
32
+    /** @var ISettingsManager */
33
+    private $settingsManager;
34
+
35
+    /** @var INavigationManager */
36
+    private $navigationManager;
37
+
38
+    /** @var IUserSession */
39
+    private $userSession;
40
+
41
+    /** @var IGroupManager */
42
+    private $groupManager;
43
+
44
+    /** @var ISubAdmin */
45
+    private $subAdmin;
46
+
47
+    private IDeclarativeManager $declarativeSettingsManager;
48
+
49
+    /** @var IInitialState */
50
+    private $initialState;
51
+
52
+    /**
53
+     * @param IIconSection[][] $sections
54
+     * @psalm-param 'admin'|'personal' $type
55
+     * @return list<array{id: string, name: string, active: bool, icon: string}>
56
+     */
57
+    protected function formatSections(array $sections, string $currentSection, string $type, string $currentType): array {
58
+        $templateParameters = [];
59
+        foreach ($sections as $prioritizedSections) {
60
+            foreach ($prioritizedSections as $section) {
61
+                if ($type === 'admin') {
62
+                    $settings = $this->settingsManager->getAllowedAdminSettings($section->getID(), $this->userSession->getUser());
63
+                } elseif ($type === 'personal') {
64
+                    $settings = $this->settingsManager->getPersonalSettings($section->getID());
65
+                }
66
+
67
+                /** @psalm-suppress PossiblyNullArgument */
68
+                $declarativeFormIDs = $this->declarativeSettingsManager->getFormIDs($this->userSession->getUser(), $type, $section->getID());
69
+
70
+                if (empty($settings) && empty($declarativeFormIDs)) {
71
+                    continue;
72
+                }
73
+
74
+                $icon = $section->getIcon();
75
+
76
+                $active = $section->getID() === $currentSection
77
+                    && $type === $currentType;
78
+
79
+                $templateParameters[] = [
80
+                    'id' => $section->getID(),
81
+                    'name' => $section->getName(),
82
+                    'active' => $active,
83
+                    'icon' => $icon,
84
+                ];
85
+            }
86
+        }
87
+        return $templateParameters;
88
+    }
89
+
90
+    /**
91
+     * @return list<array{id: string, name: string, active: bool, icon: string}>
92
+     */
93
+    protected function formatPersonalSections(string $currentType, string $currentSection): array {
94
+        $sections = $this->settingsManager->getPersonalSections();
95
+        return $this->formatSections($sections, $currentSection, 'personal', $currentType);
96
+    }
97
+
98
+    /**
99
+     * @return list<array{id: string, name: string, active: bool, icon: string}>
100
+     */
101
+    protected function formatAdminSections(string $currentType, string $currentSection): array {
102
+        $sections = $this->settingsManager->getAdminSections();
103
+        return $this->formatSections($sections, $currentSection, 'admin', $currentType);
104
+    }
105
+
106
+    /**
107
+     * @param list<ISettings> $settings
108
+     * @param list<DeclarativeSettingsFormSchemaWithValues> $declarativeSettings
109
+     * @return array{content: string}
110
+     */
111
+    private function formatSettings(array $settings, array $declarativeSettings): array {
112
+        $settings = array_merge($settings, $declarativeSettings);
113
+
114
+        usort($settings, function ($first, $second) {
115
+            $priorityOne = $first instanceof ISettings ? $first->getPriority() : $first['priority'];
116
+            $priorityTwo = $second instanceof ISettings ? $second->getPriority() : $second['priority'];
117
+            return $priorityOne - $priorityTwo;
118
+        });
119
+
120
+        $html = '';
121
+        foreach ($settings as $setting) {
122
+            if ($setting instanceof ISettings) {
123
+                $form = $setting->getForm();
124
+                $html .= $form->renderAs('')->render();
125
+            } else {
126
+                $html .= '<div id="' . $setting['app'] . '_' . $setting['id'] . '"></div>';
127
+            }
128
+        }
129
+        return ['content' => $html];
130
+    }
131
+
132
+    /**
133
+     * @psalm-param 'admin'|'personal' $type
134
+     */
135
+    private function getIndexResponse(string $type, string $section): TemplateResponse {
136
+        $user = $this->userSession->getUser();
137
+        assert($user !== null, 'No user logged in for settings');
138
+
139
+        $this->declarativeSettingsManager->loadSchemas();
140
+        $declarativeSettings = $this->declarativeSettingsManager->getFormsWithValues($user, $type, $section);
141
+
142
+        foreach ($declarativeSettings as &$form) {
143
+            foreach ($form['fields'] as &$field) {
144
+                if (isset($field['sensitive']) && $field['sensitive'] === true && !empty($field['value'])) {
145
+                    $field['value'] = 'dummySecret';
146
+                }
147
+            }
148
+        }
149
+
150
+        if ($type === 'personal') {
151
+            $settings = array_values($this->settingsManager->getPersonalSettings($section));
152
+            if ($section === 'theming') {
153
+                $this->navigationManager->setActiveEntry('accessibility_settings');
154
+            } else {
155
+                $this->navigationManager->setActiveEntry('settings');
156
+            }
157
+        } elseif ($type === 'admin') {
158
+            $settings = array_values($this->settingsManager->getAllowedAdminSettings($section, $user));
159
+            if (empty($settings) && empty($declarativeSettings)) {
160
+                throw new NotAdminException('Logged in user does not have permission to access these settings.');
161
+            }
162
+            $this->navigationManager->setActiveEntry('admin_settings');
163
+        } else {
164
+            throw new InvalidArgumentException('$type must be either "admin" or "personal"');
165
+        }
166
+
167
+        if (!empty($declarativeSettings)) {
168
+            Util::addScript(Application::APP_ID, 'declarative-settings-forms');
169
+            $this->initialState->provideInitialState('declarative-settings-forms', $declarativeSettings);
170
+        }
171
+
172
+        $this->initialState->provideInitialState('sections', [
173
+            'personal' => $this->formatPersonalSections($type, $section),
174
+            'admin' => $this->formatAdminSections($type, $section),
175
+        ]);
176
+
177
+        $settings = array_merge(...$settings);
178
+        $templateParams = $this->formatSettings($settings, $declarativeSettings);
179
+
180
+        $activeSection = $this->settingsManager->getSection($type, $section);
181
+        if ($activeSection) {
182
+            $templateParams['pageTitle'] = $activeSection->getName();
183
+            $templateParams['activeSectionId'] = $activeSection->getID();
184
+            $templateParams['activeSectionType'] = $type;
185
+        }
186
+
187
+        Util::addScript(Application::APP_ID, 'main', prepend: true);
188
+        return new TemplateResponse('settings', 'settings/frame', $templateParams);
189
+    }
190 190
 }
Please login to merge, or discard this patch.
apps/settings/tests/Controller/AdminSettingsControllerTest.php 2 patches
Indentation   +98 added lines, -98 removed lines patch added patch discarded remove patch
@@ -32,113 +32,113 @@
 block discarded – undo
32 32
 #[\PHPUnit\Framework\Attributes\Group(name: 'DB')]
33 33
 class AdminSettingsControllerTest extends TestCase {
34 34
 
35
-	private IRequest&MockObject $request;
36
-	private INavigationManager&MockObject $navigationManager;
37
-	private IManager&MockObject $settingsManager;
38
-	private IUserSession&MockObject $userSession;
39
-	private IGroupManager&MockObject $groupManager;
40
-	private ISubAdmin&MockObject $subAdmin;
41
-	private IDeclarativeManager&MockObject $declarativeSettingsManager;
42
-	private IInitialState&MockObject $initialState;
35
+    private IRequest&MockObject $request;
36
+    private INavigationManager&MockObject $navigationManager;
37
+    private IManager&MockObject $settingsManager;
38
+    private IUserSession&MockObject $userSession;
39
+    private IGroupManager&MockObject $groupManager;
40
+    private ISubAdmin&MockObject $subAdmin;
41
+    private IDeclarativeManager&MockObject $declarativeSettingsManager;
42
+    private IInitialState&MockObject $initialState;
43 43
 
44
-	private string $adminUid = 'lololo';
45
-	private AdminSettingsController $adminSettingsController;
44
+    private string $adminUid = 'lololo';
45
+    private AdminSettingsController $adminSettingsController;
46 46
 
47
-	protected function setUp(): void {
48
-		parent::setUp();
47
+    protected function setUp(): void {
48
+        parent::setUp();
49 49
 
50
-		$this->request = $this->createMock(IRequest::class);
51
-		$this->navigationManager = $this->createMock(INavigationManager::class);
52
-		$this->settingsManager = $this->createMock(IManager::class);
53
-		$this->userSession = $this->createMock(IUserSession::class);
54
-		$this->groupManager = $this->createMock(IGroupManager::class);
55
-		$this->subAdmin = $this->createMock(ISubAdmin::class);
56
-		$this->declarativeSettingsManager = $this->createMock(IDeclarativeManager::class);
57
-		$this->initialState = $this->createMock(IInitialState::class);
50
+        $this->request = $this->createMock(IRequest::class);
51
+        $this->navigationManager = $this->createMock(INavigationManager::class);
52
+        $this->settingsManager = $this->createMock(IManager::class);
53
+        $this->userSession = $this->createMock(IUserSession::class);
54
+        $this->groupManager = $this->createMock(IGroupManager::class);
55
+        $this->subAdmin = $this->createMock(ISubAdmin::class);
56
+        $this->declarativeSettingsManager = $this->createMock(IDeclarativeManager::class);
57
+        $this->initialState = $this->createMock(IInitialState::class);
58 58
 
59
-		$this->adminSettingsController = new AdminSettingsController(
60
-			'settings',
61
-			$this->request,
62
-			$this->navigationManager,
63
-			$this->settingsManager,
64
-			$this->userSession,
65
-			$this->groupManager,
66
-			$this->subAdmin,
67
-			$this->declarativeSettingsManager,
68
-			$this->initialState,
69
-		);
59
+        $this->adminSettingsController = new AdminSettingsController(
60
+            'settings',
61
+            $this->request,
62
+            $this->navigationManager,
63
+            $this->settingsManager,
64
+            $this->userSession,
65
+            $this->groupManager,
66
+            $this->subAdmin,
67
+            $this->declarativeSettingsManager,
68
+            $this->initialState,
69
+        );
70 70
 
71
-		$user = Server::get(IUserManager::class)->createUser($this->adminUid, 'mylongrandompassword');
72
-		\OC_User::setUserId($user->getUID());
73
-		Server::get(IGroupManager::class)->createGroup('admin')->addUser($user);
74
-	}
71
+        $user = Server::get(IUserManager::class)->createUser($this->adminUid, 'mylongrandompassword');
72
+        \OC_User::setUserId($user->getUID());
73
+        Server::get(IGroupManager::class)->createGroup('admin')->addUser($user);
74
+    }
75 75
 
76
-	protected function tearDown(): void {
77
-		Server::get(IUserManager::class)
78
-			->get($this->adminUid)
79
-			->delete();
80
-		\OC_User::setUserId(null);
81
-		Server::get(IUserSession::class)->setUser(null);
76
+    protected function tearDown(): void {
77
+        Server::get(IUserManager::class)
78
+            ->get($this->adminUid)
79
+            ->delete();
80
+        \OC_User::setUserId(null);
81
+        Server::get(IUserSession::class)->setUser(null);
82 82
 
83
-		parent::tearDown();
84
-	}
83
+        parent::tearDown();
84
+    }
85 85
 
86
-	public function testIndex(): void {
87
-		$user = $this->createMock(IUser::class);
88
-		$this->userSession
89
-			->method('getUser')
90
-			->willReturn($user);
91
-		$user->method('getUID')->willReturn('user123');
92
-		$this->groupManager
93
-			->method('isAdmin')
94
-			->with('user123')
95
-			->willReturn(true);
96
-		$this->subAdmin
97
-			->method('isSubAdmin')
98
-			->with($user)
99
-			->willReturn(false);
86
+    public function testIndex(): void {
87
+        $user = $this->createMock(IUser::class);
88
+        $this->userSession
89
+            ->method('getUser')
90
+            ->willReturn($user);
91
+        $user->method('getUID')->willReturn('user123');
92
+        $this->groupManager
93
+            ->method('isAdmin')
94
+            ->with('user123')
95
+            ->willReturn(true);
96
+        $this->subAdmin
97
+            ->method('isSubAdmin')
98
+            ->with($user)
99
+            ->willReturn(false);
100 100
 
101
-		$form = new TemplateResponse('settings', 'settings/empty');
102
-		$setting = $this->createMock(ServerDevNotice::class);
103
-		$setting->expects(self::any())
104
-			->method('getForm')
105
-			->willReturn($form);
106
-		$this->settingsManager
107
-			->expects($this->once())
108
-			->method('getAdminSections')
109
-			->willReturn([]);
110
-		$this->settingsManager
111
-			->expects($this->once())
112
-			->method('getPersonalSections')
113
-			->willReturn([]);
114
-		$this->settingsManager
115
-			->expects($this->once())
116
-			->method('getAllowedAdminSettings')
117
-			->with('test')
118
-			->willReturn([5 => [$setting]]);
119
-		$this->declarativeSettingsManager
120
-			->expects($this->any())
121
-			->method('getFormIDs')
122
-			->with($user, 'admin', 'test')
123
-			->willReturn([]);
101
+        $form = new TemplateResponse('settings', 'settings/empty');
102
+        $setting = $this->createMock(ServerDevNotice::class);
103
+        $setting->expects(self::any())
104
+            ->method('getForm')
105
+            ->willReturn($form);
106
+        $this->settingsManager
107
+            ->expects($this->once())
108
+            ->method('getAdminSections')
109
+            ->willReturn([]);
110
+        $this->settingsManager
111
+            ->expects($this->once())
112
+            ->method('getPersonalSections')
113
+            ->willReturn([]);
114
+        $this->settingsManager
115
+            ->expects($this->once())
116
+            ->method('getAllowedAdminSettings')
117
+            ->with('test')
118
+            ->willReturn([5 => [$setting]]);
119
+        $this->declarativeSettingsManager
120
+            ->expects($this->any())
121
+            ->method('getFormIDs')
122
+            ->with($user, 'admin', 'test')
123
+            ->willReturn([]);
124 124
 
125
-		$initialState = [];
126
-		$this->initialState->expects(self::atLeastOnce())
127
-			->method('provideInitialState')
128
-			->willReturnCallback(function () use (&$initialState) {
129
-				$initialState[] = func_get_args();
130
-			});
125
+        $initialState = [];
126
+        $this->initialState->expects(self::atLeastOnce())
127
+            ->method('provideInitialState')
128
+            ->willReturnCallback(function () use (&$initialState) {
129
+                $initialState[] = func_get_args();
130
+            });
131 131
 
132
-		$expected = new TemplateResponse(
133
-			'settings',
134
-			'settings/frame',
135
-			[
136
-				'content' => ''
137
-			],
138
-		);
139
-		$this->assertEquals($expected, $this->adminSettingsController->index('test'));
140
-		$this->assertEquals([
141
-			['sections', ['admin' => [], 'personal' => []]],
142
-		], $initialState);
143
-	}
132
+        $expected = new TemplateResponse(
133
+            'settings',
134
+            'settings/frame',
135
+            [
136
+                'content' => ''
137
+            ],
138
+        );
139
+        $this->assertEquals($expected, $this->adminSettingsController->index('test'));
140
+        $this->assertEquals([
141
+            ['sections', ['admin' => [], 'personal' => []]],
142
+        ], $initialState);
143
+    }
144 144
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -125,7 +125,7 @@
 block discarded – undo
125 125
 		$initialState = [];
126 126
 		$this->initialState->expects(self::atLeastOnce())
127 127
 			->method('provideInitialState')
128
-			->willReturnCallback(function () use (&$initialState) {
128
+			->willReturnCallback(function() use (&$initialState) {
129 129
 				$initialState[] = func_get_args();
130 130
 			});
131 131
 
Please login to merge, or discard this patch.