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 all 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
5 changes: 5 additions & 0 deletions doc/specifications/rest/REST-API-V2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ XML Example
<Content media-type="application/vnd.ez.api.ContentInfo+xml" href="/api/ezp/v2/content/objects/1" remoteId="9459d3c29e15006e45197295722c7ade" id="1">
<ContentType media-type="application/vnd.ez.api.ContentType+xml" href="/api/ezp/v2/content/types/1"/>
<Name>eZ Platform</Name>
<TranslatedName>eZ Platform</TranslatedName>
<Versions media-type="application/vnd.ez.api.VersionList+xml" href="/api/ezp/v2/content/objects/1/versions"/>
<CurrentVersion media-type="application/vnd.ez.api.Version+xml" href="/api/ezp/v2/content/objects/1/currentversion"/>
<Section media-type="application/vnd.ez.api.Section+xml" href="/api/ezp/v2/content/sections/1"/>
Expand Down Expand Up @@ -372,6 +373,7 @@ JSON Example
"_href": "/api/ezp/v2/content/types/1"
},
"Name": "eZ Platform",
"TranslatedName": "eZ Platform",
"Versions": {
"_media-type": "application/vnd.ez.api.VersionList+json",
"_href": "/api/ezp/v2/content/objects/1/versions"
Expand Down Expand Up @@ -825,6 +827,7 @@ XML Example
media-type="application/vnd.ez.api.Content+xml" remoteId="remoteId12345678" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ContentType href="/content/types/10" media-type="application/vnd.ez.api.ContentType+xml" />
<Name>This is a title</Name>
<TranslatedName>This is a title</TranslatedName>
<Versions href="/content/objects/23/versions" media-type="application/vnd.ez.api.VersionList+xml" />
<CurrentVersion href="/content/objects/23/currentversion"
media-type="application/vnd.ez.api.Version+xml">
Expand Down Expand Up @@ -1129,6 +1132,7 @@ XML Example
media-type="application/vnd.ez.api.Content+xml" remoteId="qwert123">
<ContentType href="/content/types/10" media-type="application/vnd.ez.api.ContentType+xml" />
<Name>This is a title</Name>
<TranslatedName>This is a title</TranslatedName>
<Versions href="/content/objects/23/versions" media-type="application/vnd.ez.api.VersionList+xml" />
<CurrentVersion href="/content/objects/23/currentversion"
media-type="application/vnd.ez.api.Version+xml"/>
Expand Down Expand Up @@ -1226,6 +1230,7 @@ In this example
media-type="application/vnd.ez.api.Content+xml" remoteId="qwert4321">
<ContentType href="/content/types/10" media-type="application/vnd.ez.api.ContentType+xml" />
<Name>This is a title</Name>
<TranslatedName>This is a title</TranslatedName>
<Versions href="/content/objects/23/versions" media-type="application/vnd.ez.api.VersionList+xml" />
<CurrentVersion href="/content/objects/23/currentversion"
media-type="application/vnd.ez.api.Version+xml"/>
Expand Down
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
);
}
}