Skip to content
Snippets Groups Projects
Unverified Commit 154e3869 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3148959 by Kumar Kundan, benjifisher, heddn, kishor_kolekar,...

Issue #3148959 by Kumar Kundan, benjifisher, heddn, kishor_kolekar, ayushmishra206, alexpott, quietone: Improve migrate messages from the extract plugin
parent ab50d1b2
No related branches found
No related tags found
No related merge requests found
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
namespace Drupal\migrate\Plugin\migrate\process; namespace Drupal\migrate\Plugin\migrate\process;
use Drupal\Component\Utility\NestedArray; use Drupal\Component\Utility\NestedArray;
use Drupal\Component\Utility\Variable;
use Drupal\migrate\ProcessPluginBase; use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\MigrateException; use Drupal\migrate\MigrateException;
use Drupal\migrate\MigrateExecutableInterface; use Drupal\migrate\MigrateExecutableInterface;
...@@ -68,15 +69,16 @@ class Extract extends ProcessPluginBase { ...@@ -68,15 +69,16 @@ class Extract extends ProcessPluginBase {
*/ */
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
if (!is_array($value)) { 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); $new_value = NestedArray::getValue($value, $this->configuration['index'], $key_exists);
if (!$key_exists) { if (!$key_exists) {
if (array_key_exists('default', $this->configuration)) { if (array_key_exists('default', $this->configuration)) {
$new_value = $this->configuration['default']; $new_value = $this->configuration['default'];
} }
else { 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; return $new_value;
......
...@@ -30,11 +30,14 @@ public function testExtract() { ...@@ -30,11 +30,14 @@ public function testExtract() {
/** /**
* Tests invalid input. * Tests invalid input.
*
* @dataProvider providerTestExtractInvalid
*/ */
public function testExtractFromString() { public function testExtractInvalid($value) {
$this->expectException(MigrateException::class); $this->expectException(MigrateException::class);
$this->expectExceptionMessage('Input should be an array.'); $type = gettype($value);
$this->plugin->transform('bar', $this->migrateExecutable, $this->row, 'destination_property'); $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() { ...@@ -42,7 +45,7 @@ public function testExtractFromString() {
*/ */
public function testExtractFail() { public function testExtractFail() {
$this->expectException(MigrateException::class); $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'); $this->plugin->transform(['bar' => 'foo'], $this->migrateExecutable, $this->row, 'destination_property');
} }
...@@ -132,4 +135,47 @@ public function providerExtractDefault() { ...@@ -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,
],
];
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment