From aa94a6998142c89ae7366a7d96331018c89b582d Mon Sep 17 00:00:00 2001 From: Matroskeen <Matroskeen@3426249.no-reply.drupal.org> Date: Fri, 22 Apr 2022 14:54:05 +0300 Subject: [PATCH] Issue #3112571 by marvil07, Matroskeen: Support xml parsing on dom plugin --- src/Plugin/migrate/process/Dom.php | 9 +++++++-- tests/src/Unit/process/DomTest.php | 16 ++++++++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/Plugin/migrate/process/Dom.php b/src/Plugin/migrate/process/Dom.php index aa3051d8..8de8730e 100644 --- a/src/Plugin/migrate/process/Dom.php +++ b/src/Plugin/migrate/process/Dom.php @@ -34,6 +34,7 @@ use Masterminds\HTML5; * - import_method: (optional) What parser to use. Possible values: * - 'html': (default) use dom extension parsing. * - 'html5': use html5 parsing. + * - 'xml': use XML parsing. * * @codingStandardsIgnoreStart * @@ -100,8 +101,8 @@ class Dom extends ProcessPluginBase { throw new \InvalidArgumentException('The "method" must be "import" or "export".'); } $configuration['import_method'] = $configuration['import_method'] ?? 'html'; - if (!in_array($configuration['import_method'], ['html', 'html5'])) { - throw new \InvalidArgumentException('The "import_method" must be "html" or "html5".'); + if (!in_array($configuration['import_method'], ['html', 'html5', 'xml'])) { + throw new \InvalidArgumentException('The "import_method" must be "html", "html5", or "xml".'); } parent::__construct($configuration, $plugin_id, $plugin_definition); $this->configuration += $this->defaultValues(); @@ -176,6 +177,10 @@ class Dom extends ProcessPluginBase { $html5->loadHTML($html); break; + case 'xml': + $document->loadXML($html); + break; + case 'html': default: $document->loadHTML($html); diff --git a/tests/src/Unit/process/DomTest.php b/tests/src/Unit/process/DomTest.php index 6fb9f383..a90c31b4 100644 --- a/tests/src/Unit/process/DomTest.php +++ b/tests/src/Unit/process/DomTest.php @@ -45,11 +45,11 @@ final class DomTest extends MigrateProcessTestCase { /** * @covers ::__construct */ - public function testInvalidImportMethod() { + public function testInvalidImportMethod(): void { $configuration['method'] = 'import'; $configuration['import_method'] = 'invalid'; $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('The "import_method" must be "html" or "html5".'); + $this->expectExceptionMessage('The "import_method" must be "html", "html5", or "xml".'); (new Dom($configuration, 'dom', [])); } @@ -77,6 +77,18 @@ final class DomTest extends MigrateProcessTestCase { $this->assertTrue($document instanceof \DOMDocument); } + /** + * @covers ::import + */ + public function testImportMethodXml(): void { + $configuration['method'] = 'import'; + $configuration['import_method'] = 'xml'; + $value = '<item><value>A simple paragraph.</value></item>'; + $document = (new Dom($configuration, 'dom', [])) + ->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty'); + $this->assertTrue($document instanceof \DOMDocument); + } + /** * @covers ::import */ -- GitLab