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 Prophecy\Argument;
17 * @coversDefaultClass \Drupal\migrate\Plugin\migrate\process\MigrationLookup
20 class MigrationLookupTest extends MigrateProcessTestCase {
25 public function testTransformWithStubSkipping() {
26 $migration_plugin = $this->prophesize(MigrationInterface::class);
27 $migration_plugin_manager = $this->prophesize(MigrationPluginManagerInterface::class);
28 $process_plugin_manager = $this->prophesize(MigratePluginManager::class);
30 $destination_id_map = $this->prophesize(MigrateIdMapInterface::class);
31 $destination_migration = $this->prophesize(MigrationInterface::class);
32 $destination_migration->getIdMap()->willReturn($destination_id_map->reveal());
33 $destination_id_map->lookupDestinationId([1])->willReturn(NULL);
35 // Ensure the migration plugin manager returns our migration.
36 $migration_plugin_manager->createInstances(Argument::exact(['destination_migration']))
37 ->willReturn(['destination_migration' => $destination_migration->reveal()]);
41 'migration' => 'destination_migration',
44 $migration_plugin->id()->willReturn('actual_migration');
45 $destination_migration->getDestinationPlugin(TRUE)->shouldNotBeCalled();
47 $migration = new MigrationLookup($configuration, '', [], $migration_plugin->reveal(), $migration_plugin_manager->reveal(), $process_plugin_manager->reveal());
48 $result = $migration->transform(1, $this->migrateExecutable, $this->row, '');
49 $this->assertNull($result);
55 public function testTransformWithStubbing() {
56 $migration_plugin = $this->prophesize(MigrationInterface::class);
57 $migration_plugin_manager = $this->prophesize(MigrationPluginManagerInterface::class);
58 $process_plugin_manager = $this->prophesize(MigratePluginManager::class);
60 $destination_id_map = $this->prophesize(MigrateIdMapInterface::class);
61 $destination_migration = $this->prophesize('Drupal\migrate\Plugin\Migration');
62 $destination_migration->getIdMap()->willReturn($destination_id_map->reveal());
63 $migration_plugin_manager->createInstances(['destination_migration'])
64 ->willReturn(['destination_migration' => $destination_migration->reveal()]);
65 $destination_id_map->lookupDestinationId([1])->willReturn(NULL);
66 $destination_id_map->saveIdMapping(Argument::any(), Argument::any(), MigrateIdMapInterface::STATUS_NEEDS_UPDATE)->willReturn(NULL);
70 'migration' => 'destination_migration',
73 $migration_plugin->id()->willReturn('actual_migration');
74 $destination_migration->id()->willReturn('destination_migration');
75 $destination_migration->getDestinationPlugin(TRUE)->shouldBeCalled();
76 $destination_migration->getProcess()->willReturn([]);
77 $destination_migration->getSourceConfiguration()->willReturn([]);
79 $source_plugin = $this->prophesize(MigrateSourceInterface::class);
80 $source_plugin->getIds()->willReturn(['nid']);
81 $destination_migration->getSourcePlugin()->willReturn($source_plugin->reveal());
82 $destination_plugin = $this->prophesize(MigrateDestinationInterface::class);
83 $destination_plugin->import(Argument::any())->willReturn([2]);
84 $destination_migration->getDestinationPlugin(TRUE)->willReturn($destination_plugin->reveal());
86 $migration = new MigrationLookup($configuration, '', [], $migration_plugin->reveal(), $migration_plugin_manager->reveal(), $process_plugin_manager->reveal());
87 $result = $migration->transform(1, $this->migrateExecutable, $this->row, '');
88 $this->assertEquals(2, $result);
92 * Tests that processing is skipped when the input value is empty.
94 public function testSkipOnEmpty() {
95 $migration_plugin = $this->prophesize(MigrationInterface::class);
96 $migration_plugin_manager = $this->prophesize(MigrationPluginManagerInterface::class);
97 $process_plugin_manager = $this->prophesize(MigratePluginManager::class);
100 'migration' => 'foobaz',
102 $migration_plugin->id()->willReturn(uniqid());
103 $migration = new MigrationLookup($configuration, 'migration_lookup', [], $migration_plugin->reveal(), $migration_plugin_manager->reveal(), $process_plugin_manager->reveal());
104 $this->setExpectedException(MigrateSkipProcessException::class);
105 $migration->transform(0, $this->migrateExecutable, $this->row, 'foo');
109 * Tests a successful lookup.
111 * @dataProvider successfulLookupDataProvider
113 * @param array $source_id_values
114 * The source id(s) of the migration map.
115 * @param array $destination_id_values
116 * The destination id(s) of the migration map.
117 * @param string|array $source_value
118 * The source value(s) for the migration process plugin.
119 * @param string|array $expected_value
120 * The expected value(s) of the migration process plugin.
122 public function testSuccessfulLookup($source_id_values, $destination_id_values, $source_value, $expected_value) {
123 $migration_plugin = $this->prophesize(MigrationInterface::class);
124 $migration_plugin_manager = $this->prophesize(MigrationPluginManagerInterface::class);
125 $process_plugin_manager = $this->prophesize(MigratePluginManager::class);
128 'migration' => 'foobaz',
130 $migration_plugin->id()->willReturn(uniqid());
132 $id_map = $this->prophesize(MigrateIdMapInterface::class);
133 $id_map->lookupDestinationId($source_id_values)->willReturn($destination_id_values);
134 $migration_plugin->getIdMap()->willReturn($id_map->reveal());
136 $migration_plugin_manager->createInstances(['foobaz'])
137 ->willReturn(['foobaz' => $migration_plugin->reveal()]);
139 $migrationStorage = $this->prophesize(EntityStorageInterface::class);
141 ->loadMultiple(['foobaz'])
142 ->willReturn([$migration_plugin->reveal()]);
144 $migration = new MigrationLookup($configuration, 'migration_lookup', [], $migration_plugin->reveal(), $migration_plugin_manager->reveal(), $process_plugin_manager->reveal());
145 $this->assertSame($expected_value, $migration->transform($source_value, $this->migrateExecutable, $this->row, 'foo'));
149 * Provides data for the successful lookup test.
153 public function successfulLookupDataProvider() {
155 // Test data for scalar to scalar.
157 // Source ID of the migration map.
159 // Destination ID of the migration map.
161 // Input value for the migration plugin.
163 // Expected output value of the migration plugin.
166 // Test data for scalar to array.
168 // Source ID of the migration map.
170 // Destination IDs of the migration map.
172 // Input value for the migration plugin.
174 // Expected output values of the migration plugin.
177 // Test data for array to scalar.
179 // Source IDs of the migration map.
181 // Destination ID of the migration map.
183 // Input values for the migration plugin.
185 // Expected output value of the migration plugin.
188 // Test data for array to array.
190 // Source IDs of the migration map.
192 // Destination IDs of the migration map.
194 // Input values for the migration plugin.
196 // Expected output values of the migration plugin.