3 namespace Drupal\Tests\migrate\Unit\process;
5 use Drupal\migrate\MigrateException;
6 use Drupal\migrate\MigrateSkipRowException;
7 use Drupal\migrate\Plugin\migrate\process\StaticMap;
10 * Tests the static map process plugin.
14 class StaticMapTest extends MigrateProcessTestCase {
19 protected function setUp() {
20 $configuration['map']['foo']['bar'] = 'baz';
21 $this->plugin = new StaticMap($configuration, 'map', []);
26 * Tests map when the source is a string.
28 public function testMapWithSourceString() {
29 $value = $this->plugin->transform('foo', $this->migrateExecutable, $this->row, 'destinationproperty');
30 $this->assertSame(['bar' => 'baz'], $value);
34 * Tests map when the source is a list.
36 public function testMapWithSourceList() {
37 $value = $this->plugin->transform(['foo', 'bar'], $this->migrateExecutable, $this->row, 'destinationproperty');
38 $this->assertSame('baz', $value);
42 * Tests when the source is empty.
44 public function testMapwithEmptySource() {
45 $this->setExpectedException(MigrateException::class);
46 $this->plugin->transform([], $this->migrateExecutable, $this->row, 'destinationproperty');
50 * Tests when the source is invalid.
52 public function testMapwithInvalidSource() {
53 $this->setExpectedException(MigrateSkipRowException::class);
54 $this->plugin->transform(['bar'], $this->migrateExecutable, $this->row, 'destinationproperty');
58 * Tests when the source is invalid but there's a default.
60 public function testMapWithInvalidSourceWithADefaultValue() {
61 $configuration['map']['foo']['bar'] = 'baz';
62 $configuration['default_value'] = 'test';
63 $this->plugin = new StaticMap($configuration, 'map', []);
64 $value = $this->plugin->transform(['bar'], $this->migrateExecutable, $this->row, 'destinationproperty');
65 $this->assertSame('test', $value);
69 * Tests when the source is invalid but there's a default value of NULL.
71 public function testMapWithInvalidSourceWithANullDefaultValue() {
72 $configuration['map']['foo']['bar'] = 'baz';
73 $configuration['default_value'] = NULL;
74 $this->plugin = new StaticMap($configuration, 'map', []);
75 $value = $this->plugin->transform(['bar'], $this->migrateExecutable, $this->row, 'destinationproperty');
76 $this->assertNull($value);
80 * Tests when the source is invalid and bypass is enabled.
82 public function testMapWithInvalidSourceAndBypass() {
83 $configuration['map']['foo']['bar'] = 'baz';
84 $configuration['default_value'] = 'test';
85 $configuration['bypass'] = TRUE;
86 $this->plugin = new StaticMap($configuration, 'map', []);
87 $this->setExpectedException(MigrateException::class, 'Setting both default_value and bypass is invalid.');
88 $this->plugin->transform(['bar'], $this->migrateExecutable, $this->row, 'destinationproperty');