9158cdbdbdec3e00825bfd83791e227c8d747166
[yaffs-website] / web / core / modules / migrate / tests / src / Unit / process / MigrationLookupTest.php
1 <?php
2
3 namespace Drupal\Tests\migrate\Unit\process;
4
5 use Drupal\Core\Entity\EntityStorageInterface;
6 use Drupal\migrate\MigrateSkipProcessException;
7 use Drupal\migrate\Plugin\migrate\process\Get;
8 use Drupal\migrate\Plugin\MigrationInterface;
9 use Drupal\migrate\Plugin\migrate\process\MigrationLookup;
10 use Drupal\migrate\Plugin\MigrateDestinationInterface;
11 use Drupal\migrate\Plugin\MigrateIdMapInterface;
12 use Drupal\migrate\Plugin\MigratePluginManager;
13 use Drupal\migrate\Plugin\MigrateSourceInterface;
14 use Drupal\migrate\Plugin\MigrationPluginManagerInterface;
15 use Drupal\migrate\Row;
16 use Prophecy\Argument;
17
18 /**
19  * @coversDefaultClass \Drupal\migrate\Plugin\migrate\process\MigrationLookup
20  * @group migrate
21  */
22 class MigrationLookupTest extends MigrateProcessTestCase {
23
24   /**
25    * @covers ::transform
26    */
27   public function testTransformWithStubSkipping() {
28     $migration_plugin = $this->prophesize(MigrationInterface::class);
29     $migration_plugin_manager = $this->prophesize(MigrationPluginManagerInterface::class);
30     $process_plugin_manager = $this->prophesize(MigratePluginManager::class);
31
32     $destination_id_map = $this->prophesize(MigrateIdMapInterface::class);
33     $destination_migration = $this->prophesize(MigrationInterface::class);
34     $destination_migration->getIdMap()->willReturn($destination_id_map->reveal());
35     $destination_id_map->lookupDestinationId([1])->willReturn(NULL);
36
37     // Ensure the migration plugin manager returns our migration.
38     $migration_plugin_manager->createInstances(Argument::exact(['destination_migration']))
39       ->willReturn(['destination_migration' => $destination_migration->reveal()]);
40
41     $configuration = [
42       'no_stub' => TRUE,
43       'migration' => 'destination_migration',
44     ];
45
46     $migration_plugin->id()->willReturn('actual_migration');
47     $destination_migration->getDestinationPlugin(TRUE)->shouldNotBeCalled();
48
49     $migration = new MigrationLookup($configuration, '', [], $migration_plugin->reveal(), $migration_plugin_manager->reveal(), $process_plugin_manager->reveal());
50     $result = $migration->transform(1, $this->migrateExecutable, $this->row, '');
51     $this->assertNull($result);
52   }
53
54   /**
55    * @covers ::transform
56    */
57   public function testTransformWithStubbing() {
58     $migration_plugin = $this->prophesize(MigrationInterface::class);
59     $migration_plugin_manager = $this->prophesize(MigrationPluginManagerInterface::class);
60     $process_plugin_manager = $this->prophesize(MigratePluginManager::class);
61
62     $destination_id_map = $this->prophesize(MigrateIdMapInterface::class);
63     $destination_migration = $this->prophesize('Drupal\migrate\Plugin\Migration');
64     $destination_migration->getIdMap()->willReturn($destination_id_map->reveal());
65     $migration_plugin_manager->createInstances(['destination_migration'])
66       ->willReturn(['destination_migration' => $destination_migration->reveal()]);
67     $destination_id_map->lookupDestinationId([1])->willReturn(NULL);
68     $destination_id_map->saveIdMapping(Argument::any(), Argument::any(), MigrateIdMapInterface::STATUS_NEEDS_UPDATE)->willReturn(NULL);
69
70     $configuration = [
71       'no_stub' => FALSE,
72       'migration' => 'destination_migration',
73     ];
74
75     $migration_plugin->id()->willReturn('actual_migration');
76     $destination_migration->id()->willReturn('destination_migration');
77     $destination_migration->getDestinationPlugin(TRUE)->shouldBeCalled();
78     $destination_migration->getProcess()->willReturn([]);
79     $destination_migration->getSourceConfiguration()->willReturn([]);
80
81     $source_plugin = $this->prophesize(MigrateSourceInterface::class);
82     $source_plugin->getIds()->willReturn(['nid']);
83     $destination_migration->getSourcePlugin()->willReturn($source_plugin->reveal());
84     $destination_plugin = $this->prophesize(MigrateDestinationInterface::class);
85     $destination_plugin->import(Argument::any())->willReturn([2]);
86     $destination_migration->getDestinationPlugin(TRUE)->willReturn($destination_plugin->reveal());
87
88     $migration = new MigrationLookup($configuration, '', [], $migration_plugin->reveal(), $migration_plugin_manager->reveal(), $process_plugin_manager->reveal());
89     $result = $migration->transform(1, $this->migrateExecutable, $this->row, '');
90     $this->assertEquals(2, $result);
91   }
92
93   /**
94    * Tests that processing is skipped when the input value is empty.
95    */
96   public function testSkipOnEmpty() {
97     $migration_plugin = $this->prophesize(MigrationInterface::class);
98     $migration_plugin_manager = $this->prophesize(MigrationPluginManagerInterface::class);
99     $process_plugin_manager = $this->prophesize(MigratePluginManager::class);
100
101     $configuration = [
102       'migration' => 'foobaz',
103     ];
104     $migration_plugin->id()->willReturn(uniqid());
105     $migration_plugin_manager->createInstances(['foobaz'])
106       ->willReturn(['foobaz' => $migration_plugin->reveal()]);
107     $migration = new MigrationLookup($configuration, 'migration_lookup', [], $migration_plugin->reveal(), $migration_plugin_manager->reveal(), $process_plugin_manager->reveal());
108     $this->setExpectedException(MigrateSkipProcessException::class);
109     $migration->transform(0, $this->migrateExecutable, $this->row, 'foo');
110   }
111
112   /**
113    * Tests a successful lookup.
114    *
115    * @dataProvider successfulLookupDataProvider
116    *
117    * @param array $source_id_values
118    *   The source id(s) of the migration map.
119    * @param array $destination_id_values
120    *   The destination id(s) of the migration map.
121    * @param string|array $source_value
122    *   The source value(s) for the migration process plugin.
123    * @param string|array $expected_value
124    *   The expected value(s) of the migration process plugin.
125    */
126   public function testSuccessfulLookup($source_id_values, $destination_id_values, $source_value, $expected_value) {
127     $migration_plugin = $this->prophesize(MigrationInterface::class);
128     $migration_plugin_manager = $this->prophesize(MigrationPluginManagerInterface::class);
129     $process_plugin_manager = $this->prophesize(MigratePluginManager::class);
130
131     $configuration = [
132       'migration' => 'foobaz',
133     ];
134     $migration_plugin->id()->willReturn(uniqid());
135
136     $id_map = $this->prophesize(MigrateIdMapInterface::class);
137     $id_map->lookupDestinationId($source_id_values)->willReturn($destination_id_values);
138     $migration_plugin->getIdMap()->willReturn($id_map->reveal());
139
140     $migration_plugin_manager->createInstances(['foobaz'])
141       ->willReturn(['foobaz' => $migration_plugin->reveal()]);
142
143     $migrationStorage = $this->prophesize(EntityStorageInterface::class);
144     $migrationStorage
145       ->loadMultiple(['foobaz'])
146       ->willReturn([$migration_plugin->reveal()]);
147
148     $migration = new MigrationLookup($configuration, 'migration_lookup', [], $migration_plugin->reveal(), $migration_plugin_manager->reveal(), $process_plugin_manager->reveal());
149     $this->assertSame($expected_value, $migration->transform($source_value, $this->migrateExecutable, $this->row, 'foo'));
150   }
151
152   /**
153    * Provides data for the successful lookup test.
154    *
155    * @return array
156    */
157   public function successfulLookupDataProvider() {
158     return [
159       // Test data for scalar to scalar.
160       [
161         // Source ID of the migration map.
162         [1],
163         // Destination ID of the migration map.
164         [3],
165         // Input value for the migration plugin.
166         1,
167         // Expected output value of the migration plugin.
168         3,
169       ],
170       // Test data for scalar to array.
171       [
172         // Source ID of the migration map.
173         [1],
174         // Destination IDs of the migration map.
175         [3, 'foo'],
176         // Input value for the migration plugin.
177         1,
178         // Expected output values of the migration plugin.
179         [3, 'foo'],
180       ],
181       // Test data for array to scalar.
182       [
183         // Source IDs of the migration map.
184         [1, 3],
185         // Destination ID of the migration map.
186         ['foo'],
187         // Input values for the migration plugin.
188         [1, 3],
189         // Expected output value of the migration plugin.
190         'foo',
191       ],
192       // Test data for array to array.
193       [
194         // Source IDs of the migration map.
195         [1, 3],
196         // Destination IDs of the migration map.
197         [3, 'foo'],
198         // Input values for the migration plugin.
199         [1, 3],
200         // Expected output values of the migration plugin.
201         [3, 'foo'],
202       ],
203     ];
204   }
205
206   /**
207    * Tests that a message is successfully created if import fails.
208    */
209   public function testImportException() {
210     $migration_plugin = $this->prophesize(MigrationInterface::class);
211     $migration_plugin_manager = $this->prophesize(MigrationPluginManagerInterface::class);
212     $process_plugin_manager = $this->prophesize(MigratePluginManager::class);
213
214     $destination_id_map = $this->prophesize(MigrateIdMapInterface::class);
215     $destination_migration = $this->prophesize('Drupal\migrate\Plugin\Migration');
216     $destination_migration->getIdMap()->willReturn($destination_id_map->reveal());
217     $migration_plugin_manager->createInstances(['destination_migration'])
218       ->willReturn(['destination_migration' => $destination_migration->reveal()]);
219     $destination_id_map->lookupDestinationId([1])->willReturn(NULL);
220     $destination_id_map->saveMessage(Argument::any(), Argument::any())->willReturn(NULL);
221     $destination_id_map->saveIdMapping(Argument::any(), Argument::any(), Argument::any())->shouldNotBeCalled();
222
223     $configuration = [
224       'no_stub' => FALSE,
225       'migration' => 'destination_migration',
226     ];
227
228     $destination_migration->id()->willReturn('destination_migration');
229     $destination_migration->getDestinationPlugin(TRUE)->shouldBeCalled();
230     $destination_migration->getProcess()->willReturn([]);
231     $destination_migration->getSourceConfiguration()->willReturn([]);
232
233     $source_plugin = $this->prophesize(MigrateSourceInterface::class);
234     $source_plugin->getIds()->willReturn(['nid']);
235     $destination_migration->getSourcePlugin()->willReturn($source_plugin->reveal());
236     $destination_plugin = $this->prophesize(MigrateDestinationInterface::class);
237     $e = new \Exception();
238     $destination_plugin->import(Argument::any())->willThrow($e);
239     $destination_migration->getDestinationPlugin(TRUE)->willReturn($destination_plugin->reveal());
240
241     $migration = new MigrationLookup($configuration, '', [], $migration_plugin->reveal(), $migration_plugin_manager->reveal(), $process_plugin_manager->reveal());
242     $migration->transform(1, $this->migrateExecutable, $this->row, '');
243   }
244
245   /**
246    * Tests processing multiple source IDs.
247    */
248   public function testMultipleSourceIds() {
249     $migration_plugin = $this->prophesize(MigrationInterface::class);
250     $migration_plugin_manager = $this->prophesize(MigrationPluginManagerInterface::class);
251     $process_plugin_manager = $this->prophesize(MigratePluginManager::class);
252     $foobaz_migration = $this->prophesize(MigrationInterface::class);
253     $get_migration = $this->prophesize(Get::class);
254     $id_map = $this->prophesize(MigrateIdMapInterface::class);
255     $destination_plugin = $this->prophesize(MigrateDestinationInterface::class);
256     $source_plugin = $this->prophesize(MigrateSourceInterface::class);
257
258     $migration_plugin_manager->createInstances(['foobaz'])
259       ->willReturn(['foobaz' => $foobaz_migration->reveal()]);
260
261     $process_plugin_manager->createInstance('get', ['source' => ['string_id', 'integer_id']], $migration_plugin->reveal())
262       ->willReturn($get_migration->reveal());
263
264     $foobaz_migration->getIdMap()->willReturn($id_map->reveal());
265     $foobaz_migration->getDestinationPlugin(TRUE)->willReturn($destination_plugin->reveal());
266     $foobaz_migration->getProcess()->willReturn([]);
267     $foobaz_migration->getSourcePlugin()->willReturn($source_plugin->reveal());
268     $foobaz_migration->id()->willReturn('foobaz');
269     $foobaz_migration->getSourceConfiguration()->willReturn([]);
270
271     $get_migration->transform(NULL, $this->migrateExecutable, $this->row, 'foo')
272       ->willReturn(['example_string', 99]);
273
274     $source_plugin_ids = [
275       'string_id' => [
276         'type' => 'string',
277         'max_length' => 128,
278         'is_ascii' => TRUE,
279         'alias' => 'wpt',
280       ],
281       'integer_id' => [
282         'type' => 'integer',
283         'unsigned' => FALSE,
284         'alias' => 'wpt',
285       ],
286     ];
287
288     $stub_row = new Row(['string_id' => 'example_string', 'integer_id' => 99], $source_plugin_ids, TRUE);
289     $destination_plugin->import($stub_row)->willReturn([2]);
290
291     $source_plugin->getIds()->willReturn($source_plugin_ids);
292
293     $configuration = [
294       'migration' => 'foobaz',
295       'source_ids' => ['foobaz' => ['string_id', 'integer_id']],
296     ];
297     $migration = new MigrationLookup($configuration, 'migration', [], $migration_plugin->reveal(), $migration_plugin_manager->reveal(), $process_plugin_manager->reveal());
298     $result = $migration->transform(NULL, $this->migrateExecutable, $this->row, 'foo');
299     $this->assertEquals(2, $result);
300   }
301
302 }