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