diff --git a/core/modules/migrate/src/Plugin/migrate/process/Flatten.php b/core/modules/migrate/src/Plugin/migrate/process/Flatten.php index 324f0b92c8..c38af71134 100644 --- a/core/modules/migrate/src/Plugin/migrate/process/Flatten.php +++ b/core/modules/migrate/src/Plugin/migrate/process/Flatten.php @@ -2,6 +2,7 @@ namespace Drupal\migrate\Plugin\migrate\process; +use Drupal\migrate\MigrateException; use Drupal\migrate\MigrateExecutableInterface; use Drupal\migrate\ProcessPluginBase; use Drupal\migrate\Row; @@ -49,6 +50,9 @@ class Flatten extends ProcessPluginBase { * For example, [[1, 2, [3, 4]]] becomes [1, 2, 3, 4]. */ public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { + if (!is_array($value) && !is_object($value)) { + throw new MigrateException(sprintf('Input should be an array or an object, instead it was %s', var_export($value, TRUE))); + } return iterator_to_array(new \RecursiveIteratorIterator(new \RecursiveArrayIterator($value)), FALSE); } diff --git a/core/modules/migrate/tests/src/Unit/process/FlattenTest.php b/core/modules/migrate/tests/src/Unit/process/FlattenTest.php index 899644f5ae..dc71dc7d94 100644 --- a/core/modules/migrate/tests/src/Unit/process/FlattenTest.php +++ b/core/modules/migrate/tests/src/Unit/process/FlattenTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\migrate\Unit\process; +use Drupal\migrate\MigrateException; use Drupal\migrate\Plugin\migrate\process\Flatten; /** @@ -11,13 +12,89 @@ */ class FlattenTest extends MigrateProcessTestCase { + /** + * {@inheritdoc} + */ + protected function setUp(): void { + $this->plugin = new Flatten([], 'flatten', []); + parent::setUp(); + } + /** * Test that various array flatten operations work properly. + * + * @dataProvider providerTestFlatten + */ + public function testFlatten($value, $expected) { + $flattened = $this->plugin->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty'); + $this->assertSame($expected, $flattened); + } + + /** + * Provides data for the testFlatten. + */ + public function providerTestFlatten() { + $object = (object) [ + 'a' => 'test', + 'b' => '1.2', + 'c' => 'NULL', + ]; + return [ + 'array' => [ + [1, 2, [3, 4, [5]], [], [7, 8]], + [1, 2, 3, 4, 5, 7, 8], + ], + 'object' => [ + $object, + ['test', '1.2', 'NULL'], + ], + ]; + } + + /** + * Tests that Flatten throws a MigrateException. + * + * @dataProvider providerTestFlattenInvalid + */ + public function testFlattenInvalid($value) { + $this->expectException(MigrateException::class); + $this->expectExceptionMessage(sprintf('Input should be an array or an object, instead it was %s', print_r($value, TRUE))); + $this->plugin->transform($value, $this->migrateExecutable, $this->row, 'destination_property'); + } + + /** + * Provides data for the testFlattenInvalid. */ - public function testFlatten() { - $plugin = new Flatten([], 'flatten', []); - $flattened = $plugin->transform([1, 2, [3, 4, [5]], [], [7, 8]], $this->migrateExecutable, $this->row, 'destination_property'); - $this->assertSame([1, 2, 3, 4, 5, 7, 8], $flattened); + public function providerTestFlattenInvalid() { + $xml_str = << + + Tamora Pierce + +XML; + return [ + 'empty string' => [ + '', + ], + 'string' => [ + 'Kate Sheppard', + ], + 'integer' => [ + 1, + ], + 'float' => [ + 1.2, + ], + 'NULL' => [ + NULL, + ], + 'boolean' => [ + TRUE, + ], + 'xml' => [ + $xml_str, + ], + ]; } }