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