99
1010use OCA \Files \AppInfo \Application ;
1111use OCA \Files \Service \UserConfig ;
12+ use OCP \AppFramework \Services \IAppConfig ;
1213use OCP \IConfig ;
1314use OCP \IUser ;
1415use OCP \IUserSession ;
@@ -34,6 +35,9 @@ class UserConfigTest extends \Test\TestCase {
3435 /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
3536 private $ userSessionMock ;
3637
38+ /** @var IAppConfig|\PHPUnit\Framework\MockObject\MockObject */
39+ private $ appConfigMock ;
40+
3741 /**
3842 * @var UserConfig|\PHPUnit\Framework\MockObject\MockObject
3943 */
@@ -42,6 +46,7 @@ class UserConfigTest extends \Test\TestCase {
4246 protected function setUp (): void {
4347 parent ::setUp ();
4448 $ this ->configMock = $ this ->createMock (IConfig::class);
49+ $ this ->appConfigMock = $ this ->createMock (IAppConfig::class);
4550
4651 $ this ->userUID = static ::getUniqueID ('user_id- ' );
4752 \OC ::$ server ->getUserManager ()->createUser ($ this ->userUID , 'test ' );
@@ -67,6 +72,7 @@ protected function getUserConfigService(array $methods = []) {
6772 ->setConstructorArgs ([
6873 $ this ->configMock ,
6974 $ this ->userSessionMock ,
75+ $ this ->appConfigMock ,
7076 ])
7177 ->setMethods ($ methods )
7278 ->getMock ();
@@ -100,15 +106,15 @@ public function testThrowsExceptionWhenNoUserLoggedInForSetConfig(): void {
100106 $ this ->expectException (\Exception::class);
101107 $ this ->expectExceptionMessage ('No user logged in ' );
102108
103- $ userConfig = new UserConfig ($ this ->configMock , $ this ->userSessionMock );
109+ $ userConfig = new UserConfig ($ this ->configMock , $ this ->userSessionMock , $ this -> appConfigMock );
104110 $ userConfig ->setConfig ('crop_image_previews ' , true );
105111 }
106112
107113 public function testThrowsInvalidArgumentExceptionForUnknownConfigKey (): void {
108114 $ this ->expectException (\InvalidArgumentException::class);
109115 $ this ->expectExceptionMessage ('Unknown config key ' );
110116
111- $ userConfig = new UserConfig ($ this ->configMock , $ this ->userSessionMock );
117+ $ userConfig = new UserConfig ($ this ->configMock , $ this ->userSessionMock , $ this -> appConfigMock );
112118 $ userConfig ->setConfig ('unknown_key ' , true );
113119 }
114120
@@ -125,6 +131,14 @@ public static function validBoolConfigValues(): array {
125131 ];
126132 }
127133
134+ public function testThrowsInvalidArgumentExceptionForInvalidConfigValue (): void {
135+ $ this ->expectException (\InvalidArgumentException::class);
136+ $ this ->expectExceptionMessage ('Invalid config value ' );
137+
138+ $ userConfig = new UserConfig ($ this ->configMock , $ this ->userSessionMock , $ this ->appConfigMock );
139+ $ userConfig ->setConfig ('crop_image_previews ' , 'foo ' );
140+ }
141+
128142 /**
129143 * @dataProvider validBoolConfigValues
130144 */
@@ -144,7 +158,13 @@ public function testGetsConfigsWithDefaultValuesSuccessfully(): void {
144158 return $ default ;
145159 });
146160
147- $ userConfig = new UserConfig ($ this ->configMock , $ this ->userSessionMock );
161+ // pass the default app settings unchanged
162+ $ this ->appConfigMock ->method ('getAppValueBool ' )
163+ ->willReturnCallback (function ($ key , $ default ) {
164+ return $ default ;
165+ });
166+
167+ $ userConfig = new UserConfig ($ this ->configMock , $ this ->userSessionMock , $ this ->appConfigMock );
148168 $ configs = $ userConfig ->getConfigs ();
149169 $ this ->assertEquals ([
150170 'crop_image_previews ' => true ,
@@ -171,7 +191,13 @@ public function testGetsConfigsOverrideWithUserValuesSuccessfully(): void {
171191 return $ default ;
172192 });
173193
174- $ userConfig = new UserConfig ($ this ->configMock , $ this ->userSessionMock );
194+ // pass the default app settings unchanged
195+ $ this ->appConfigMock ->method ('getAppValueBool ' )
196+ ->willReturnCallback (function ($ key , $ default ) {
197+ return $ default ;
198+ });
199+
200+ $ userConfig = new UserConfig ($ this ->configMock , $ this ->userSessionMock , $ this ->appConfigMock );
175201 $ configs = $ userConfig ->getConfigs ();
176202 $ this ->assertEquals ([
177203 'crop_image_previews ' => false ,
@@ -182,4 +208,36 @@ public function testGetsConfigsOverrideWithUserValuesSuccessfully(): void {
182208 'folder_tree ' => true ,
183209 ], $ configs );
184210 }
211+
212+ public function testGetsConfigsOverrideWithAppsValuesSuccessfully (): void {
213+ $ this ->userSessionMock ->method ('getUser ' )->willReturn ($ this ->userMock );
214+
215+ // set all user values to true
216+ $ this ->configMock ->method ('getUserValue ' )
217+ ->willReturnCallback (function () {
218+ return true ;
219+ });
220+
221+ // emulate override by the app config values
222+ $ this ->appConfigMock ->method ('getAppValueBool ' )
223+ ->willReturnCallback (function ($ key , $ default ) {
224+ if ($ key === 'crop_image_previews ' ) {
225+ return false ;
226+ } elseif ($ key === 'show_hidden ' ) {
227+ return false ;
228+ }
229+ return $ default ;
230+ });
231+
232+ $ userConfig = new UserConfig ($ this ->configMock , $ this ->userSessionMock , $ this ->appConfigMock );
233+ $ configs = $ userConfig ->getConfigs ();
234+ $ this ->assertEquals ([
235+ 'crop_image_previews ' => false ,
236+ 'show_hidden ' => false ,
237+ 'sort_favorites_first ' => true ,
238+ 'sort_folders_first ' => true ,
239+ 'grid_view ' => true ,
240+ 'folder_tree ' => true ,
241+ ], $ configs );
242+ }
185243}
0 commit comments