Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[yaffs-website] / web / core / modules / migrate / tests / src / Unit / process / GetTest.php
1 <?php
2
3 namespace Drupal\Tests\migrate\Unit\process;
4
5 use Drupal\migrate\Plugin\migrate\process\Get;
6
7 /**
8  * Tests the get process plugin.
9  *
10  * @group migrate
11  */
12 class GetTest extends MigrateProcessTestCase {
13
14   /**
15    * Tests the Get plugin when source is a string.
16    */
17   public function testTransformSourceString() {
18     $this->row->expects($this->once())
19       ->method('getSourceProperty')
20       ->with('test')
21       ->will($this->returnValue('source_value'));
22     $this->plugin = new Get(['source' => 'test'], '', []);
23     $value = $this->plugin->transform(NULL, $this->migrateExecutable, $this->row, 'destinationproperty');
24     $this->assertSame('source_value', $value);
25   }
26
27   /**
28    * Tests the Get plugin when source is an array.
29    */
30   public function testTransformSourceArray() {
31     $map = [
32       'test1' => 'source_value1',
33       'test2' => 'source_value2',
34     ];
35     $this->plugin = new Get(['source' => ['test1', 'test2']], '', []);
36     $this->row->expects($this->exactly(2))
37       ->method('getSourceProperty')
38       ->will($this->returnCallback(function ($argument) use ($map) {
39         return $map[$argument];
40       }));
41     $value = $this->plugin->transform(NULL, $this->migrateExecutable, $this->row, 'destinationproperty');
42     $this->assertSame(['source_value1', 'source_value2'], $value);
43   }
44
45   /**
46    * Tests the Get plugin when source is a string pointing to destination.
47    */
48   public function testTransformSourceStringAt() {
49     $this->row->expects($this->once())
50       ->method('getSourceProperty')
51       ->with('@test')
52       ->will($this->returnValue('source_value'));
53     $this->plugin = new Get(['source' => '@@test'], '', []);
54     $value = $this->plugin->transform(NULL, $this->migrateExecutable, $this->row, 'destinationproperty');
55     $this->assertSame('source_value', $value);
56   }
57
58   /**
59    * Tests the Get plugin when source is an array pointing to destination.
60    */
61   public function testTransformSourceArrayAt() {
62     $map = [
63       'test1' => 'source_value1',
64       '@test2' => 'source_value2',
65       '@test3' => 'source_value3',
66       'test4' => 'source_value4',
67     ];
68     $this->plugin = new Get(['source' => ['test1', '@@test2', '@@test3', 'test4']], '', []);
69     $this->row->expects($this->exactly(4))
70       ->method('getSourceProperty')
71       ->will($this->returnCallback(function ($argument) use ($map) {
72         return $map[$argument];
73       }));
74     $value = $this->plugin->transform(NULL, $this->migrateExecutable, $this->row, 'destinationproperty');
75     $this->assertSame(['source_value1', 'source_value2', 'source_value3', 'source_value4'], $value);
76   }
77
78   /**
79    * Tests the Get plugin when source has integer values.
80    *
81    * @dataProvider integerValuesDataProvider
82    */
83   public function testIntegerValues($source, $expected_value) {
84     $this->row->expects($this->atMost(2))
85       ->method('getSourceProperty')
86       ->willReturnOnConsecutiveCalls('val1', 'val2');
87
88     $this->plugin = new Get(['source' => $source], '', []);
89     $return = $this->plugin->transform(NULL, $this->migrateExecutable, $this->row, 'destinationproperty');
90     $this->assertSame($expected_value, $return);
91   }
92
93   /**
94    * Provides data for the successful lookup test.
95    *
96    * @return array
97    */
98   public function integerValuesDataProvider() {
99     return [
100       [
101         'source' => [0 => 0, 1 => 'test'],
102         'expected_value' => [0 => 'val1', 1 => 'val2'],
103       ],
104       [
105         'source' => [FALSE],
106         'expected_value' => [NULL],
107       ],
108       [
109         'source' => [NULL],
110         'expected_value' => [NULL],
111       ],
112     ];
113   }
114
115   /**
116    * Tests the Get plugin for syntax errors, e.g. "Invalid tag_line detected" by
117    * creating a prophecy of the class.
118    */
119   public function testPluginSyntax() {
120     $this->assertNotNull($this->prophesize(Get::class));
121   }
122
123 }