Commit 87e10018 authored by webchick's avatar webchick

Issue #2147817 by chx: Migrate: add a static map process plugin.

parent 358b5d65
<?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;
}
}
<?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');
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment