From c2c27571cbafe6cbddfd3ba17fcdda8715430ed2 Mon Sep 17 00:00:00 2001 From: tomaszmadeyski Date: Mon, 19 Jun 2017 15:22:14 +0200 Subject: [PATCH] EZP-27396 Removing xml node text content in order to properly embed objects (#20) --- .../XmlText/Converter/EmbedToHtml5.php | 3 + .../FieldType/Converter/EmbedToHtml5Test.php | 101 ++++++++++++++++++ 2 files changed, 104 insertions(+) diff --git a/lib/FieldType/XmlText/Converter/EmbedToHtml5.php b/lib/FieldType/XmlText/Converter/EmbedToHtml5.php index 9ade7d98..41dc95db 100644 --- a/lib/FieldType/XmlText/Converter/EmbedToHtml5.php +++ b/lib/FieldType/XmlText/Converter/EmbedToHtml5.php @@ -182,6 +182,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)); } } diff --git a/tests/lib/FieldType/Converter/EmbedToHtml5Test.php b/tests/lib/FieldType/Converter/EmbedToHtml5Test.php index 9e854e6a..85e9d4ed 100644 --- a/tests/lib/FieldType/Converter/EmbedToHtml5Test.php +++ b/tests/lib/FieldType/Converter/EmbedToHtml5Test.php @@ -1081,4 +1081,105 @@ 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 + 2)) + ->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 + ), + ); + } }