From 6780ded20a86f4c1e952beaf78d18b213b17bacb Mon Sep 17 00:00:00 2001 From: tomaszmadeyski Date: Mon, 19 Jun 2017 15:22:43 +0200 Subject: [PATCH] EZP-27396 Removing xml node text content in order to properly embed object (#157) --- .../Tests/XmlText/Converter/EmbedToHtml5Test.php | 99 ++++++++++++++++++++++ .../FieldType/XmlText/Converter/EmbedToHtml5.php | 3 + 2 files changed, 102 insertions(+) diff --git a/eZ/Publish/Core/FieldType/Tests/XmlText/Converter/EmbedToHtml5Test.php b/eZ/Publish/Core/FieldType/Tests/XmlText/Converter/EmbedToHtml5Test.php index 831b69afa..9869a4af5 100644 --- a/eZ/Publish/Core/FieldType/Tests/XmlText/Converter/EmbedToHtml5Test.php +++ b/eZ/Publish/Core/FieldType/Tests/XmlText/Converter/EmbedToHtml5Test.php @@ -1076,4 +1076,103 @@ public function testEmbedLocationNotFound($input, $output) $this->assertEquals($outputDocument, $document); } + + /** + * @param string $input + * @param string $output + * @param string $contentReplacement + * @param int $contentId + * + * @dataProvider providerEmbedRemovesTextContent + */ + public function testEmbedRemovesTextContent($input, $output, $contentReplacement, $contentId) + { + $status = APIVersionInfo::STATUS_DRAFT; + $permissionsMap = array( + array('content', 'read', true), + array('content', 'versionread', true), + ); + + $dom = new \DOMDocument(); + $dom->loadXML($input); + + $fragmentHandler = $this->getMockFragmentHandler(); + $contentService = $this->getMockContentService(); + + $versionInfo = $this->getMock('eZ\\Publish\\API\\Repository\\Values\\Content\\VersionInfo'); + $versionInfo->expects($this->any()) + ->method('__get') + ->with('status') + ->will($this->returnValue($status)); + + $content = $this->getMock('eZ\\Publish\\API\\Repository\\Values\\Content\\Content'); + $content->expects($this->any()) + ->method('getVersionInfo') + ->will($this->returnValue($versionInfo)); + + $contentService->expects($this->once()) + ->method('loadContent') + ->with($this->equalTo($contentId)) + ->will($this->returnValue($content)); + + $repository = $this->getMockRepository($contentService, null); + foreach ($permissionsMap as $index => $permissions) { + $repository->expects($this->at($index + 1)) + ->method('canUser') + ->with( + $permissions[0], + $permissions[1], + $content, + null + ) + ->will( + $this->returnValue($permissions[2]) + ); + } + + $fragmentHandler->expects($this->once()) + ->method('render') + ->will($this->returnValue($contentReplacement)); + + $converter = new EmbedToHtml5( + $fragmentHandler, + $repository, + array('view', 'class', 'node_id', 'object_id'), + $this->getMock('Psr\\Log\\LoggerInterface') + ); + + $converter->convert($dom); + + $outputDocument = new DOMDocument(); + $outputDocument->loadXML($output); + + $this->assertEquals($outputDocument, $dom); + + } + + public function providerEmbedRemovesTextContent() + { + $xmlFramework = ' +
+eZ Publish + +%s + +
'; + + return array( + array( + sprintf($xmlFramework, 'content to be removed'), + sprintf($xmlFramework, 'ContentReplacement'), + 'ContentReplacement', + 123 + ), + array( + sprintf($xmlFramework, 'Content to be removed'), + sprintf($xmlFramework, 'Other random content'), + 'Other random content', + 789 + ), + ); + } } diff --git a/eZ/Publish/Core/FieldType/XmlText/Converter/EmbedToHtml5.php b/eZ/Publish/Core/FieldType/XmlText/Converter/EmbedToHtml5.php index 163c5f8de..d2fa92210 100644 --- a/eZ/Publish/Core/FieldType/XmlText/Converter/EmbedToHtml5.php +++ b/eZ/Publish/Core/FieldType/XmlText/Converter/EmbedToHtml5.php @@ -178,6 +178,9 @@ function (Repository $repository) use ($locationId) { // Remove empty embed $embed->parentNode->removeChild($embed); } else { + while ($embed->hasChildNodes()) { + $embed->removeChild($embed->firstChild); + } $embed->appendChild($xmlDoc->createCDATASection($embedContent)); } }