From 468da7b31482c33d7dac606b357e46bcec0c631c Mon Sep 17 00:00:00 2001 From: Vidar Langseid Date: Fri, 27 Apr 2018 09:04:05 +0200 Subject: [PATCH] Ensured consistent naming of variables --- .../ConvertXmlTextToRichTextCommand.php | 81 ++++++---- bundle/Resources/config/services.yml | 2 +- lib/FieldType/XmlText/Converter/RichText.php | 153 ++++++++++++++---- .../lib/FieldType/Converter/RichTextTest.php | 85 ++++++++-- ...01-title-dupid.xml => 001-dupid-title.xml} | 0 ...02-embed-dupid.xml => 002-dupid-embed.xml} | 0 ...-anchor-dupid.xml => 003-dupid-anchor.xml} | 0 ...0-embed-image-add-ez-embed-type-image.xml} | 6 +- ...d-inline-image-add-ez-embed-type-image.xml | 16 ++ ...d-inline-image-add-ez-embed-type-image.xml | 12 ++ ...01-title-dupid.xml => 001-dupid-title.xml} | 0 ...02-embed-dupid.xml => 002-dupid-embed.xml} | 0 ...-anchor-dupid.xml => 003-dupid-anchor.xml} | 0 ...20-embed-image-add-ez-embed-type-image.xml | 31 ++++ ...d-inline-image-add-ez-embed-type-image.xml | 28 ++++ ...d-inline-image-add-ez-embed-type-image.xml | 13 ++ .../input/001-no-change.xml} | 10 +- .../input/002-add-ez-embed-type-image.xml | 31 ++++ .../input/003-remove-ez-embed-type-image.xml | 31 ++++ .../output/001-no-change.xml | 23 +++ .../output/002-add-ez-embed-type-image.xml | 31 ++++ .../output/003-remove-ez-embed-type-image.xml | 31 ++++ 22 files changed, 512 insertions(+), 72 deletions(-) rename tests/lib/FieldType/Converter/_fixtures/richtext/input/{001-title-dupid.xml => 001-dupid-title.xml} (100%) rename tests/lib/FieldType/Converter/_fixtures/richtext/input/{002-embed-dupid.xml => 002-dupid-embed.xml} (100%) rename tests/lib/FieldType/Converter/_fixtures/richtext/input/{003-anchor-dupid.xml => 003-dupid-anchor.xml} (100%) rename tests/lib/FieldType/Converter/_fixtures/richtext/input/{004-embed-image-add-ez-embed-type-image.xml => 120-embed-image-add-ez-embed-type-image.xml} (76%) create mode 100644 tests/lib/FieldType/Converter/_fixtures/richtext/input/130-embed-inline-image-add-ez-embed-type-image.xml create mode 100644 tests/lib/FieldType/Converter/_fixtures/richtext/input/131-embed-inline-image-add-ez-embed-type-image.xml rename tests/lib/FieldType/Converter/_fixtures/richtext/output/{001-title-dupid.xml => 001-dupid-title.xml} (100%) rename tests/lib/FieldType/Converter/_fixtures/richtext/output/{002-embed-dupid.xml => 002-dupid-embed.xml} (100%) rename tests/lib/FieldType/Converter/_fixtures/richtext/output/{003-anchor-dupid.xml => 003-dupid-anchor.xml} (100%) create mode 100644 tests/lib/FieldType/Converter/_fixtures/richtext/output/120-embed-image-add-ez-embed-type-image.xml create mode 100644 tests/lib/FieldType/Converter/_fixtures/richtext/output/130-embed-inline-image-add-ez-embed-type-image.xml create mode 100644 tests/lib/FieldType/Converter/_fixtures/richtext/output/131-embed-inline-image-add-ez-embed-type-image.xml rename tests/lib/FieldType/Converter/_fixtures/{richtext/output/004-embed-image-add-ez-embed-type-image.xml => tag_embedded_images/input/001-no-change.xml} (98%) create mode 100644 tests/lib/FieldType/Converter/_fixtures/tag_embedded_images/input/002-add-ez-embed-type-image.xml create mode 100644 tests/lib/FieldType/Converter/_fixtures/tag_embedded_images/input/003-remove-ez-embed-type-image.xml create mode 100644 tests/lib/FieldType/Converter/_fixtures/tag_embedded_images/output/001-no-change.xml create mode 100644 tests/lib/FieldType/Converter/_fixtures/tag_embedded_images/output/002-add-ez-embed-type-image.xml create mode 100644 tests/lib/FieldType/Converter/_fixtures/tag_embedded_images/output/003-remove-ez-embed-type-image.xml diff --git a/bundle/Command/ConvertXmlTextToRichTextCommand.php b/bundle/Command/ConvertXmlTextToRichTextCommand.php index 1c086736..ffea3c33 100644 --- a/bundle/Command/ConvertXmlTextToRichTextCommand.php +++ b/bundle/Command/ConvertXmlTextToRichTextCommand.php @@ -8,6 +8,7 @@ use PDO; use Psr\Log\LoggerInterface; use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; +use Symfony\Component\Console\Exception\RuntimeException; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -32,7 +33,7 @@ class ConvertXmlTextToRichTextCommand extends ContainerAwareCommand */ private $converter; - public function __construct(Connection $dbal, RichTextConverter $converter, LoggerInterface $logger = null) + public function __construct(Connection $dbal, RichTextConverter $converter, LoggerInterface $logger) { parent::__construct(); @@ -75,7 +76,7 @@ protected function configure() 'image-content-types', null, InputOption::VALUE_OPTIONAL, - 'Comma separated list of content types which are considered as images when converting embedded tags. Default value is 27' + 'Comma separated list of content type identifiers which are considered as images when converting embedded tags. Default value is image' ) ->addOption( 'fix-embedded-images-only', @@ -96,34 +97,59 @@ protected function execute(InputInterface $input, OutputInterface $output) $dryRun = true; } - $testContentObjectId = $input->getOption('test-content-object'); + $testContentId = $input->getOption('test-content-object'); if ($input->getOption('image-content-types')) { - $contentTypes = explode(',', $input->getOption('image-content-types')); + $contentTypeIdentifiers = explode(',', $input->getOption('image-content-types')); } else { - $contentTypes = array(27); + $contentTypeIdentifiers = ['image']; } - $this->converter->setImageContentTypes($contentTypes); + $contentTypeIds = $this->getContentTypeIds($contentTypeIdentifiers); + if (count($contentTypeIds) !== count($contentTypeIdentifiers)) { + throw new RuntimeException('Unable to lookup all content type identifiers, found : ' . implode(',', $contentTypeIds)); + } + $this->converter->setImageContentTypes($contentTypeIds); if ($input->getOption('fix-embedded-images-only')) { $output->writeln("Fixing embedded images only. No other changes are done to the database\n"); - $this->fixEmbeddedImages($dryRun, $testContentObjectId, $output); + $this->fixEmbeddedImages($dryRun, $testContentId, $output); + return; } - if ($testContentObjectId === null) { + if ($testContentId === null) { $this->convertFieldDefinitions($dryRun, $output); } else { $dryRun = true; } - $this->convertFields($dryRun, $testContentObjectId, !$input->getOption('disable-duplicate-id-check'), $output); + $this->convertFields($dryRun, $testContentId, !$input->getOption('disable-duplicate-id-check'), $output); + } + + protected function getContentTypeIds($contentTypeIdentifiers) + { + $query = $this->dbal->createQueryBuilder(); + + $query->select('c.id') + ->from('ezcontentclass', 'c') + ->where( + $query->expr()->in( + 'c.identifier', + ':contentTypeIdentifiers' + ) + ) + ->setParameter(':contentTypeIdentifiers', $contentTypeIdentifiers, Connection::PARAM_STR_ARRAY); + + $statement = $query->execute(); + + return $statement->fetchAll(PDO::FETCH_COLUMN); } protected function loginAsAdmin() { $userService = $this->getContainer()->get('ezpublish.api.service.user'); - $permissionResolver = $this->getContainer()->get('date_based_publisher.permission_resolver'); + $repository = $this->getContainer()->get('ezpublish.api.repository'); + $permissionResolver = $repository->getPermissionResolver(); $permissionResolver->setCurrentUserReference($userService->loadUserByLogin('admin')); } @@ -154,10 +180,9 @@ protected function fixEmbeddedImages($dryRun, $contentId, OutputInterface $outpu $this->logger->info( "No embedded image(s) in ezrichtext field #{$row['id']} needed to be updated", [ - 'original' => $inputValue + 'original' => $inputValue, ] ); - } else { $this->updateFieldRow($dryRun, $row['id'], $row['version'], $converted); @@ -165,7 +190,7 @@ protected function fixEmbeddedImages($dryRun, $contentId, OutputInterface $outpu "Updated $count embded image(s) in ezrichtext field #{$row['id']}", [ 'original' => $inputValue, - 'converted' => $converted + 'converted' => $converted, ] ); } @@ -216,7 +241,7 @@ protected function convertFieldDefinitions($dryRun, OutputInterface $output) $output->writeln("Converted $count ezxmltext field definitions to ezrichtext"); } - protected function getRowCountOfContentObjectAttributes($datatypeString, $contentObjectId) + protected function getRowCountOfContentObjectAttributes($datatypeString, $contentId) { $query = $this->dbal->createQueryBuilder(); $query->select('count(a.id)') @@ -227,19 +252,20 @@ protected function getRowCountOfContentObjectAttributes($datatypeString, $conten ':datatypestring' ) ) - ->setParameter(':datatypestring',$datatypeString); + ->setParameter(':datatypestring', $datatypeString); - if ($contentObjectId !== null) { + if ($contentId !== null) { $query->andWhere( $query->expr()->eq( 'a.contentobject_id', - ':contentobjectid' + ':contentid' ) ) - ->setParameter(':contentobjectid', $contentObjectId); + ->setParameter(':contentid', $contentId); } $statement = $query->execute(); + return (int) $statement->fetchColumn(); } @@ -259,17 +285,18 @@ protected function getFieldRows($datatypeString, $contentId) ':datatypestring' ) ) - ->setParameter(':datatypestring',$datatypeString); + ->setParameter(':datatypestring', $datatypeString); if ($contentId !== null) { $query->andWhere( $query->expr()->eq( 'a.contentobject_id', - ':contentobjectid' + ':contentid' ) ) - ->setParameter(':contentobjectid', $contentId); + ->setParameter(':contentid', $contentId); } + return $query->execute(); } @@ -291,25 +318,25 @@ protected function updateFieldRow($dryRun, $id, $version, $datatext) ':version' ) ) - ->setParameters(array( + ->setParameters([ ':datatypestring' => 'ezrichtext', ':datatext' => $datatext, ':id' => $id, - ':version' => $version - )); + ':version' => $version, + ]); if (!$dryRun) { $updateQuery->execute(); } } - protected function convertFields($dryRun, $contentObjectId, $checkDuplicateIds, OutputInterface $output) + protected function convertFields($dryRun, $contentId, $checkDuplicateIds, OutputInterface $output) { - $count = $this->getRowCountOfContentObjectAttributes('ezxmltext', $contentObjectId); + $count = $this->getRowCountOfContentObjectAttributes('ezxmltext', $contentId); $output->writeln("Found $count field rows to convert."); - $statement = $this->getFieldRows('ezxmltext', $contentObjectId); + $statement = $this->getFieldRows('ezxmltext', $contentId); while ($row = $statement->fetch(PDO::FETCH_ASSOC)) { if (empty($row['data_text'])) { diff --git a/bundle/Resources/config/services.yml b/bundle/Resources/config/services.yml index 5ac040f5..87907bae 100644 --- a/bundle/Resources/config/services.yml +++ b/bundle/Resources/config/services.yml @@ -19,5 +19,5 @@ services: ezxmltext.richtext_converter: class: eZ\Publish\Core\FieldType\XmlText\Converter\RichText arguments: - - "@?logger" - "@ezpublish.api.repository" + - "@?logger" diff --git a/lib/FieldType/XmlText/Converter/RichText.php b/lib/FieldType/XmlText/Converter/RichText.php index 9ed3b0f5..b3162c73 100644 --- a/lib/FieldType/XmlText/Converter/RichText.php +++ b/lib/FieldType/XmlText/Converter/RichText.php @@ -11,11 +11,14 @@ use eZ\Publish\Core\FieldType\XmlText\Converter; use DOMDocument; use DOMXPath; +use DOMNode; use Psr\Log\LoggerInterface; use eZ\Publish\Core\FieldType\RichText\Converter\Aggregate; use eZ\Publish\Core\FieldType\RichText\Converter\Ezxml\ToRichTextPreNormalize; use eZ\Publish\Core\FieldType\RichText\Converter\Xslt; use eZ\Publish\Core\FieldType\RichText\Validator; +use eZ\Publish\Core\Base\Exceptions\NotFoundException; +use Psr\Log\NullLogger; class RichText implements Converter { @@ -35,10 +38,17 @@ class RichText implements Converter private $apiRepository; - public function __construct(LoggerInterface $logger = null, $apiRepository = null, $imageContentTypes = array()) + /** + * Holds the id of the current contentField being converted. + * + * @var null|int + */ + private $currentContentFieldId; + + public function __construct($apiRepository = null, LoggerInterface $logger = null) { - $this->logger = $logger; - $this->imageContentTypes = $imageContentTypes; + $this->logger = $logger instanceof LoggerInterface ? $logger : new NullLogger(); + $this->imageContentTypes = []; $this->apiRepository = $apiRepository; $this->converter = new Aggregate( @@ -82,62 +92,151 @@ protected function removeComments(DOMDocument $document) } } - protected function reportNonUniqueIds(DOMDocument $document, $contentObjectAttributeId) + protected function reportNonUniqueIds(DOMDocument $document, $contentFieldId) { $xpath = new DOMXPath($document); - $ns = $document->documentElement->namespaceURI; $nodes = $xpath->query("//*[contains(@xml:id, 'duplicated_id_')]"); + if ($contentFieldId === null) { + $contentFieldId = '[unknown]'; + } foreach ($nodes as $node) { $id = $node->attributes->getNamedItem('id')->nodeValue; // id has format "duplicated_id_foo_bar_idm45226413447104" where "foo_bar" is the duplicated id $duplicatedId = substr($id, strlen('duplicated_id_'), strrpos($id, '_') - strlen('duplicated_id_')); if ($this->logger !== null) { - $this->logger->warning("Duplicated id in original ezxmltext for contentobject_attribute.id=$contentObjectAttributeId, automatically generated new id : $duplicatedId --> $id"); + $this->logger->warning("Duplicated id in original ezxmltext for contentobject_attribute.id=$contentFieldId, automatically generated new id : $duplicatedId --> $id"); } } } - protected function isImageClass($contentId) + /** + * @param $id + * @param bool $isContentId Whatever provided $id is a content id or location id + * @param $contentFieldId + * @return bool + */ + protected function isImageContentType($id, $isContentId, $contentFieldId) { - $contentService = $this->apiRepository->getContentService(); - $contentInfo = $contentService->loadContentInfo($contentId); + if ($contentFieldId === null) { + $contentFieldId = '[unknown]'; + } + + try { + if ($isContentId) { + $contentService = $this->apiRepository->getContentService(); + $contentInfo = $contentService->loadContentInfo($id); + } else { + $locationService = $this->apiRepository->getLocationService(); + try { + $location = $locationService->loadLocation($id); + } catch (NotFoundException $e) { + $this->logger->warning("Unable to find node_id=$id, referred to in embedded tag in contentobject_attribute.id=$contentFieldId."); + + return false; + } + $contentInfo = $location->getContentInfo(); + } + } catch (NotFoundException $e) { + $this->logger->warning("Unable to find content_id=$id, referred to in embedded tag in contentobject_attribute.id=$contentFieldId."); + + return false; + } + return in_array($contentInfo->contentTypeId, $this->imageContentTypes); } /** - * Embedded images needs to include an attribute (ezxhtml:class="ez-embed-type-image) in order to be recognized by editor + * @param DOMNode $node + * @param $value + * @return bool returns true if node was changed + */ + private function addXhtmlClassValue(DOMNode $node, $value) + { + $classAttributes = $node->attributes->getNamedItemNS('http://ez.no/xmlns/ezpublish/docbook/xhtml', 'class'); + if ($classAttributes == null) { + $node->setAttribute('ezxhtml:class', 'ez-embed-type-image'); + + return true; + } + + $attributes = explode(' ', $classAttributes->nodeValue); + + $key = array_search($value, $attributes); + if ($key === false) { + $classAttributes->value = $classAttributes->nodeValue . " $value"; + + return true; + } + + return false; + } + + /** + * @param DOMNode $node + * @param $value + * @return bool returns true if node was changed + */ + private function removeXhtmlClassValue(DOMNode $node, $value) + { + $classAttributes = $node->attributes->getNamedItemNS('http://ez.no/xmlns/ezpublish/docbook/xhtml', 'class'); + if ($classAttributes == null) { + return false; + } + + $attributes = explode(' ', $classAttributes->nodeValue); + + $classNameFound = false; + $key = array_search($value, $attributes); + if ($key !== false) { + unset($attributes[$key]); + $classNameFound = true; + } + + if ($classNameFound) { + if (count($attributes) === 0) { + $node->removeAttribute('ezxhtml:class'); + } else { + $classAttributes->value = implode(' ', $attributes); + } + } + + return $classNameFound; + } + + /** + * Embedded images needs to include an attribute (ezxhtml:class="ez-embed-type-image) in order to be recognized by editor. * * Before calling this function, make sure you are logged in as admin, or at least have access to all the objects * being embedded in the $richtextDocument. * * @param DOMDocument $richtextDocument + * @param $contentFieldId * @return int Number of ezembed tags which where changed */ - public function tagEmbeddedImages(DOMDocument $richtextDocument) + public function tagEmbeddedImages(DOMDocument $richtextDocument, $contentFieldId) { $count = 0; $xpath = new DOMXPath($richtextDocument); $ns = $richtextDocument->documentElement->namespaceURI; $xpath->registerNamespace('doc', $ns); - $nodes = $xpath->query('//doc:ezembed'); + $nodes = $xpath->query('//doc:ezembed | //doc:ezembedinline'); foreach ($nodes as $node) { - //href is in format : ezcontent://123 - $href=$node->attributes->getNamedItem('href')->nodeValue; - $contentId = (int) substr($href, strrpos($href, '/')+1); - $classAttribute = $node->attributes->getNamedItem('class'); - if ($this->isImageClass($contentId)) { - if (($classAttribute === null) || (($classAttribute !== null) && ($node->attributes->getNamedItem('class')->nodeValue !== 'ez-embed-type-image'))) { - $node->setAttribute('ezxhtml:class', 'ez-embed-type-image'); + //href is in format : ezcontent://123 or ezlocation://123 + $href = $node->attributes->getNamedItem('href')->nodeValue; + $isContentId = strpos($href, 'ezcontent') === 0; + $id = (int) substr($href, strrpos($href, '/') + 1); + $isImage = $this->isImageContentType($id, $isContentId, $contentFieldId); + if ($isImage) { + if ($this->addXhtmlClassValue($node, 'ez-embed-type-image')) { ++$count; } } else { - if (($classAttribute !== null) && ($node->attributes->getNamedItem('class')->nodeValue === 'ez-embed-type-image')) { - $node->removeAttribute('ezxhtml:class'); - //$node->setAttribute('ezxhtml:class', 'ez-embed-type-image'); + if ($this->removeXhtmlClassValue($node, 'ez-embed-type-image')) { ++$count; } } } + return $count; } @@ -147,22 +246,22 @@ public function tagEmbeddedImages(DOMDocument $richtextDocument) * * @param DOMDocument $inputDocument * @param bool $checkDuplicateIds - * @param null $contentObjectAttributeId + * @param null|int $contentFieldId * @return string */ - public function convert(DOMDocument $inputDocument, $checkDuplicateIds = false, $contentObjectAttributeId = null) + public function convert(DOMDocument $inputDocument, $checkDuplicateIds = false, $contentFieldId = null) { $this->removeComments($inputDocument); $convertedDocument = $this->converter->convert($inputDocument); if ($checkDuplicateIds) { - $this->reportNonUniqueIds($convertedDocument, $contentObjectAttributeId); + $this->reportNonUniqueIds($convertedDocument, $contentFieldId); } // Needed by some disabled output escaping (eg. legacy ezxml paragraph elements) $convertedDocumentNormalized = new DOMDocument(); $convertedDocumentNormalized->loadXML($convertedDocument->saveXML()); - $this->tagEmbeddedImages($convertedDocumentNormalized); + $this->tagEmbeddedImages($convertedDocumentNormalized, $contentFieldId); $errors = $this->validator->validate($convertedDocumentNormalized); @@ -170,7 +269,7 @@ public function convert(DOMDocument $inputDocument, $checkDuplicateIds = false, if (!empty($errors) && $this->logger !== null) { $this->logger->error( - "Validation errors when converting ezxmltext for contentobject_attribute.id=$contentObjectAttributeId", + "Validation errors when converting ezxmltext for contentobject_attribute.id=$contentFieldId", ['result' => $result, 'errors' => $errors, 'xmlString' => $inputDocument->saveXML()] ); } diff --git a/tests/lib/FieldType/Converter/RichTextTest.php b/tests/lib/FieldType/Converter/RichTextTest.php index 2ee9cc78..01c622cc 100644 --- a/tests/lib/FieldType/Converter/RichTextTest.php +++ b/tests/lib/FieldType/Converter/RichTextTest.php @@ -14,7 +14,9 @@ use PHPUnit\Framework\TestCase; use eZ\Publish\Core\SignalSlot\Repository; use eZ\Publish\API\Repository\ContentService; +use eZ\Publish\API\Repository\LocationService; use eZ\Publish\API\Repository\Values\Content\ContentInfo; +use eZ\Publish\API\Repository\Values\Content\Location; class RichTextTest extends TestCase { @@ -47,13 +49,32 @@ protected function createDocument($xml, $isPath = true) */ public function providerForTestConvert() { - $map = array(); + $map = []; foreach (glob(__DIR__ . '/_fixtures/richtext/input/*.xml') as $inputFilePath) { $basename = basename($inputFilePath, '.xml'); $outputFilePath = __DIR__ . "/_fixtures/richtext/output/{$basename}.xml"; - $map[] = array($inputFilePath, $outputFilePath); + $map[] = [$inputFilePath, $outputFilePath]; + } + + return $map; + } + + /** + * Provider for conversion test. + * + * @return array + */ + public function providerForTestTagEmbeddedImages() + { + $map = []; + + foreach (glob(__DIR__ . '/_fixtures/tag_embedded_images/input/*.xml') as $inputFilePath) { + $basename = basename($inputFilePath, '.xml'); + $outputFilePath = __DIR__ . "/_fixtures/tag_embedded_images/output/{$basename}.xml"; + + $map[] = [$inputFilePath, $outputFilePath]; } return $map; @@ -71,26 +92,24 @@ protected function normalizeRewrittenIds(DOMDocument $xmlDoc) } } - /** - * @param string $inputFilePath - * @param string $outputFilePath - * - * @dataProvider providerForTestConvert - */ - public function testConvert($inputFilePath, $outputFilePath) + private function createApiRepositoryStub() { $apiRepositoryStub = $this->createMock(Repository::class); $contentServiceStub = $this->createMock(ContentService::class); + $locationServiceStub = $this->createMock(LocationService::class); $contentInfoImageStub = $this->createMock(ContentInfo::class); $contentInfoFileStub = $this->createMock(ContentInfo::class); + $locationStub = $this->createMock(Location::class); // content with id=126 is an image, content with id=128,129 is a file $map = [ [126, $contentInfoImageStub], [128, $contentInfoFileStub], - [129, $contentInfoFileStub] + [129, $contentInfoFileStub], ]; $apiRepositoryStub->method('getContentService') ->willReturn($contentServiceStub); + $apiRepositoryStub->method('getLocationService') + ->willReturn($locationServiceStub); $contentServiceStub->method('loadContentInfo') ->will($this->returnValueMap($map)); @@ -98,8 +117,25 @@ public function testConvert($inputFilePath, $outputFilePath) $contentInfoImageStub->method('__get')->willReturn(27); $contentInfoFileStub->method('__get')->willReturn(25); + $locationServiceStub->method('loadLocation')->willReturn($locationStub); + $locationStub->method('getContentInfo')->willReturn($contentInfoImageStub); + + return $apiRepositoryStub; + } + + /** + * @param string $inputFilePath + * @param string $outputFilePath + * + * @dataProvider providerForTestConvert + */ + public function testConvert($inputFilePath, $outputFilePath) + { + $apiRepositoryStub = $this->createApiRepositoryStub(); + $inputDocument = $this->createDocument($inputFilePath); - $richText = new RichText(null, $apiRepositoryStub, array(27)); + $richText = new RichText($apiRepositoryStub); + $richText->setImageContentTypes([27]); $result = $richText->convert($inputDocument, true); @@ -114,4 +150,31 @@ public function testConvert($inputFilePath, $outputFilePath) $convertedDocument ); } + + /** + * @param string $inputFilePath + * @param string $outputFilePath + * + * @dataProvider providerForTestTagEmbeddedImages + */ + public function testTagEmbeddedImages($inputFilePath, $outputFilePath) + { + $apiRepositoryStub = $this->createApiRepositoryStub(); + + $inputDocument = $this->createDocument($inputFilePath); + $richText = new RichText($apiRepositoryStub); + $richText->setImageContentTypes(array(27)); + + $richText->tagEmbeddedImages($inputDocument, null); + + $expectedDocument = $this->createDocument($outputFilePath); + + // since duplicate ids are rewritten with random values, we need to normalize those + $this->normalizeRewrittenIds($inputDocument); + + $this->assertEquals( + $expectedDocument, + $inputDocument + ); + } } diff --git a/tests/lib/FieldType/Converter/_fixtures/richtext/input/001-title-dupid.xml b/tests/lib/FieldType/Converter/_fixtures/richtext/input/001-dupid-title.xml similarity index 100% rename from tests/lib/FieldType/Converter/_fixtures/richtext/input/001-title-dupid.xml rename to tests/lib/FieldType/Converter/_fixtures/richtext/input/001-dupid-title.xml diff --git a/tests/lib/FieldType/Converter/_fixtures/richtext/input/002-embed-dupid.xml b/tests/lib/FieldType/Converter/_fixtures/richtext/input/002-dupid-embed.xml similarity index 100% rename from tests/lib/FieldType/Converter/_fixtures/richtext/input/002-embed-dupid.xml rename to tests/lib/FieldType/Converter/_fixtures/richtext/input/002-dupid-embed.xml diff --git a/tests/lib/FieldType/Converter/_fixtures/richtext/input/003-anchor-dupid.xml b/tests/lib/FieldType/Converter/_fixtures/richtext/input/003-dupid-anchor.xml similarity index 100% rename from tests/lib/FieldType/Converter/_fixtures/richtext/input/003-anchor-dupid.xml rename to tests/lib/FieldType/Converter/_fixtures/richtext/input/003-dupid-anchor.xml diff --git a/tests/lib/FieldType/Converter/_fixtures/richtext/input/004-embed-image-add-ez-embed-type-image.xml b/tests/lib/FieldType/Converter/_fixtures/richtext/input/120-embed-image-add-ez-embed-type-image.xml similarity index 76% rename from tests/lib/FieldType/Converter/_fixtures/richtext/input/004-embed-image-add-ez-embed-type-image.xml rename to tests/lib/FieldType/Converter/_fixtures/richtext/input/120-embed-image-add-ez-embed-type-image.xml index b1154f44..e725fa61 100644 --- a/tests/lib/FieldType/Converter/_fixtures/richtext/input/004-embed-image-add-ez-embed-type-image.xml +++ b/tests/lib/FieldType/Converter/_fixtures/richtext/input/120-embed-image-add-ez-embed-type-image.xml @@ -12,4 +12,8 @@ xmlns:tmp="http://ez.no/namespaces/ezpublish3/temporary/"> - \ No newline at end of file + Here we have a nice image with node_id + + + + diff --git a/tests/lib/FieldType/Converter/_fixtures/richtext/input/130-embed-inline-image-add-ez-embed-type-image.xml b/tests/lib/FieldType/Converter/_fixtures/richtext/input/130-embed-inline-image-add-ez-embed-type-image.xml new file mode 100644 index 00000000..50e7686a --- /dev/null +++ b/tests/lib/FieldType/Converter/_fixtures/richtext/input/130-embed-inline-image-add-ez-embed-type-image.xml @@ -0,0 +1,16 @@ + +
+ Here we have a nice image + + + And here we have a file + + + Here we have a nice image with node_id + + +
diff --git a/tests/lib/FieldType/Converter/_fixtures/richtext/input/131-embed-inline-image-add-ez-embed-type-image.xml b/tests/lib/FieldType/Converter/_fixtures/richtext/input/131-embed-inline-image-add-ez-embed-type-image.xml new file mode 100644 index 00000000..4d4c9929 --- /dev/null +++ b/tests/lib/FieldType/Converter/_fixtures/richtext/input/131-embed-inline-image-add-ez-embed-type-image.xml @@ -0,0 +1,12 @@ + +
+ Here we have a nice image + + + Here we have another nice image + + +
diff --git a/tests/lib/FieldType/Converter/_fixtures/richtext/output/001-title-dupid.xml b/tests/lib/FieldType/Converter/_fixtures/richtext/output/001-dupid-title.xml similarity index 100% rename from tests/lib/FieldType/Converter/_fixtures/richtext/output/001-title-dupid.xml rename to tests/lib/FieldType/Converter/_fixtures/richtext/output/001-dupid-title.xml diff --git a/tests/lib/FieldType/Converter/_fixtures/richtext/output/002-embed-dupid.xml b/tests/lib/FieldType/Converter/_fixtures/richtext/output/002-dupid-embed.xml similarity index 100% rename from tests/lib/FieldType/Converter/_fixtures/richtext/output/002-embed-dupid.xml rename to tests/lib/FieldType/Converter/_fixtures/richtext/output/002-dupid-embed.xml diff --git a/tests/lib/FieldType/Converter/_fixtures/richtext/output/003-anchor-dupid.xml b/tests/lib/FieldType/Converter/_fixtures/richtext/output/003-dupid-anchor.xml similarity index 100% rename from tests/lib/FieldType/Converter/_fixtures/richtext/output/003-anchor-dupid.xml rename to tests/lib/FieldType/Converter/_fixtures/richtext/output/003-dupid-anchor.xml diff --git a/tests/lib/FieldType/Converter/_fixtures/richtext/output/120-embed-image-add-ez-embed-type-image.xml b/tests/lib/FieldType/Converter/_fixtures/richtext/output/120-embed-image-add-ez-embed-type-image.xml new file mode 100644 index 00000000..2e611d77 --- /dev/null +++ b/tests/lib/FieldType/Converter/_fixtures/richtext/output/120-embed-image-add-ez-embed-type-image.xml @@ -0,0 +1,31 @@ + +
+ Here we have a nice image + + + medium + 0 + 5 + + + And here we have a file + + + medium + 0 + 5 + + + Here we have a nice image with node_id + + + medium + 0 + 5 + + +
diff --git a/tests/lib/FieldType/Converter/_fixtures/richtext/output/130-embed-inline-image-add-ez-embed-type-image.xml b/tests/lib/FieldType/Converter/_fixtures/richtext/output/130-embed-inline-image-add-ez-embed-type-image.xml new file mode 100644 index 00000000..6f991afc --- /dev/null +++ b/tests/lib/FieldType/Converter/_fixtures/richtext/output/130-embed-inline-image-add-ez-embed-type-image.xml @@ -0,0 +1,28 @@ + +
+ Here we have a nice image + + + medium + + + + And here we have a file + + + medium + + + + Here we have a nice image with node_id + + + medium + + + +
diff --git a/tests/lib/FieldType/Converter/_fixtures/richtext/output/131-embed-inline-image-add-ez-embed-type-image.xml b/tests/lib/FieldType/Converter/_fixtures/richtext/output/131-embed-inline-image-add-ez-embed-type-image.xml new file mode 100644 index 00000000..895744f4 --- /dev/null +++ b/tests/lib/FieldType/Converter/_fixtures/richtext/output/131-embed-inline-image-add-ez-embed-type-image.xml @@ -0,0 +1,13 @@ + +
+ Here we have a nice image + medium + + Here we have another nice image + medium + +
diff --git a/tests/lib/FieldType/Converter/_fixtures/richtext/output/004-embed-image-add-ez-embed-type-image.xml b/tests/lib/FieldType/Converter/_fixtures/tag_embedded_images/input/001-no-change.xml similarity index 98% rename from tests/lib/FieldType/Converter/_fixtures/richtext/output/004-embed-image-add-ez-embed-type-image.xml rename to tests/lib/FieldType/Converter/_fixtures/tag_embedded_images/input/001-no-change.xml index d7193ee9..18de8a94 100644 --- a/tests/lib/FieldType/Converter/_fixtures/richtext/output/004-embed-image-add-ez-embed-type-image.xml +++ b/tests/lib/FieldType/Converter/_fixtures/tag_embedded_images/input/001-no-change.xml @@ -4,20 +4,20 @@ xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ezxhtml="http://ez.no/xmlns/ezpublish/docbook/xhtml" xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom" version="5.0-variant ezpublish-1.0"> - Here we have a nice image - + And here we have a file + medium 0 5 - And here we have a file - + Here we have a nice image + medium 0 5 - \ No newline at end of file + diff --git a/tests/lib/FieldType/Converter/_fixtures/tag_embedded_images/input/002-add-ez-embed-type-image.xml b/tests/lib/FieldType/Converter/_fixtures/tag_embedded_images/input/002-add-ez-embed-type-image.xml new file mode 100644 index 00000000..94518dd7 --- /dev/null +++ b/tests/lib/FieldType/Converter/_fixtures/tag_embedded_images/input/002-add-ez-embed-type-image.xml @@ -0,0 +1,31 @@ + +
+ Here we have a file + + + medium + 0 + 5 + + + Here we have a nice image + + + medium + 0 + 5 + + + Here we have a nice image with a different class + + + medium + 0 + 5 + + +
diff --git a/tests/lib/FieldType/Converter/_fixtures/tag_embedded_images/input/003-remove-ez-embed-type-image.xml b/tests/lib/FieldType/Converter/_fixtures/tag_embedded_images/input/003-remove-ez-embed-type-image.xml new file mode 100644 index 00000000..e02385ec --- /dev/null +++ b/tests/lib/FieldType/Converter/_fixtures/tag_embedded_images/input/003-remove-ez-embed-type-image.xml @@ -0,0 +1,31 @@ + +
+ And here we have a file which should not contain ez-embed-type-image + + + medium + 0 + 5 + + + And here we have a file which should only have foobar + + + medium + 0 + 5 + + + Here we have a nice image which should keep class as is + + + medium + 0 + 5 + + +
diff --git a/tests/lib/FieldType/Converter/_fixtures/tag_embedded_images/output/001-no-change.xml b/tests/lib/FieldType/Converter/_fixtures/tag_embedded_images/output/001-no-change.xml new file mode 100644 index 00000000..18de8a94 --- /dev/null +++ b/tests/lib/FieldType/Converter/_fixtures/tag_embedded_images/output/001-no-change.xml @@ -0,0 +1,23 @@ + +
+ And here we have a file + + + medium + 0 + 5 + + + Here we have a nice image + + + medium + 0 + 5 + + +
diff --git a/tests/lib/FieldType/Converter/_fixtures/tag_embedded_images/output/002-add-ez-embed-type-image.xml b/tests/lib/FieldType/Converter/_fixtures/tag_embedded_images/output/002-add-ez-embed-type-image.xml new file mode 100644 index 00000000..7db2cf62 --- /dev/null +++ b/tests/lib/FieldType/Converter/_fixtures/tag_embedded_images/output/002-add-ez-embed-type-image.xml @@ -0,0 +1,31 @@ + +
+ Here we have a file + + + medium + 0 + 5 + + + Here we have a nice image + + + medium + 0 + 5 + + + Here we have a nice image with a different class + + + medium + 0 + 5 + + +
diff --git a/tests/lib/FieldType/Converter/_fixtures/tag_embedded_images/output/003-remove-ez-embed-type-image.xml b/tests/lib/FieldType/Converter/_fixtures/tag_embedded_images/output/003-remove-ez-embed-type-image.xml new file mode 100644 index 00000000..3507c35d --- /dev/null +++ b/tests/lib/FieldType/Converter/_fixtures/tag_embedded_images/output/003-remove-ez-embed-type-image.xml @@ -0,0 +1,31 @@ + +
+ And here we have a file which should not contain ez-embed-type-image + + + medium + 0 + 5 + + + And here we have a file which should only have foobar + + + medium + 0 + 5 + + + Here we have a nice image which should keep class as is + + + medium + 0 + 5 + + +