Skip to content

Commit

Permalink
OXDEV-8215: Adjust languageService to get the correct current transla…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
MarcelOxid committed Aug 6, 2024
1 parent c164a1a commit c8f9ab9
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 68 deletions.
12 changes: 3 additions & 9 deletions src/Shared/Infrastructure/LanguageWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,9 @@ public function __construct(
) {
}

public function getBaseLanguage(): int
public function getCurrentLanguageAbbr(): string
{
/** @var int|null $langId */
$langId = $this->language->getBaseLanguage();
return (int)$langId;
}

public function getLanguageAbbr(?int $langId = null): string
{
return $this->language->getLanguageAbbr(iLanguage: $langId);
$this->language->getLanguageAbbr();
return $this->language->getLanguageAbbr();
}
}
3 changes: 1 addition & 2 deletions src/Shared/Infrastructure/LanguageWrapperInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@

interface LanguageWrapperInterface
{
public function getBaseLanguage(): int;
public function getLanguageAbbr(?int $langId = null): string;
public function getCurrentLanguageAbbr(): string;
}
14 changes: 7 additions & 7 deletions src/Shared/Service/LanguageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@
class LanguageService implements LanguageServiceInterface
{
public function __construct(
private LanguageWrapperInterface $languageWrapper
private LanguageWrapperInterface $language
) {
}

public function filterByLanguageAbbreviation(array $data): ?string
public function filterByLanguageAbbreviation(array $data, string $defaultLang): string
{
$langId = $this->languageWrapper->getBaseLanguage();
$languageAbbr = $this->languageWrapper->getLanguageAbbr(langId: $langId);
$languageAbbr = $this->language->getCurrentLanguageAbbr();

if (isset($data[$languageAbbr])) {
return $data[$languageAbbr];
}

if (isset($data['en'])) {
return $data['en'];
if (isset($data[$defaultLang])) {
return $data[$defaultLang];
}

return null;
$dataReversed = array_reverse($data);
return array_pop($dataReversed);
}
}
2 changes: 1 addition & 1 deletion src/Shared/Service/LanguageServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@

interface LanguageServiceInterface
{
public function filterByLanguageAbbreviation(array $data): ?string;
public function filterByLanguageAbbreviation(array $data, string $defaultLang): string;
}
32 changes: 18 additions & 14 deletions tests/Unit/Module/DataType/ModuleDataTypeFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

namespace OxidEsales\GraphQL\ConfigurationAccess\Tests\Unit\Module\DataType;

use OxidEsales\Eshop\Core\Module\Module;
use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\DataObject\ModuleConfiguration;
use OxidEsales\GraphQL\ConfigurationAccess\Module\DataType\ModuleDataTypeFactory;
use OxidEsales\GraphQL\ConfigurationAccess\Shared\Service\LanguageService;
Expand All @@ -36,24 +35,29 @@ public function testCreateFromCoreModule()
$expectedAuthor = uniqid();
$expectedUrl = uniqid();
$expectedEmail = uniqid();
$expectedLang = uniqid();
$expectedIsActivated = (bool)random_int(0, 1);

$moduleConfigMock = $this->createMock(ModuleConfiguration::class);
$moduleConfigMock->method('getId')->willReturn($expectedId);
$moduleConfigMock->method('getVersion')->willReturn($expectedVersion);
$moduleConfigMock->method('getTitle')->willReturn($titlesData);
$moduleConfigMock->method('getDescription')->willReturn($descriptionData);
$moduleConfigMock->method('getThumbnail')->willReturn($expectedThumbnail);
$moduleConfigMock->method('getAuthor')->willReturn($expectedAuthor);
$moduleConfigMock->method('getUrl')->willReturn($expectedUrl);
$moduleConfigMock->method('getEmail')->willReturn($expectedEmail);
$moduleConfigMock->method('isActivated')->willReturn((bool)random_int(0, 1));
$moduleConfigMock = $this->createConfiguredStub(ModuleConfiguration::class, [
'getId' => $expectedId,
'getVersion' => $expectedVersion,
'getTitle' => $titlesData,
'getDescription' => $descriptionData,
'getThumbnail' => $expectedThumbnail,
'getAuthor' => $expectedAuthor,
'getUrl' => $expectedUrl,
'getEmail' => $expectedEmail,
'isActivated' => $expectedIsActivated,
'getLang' => $expectedLang
]);

$languageServiceMock = $this->createMock(LanguageService::class);
$languageServiceMock
->expects($this->exactly(2))
->method('filterByLanguageAbbreviation')
->willReturnMap([
[$titlesData, $titlesData['de']],
[$descriptionData, $descriptionData['de']]
[$titlesData, $expectedLang, $titlesData['de']],
[$descriptionData, $expectedLang, $descriptionData['de']]
]);

$moduleDataTypeFactory = new ModuleDataTypeFactory($languageServiceMock);
Expand All @@ -67,6 +71,6 @@ public function testCreateFromCoreModule()
$this->assertSame($expectedAuthor, $moduleDataType->getAuthor());
$this->assertSame($expectedUrl, $moduleDataType->getUrl());
$this->assertSame($expectedEmail, $moduleDataType->getEmail());
$this->assertIsBool($moduleDataType->isActive());
$this->assertSame($expectedIsActivated, $moduleDataType->isActive());
}
}
26 changes: 7 additions & 19 deletions tests/Unit/Shared/Infrastructure/LanguageWrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,16 @@
*/
class LanguageWrapperTest extends TestCase
{
public function testGetBaseLanguage(): void
public function testGetCurrentLanguageAbbr(): void
{
$expectedLangId = 1;
$languageMock = $this->createPartialMock(Language::class, ['getBaseLanguage']);
$languageMock->method('getBaseLanguage')->willReturn($expectedLangId);
$langAbbr = uniqid();
$languageMock = $this->createConfiguredStub(Language::class, [
'getLanguageAbbr' => $langAbbr,
]);

$languageWrapper = new LanguageWrapper($languageMock);
$actualLangId = $languageWrapper->getBaseLanguage();
$actualLangAbbr = $languageWrapper->getCurrentLanguageAbbr();

$this->assertSame($expectedLangId, $actualLangId);
}

public function testGetLanguageAbbr(): void
{
$langId = 1;
$expectedLangAbbr = uniqid();
$languageMock = $this->createPartialMock(Language::class, ['getLanguageAbbr']);
$languageMock->method('getLanguageAbbr')->willReturn($expectedLangAbbr);

$languageWrapper = new LanguageWrapper($languageMock);
$actualLangAbbr = $languageWrapper->getLanguageAbbr($langId);

$this->assertSame($expectedLangAbbr, $actualLangAbbr);
$this->assertSame($langAbbr, $actualLangAbbr);
}
}
34 changes: 18 additions & 16 deletions tests/Unit/Shared/Service/LanguageServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,51 +20,53 @@ class LanguageServiceTest extends TestCase
{
public function testFilterByLanguageAbbreviationCorrectLangValue()
{
$expectedResult = "Title in de language";
$titlesData = [
'de' => 'Title in de language',
'en' => 'Title in en language',
];
$expectedExistingLanguageAbbreviation = 'de';

$languageWrapperMock = $this->createMock(LanguageWrapperInterface::class);
$languageWrapperMock->method('getBaseLanguage')->willReturn(1);
$languageWrapperMock->method('getLanguageAbbr')->with(1)->willReturn('de');
$languageWrapperMock->method('getCurrentLanguageAbbr')
->willReturn($expectedExistingLanguageAbbreviation);

$languageService = new LanguageService($languageWrapperMock);
$actualResult = $languageService->filterByLanguageAbbreviation($titlesData);
$actualResult = $languageService->filterByLanguageAbbreviation($titlesData, 'en');

$this->assertSame($actualResult, $expectedResult);
$this->assertSame($titlesData[$expectedExistingLanguageAbbreviation], $actualResult);
}

public function testFilterByLanguageAbbreviationDefaultedToEnglish()
{
$expectedResult = "Title in en language";
$titlesData = [
'de' => 'Title in de language',
'en' => 'Title in en language',
];
$defaultLangAbbr = 'en';

$languageWrapperMock = $this->createMock(LanguageWrapperInterface::class);
$languageWrapperMock->method('getBaseLanguage')->willReturn(2);
$languageWrapperMock->method('getLanguageAbbr')->with(2)->willReturn('fr');
$languageWrapperMock->method('getCurrentLanguageAbbr')->willReturn('fr');

$languageService = new LanguageService($languageWrapperMock);
$actualResult = $languageService->filterByLanguageAbbreviation($titlesData);
$actualResult = $languageService->filterByLanguageAbbreviation($titlesData, $defaultLangAbbr);

$this->assertSame($expectedResult, $actualResult);
$this->assertSame($titlesData[$defaultLangAbbr], $actualResult);
}

public function testFilterByLanguageAbbreviationReturnNull(): void
public function testFilterByLanguageAbbreviationDefaultedToFirstInArray()
{
$titlesData = [];
$titlesData = [
'de' => 'Title in de language',
'en' => 'Title in en language',
];

$languageWrapperMock = $this->createMock(LanguageWrapperInterface::class);
$languageWrapperMock->method('getBaseLanguage')->willReturn(1);
$languageWrapperMock->method('getLanguageAbbr')->with(1)->willReturn('de');
$languageWrapperMock->expects($this->once())
->method('getCurrentLanguageAbbr')->willReturn('pl');

$languageService = new LanguageService($languageWrapperMock);
$actualResult = $languageService->filterByLanguageAbbreviation($titlesData);
$actualResult = $languageService->filterByLanguageAbbreviation($titlesData, 'fr');

$this->assertNull($actualResult);
$this->assertSame($titlesData['de'], $actualResult);
}
}

0 comments on commit c8f9ab9

Please sign in to comment.