diff --git a/eZ/Publish/API/Repository/Tests/PHPUnitConstraint/ContentItemEquals.php b/eZ/Publish/API/Repository/Tests/PHPUnitConstraint/ContentItemEquals.php index c3284a0ec2..e4981c81a3 100644 --- a/eZ/Publish/API/Repository/Tests/PHPUnitConstraint/ContentItemEquals.php +++ b/eZ/Publish/API/Repository/Tests/PHPUnitConstraint/ContentItemEquals.php @@ -56,6 +56,11 @@ public function evaluate($content, string $description = '', bool $returnResult $this->expectedContent->getThumbnail(), $content->getThumbnail() ); + $this->compareStrings( + $comparatorFactory, + $this->expectedContent->getDefaultLanguageCode(), + $content->getDefaultLanguageCode() + ); $this->compareArrays( $comparatorFactory, $this->expectedContent->fields, @@ -133,4 +138,20 @@ private function compareArrays( $actual, ); } + + private function compareStrings( + ComparatorFactory $comparatorFactory, + ?string $expected, + ?string $actual + ): void { + $comparator = $comparatorFactory->getComparatorFor( + $expected, + $actual + ); + + $comparator->assertEquals( + $expected, + $actual, + ); + } } diff --git a/eZ/Publish/API/Repository/Values/Content/Content.php b/eZ/Publish/API/Repository/Values/Content/Content.php index a8516a6a5d..407bce09c1 100644 --- a/eZ/Publish/API/Repository/Values/Content/Content.php +++ b/eZ/Publish/API/Repository/Values/Content/Content.php @@ -108,4 +108,6 @@ abstract public function getField(string $fieldDefIdentifier, ?string $languageC abstract public function getContentType(): ContentType; abstract public function getThumbnail(): ?Thumbnail; + + abstract public function getDefaultLanguageCode(): string; } diff --git a/eZ/Publish/Core/Repository/Values/Content/Content.php b/eZ/Publish/Core/Repository/Values/Content/Content.php index 6ed2cf1f88..734355f3b9 100644 --- a/eZ/Publish/Core/Repository/Values/Content/Content.php +++ b/eZ/Publish/Core/Repository/Values/Content/Content.php @@ -92,7 +92,7 @@ public function getContentType(): ContentType public function getFieldValue(string $fieldDefIdentifier, ?string $languageCode = null): ?Value { if (null === $languageCode) { - $languageCode = $this->prioritizedFieldLanguageCode ?: $this->versionInfo->contentInfo->mainLanguageCode; + $languageCode = $this->getDefaultLanguageCode(); } if (isset($this->fields[$fieldDefIdentifier][$languageCode])) { @@ -118,7 +118,7 @@ public function getFieldsByLanguage(?string $languageCode = null): iterable $fields = []; if (null === $languageCode) { - $languageCode = $this->prioritizedFieldLanguageCode ?: $this->versionInfo->contentInfo->mainLanguageCode; + $languageCode = $this->getDefaultLanguageCode(); } foreach ($this->getFields() as $field) { @@ -137,7 +137,7 @@ public function getFieldsByLanguage(?string $languageCode = null): iterable public function getField(string $fieldDefIdentifier, ?string $languageCode = null): ?Field { if (null === $languageCode) { - $languageCode = $this->prioritizedFieldLanguageCode ?: $this->versionInfo->contentInfo->mainLanguageCode; + $languageCode = $this->getDefaultLanguageCode(); } foreach ($this->getFields() as $field) { @@ -150,6 +150,11 @@ public function getField(string $fieldDefIdentifier, ?string $languageCode = nul return null; } + public function getDefaultLanguageCode(): string + { + return $this->prioritizedFieldLanguageCode ?: $this->versionInfo->contentInfo->mainLanguageCode; + } + /** * {@inheritdoc} */ diff --git a/eZ/Publish/Core/Repository/Values/User/User.php b/eZ/Publish/Core/Repository/Values/User/User.php index ff405b32a8..1c027f4a8a 100644 --- a/eZ/Publish/Core/Repository/Values/User/User.php +++ b/eZ/Publish/Core/Repository/Values/User/User.php @@ -188,4 +188,9 @@ public function getThumbnail(): ?Thumbnail { return $this->content->getThumbnail(); } + + public function getDefaultLanguageCode(): string + { + return $this->content->getDefaultLanguageCode(); + } } diff --git a/eZ/Publish/Core/Repository/Values/User/UserGroup.php b/eZ/Publish/Core/Repository/Values/User/UserGroup.php index 5efb8c2537..067d6edc3e 100644 --- a/eZ/Publish/Core/Repository/Values/User/UserGroup.php +++ b/eZ/Publish/Core/Repository/Values/User/UserGroup.php @@ -187,4 +187,9 @@ public function getThumbnail(): ?Thumbnail { return $this->content->getThumbnail(); } + + public function getDefaultLanguageCode(): string + { + return $this->content->getDefaultLanguageCode(); + } } diff --git a/tests/integration/Core/Repository/Values/Content/DefaultLanguageCodeForContentTest.php b/tests/integration/Core/Repository/Values/Content/DefaultLanguageCodeForContentTest.php new file mode 100644 index 0000000000..a614437f2f --- /dev/null +++ b/tests/integration/Core/Repository/Values/Content/DefaultLanguageCodeForContentTest.php @@ -0,0 +1,62 @@ + 'Test GB', + 'ger-DE' => 'Test DE', + 'eng-US' => 'Test US', + ]; + $testFolder = $this->createFolder( + $names, + 2, + null, + false + ); + + self::assertEquals('eng-GB', $testFolder->getDefaultLanguageCode()); + } + + /** + * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException + * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException + * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException + */ + public function testDefaultLanguageCodeForCreatedContentWithPrioritizedLanguage(): void + { + $names = [ + 'eng-GB' => 'Test GB', + 'ger-DE' => 'Test DE', + 'eng-US' => 'Test US', + ]; + + $testFolder = $this->createFolder( + $names, + 2, + null, + false + ); + + $repository = $this->getRepository(); + $testFolderInGerman = $repository->getContentService()->loadContent($testFolder->id, ['ger-DE']); + + self::assertEquals('ger-DE', $testFolderInGerman->getDefaultLanguageCode()); + } +}