From 154e386934224619fa1402beabf66a940b484688 Mon Sep 17 00:00:00 2001 From: Alex Pott <alex.a.pott@googlemail.com> Date: Wed, 9 Dec 2020 14:18:23 +0000 Subject: [PATCH] Issue #3148959 by Kumar Kundan, benjifisher, heddn, kishor_kolekar, ayushmishra206, alexpott, quietone: Improve migrate messages from the extract plugin --- .../src/Plugin/migrate/process/Extract.php | 6 ++- .../tests/src/Unit/process/ExtractTest.php | 54 +++++++++++++++++-- 2 files changed, 54 insertions(+), 6 deletions(-) diff --git a/core/modules/migrate/src/Plugin/migrate/process/Extract.php b/core/modules/migrate/src/Plugin/migrate/process/Extract.php index 380a7364ab05..4af44f40fd24 100644 --- a/core/modules/migrate/src/Plugin/migrate/process/Extract.php +++ b/core/modules/migrate/src/Plugin/migrate/process/Extract.php @@ -3,6 +3,7 @@ namespace Drupal\migrate\Plugin\migrate\process; use Drupal\Component\Utility\NestedArray; +use Drupal\Component\Utility\Variable; use Drupal\migrate\ProcessPluginBase; use Drupal\migrate\MigrateException; use Drupal\migrate\MigrateExecutableInterface; @@ -68,15 +69,16 @@ class Extract extends ProcessPluginBase { */ public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { if (!is_array($value)) { - throw new MigrateException('Input should be an array.'); + throw new MigrateException(sprintf("Input should be an array, instead it was of type '%s'", gettype($value))); } $new_value = NestedArray::getValue($value, $this->configuration['index'], $key_exists); + if (!$key_exists) { if (array_key_exists('default', $this->configuration)) { $new_value = $this->configuration['default']; } else { - throw new MigrateException('Array index missing, extraction failed.'); + throw new MigrateException(sprintf("Array index missing, extraction failed for '%s'. Consider adding a `default` key to the configuration.", Variable::export($value))); } } return $new_value; diff --git a/core/modules/migrate/tests/src/Unit/process/ExtractTest.php b/core/modules/migrate/tests/src/Unit/process/ExtractTest.php index 6dbcecbb6407..9972ecf9f478 100644 --- a/core/modules/migrate/tests/src/Unit/process/ExtractTest.php +++ b/core/modules/migrate/tests/src/Unit/process/ExtractTest.php @@ -30,11 +30,14 @@ public function testExtract() { /** * Tests invalid input. + * + * @dataProvider providerTestExtractInvalid */ - public function testExtractFromString() { + public function testExtractInvalid($value) { $this->expectException(MigrateException::class); - $this->expectExceptionMessage('Input should be an array.'); - $this->plugin->transform('bar', $this->migrateExecutable, $this->row, 'destination_property'); + $type = gettype($value); + $this->expectExceptionMessage(sprintf("Input should be an array, instead it was of type '%s'", $type)); + $this->plugin->transform($value, $this->migrateExecutable, $this->row, 'destination_property'); } /** @@ -42,7 +45,7 @@ public function testExtractFromString() { */ public function testExtractFail() { $this->expectException(MigrateException::class); - $this->expectExceptionMessage('Array index missing, extraction failed.'); + $this->expectExceptionMessage("Array index missing, extraction failed for 'array(\n 'bar' => 'foo',\n)'. Consider adding a `default` key to the configuration."); $this->plugin->transform(['bar' => 'foo'], $this->migrateExecutable, $this->row, 'destination_property'); } @@ -132,4 +135,47 @@ public function providerExtractDefault() { ]; } + /** + * Provides data for the testExtractInvalid. + */ + public function providerTestExtractInvalid() { + $xml_str = <<<XML + <xml version='1.0'?> + <authors> + <name>Test Extract Invalid</name> + </authors> + XML; + $object = (object) [ + 'one' => 'test1', + 'two' => 'test2', + 'three' => 'test3', + ]; + return [ + 'empty string' => [ + '', + ], + 'string' => [ + 'Extract Test', + ], + 'integer' => [ + 1, + ], + 'float' => [ + 1.0, + ], + 'NULL' => [ + NULL, + ], + 'boolean' => [ + TRUE, + ], + 'xml' => [ + $xml_str, + ], + 'object' => [ + $object, + ], + ]; + } + } -- GitLab