99
1010namespace OxidEsales \SecurityModule \Tests \Unit \Authentication \OAuth2 \Service ;
1111
12+ use OxidEsales \Eshop \Core \Config ;
1213use OxidEsales \EshopCommunity \Internal \Framework \Module \Facade \ModuleSettingService ;
1314use OxidEsales \SecurityModule \Authentication \OAuth2 \Service \ModuleSettingsService ;
1415use OxidEsales \SecurityModule \Core \Module ;
@@ -25,13 +26,11 @@ public static function gettersDataProvider(): array
2526 self ::prepareBooleanSetting ('isFacebookLoginEnabled ' , ModuleSettingsService::FACEBOOK_LOGIN_ENABLED , false ),
2627 self ::prepareStringTestItem ('getFacebookClientId ' , ModuleSettingsService::FACEBOOK_CLIENT_ID ),
2728 self ::prepareStringTestItem ('getFacebookClientSecret ' , ModuleSettingsService::FACEBOOK_CLIENT_SECRET ),
28- self ::prepareStringTestItem ('getFacebookRedirectUrl ' , ModuleSettingsService::FACEBOOK_REDIRECT_URL ),
2929
3030 self ::prepareBooleanSetting ('isGoogleLoginEnabled ' , ModuleSettingsService::GOOGLE_LOGIN_ENABLED , true ),
3131 self ::prepareBooleanSetting ('isGoogleLoginEnabled ' , ModuleSettingsService::GOOGLE_LOGIN_ENABLED , false ),
3232 self ::prepareStringTestItem ('getGoogleClientId ' , ModuleSettingsService::GOOGLE_CLIENT_ID ),
3333 self ::prepareStringTestItem ('getGoogleClientSecret ' , ModuleSettingsService::GOOGLE_CLIENT_SECRET ),
34- self ::prepareStringTestItem ('getGoogleRedirectUrl ' , ModuleSettingsService::GOOGLE_REDIRECT_URL ),
3534 ];
3635 }
3736
@@ -44,7 +43,9 @@ public function testGetters($method, $systemMethod, $key, $mockValue, $expectedV
4443 Module::MODULE_ID
4544 )->willReturn ($ mockValue );
4645
47- $ sut = new ModuleSettingsService ($ mssMock );
46+ $ configMock = $ this ->createMock (Config::class);
47+
48+ $ sut = new ModuleSettingsService ($ mssMock , $ configMock );
4849 $ this ->assertSame ($ expectedValue , $ sut ->$ method ());
4950 }
5051
@@ -94,7 +95,72 @@ public function testSaveBooleanSettings(string $method, string $key, bool $value
9495 ->method ('saveBoolean ' )
9596 ->with ($ key , $ value , Module::MODULE_ID );
9697
97- $ sut = new ModuleSettingsService ($ mssMock );
98+ $ configMock = $ this ->createMock (Config::class);
99+
100+ $ sut = new ModuleSettingsService ($ mssMock , $ configMock );
98101 $ sut ->$ method ($ value );
99102 }
103+
104+ public static function redirectUrlDataProvider (): array
105+ {
106+ return [
107+ 'facebook ' => [
108+ 'method ' => 'getFacebookRedirectUrl ' ,
109+ 'settingKey ' => ModuleSettingsService::FACEBOOK_REDIRECT_URL ,
110+ 'provider ' => 'facebook ' ,
111+ ],
112+ 'google ' => [
113+ 'method ' => 'getGoogleRedirectUrl ' ,
114+ 'settingKey ' => ModuleSettingsService::GOOGLE_REDIRECT_URL ,
115+ 'provider ' => 'google ' ,
116+ ],
117+ ];
118+ }
119+
120+ #[DataProvider('redirectUrlDataProvider ' )]
121+ public function testRedirectUrlIsGeneratedFromConfig (
122+ string $ method ,
123+ string $ settingKey ,
124+ string $ provider
125+ ): void {
126+ $ shopUrl = 'https://myshop.com/ ' ;
127+ $ expectedUrl = $ shopUrl . 'index.php?cl=oauth&fnc=redirect&provider= ' . $ provider ;
128+
129+ $ configMock = $ this ->createMock (Config::class);
130+ $ configMock ->method ('getShopUrl ' )->willReturn ($ shopUrl );
131+
132+ $ mssMock = $ this ->createPartialMock (ModuleSettingService::class, ['getString ' , 'saveString ' ]);
133+ $ mssMock ->method ('getString ' )
134+ ->with ($ settingKey , Module::MODULE_ID )
135+ ->willReturn (new UnicodeString ('old-value ' ));
136+ $ mssMock ->expects ($ this ->once ())
137+ ->method ('saveString ' )
138+ ->with ($ settingKey , $ expectedUrl , Module::MODULE_ID );
139+
140+ $ sut = new ModuleSettingsService ($ mssMock , $ configMock );
141+ $ this ->assertSame ($ expectedUrl , $ sut ->$ method ());
142+ }
143+
144+ #[DataProvider('redirectUrlDataProvider ' )]
145+ public function testRedirectUrlSkipsSaveWhenUnchanged (
146+ string $ method ,
147+ string $ settingKey ,
148+ string $ provider
149+ ): void {
150+ $ shopUrl = 'https://myshop.com/ ' ;
151+ $ expectedUrl = $ shopUrl . 'index.php?cl=oauth&fnc=redirect&provider= ' . $ provider ;
152+
153+ $ configMock = $ this ->createMock (Config::class);
154+ $ configMock ->method ('getShopUrl ' )->willReturn ($ shopUrl );
155+
156+ $ mssMock = $ this ->createPartialMock (ModuleSettingService::class, ['getString ' , 'saveString ' ]);
157+ $ mssMock ->method ('getString ' )
158+ ->with ($ settingKey , Module::MODULE_ID )
159+ ->willReturn (new UnicodeString ($ expectedUrl ));
160+ $ mssMock ->expects ($ this ->never ())
161+ ->method ('saveString ' );
162+
163+ $ sut = new ModuleSettingsService ($ mssMock , $ configMock );
164+ $ this ->assertSame ($ expectedUrl , $ sut ->$ method ());
165+ }
100166}
0 commit comments