1a9a61acd68a3c8115a26ed5be5a909bd9a44953
[yaffs-website] / web / core / modules / migrate / tests / src / Unit / process / StaticMapTest.php
1 <?php
2
3 namespace Drupal\Tests\migrate\Unit\process;
4
5 use Drupal\Component\Utility\Variable;
6 use Drupal\migrate\MigrateException;
7 use Drupal\migrate\MigrateSkipRowException;
8 use Drupal\migrate\Plugin\migrate\process\StaticMap;
9
10 /**
11  * Tests the static map process plugin.
12  *
13  * @group migrate
14  */
15 class StaticMapTest extends MigrateProcessTestCase {
16
17   /**
18    * {@inheritdoc}
19    */
20   protected function setUp() {
21     $configuration['map']['foo']['bar'] = 'baz';
22     $this->plugin = new StaticMap($configuration, 'map', []);
23     parent::setUp();
24   }
25
26   /**
27    * Tests map when the source is a string.
28    */
29   public function testMapWithSourceString() {
30     $value = $this->plugin->transform('foo', $this->migrateExecutable, $this->row, 'destinationproperty');
31     $this->assertSame(['bar' => 'baz'], $value);
32   }
33
34   /**
35    * Tests map when the source is a list.
36    */
37   public function testMapWithSourceList() {
38     $value = $this->plugin->transform(['foo', 'bar'], $this->migrateExecutable, $this->row, 'destinationproperty');
39     $this->assertSame('baz', $value);
40   }
41
42   /**
43    * Tests when the source is empty.
44    */
45   public function testMapwithEmptySource() {
46     $this->setExpectedException(MigrateException::class);
47     $this->plugin->transform([], $this->migrateExecutable, $this->row, 'destinationproperty');
48   }
49
50   /**
51    * Tests when the source is invalid.
52    */
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');
56   }
57
58   /**
59    * Tests when the source is invalid but there's a default.
60    */
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);
67   }
68
69   /**
70    * Tests when the source is invalid but there's a default value of NULL.
71    */
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);
78   }
79
80   /**
81    * Tests when the source is invalid and bypass is enabled.
82    */
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');
90   }
91
92 }