Issue #3148959 by Kumar Kundan, benjifisher, heddn, kishor_kolekar, ayushmishra206, alexpott, quietone: Improve migrate messages from the extract plugin

merge-requests/123/head
Alex Pott 2020-12-09 14:18:23 +00:00
parent ab50d1b243
commit 154e386934
No known key found for this signature in database
GPG Key ID: 31905460D4A69276
2 changed files with 54 additions and 6 deletions

View File

@ -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;

View File

@ -30,11 +30,14 @@ class ExtractTest extends MigrateProcessTestCase {
/**
* 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 @@ class ExtractTest extends MigrateProcessTestCase {
*/
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 @@ class ExtractTest extends MigrateProcessTestCase {
];
}
/**
* 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,
],
];
}
}