From 7cb9dccc7275a090e8df3bca96baec8395924ae9 Mon Sep 17 00:00:00 2001 From: RahatHameed Date: Thu, 4 Jul 2024 18:29:11 +0200 Subject: [PATCH] OXDEV-8215 Implemented default language case --- src/Shared/Service/LanguageService.php | 10 +++++++++- .../Unit/Shared/Core/LanguageWrapperTest.php | 3 --- .../Shared/Service/LanguageServiceTest.php | 20 +++++++++++++++---- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/Shared/Service/LanguageService.php b/src/Shared/Service/LanguageService.php index 405075b..d2efe19 100644 --- a/src/Shared/Service/LanguageService.php +++ b/src/Shared/Service/LanguageService.php @@ -23,6 +23,14 @@ public function filterByLanguageAbbreviation(array $data): ?string $langId = $this->language->getBaseLanguage(); $languageAbbr = $this->language->getLanguageAbbr(langId: $langId); - return $data[$languageAbbr] ?? null; + if (isset($data[$languageAbbr])) { + return $data[$languageAbbr]; + } + + if (isset($data['en'])) { + return $data['en']; + } + + return null; } } diff --git a/tests/Unit/Shared/Core/LanguageWrapperTest.php b/tests/Unit/Shared/Core/LanguageWrapperTest.php index 30fd0cb..83d558d 100644 --- a/tests/Unit/Shared/Core/LanguageWrapperTest.php +++ b/tests/Unit/Shared/Core/LanguageWrapperTest.php @@ -13,9 +13,6 @@ use PHPUnit\Framework\TestCase; use OxidEsales\Eshop\Core\Language; -/** - * @covers \OxidEsales\GraphQL\ConfigurationAccess\Shared\Core\LanguageWrapper; - */ class LanguageWrapperTest extends TestCase { public function testGetBaseLanguage(): void diff --git a/tests/Unit/Shared/Service/LanguageServiceTest.php b/tests/Unit/Shared/Service/LanguageServiceTest.php index ff0a589..014be34 100644 --- a/tests/Unit/Shared/Service/LanguageServiceTest.php +++ b/tests/Unit/Shared/Service/LanguageServiceTest.php @@ -13,9 +13,6 @@ use OxidEsales\GraphQL\ConfigurationAccess\Shared\Service\LanguageService; use PHPUnit\Framework\TestCase; -/** - * @covers \OxidEsales\GraphQL\ConfigurationAccess\Shared\Service\LanguageService; - */ class LanguageServiceTest extends TestCase { public function testFilterByLanguageAbbreviationCorrectLangValue() @@ -36,8 +33,9 @@ public function testFilterByLanguageAbbreviationCorrectLangValue() $this->assertSame($actualResult, $expectedResult); } - public function testFilterByLanguageAbbreviationWrongLangValue() + public function testFilterByLanguageAbbreviationDefaultedToEnglish() { + $expectedResult = "Title in en language"; $titlesData = [ 'de' => 'Title in de language', 'en' => 'Title in en language', @@ -50,6 +48,20 @@ public function testFilterByLanguageAbbreviationWrongLangValue() $languageService = new LanguageService($languageWrapperMock); $actualResult = $languageService->filterByLanguageAbbreviation($titlesData); + $this->assertSame($expectedResult, $actualResult); + } + + public function testFilterByLanguageAbbreviationReturnNull(): void + { + $titlesData = []; + + $languageWrapperMock = $this->createMock(LanguageWrapperInterface::class); + $languageWrapperMock->method('getBaseLanguage')->willReturn(1); + $languageWrapperMock->method('getLanguageAbbr')->with(1)->willReturn('de'); + + $languageService = new LanguageService($languageWrapperMock); + $actualResult = $languageService->filterByLanguageAbbreviation($titlesData); + $this->assertNull($actualResult); } }