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