Issue #3048464 by danflanagan8, hctom, quietone, mikelutz, heddn: SubProcess migrate process plugin should throw exception on invalid input

merge-requests/1733/head
Alex Pott 2022-01-27 03:07:47 +00:00
parent 4c6d65d3b0
commit bcb89890b2
No known key found for this signature in database
GPG Key ID: BDA67E7EE836E5CE
2 changed files with 54 additions and 2 deletions

View File

@ -2,6 +2,7 @@
namespace Drupal\migrate\Plugin\migrate\process;
use Drupal\migrate\MigrateException;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Row;
@ -203,6 +204,9 @@ class SubProcess extends ProcessPluginBase {
if (is_array($value) || $value instanceof \Traversable) {
foreach ($value as $key => $new_value) {
if (!is_array($new_value)) {
throw new MigrateException(sprintf("Input array should hold elements of type array, instead element was of type '%s'", gettype($new_value)));
}
$new_row = new Row($new_value + $source);
$migrate_executable->processRow($new_row, $this->configuration['process']);
$destination = $new_row->getDestination();

View File

@ -2,12 +2,12 @@
namespace Drupal\Tests\migrate\Unit\process;
use Drupal\migrate\MigrateException;
use Drupal\migrate\MigrateExecutable;
use Drupal\migrate\MigrateMessageInterface;
use Drupal\migrate\Plugin\migrate\process\Get;
use Drupal\migrate\Plugin\migrate\process\SubProcess;
use Drupal\migrate\Row;
use Drupal\Tests\migrate\Unit\MigrateTestCase;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
// cspell:ignore baaa
@ -17,7 +17,7 @@ use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
*
* @group migrate
*/
class SubProcessTest extends MigrateTestCase {
class SubProcessTest extends MigrateProcessTestCase {
/**
* The sub_process plugin being tested.
@ -193,4 +193,52 @@ class SubProcessTest extends MigrateTestCase {
];
}
/**
* Tests behavior when source children are not arrays.
*
* @dataProvider providerTestSourceNotArray
*/
public function testSourceNotArray($source_values, $type) {
$process = new SubProcess(['process' => ['foo' => 'source_foo']], 'sub_process', []);
$this->expectException(MigrateException::class);
$this->expectExceptionMessage("Input array should hold elements of type array, instead element was of type '$type'");
$process->transform($source_values, $this->migrateExecutable, $this->row, 'destination_property');
}
/**
* Data provider for testSourceNotArray().
*/
public function providerTestSourceNotArray() {
return [
'strings cannot be subprocess items' => [
['strings', 'cannot', 'be', 'children'],
'string',
],
'xml elements cannot be subprocess items' => [
[new \SimpleXMLElement("<element>Content</element>")],
'object',
],
'integers cannot be subprocess items' => [
[1, 2, 3, 4],
'integer',
],
'booleans cannot be subprocess items' => [
[TRUE, FALSE],
'boolean',
],
'null cannot be subprocess items' => [
[NULL],
'NULL',
],
'iterator cannot be subprocess items' => [
[new \ArrayIterator(['some', 'array'])],
'object',
],
'all subprocess items must be arrays' => [
[['array'], 'not array'],
'string',
],
];
}
}