diff --git a/core/modules/migrate/src/Plugin/migrate/process/ArrayMap.php b/core/modules/migrate/src/Plugin/migrate/process/ArrayMap.php index 022d09b..da06bee 100644 --- a/core/modules/migrate/src/Plugin/migrate/process/ArrayMap.php +++ b/core/modules/migrate/src/Plugin/migrate/process/ArrayMap.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; @@ -23,6 +24,21 @@ public function transform($value, MigrateExecutableInterface $migrate_executable $new_value = []; foreach ((array) $value as $old_key => $old_value) { + // Checks that $old_value is an array. + if (!is_array($old_value)) { + throw new MigrateException("The input should be an array of arrays."); + } + + // Checks that the key exists. + if (!array_key_exists($this->configuration['key'], $old_value)) { + throw new MigrateException("The key '" . $this->configuration['key'] . "' does not exist."); + } + + // Checks that the value exists + if (!array_key_exists($this->configuration['value'], $old_value)) { + throw new MigrateException("The key '" . $this->configuration['value'] . "' does not exist."); + } + $new_value[$old_value[$this->configuration['key']]] = $old_value[$this->configuration['value']]; } diff --git a/core/modules/migrate/tests/src/Unit/process/ArrayMapTest.php b/core/modules/migrate/tests/src/Unit/process/ArrayMapTest.php new file mode 100644 index 0000000..b5953ed --- /dev/null +++ b/core/modules/migrate/tests/src/Unit/process/ArrayMapTest.php @@ -0,0 +1,89 @@ + 'foo', + 'value' => 'bar', + ]; + $this->plugin = new ArrayMap($configuration, 'map', array()); + parent::setUp(); + } + + /** + * Tests successful mapping. + */ + public function testExtract() { + $source = [ + ['foo' => 'Foo', 'bar' => 'Bar'], + ['foo' => 'foo bar', 'bar' => 'bar foo'], + ]; + $expected = [ + 'Foo' => 'Bar', + 'foo bar' => 'bar foo', + ]; + $value = $this->plugin->transform($source, $this->migrateExecutable, $this->row, 'destinationproperty'); + $this->assertSame($value, $expected); + } + + /** + * Tests non-existent key for the mapping key. + * + * @expectedException \Drupal\migrate\MigrateException + * @expectedExceptionMessage The key 'foo' does not exist. + */ + public function testNonExistentKey() { + $source = [ + ['bar' => 'foo'], + ]; + $this->plugin->transform($source, $this->migrateExecutable, $this->row, 'destinationproperty'); + } + + /** + * Tests non-existent key for the mapping value. + * + * @expectedException \Drupal\migrate\MigrateException + * @expectedExceptionMessage The key 'bar' does not exist. + */ + public function testNonExistentValue() { + $source = [ + ['foo' => 'bar'], + ]; + $this->plugin->transform($source, $this->migrateExecutable, $this->row, 'destinationproperty'); + } + + /** + * Tests one-dimensional array input. + * + * @expectedException \Drupal\migrate\MigrateException + * @expectedExceptionMessage The input should be an array of arrays. + */ + public function testOneDimensionalArrayInput() { + $source = ['foo' => 'bar']; + $this->plugin->transform($source, $this->migrateExecutable, $this->row, 'destinationproperty'); + } + + /** + * Tests string input. + * + * @expectedException \Drupal\migrate\MigrateException + * @expectedExceptionMessage The input should be an array of arrays. + */ + public function testStringInput() { + $source = 'foo'; + $this->plugin->transform($source, $this->migrateExecutable, $this->row, 'destinationproperty'); + } + +}