1010use OC \Settings \AuthorizedGroupMapper ;
1111use OC \Settings \Manager ;
1212use OCA \WorkflowEngine \Settings \Section ;
13+ use OCP \App \IAppManager ;
1314use OCP \Group \ISubAdmin ;
1415use OCP \IGroupManager ;
1516use OCP \IL10N ;
@@ -32,6 +33,7 @@ class ManagerTest extends TestCase {
3233 private AuthorizedGroupMapper &MockObject $ mapper ;
3334 private IGroupManager &MockObject $ groupManager ;
3435 private ISubAdmin &MockObject $ subAdmin ;
36+ private IAppManager &MockObject $ appManager ;
3537
3638 private Manager $ manager ;
3739
@@ -47,6 +49,7 @@ protected function setUp(): void {
4749 $ this ->mapper = $ this ->createMock (AuthorizedGroupMapper::class);
4850 $ this ->groupManager = $ this ->createMock (IGroupManager::class);
4951 $ this ->subAdmin = $ this ->createMock (ISubAdmin::class);
52+ $ this ->appManager = $ this ->createMock (IAppManager::class);
5053
5154 $ this ->manager = new Manager (
5255 $ this ->logger ,
@@ -56,6 +59,7 @@ protected function setUp(): void {
5659 $ this ->mapper ,
5760 $ this ->groupManager ,
5861 $ this ->subAdmin ,
62+ $ this ->appManager ,
5963 );
6064 }
6165
@@ -186,6 +190,70 @@ public function testGetPersonalSettings(): void {
186190 ], $ settings );
187191 }
188192
193+ public function testGetPersonalSettingsHidesSettingsOfAppsNotEnabledForUser (): void {
194+ $ visible = $ this ->createMock (ISettings::class);
195+ $ visible ->method ('getPriority ' )
196+ ->willReturn (16 );
197+ $ visible ->method ('getSection ' )
198+ ->willReturn ('security ' );
199+
200+ $ this ->manager ->registerSetting ('personal ' , 'visibleClass ' , 'enabled_app ' );
201+ $ this ->manager ->registerSetting ('personal ' , 'hiddenClass ' , 'restricted_app ' );
202+
203+ $ this ->appManager ->method ('isEnabledForUser ' )
204+ ->willReturnCallback (static fn (string $ appId ): bool => $ appId === 'enabled_app ' );
205+
206+ // The settings of the app the user has no access to are never instantiated.
207+ $ this ->container ->expects ($ this ->once ())
208+ ->method ('get ' )
209+ ->with ('visibleClass ' )
210+ ->willReturn ($ visible );
211+
212+ $ this ->assertEquals ([
213+ 16 => [$ visible ],
214+ ], $ this ->manager ->getPersonalSettings ('security ' ));
215+ }
216+
217+ public function testGetPersonalSectionsHidesSectionsOfAppsNotEnabledForUser (): void {
218+ $ this ->l10nFactory ->method ('get ' )
219+ ->with ('lib ' )
220+ ->willReturn ($ this ->l10n );
221+ $ this ->l10n ->method ('t ' )
222+ ->willReturnArgument (0 );
223+
224+ $ this ->manager ->registerSection ('personal ' , Section::class, 'restricted_app ' );
225+
226+ $ this ->appManager ->method ('isEnabledForUser ' )
227+ ->with ('restricted_app ' )
228+ ->willReturn (false );
229+
230+ $ this ->container ->expects ($ this ->never ())
231+ ->method ('get ' );
232+
233+ $ this ->assertEquals ([], $ this ->manager ->getPersonalSections ());
234+ }
235+
236+ public function testGetAdminSettingsAreNotHiddenForAppsNotEnabledForUser (): void {
237+ // Admins configure apps they are not a member of themselves.
238+ $ setting = $ this ->createMock (ISettings::class);
239+ $ setting ->method ('getPriority ' )
240+ ->willReturn (13 );
241+ $ setting ->method ('getSection ' )
242+ ->willReturn ('sharing ' );
243+
244+ $ this ->manager ->registerSetting ('admin ' , 'myAdminClass ' , 'restricted_app ' );
245+
246+ $ this ->appManager ->expects ($ this ->never ())
247+ ->method ('isEnabledForUser ' );
248+ $ this ->container ->method ('get ' )
249+ ->with ('myAdminClass ' )
250+ ->willReturn ($ setting );
251+
252+ $ this ->assertEquals ([
253+ 13 => [$ setting ],
254+ ], $ this ->manager ->getAdminSettings ('sharing ' ));
255+ }
256+
189257 public function testSameSectionAsPersonalAndAdmin (): void {
190258 $ this ->l10nFactory
191259 ->expects ($ this ->once ())
0 commit comments