From 9e89884587fc4f10086e8873d499b80b96b81816 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20W=C3=B3js?= Date: Mon, 30 Oct 2017 10:47:31 +0100 Subject: [PATCH] Fix EZP-28122: Hide embedded content, which was moved to trash, when rendering xmltext field type --- .../Tests/XmlText/Converter/EmbedToHtml5Test.php | 100 +++++++++++++++++++++ .../FieldType/XmlText/Converter/EmbedToHtml5.php | 10 +++ 2 files changed, 110 insertions(+) diff --git a/eZ/Publish/Core/FieldType/Tests/XmlText/Converter/EmbedToHtml5Test.php b/eZ/Publish/Core/FieldType/Tests/XmlText/Converter/EmbedToHtml5Test.php index 9869a4af5..1b21d1ab1 100644 --- a/eZ/Publish/Core/FieldType/Tests/XmlText/Converter/EmbedToHtml5Test.php +++ b/eZ/Publish/Core/FieldType/Tests/XmlText/Converter/EmbedToHtml5Test.php @@ -664,6 +664,15 @@ public function runNodeEmbedContent($xmlString, $contentId, $status, $view, $par $fragmentHandler = $this->getMockFragmentHandler(); $contentService = $this->getMockContentService(); + $mainLocationId = 2; + + $contentInfoMock = $this->getMock('eZ\\Publish\\API\\Repository\\Values\\Content\\ContentInfo'); + $contentInfoMock + ->expects($this->once()) + ->method('__get') + ->with('mainLocationId') + ->willReturn($mainLocationId); + $versionInfo = $this->getMock('eZ\\Publish\\API\\Repository\\Values\\Content\\VersionInfo'); $versionInfo->expects($this->any()) ->method('__get') @@ -675,6 +684,11 @@ public function runNodeEmbedContent($xmlString, $contentId, $status, $view, $par ->method('getVersionInfo') ->will($this->returnValue($versionInfo)); + $content + ->method('__get') + ->with('contentInfo') + ->willReturn($contentInfoMock); + $contentService->expects($this->once()) ->method('loadContent') ->with($this->equalTo($contentId)) @@ -837,6 +851,63 @@ public function providerForTestEmbedContentThrowsUnauthorizedException() ); } + public function dataProviderForTestEmbedContentTrashed() + { + return array( + array( + '
', + '
', + ), + ); + } + + public function testEmbedContentTrashed() + { + $fragmentHandler = $this->getMockFragmentHandler(); + $contentService = $this->getMockContentService(); + $repository = $this->getMockRepository($contentService, null); + $logger = $this->getLoggerMock(); + $xml = '
'; + $contentId = 42; + $status = APIVersionInfo::STATUS_PUBLISHED; + + $contentInfoMock = $this->getMock('eZ\\Publish\\API\\Repository\\Values\\Content\\ContentInfo'); + $contentInfoMock + ->expects($this->once()) + ->method('__get') + ->with('mainLocationId') + ->willReturn(null); + + $contentMock = $this->getMock('\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content'); + $contentMock + ->expects($this->once()) + ->method('__get') + ->with('contentInfo') + ->willReturn($contentInfoMock); + + $contentService->expects($this->once()) + ->method('loadContent') + ->with($this->equalTo(42)) + ->willReturn($contentMock); + + $logger + ->expects($this->once()) + ->method('error') + ->with("Could not render embedded resource: Content #{$contentId} is trashed."); + + $dom = new \DOMDocument(); + $dom->loadXML($xml); + + $converter = new EmbedToHtml5( + $fragmentHandler, + $repository, + ['view', 'class', 'node_id', 'object_id'], + $logger + ); + + $converter->convert($dom); + } + /** * @dataProvider providerForTestEmbedContentThrowsUnauthorizedException * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException @@ -855,11 +926,26 @@ public function testEmbedContentThrowsUnauthorizedException($permissionsMap) ->with('status') ->will($this->returnValue(APIVersionInfo::STATUS_DRAFT)); + $mainLocationId = 2; + + $contentInfoMock = $this->getMock('eZ\\Publish\\API\\Repository\\Values\\Content\\ContentInfo'); + $contentInfoMock + ->expects($this->once()) + ->method('__get') + ->with('mainLocationId') + ->willReturn($mainLocationId); + $content = $this->getMock('eZ\\Publish\\API\\Repository\\Values\\Content\\Content'); $content->expects($this->any()) ->method('getVersionInfo') ->will($this->returnValue($versionInfo)); + $content + ->expects($this->once()) + ->method('__get') + ->with('contentInfo') + ->willReturn($contentInfoMock); + $contentService->expects($this->once()) ->method('loadContent') ->with($this->equalTo(42)) @@ -1099,6 +1185,15 @@ public function testEmbedRemovesTextContent($input, $output, $contentReplacement $fragmentHandler = $this->getMockFragmentHandler(); $contentService = $this->getMockContentService(); + $mainLocationId = 2; + + $contentInfoMock = $this->getMock('eZ\\Publish\\API\\Repository\\Values\\Content\\ContentInfo'); + $contentInfoMock + ->expects($this->once()) + ->method('__get') + ->with('mainLocationId') + ->willReturn($mainLocationId); + $versionInfo = $this->getMock('eZ\\Publish\\API\\Repository\\Values\\Content\\VersionInfo'); $versionInfo->expects($this->any()) ->method('__get') @@ -1110,6 +1205,11 @@ public function testEmbedRemovesTextContent($input, $output, $contentReplacement ->method('getVersionInfo') ->will($this->returnValue($versionInfo)); + $content + ->method('__get') + ->with('contentInfo') + ->willReturn($contentInfoMock); + $contentService->expects($this->once()) ->method('loadContent') ->with($this->equalTo($contentId)) diff --git a/eZ/Publish/Core/FieldType/XmlText/Converter/EmbedToHtml5.php b/eZ/Publish/Core/FieldType/XmlText/Converter/EmbedToHtml5.php index d2fa92210..b3e450dbd 100644 --- a/eZ/Publish/Core/FieldType/XmlText/Converter/EmbedToHtml5.php +++ b/eZ/Publish/Core/FieldType/XmlText/Converter/EmbedToHtml5.php @@ -103,6 +103,16 @@ function (Repository $repository) use ($contentId) { } ); + if ( !$content->contentInfo->mainLocationId ) { + if ( $this->logger ) { + $this->logger->error( + "Could not render embedded resource: Content #{$contentId} is trashed." + ); + } + + return null; + } + if ( !$this->repository->canUser('content', 'read', $content) && !$this->repository->canUser('content', 'view_embed', $content)