3 namespace Drupal\Tests\migrate\Unit\process;
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;
19 * @coversDefaultClass \Drupal\migrate\Plugin\migrate\process\MigrationLookup
22 class MigrationLookupTest extends MigrateProcessTestCase {
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);
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);
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()]);
43 'migration' => 'destination_migration',
46 $migration_plugin->id()->willReturn('actual_migration');
47 $destination_migration->getDestinationPlugin(TRUE)->shouldNotBeCalled();
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);
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);
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);
72 'migration' => 'destination_migration',
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([]);
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());
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);
94 * Tests that processing is skipped when the input value is empty.
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);
102 'migration' => 'foobaz',
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');
113 * Tests a successful lookup.
115 * @dataProvider successfulLookupDataProvider
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.
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);
132 'migration' => 'foobaz',
134 $migration_plugin->id()->willReturn(uniqid());
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());
140 $migration_plugin_manager->createInstances(['foobaz'])
141 ->willReturn(['foobaz' => $migration_plugin->reveal()]);
143 $migrationStorage = $this->prophesize(EntityStorageInterface::class);
145 ->loadMultiple(['foobaz'])
146 ->willReturn([$migration_plugin->reveal()]);
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'));
153 * Provides data for the successful lookup test.
157 public function successfulLookupDataProvider() {
159 // Test data for scalar to scalar.
161 // Source ID of the migration map.
163 // Destination ID of the migration map.
165 // Input value for the migration plugin.
167 // Expected output value of the migration plugin.
170 // Test data for scalar to array.
172 // Source ID of the migration map.
174 // Destination IDs of the migration map.
176 // Input value for the migration plugin.
178 // Expected output values of the migration plugin.
181 // Test data for array to scalar.
183 // Source IDs of the migration map.
185 // Destination ID of the migration map.
187 // Input values for the migration plugin.
189 // Expected output value of the migration plugin.
192 // Test data for array to array.
194 // Source IDs of the migration map.
196 // Destination IDs of the migration map.
198 // Input values for the migration plugin.
200 // Expected output values of the migration plugin.
207 * Tests that a message is successfully created if import fails.
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);
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();
225 'migration' => 'destination_migration',
228 $destination_migration->id()->willReturn('destination_migration');
229 $destination_migration->getDestinationPlugin(TRUE)->shouldBeCalled();
230 $destination_migration->getProcess()->willReturn([]);
231 $destination_migration->getSourceConfiguration()->willReturn([]);
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());
241 $migration = new MigrationLookup($configuration, '', [], $migration_plugin->reveal(), $migration_plugin_manager->reveal(), $process_plugin_manager->reveal());
242 $migration->transform(1, $this->migrateExecutable, $this->row, '');
246 * Tests processing multiple source IDs.
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);
258 $migration_plugin_manager->createInstances(['foobaz'])
259 ->willReturn(['foobaz' => $foobaz_migration->reveal()]);
261 $process_plugin_manager->createInstance('get', ['source' => ['string_id', 'integer_id']], $migration_plugin->reveal())
262 ->willReturn($get_migration->reveal());
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([]);
271 $get_migration->transform(NULL, $this->migrateExecutable, $this->row, 'foo')
272 ->willReturn(['example_string', 99]);
274 $source_plugin_ids = [
288 $stub_row = new Row(['string_id' => 'example_string', 'integer_id' => 99], $source_plugin_ids, TRUE);
289 $destination_plugin->import($stub_row)->willReturn([2]);
291 $source_plugin->getIds()->willReturn($source_plugin_ids);
294 'migration' => 'foobaz',
295 'source_ids' => ['foobaz' => ['string_id', 'integer_id']],
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);