diff --git a/core/modules/migrate/lib/Drupal/migrate/Plugin/migrate/process/StaticMap.php b/core/modules/migrate/lib/Drupal/migrate/Plugin/migrate/process/StaticMap.php new file mode 100644 index 0000000000000000000000000000000000000000..863e41c04224efaace07831c604d9e9a2a220944 --- /dev/null +++ b/core/modules/migrate/lib/Drupal/migrate/Plugin/migrate/process/StaticMap.php @@ -0,0 +1,44 @@ +<?php + +/** + * @file + * Contains \Drupal\migrate\Plugin\migrate\process\MultipleColumnsMap. + */ + +namespace Drupal\migrate\Plugin\migrate\process; + +use Drupal\Component\Utility\NestedArray; +use Drupal\Core\Plugin\PluginBase; +use Drupal\migrate\MigrateException; +use Drupal\migrate\MigrateExecutable; +use Drupal\migrate\Plugin\MigrateProcessInterface; +use Drupal\migrate\Row; + +/** + * This plugin changes the current value based on a static lookup map. + * + * @see https://drupal.org/node/2143521 + * + * @PluginId("static_map") + */ +class StaticMap extends PluginBase implements MigrateProcessInterface { + + /** + * {@inheritdoc} + */ + public function transform($value, MigrateExecutable $migrate_executable, Row $row, $destination_property) { + if (!is_array($value)) { + $value = array($value); + } + if (!$value) { + throw new MigrateException('Can not lookup without a value.'); + } + $new_value = NestedArray::getValue($this->configuration['map'], $value, $key_exists); + if (!$key_exists) { + throw new MigrateException('Lookup failed.'); + } + return $new_value; + } + +} + diff --git a/core/modules/migrate/tests/Drupal/migrate/Tests/process/StaticMapTest.php b/core/modules/migrate/tests/Drupal/migrate/Tests/process/StaticMapTest.php new file mode 100644 index 0000000000000000000000000000000000000000..5bd66cbc3664d67d3abfa9fb7bc9f0a4cf2822e3 --- /dev/null +++ b/core/modules/migrate/tests/Drupal/migrate/Tests/process/StaticMapTest.php @@ -0,0 +1,77 @@ +<?php +/** + * @file + * Contains \Drupal\migrate\Tests\process\StaticMapTest. + */ + +namespace Drupal\migrate\Tests\process; + +use Drupal\migrate\Plugin\migrate\process\StaticMap; + +/** + * @group migrate + * @group Drupal + */ +class StaticMapTest extends MigrateProcessTestCase { + + /** + * {@inheritdoc} + */ + public static function getInfo() { + return array( + 'name' => 'Map process plugin', + 'description' => 'Tests the map process plugin.', + 'group' => 'Migrate', + ); + } + + /** + * {@inheritdoc} + */ + function setUp() { + $this->row = $this->getMockBuilder('Drupal\migrate\Row') + ->disableOriginalConstructor() + ->getMock(); + $this->migrateExecutable = $this->getMockBuilder('Drupal\migrate\MigrateExecutable') + ->disableOriginalConstructor() + ->getMock(); + $configuration['map']['foo']['bar'] = 'baz'; + $this->plugin = new StaticMap($configuration, 'map', array()); + parent::setUp(); + } + + /** + * Tests map when the source is a string. + */ + function testMapWithSourceString() { + $value = $this->plugin->transform('foo', $this->migrateExecutable, $this->row, 'destinationproperty'); + $this->assertSame($value, array('bar' => 'baz')); + } + + /** + * Tests map when the source is a list. + */ + function testMapWithSourceList() { + $value = $this->plugin->transform(array('foo', 'bar'), $this->migrateExecutable, $this->row, 'destinationproperty'); + $this->assertSame($value, 'baz'); + } + + /** + * Tests when the source is empty. + * + * @expectedException \Drupal\migrate\MigrateException + */ + function testMapwithEmptySource() { + $this->plugin->transform(array(), $this->migrateExecutable, $this->row, 'destinationproperty'); + } + + /** + * Tests when the source is invalid. + * + * @expectedException \Drupal\migrate\MigrateException + */ + function testMapwithInvalidSource() { + $this->plugin->transform(array('bar'), $this->migrateExecutable, $this->row, 'destinationproperty'); + } + +}