3 namespace Drupal\Tests\migrate\Unit\process;
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;
18 * @coversDefaultClass \Drupal\migrate\Plugin\migrate\process\MigrationLookup
21 class MigrationLookupTest extends MigrateProcessTestCase {
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);
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);
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()]);
42 'migration' => 'destination_migration',
45 $migration_plugin->id()->willReturn('actual_migration');
46 $destination_migration->getDestinationPlugin(TRUE)->shouldNotBeCalled();
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);
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);
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);
71 'migration' => 'destination_migration',
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([]);
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());
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);
93 * Tests that processing is skipped when the input value is empty.
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);
101 'migration' => 'foobaz',
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');
112 * Tests a successful lookup.
114 * @dataProvider successfulLookupDataProvider
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.
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);
131 'migration' => 'foobaz',
133 $migration_plugin->id()->willReturn(uniqid());
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());
139 $migration_plugin_manager->createInstances(['foobaz'])
140 ->willReturn(['foobaz' => $migration_plugin->reveal()]);
142 $migrationStorage = $this->prophesize(EntityStorageInterface::class);
144 ->loadMultiple(['foobaz'])
145 ->willReturn([$migration_plugin->reveal()]);
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'));
152 * Provides data for the successful lookup test.
156 public function successfulLookupDataProvider() {
158 // Test data for scalar to scalar.
160 // Source ID of the migration map.
162 // Destination ID of the migration map.
164 // Input value for the migration plugin.
166 // Expected output value of the migration plugin.
169 // Test data for scalar to array.
171 // Source ID of the migration map.
173 // Destination IDs of the migration map.
175 // Input value for the migration plugin.
177 // Expected output values of the migration plugin.
180 // Test data for array to scalar.
182 // Source IDs of the migration map.
184 // Destination ID of the migration map.
186 // Input values for the migration plugin.
188 // Expected output value of the migration plugin.
191 // Test data for array to array.
193 // Source IDs of the migration map.
195 // Destination IDs of the migration map.
197 // Input values for the migration plugin.
199 // Expected output values of the migration plugin.
206 * Tests that a message is successfully created if import fails.
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);
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();
224 'migration' => 'destination_migration',
227 $destination_migration->id()->willReturn('destination_migration');
228 $destination_migration->getDestinationPlugin(TRUE)->shouldBeCalled();
229 $destination_migration->getProcess()->willReturn([]);
230 $destination_migration->getSourceConfiguration()->willReturn([]);
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());
240 $migration = new MigrationLookup($configuration, '', [], $migration_plugin->reveal(), $migration_plugin_manager->reveal(), $process_plugin_manager->reveal());
241 $migration->transform(1, $this->migrateExecutable, $this->row, '');
245 * Tests processing multiple source IDs.
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);
257 $migration_plugin_manager->createInstances(['foobaz'])
258 ->willReturn(['foobaz' => $foobaz_migration->reveal()]);
260 $process_plugin_manager->createInstance('get', ['source' => ['string_id', 'integer_id']], $migration_plugin->reveal())
261 ->willReturn($get_migration->reveal());
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([]);
270 $get_migration->transform(NULL, $this->migrateExecutable, $this->row, 'foo')
271 ->willReturn(['example_string', 99]);
273 $source_plugin_ids = [
287 $stub_row = new Row(['string_id' => 'example_string', 'integer_id' => 99], $source_plugin_ids, TRUE);
288 $destination_plugin->import($stub_row)->willReturn([2]);
290 $source_plugin->getIds()->willReturn($source_plugin_ids);
293 'migration' => 'foobaz',
294 'source_ids' => ['foobaz' => ['string_id', 'integer_id']],
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);