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,31 +106,31 @@ 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
115121 public function testThrowsInvalidArgumentExceptionForInvalidConfigValue (): void {
116122 $ this ->expectException (\InvalidArgumentException::class);
117123 $ this ->expectExceptionMessage ('Invalid config value ' );
118124
119- $ userConfig = new UserConfig ($ this ->configMock , $ this ->userSessionMock );
125+ $ userConfig = new UserConfig ($ this ->configMock , $ this ->userSessionMock , $ this -> appConfigMock );
120126 $ userConfig ->setConfig ('crop_image_previews ' , 'foo ' );
121127 }
122128
123129 public function testSetsConfigSuccessfully (): void {
124130 $ this ->configMock ->expects ($ this ->once ())
125131 ->method ('setUserValue ' )
126132 ->with ($ this ->userUID , Application::APP_ID , 'crop_image_previews ' , '1 ' );
127- $ userConfig = new UserConfig ($ this ->configMock , $ this ->userSessionMock );
133+ $ userConfig = new UserConfig ($ this ->configMock , $ this ->userSessionMock , $ this -> appConfigMock );
128134 $ userConfig ->setConfig ('crop_image_previews ' , true );
129135 }
130136
@@ -135,7 +141,13 @@ public function testGetsConfigsWithDefaultValuesSuccessfully(): void {
135141 return $ default ;
136142 });
137143
138- $ userConfig = new UserConfig ($ this ->configMock , $ this ->userSessionMock );
144+ // pass the default app settings unchanged
145+ $ this ->appConfigMock ->method ('getAppValueBool ' )
146+ ->willReturnCallback (function ($ key , $ default ) {
147+ return $ default ;
148+ });
149+
150+ $ userConfig = new UserConfig ($ this ->configMock , $ this ->userSessionMock , $ this ->appConfigMock );
139151 $ configs = $ userConfig ->getConfigs ();
140152 $ this ->assertEquals ([
141153 'crop_image_previews ' => true ,
@@ -162,7 +174,13 @@ public function testGetsConfigsOverrideWithUserValuesSuccessfully(): void {
162174 return $ default ;
163175 });
164176
165- $ userConfig = new UserConfig ($ this ->configMock , $ this ->userSessionMock );
177+ // pass the default app settings unchanged
178+ $ this ->appConfigMock ->method ('getAppValueBool ' )
179+ ->willReturnCallback (function ($ key , $ default ) {
180+ return $ default ;
181+ });
182+
183+ $ userConfig = new UserConfig ($ this ->configMock , $ this ->userSessionMock , $ this ->appConfigMock );
166184 $ configs = $ userConfig ->getConfigs ();
167185 $ this ->assertEquals ([
168186 'crop_image_previews ' => false ,
@@ -173,4 +191,36 @@ public function testGetsConfigsOverrideWithUserValuesSuccessfully(): void {
173191 'folder_tree ' => true ,
174192 ], $ configs );
175193 }
194+
195+ public function testGetsConfigsOverrideWithAppsValuesSuccessfully (): void {
196+ $ this ->userSessionMock ->method ('getUser ' )->willReturn ($ this ->userMock );
197+
198+ // set all user values to true
199+ $ this ->configMock ->method ('getUserValue ' )
200+ ->willReturnCallback (function () {
201+ return true ;
202+ });
203+
204+ // emulate override by the app config values
205+ $ this ->appConfigMock ->method ('getAppValueBool ' )
206+ ->willReturnCallback (function ($ key , $ default ) {
207+ if ($ key === 'crop_image_previews ' ) {
208+ return false ;
209+ } elseif ($ key === 'show_hidden ' ) {
210+ return false ;
211+ }
212+ return $ default ;
213+ });
214+
215+ $ userConfig = new UserConfig ($ this ->configMock , $ this ->userSessionMock , $ this ->appConfigMock );
216+ $ configs = $ userConfig ->getConfigs ();
217+ $ this ->assertEquals ([
218+ 'crop_image_previews ' => false ,
219+ 'show_hidden ' => false ,
220+ 'sort_favorites_first ' => true ,
221+ 'sort_folders_first ' => true ,
222+ 'grid_view ' => true ,
223+ 'folder_tree ' => true ,
224+ ], $ configs );
225+ }
176226}
0 commit comments