Skip to content

Commit

Permalink
Upgraded package to be compatible with eZ Platform v3.0 (#119)
Browse files Browse the repository at this point in the history
* EZP-31512: Fixed extracting text from RichText for no child nodes

* [CS] Disabled native_function_invocation php-cs-fixer rule

* [Tests] Upgraded test setup to rely on Doctrine instead of eZc Database

* Delegated importing test fixtures to eZ Platform Kernel FixtureImporter.

* Replaced usages of eZc Database Handler with Doctrine Connection.

* Aligned CS to follow eZ Platform Code Style.

* Replaced usages of PDO::FETCH_* with Doctrine FetchMode

* [SF5] Replaced ContextErrorException with ErrorException

ContextErrorException was dropped in Symfony 4.0 in favor of the native \ErrorException

* Update README.md

Co-authored-by: André R <[email protected]>
  • Loading branch information
alongosz and andrerom authored Apr 10, 2020
1 parent 9a9ad62 commit d0a83c0
Show file tree
Hide file tree
Showing 11 changed files with 146 additions and 194 deletions.
1 change: 1 addition & 0 deletions .php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ return PhpCsFixer\Config::create()
'yoda_style' => false,
'no_break_comment' => false,
'no_superfluous_phpdoc_tags' => false,
'native_function_invocation' => false,

// 2019 style updates with cs-fixer 2.14, all above are in sync with kernel
'@PHPUnit57Migration:risky' => true,
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@

This is the XmlText field type for eZ Platform. It was extracted from the eZ Publish / Platform 5.x as it has been suceeded by docbook based [RichText](https://github.com/ezsystems/ezplatform-richtext) field type.

_Note: This Field Type supports editing via Platform UI v1 / Admin UI v2, however only as raw (simplified) xml. There has currently not been any attempts at getting Online Editor from legacy extension to work with within Platform UI, to do that among other things someone would need to port the custom html to xml handler from oe extension to this field type. So this Field Type is mainly meant for use for migrating to RichText, see below._

### Support limitations

- 2.x: For eZ Platform v3, this bundle is **only** supported for the purpose of migrating content from XmlText to RichText field type
- 1.x: For eZ Platform v1 & v2, this bundle is supported for two use cases: Migration to Richtext, & rendering via Platform frontend\*.

_* While it does support editing via Platform Admin UI it is only editable as raw xmltext in a textbox, so supported use as of this field type is for temprary upgrde scenarios where legacy admin interface is used, and frontend code is being developed/migrated to eZ Platform stack (including to Symfony)._


## Installation
Expand Down
16 changes: 9 additions & 7 deletions bundle/Command/ConvertXmlTextToRichTextCommand.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\EzPlatformXmlTextFieldTypeBundle\Command;

use Doctrine\DBAL\FetchMode;
use DOMDocument;
use ErrorException;
use eZ\Publish\API\Repository\Repository;
use eZ\Publish\Core\FieldType\XmlText\Converter\RichText as RichTextConverter;
use eZ\Publish\Core\FieldType\XmlText\Persistence\Legacy\ContentModelGateway as Gateway;
use eZ\Publish\Core\FieldType\XmlText\Value;
use PDO;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Symfony\Component\Console\Command\Command;
Expand All @@ -19,7 +22,6 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Debug\Exception\ContextErrorException;
use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Component\Process\Process;

Expand Down Expand Up @@ -385,7 +387,7 @@ protected function getContentTypeIds($contentTypeIdentifiers)

$statement = $query->execute();

$columns = $statement->fetchAll(PDO::FETCH_ASSOC);
$columns = $statement->fetchAll(FetchMode::ASSOCIATIVE);
$result = [];
foreach ($columns as $column) {
$result[$column['identifier']] = $column['id'];
Expand Down Expand Up @@ -414,7 +416,7 @@ protected function fixEmbeddedImages($dryRun, $contentId, OutputInterface $outpu
$limit = self::MAX_OBJECTS_PER_CHILD;

$statement = $this->gateway->getFieldRows('ezrichtext', $contentId, $offset, $limit);
while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
while ($row = $statement->fetch(FetchMode::ASSOCIATIVE)) {
if (empty($row['data_text'])) {
$inputValue = Value::EMPTY_VALUE;
} else {
Expand Down Expand Up @@ -623,7 +625,7 @@ protected function dumpOnErrors($errors, $dataText, $contentobjectId, $contentob
protected function convertFields($dryRun, $contentId, $checkDuplicateIds, $checkIdValues, $offset, $limit)
{
$statement = $this->gateway->getFieldRows(['ezxmltext', 'ezrichtext'], $contentId, $offset, $limit);
while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
while ($row = $statement->fetch(FetchMode::ASSOCIATIVE)) {
if ($row['data_type_string'] === 'ezrichtext') {
continue;
}
Expand Down Expand Up @@ -731,13 +733,13 @@ protected function createDocument($xmlString)
$document->preserveWhiteSpace = false;
$document->formatOutput = false;

// In dev mode, symfony may throw Symfony\Component\Debug\Exception\ContextErrorException
// In dev mode, symfony may throw \ErrorException
try {
$result = $document->loadXml($xmlString);
if ($result === false) {
throw new RuntimeException('Unable to parse ezxmltext. Invalid XML format');
}
} catch (ContextErrorException $e) {
} catch (ErrorException $e) {
throw new RuntimeException($e->getMessage(), $e->getCode());
}

Expand Down
6 changes: 3 additions & 3 deletions bundle/Command/ImportXmlCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace EzSystems\EzPlatformXmlTextFieldTypeBundle\Command;

use DOMDocument;
use ErrorException;
use eZ\Publish\Core\FieldType\XmlText\Converter\RichText as RichTextConverter;
use eZ\Publish\Core\FieldType\XmlText\Persistence\Legacy\ContentModelGateway as Gateway;
use Psr\Log\LogLevel;
Expand All @@ -13,7 +14,6 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Debug\Exception\ContextErrorException;

class ImportXmlCommand extends Command
{
Expand Down Expand Up @@ -205,13 +205,13 @@ protected function createDocument($xmlString)
$document->preserveWhiteSpace = false;
$document->formatOutput = false;

// In dev mode, symfony may throw Symfony\Component\Debug\Exception\ContextErrorException
// In dev mode, symfony may throw \ErrorException
try {
$result = $document->loadXml($xmlString);
if ($result === false) {
throw new RuntimeException('Unable to parse ezxmltext. Invalid XML format');
}
} catch (ContextErrorException $e) {
} catch (ErrorException $e) {
throw new RuntimeException($e->getMessage(), $e->getCode());
}

Expand Down
8 changes: 3 additions & 5 deletions lib/FieldType/XmlText/Converter/RichText.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php

/**
* This file is part of the eZ Platform XmlText Field Type package.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
Expand All @@ -12,6 +10,7 @@
use DOMElement;
use DOMNode;
use DOMXPath;
use ErrorException;
use eZ\Publish\API\Repository\Repository;
use eZ\Publish\Core\Base\Exceptions\NotFoundException;
use eZ\Publish\Core\FieldType\XmlText\Converter;
Expand All @@ -21,7 +20,6 @@
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Psr\Log\NullLogger;
use Symfony\Component\Debug\Exception\ContextErrorException;

class RichText implements Converter
{
Expand Down Expand Up @@ -614,15 +612,15 @@ public function convert(DOMDocument $inputDocument, $checkDuplicateIds = false,
// Needed by some disabled output escaping (eg. legacy ezxml paragraph <line/> elements)
$convertedDocumentNormalized = new DOMDocument();
try {
// If env=dev, Symfony will throw ContextErrorException on line below if xml is invalid
// If env=dev, Symfony will throw \ErrorException on line below if xml is invalid
$result = $convertedDocumentNormalized->loadXML($convertedDocument->saveXML());
if ($result === false) {
$this->log(LogLevel::ERROR,
"Unable to convert ezmltext for contentobject_attribute.id=$contentFieldId",
['result' => $convertedDocument->saveXML(), 'errors' => ['Unable to parse converted richtext output. See warning in logs or use --env=dev in order to se more verbose output.'], 'xmlString' => $inputDocument->saveXML()]
);
}
} catch (ContextErrorException $e) {
} catch (ErrorException $e) {
$this->log(LogLevel::ERROR,
"Unable to convert ezmltext for contentobject_attribute.id=$contentFieldId",
['result' => $convertedDocument->saveXML(), 'errors' => [$e->getMessage()], 'xmlString' => $inputDocument->saveXML()]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace eZ\Publish\Core\FieldType\XmlText\Persistence\Legacy;

use Doctrine\DBAL\Connection;
use PDO;
use Doctrine\DBAL\FetchMode;

class ContentModelGateway
{
Expand Down Expand Up @@ -35,7 +37,7 @@ public function getContentTypeIds($contentTypeIdentifiers)

$statement = $query->execute();

$columns = $statement->fetchAll(PDO::FETCH_ASSOC);
$columns = $statement->fetchAll(FetchMode::ASSOCIATIVE);
$result = [];
foreach ($columns as $column) {
$result[$column['identifier']] = $column['id'];
Expand Down
2 changes: 1 addition & 1 deletion lib/FieldType/XmlText/SearchField.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private function extractText(DOMNode $node)
{
$text = '';

if ($node->childNodes !== null && $node->childNodes->length > 0) {
if ($node->childNodes !== null && $node->childNodes->count() > 0) {
foreach ($node->childNodes as $child) {
$text .= $this->extractText($child);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
<?php

/**
* This file is part of the eZ Platform XmlText Field Type package.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*
* @version //autogentag//
*/
namespace eZ\Publish\Core\FieldType\XmlText\XmlTextStorage\Gateway;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\FetchMode;
use DOMDocument;
use eZ\Publish\Core\Base\Exceptions\NotFoundException;
use eZ\Publish\Core\FieldType\Url\UrlStorage\Gateway as UrlGateway;
use eZ\Publish\Core\FieldType\XmlText\XmlTextStorage\Gateway;
use eZ\Publish\SPI\Persistence\Content\Field;
use eZ\Publish\SPI\Persistence\Content\VersionInfo;
use PDO;

class DoctrineStorage extends Gateway
{
Expand Down Expand Up @@ -223,7 +219,7 @@ protected function getObjectId(array $linksRemoteIds)
);

$statement = $q->execute();
foreach ($statement->fetchAll(PDO::FETCH_ASSOC) as $row) {
foreach ($statement->fetchAll(FetchMode::ASSOCIATIVE) as $row) {
$objectRemoteIdMap[$row['remote_id']] = $row['id'];
}
}
Expand Down
77 changes: 13 additions & 64 deletions tests/lib/FieldType/Persistence/Legacy/BaseTest.php
Original file line number Diff line number Diff line change
@@ -1,82 +1,31 @@
<?php

/**
* This file is part of the eZ Platform XmlText Field Type package.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace EzSystems\EzPlatformXmlTextFieldType\Tests\FieldType\Persistence\Legacy;

use Doctrine\DBAL\DBALException;
use ErrorException;
use eZ\Publish\API\Repository\Tests\BaseTest as APIBaseTest;
use eZ\Publish\SPI\Tests\Persistence\FileFixtureFactory;
use eZ\Publish\SPI\Tests\Persistence\FixtureImporter;

abstract class BaseTest extends APIBaseTest
{
/**
* Taken from ezplatform-kernel/eZ/Publish/Core/Persistence/Legacy/Tests/TestCase.php.
*
* @param string $file
* @throws \Exception
*/
protected function insertDatabaseFixture($file)
protected function insertDatabaseFixture(string $file): void
{
$data = require $file;
$db = $this->getSetupFactory()->getDatabaseHandler();

foreach ($data as $table => $rows) {
// Check that at least one row exists
if (!isset($rows[0])) {
continue;
}

$q = $db->createInsertQuery();
$q->insertInto($db->quoteIdentifier($table));

// Contains the bound parameters
$values = [];

// Binding the parameters
foreach ($rows[0] as $col => $val) {
$q->set(
$db->quoteIdentifier($col),
$q->bindParam($values[$col])
);
}

$stmt = $q->prepare();

foreach ($rows as $row) {
try {
// This CANNOT be replaced by:
// $values = $row
// each $values[$col] is a PHP reference which should be
// kept for parameters binding to work
foreach ($row as $col => $val) {
$values[$col] = $val;
}

$stmt->execute();
} catch (Exception $e) {
echo "$table ( ", implode(', ', $row), " )\n";
throw $e;
}
}
}

$this->resetSequences();
}

public function resetSequences()
{
switch ($this->getDB()) {
case 'pgsql':
// Update PostgreSQL sequences
$handler = $this->getSetupFactory()->getDatabaseHandler();

$queries = array_filter(preg_split('(;\\s*$)m',
file_get_contents(__DIR__ . '/_fixtures/setval.pgsql.sql')));
foreach ($queries as $query) {
$handler->exec($query);
}
try {
$fixtureImporter = new FixtureImporter($this->getRawDatabaseConnection());
$fixtureImporter->import((new FileFixtureFactory())->buildFixture($file));
} catch (ErrorException | DBALException $e) {
self::fail('Database fixture import failed: ' . $e->getMessage());
}
}
}
Loading

0 comments on commit d0a83c0

Please sign in to comment.