3 namespace Drupal\Tests\migrate\Unit\process;
5 use Drupal\Component\Utility\Variable;
6 use Drupal\migrate\MigrateException;
7 use Drupal\migrate\MigrateSkipRowException;
8 use Drupal\migrate\Plugin\migrate\process\StaticMap;
11 * Tests the static map process plugin.
15 class StaticMapTest extends MigrateProcessTestCase {
20 protected function setUp() {
21 $configuration['map']['foo']['bar'] = 'baz';
22 $this->plugin = new StaticMap($configuration, 'map', []);
27 * Tests map when the source is a string.
29 public function testMapWithSourceString() {
30 $value = $this->plugin->transform('foo', $this->migrateExecutable, $this->row, 'destinationproperty');
31 $this->assertSame(['bar' => 'baz'], $value);
35 * Tests map when the source is a list.
37 public function testMapWithSourceList() {
38 $value = $this->plugin->transform(['foo', 'bar'], $this->migrateExecutable, $this->row, 'destinationproperty');
39 $this->assertSame('baz', $value);
43 * Tests when the source is empty.
45 public function testMapwithEmptySource() {
46 $this->setExpectedException(MigrateException::class);
47 $this->plugin->transform([], $this->migrateExecutable, $this->row, 'destinationproperty');
51 * Tests when the source is invalid.
53 public function testMapwithInvalidSource() {
54 $this->setExpectedException(MigrateSkipRowException::class, sprintf("No static mapping found for '%s' and no default value provided for destination '%s'.", Variable::export(['bar']), 'destinationproperty'));
55 $this->plugin->transform(['bar'], $this->migrateExecutable, $this->row, 'destinationproperty');
59 * Tests when the source is invalid but there's a default.
61 public function testMapWithInvalidSourceWithADefaultValue() {
62 $configuration['map']['foo']['bar'] = 'baz';
63 $configuration['default_value'] = 'test';
64 $this->plugin = new StaticMap($configuration, 'map', []);
65 $value = $this->plugin->transform(['bar'], $this->migrateExecutable, $this->row, 'destinationproperty');
66 $this->assertSame('test', $value);
70 * Tests when the source is invalid but there's a default value of NULL.
72 public function testMapWithInvalidSourceWithANullDefaultValue() {
73 $configuration['map']['foo']['bar'] = 'baz';
74 $configuration['default_value'] = NULL;
75 $this->plugin = new StaticMap($configuration, 'map', []);
76 $value = $this->plugin->transform(['bar'], $this->migrateExecutable, $this->row, 'destinationproperty');
77 $this->assertNull($value);
81 * Tests when the source is invalid and bypass is enabled.
83 public function testMapWithInvalidSourceAndBypass() {
84 $configuration['map']['foo']['bar'] = 'baz';
85 $configuration['default_value'] = 'test';
86 $configuration['bypass'] = TRUE;
87 $this->plugin = new StaticMap($configuration, 'map', []);
88 $this->setExpectedException(MigrateException::class, 'Setting both default_value and bypass is invalid.');
89 $this->plugin->transform(['bar'], $this->migrateExecutable, $this->row, 'destinationproperty');