Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EZP-31125: Added TranslatedName to Content/ContentInfo in REST response #2885

Merged
merged 3 commits into from
Dec 12, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -311,12 +311,14 @@ services:
ezpublish_rest.output.value_object_visitor.RestContent:
parent: ezpublish_rest.output.value_object_visitor.base
class: "%ezpublish_rest.output.value_object_visitor.RestContent.class%"
arguments: ['@ezpublish.translation_helper']
tags:
- { name: ezpublish_rest.output.value_object_visitor, type: eZ\Publish\Core\REST\Server\Values\RestContent }

ezpublish_rest.output.value_object_visitor.CreatedContent:
parent: ezpublish_rest.output.value_object_visitor.base
class: "%ezpublish_rest.output.value_object_visitor.CreatedContent.class%"
arguments: ['@ezpublish.translation_helper']
tags:
- { name: ezpublish_rest.output.value_object_visitor, type: eZ\Publish\Core\REST\Server\Values\CreatedContent }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use eZ\Publish\API\Repository\Values\Content\ContentInfo;
use eZ\Publish\Core\Base\Exceptions\BadStateException as CoreBadStateException;
use eZ\Publish\Core\Helper\TranslationHelper;
use eZ\Publish\Core\REST\Common\Output\ValueObjectVisitor;
use eZ\Publish\Core\REST\Common\Output\Generator;
use eZ\Publish\Core\REST\Common\Output\Visitor;
Expand All @@ -20,6 +21,14 @@
*/
class RestContent extends ValueObjectVisitor
{
/** @var \eZ\Publish\Core\Helper\TranslationHelper */
private $translationHelper;

public function __construct(TranslationHelper $translationHelper)
{
$this->translationHelper = $translationHelper;
}

/**
* Visit struct returned by controllers.
*
Expand All @@ -31,6 +40,7 @@ public function visit(Visitor $visitor, Generator $generator, $data)
{
$restContent = $data;
$contentInfo = $restContent->contentInfo;
$translatedContentName = $this->translationHelper->getTranslatedContentNameByContentInfo($contentInfo);
$contentType = $restContent->contentType;
$mainLocation = $restContent->mainLocation;
$currentVersion = $restContent->currentVersion;
Expand Down Expand Up @@ -69,6 +79,9 @@ public function visit(Visitor $visitor, Generator $generator, $data)
$generator->startValueElement('Name', $contentInfo->name);
$generator->endValueElement('Name');

$generator->startValueElement('TranslatedName', $translatedContentName);
$generator->endValueElement('TranslatedName');

$generator->startObjectElement('Versions', 'VersionList');
$generator->startAttribute(
'href',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace eZ\Publish\Core\REST\Server\Tests\Output\ValueObjectVisitor;

use eZ\Publish\API\Repository\Values\ContentType\ContentType;
use eZ\Publish\Core\Helper\TranslationHelper;
use eZ\Publish\Core\REST\Common\Tests\Output\ValueObjectVisitorBaseTest;
use eZ\Publish\Core\REST\Server\Values\RestContent;
use eZ\Publish\Core\REST\Server\Output\ValueObjectVisitor;
Expand All @@ -18,6 +19,19 @@

class RestContentTest extends ValueObjectVisitorBaseTest
{
/** @var \eZ\Publish\Core\Helper\TranslationHelper|\PHPUnit\Framework\MockObject\MockObject */
private $translationHelper;

protected function setUp(): void
{
$this->translationHelper = $this->createMock(TranslationHelper::class);
$this->translationHelper
->method('getTranslatedContentNameByContentInfo')
->willReturnCallback(function (ContentInfo $content) {
return $content->name . ' (Translated)';
});
}

/**
* @return \DOMDocument
*/
Expand Down Expand Up @@ -196,6 +210,16 @@ public function testNameCorrect(\DOMDocument $dom)
$this->assertXPath($dom, '/Content/Name[text()="Sindelfingen"]');
}

/**
* @param \DOMDocument $dom
*
* @depends testVisitWithoutEmbeddedVersion
*/
public function testTranslatedNameCorrect(\DOMDocument $dom)
{
$this->assertXPath($dom, '/Content/TranslatedName[text()="Sindelfingen (Translated)"]');
}

/**
* @param \DOMDocument $dom
*
Expand Down Expand Up @@ -497,6 +521,8 @@ public function testEmbeddedCurrentVersionMediaTypeCorrect(\DOMDocument $dom)
*/
protected function internalGetVisitor()
{
return new ValueObjectVisitor\RestContent();
return new ValueObjectVisitor\RestContent(
$this->translationHelper
);
}
}