Skip to content

Commit

Permalink
IBX-1784: Added getDefaultLanguageCode method to API/Repository/Value…
Browse files Browse the repository at this point in the history
…s/Content/Content (#279)

* Added getDefaultLanguageCode method to API/Repository/Values/Content/Content

* Added test cases for Content's DefaultLanguageCode

* Changes after CR

* Moved DefaultLanguageCodeForContentTest to Integration scope

* Removed duplicated suite
  • Loading branch information
mateuszbieniek authored Jan 24, 2022
1 parent 07bd222 commit 94e65e0
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
);
}
}
2 changes: 2 additions & 0 deletions eZ/Publish/API/Repository/Values/Content/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
11 changes: 8 additions & 3 deletions eZ/Publish/Core/Repository/Values/Content/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -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])) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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}
*/
Expand Down
5 changes: 5 additions & 0 deletions eZ/Publish/Core/Repository/Values/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,9 @@ public function getThumbnail(): ?Thumbnail
{
return $this->content->getThumbnail();
}

public function getDefaultLanguageCode(): string
{
return $this->content->getDefaultLanguageCode();
}
}
5 changes: 5 additions & 0 deletions eZ/Publish/Core/Repository/Values/User/UserGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,9 @@ public function getThumbnail(): ?Thumbnail
{
return $this->content->getThumbnail();
}

public function getDefaultLanguageCode(): string
{
return $this->content->getDefaultLanguageCode();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\Integration\Core\Repository\Values\Content;

use eZ\Publish\API\Repository\Tests\BaseTest;

final class DefaultLanguageCodeForContentTest extends BaseTest
{
/**
* @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
*/
public function testDefaultLanguageCodeForCreatedContentWithoutPrioritizedLanguage(): void
{
$names = [
'eng-GB' => '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());
}
}

0 comments on commit 94e65e0

Please sign in to comment.