f30c184458da96031b37731da6e070e33920b969
[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 Prophecy\Argument;
15
16 /**
17  * @coversDefaultClass \Drupal\migrate\Plugin\migrate\process\MigrationLookup
18  * @group migrate
19  */
20 class MigrationLookupTest extends MigrateProcessTestCase {
21
22   /**
23    * @covers ::transform
24    */
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);
29
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);
34
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()]);
38
39     $configuration = [
40       'no_stub' => TRUE,
41       'migration' => 'destination_migration',
42     ];
43
44     $migration_plugin->id()->willReturn('actual_migration');
45     $destination_migration->getDestinationPlugin(TRUE)->shouldNotBeCalled();
46
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);
50   }
51
52   /**
53    * @covers ::transform
54    */
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);
59
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);
67
68     $configuration = [
69       'no_stub' => FALSE,
70       'migration' => 'destination_migration',
71     ];
72
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([]);
78
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());
85
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);
89   }
90
91   /**
92    * Tests that processing is skipped when the input value is empty.
93    */
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);
98
99     $configuration = [
100       'migration' => 'foobaz',
101     ];
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');
106   }
107
108   /**
109    * Tests a successful lookup.
110    *
111    * @dataProvider successfulLookupDataProvider
112    *
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.
121    */
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);
126
127     $configuration = [
128       'migration' => 'foobaz',
129     ];
130     $migration_plugin->id()->willReturn(uniqid());
131
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());
135
136     $migration_plugin_manager->createInstances(['foobaz'])
137       ->willReturn(['foobaz' => $migration_plugin->reveal()]);
138
139     $migrationStorage = $this->prophesize(EntityStorageInterface::class);
140     $migrationStorage
141       ->loadMultiple(['foobaz'])
142       ->willReturn([$migration_plugin->reveal()]);
143
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'));
146   }
147
148   /**
149    * Provides data for the successful lookup test.
150    *
151    * @return array
152    */
153   public function successfulLookupDataProvider() {
154     return [
155       // Test data for scalar to scalar.
156       [
157         // Source ID of the migration map.
158         [1],
159         // Destination ID of the migration map.
160         [3],
161         // Input value for the migration plugin.
162         1,
163         // Expected output value of the migration plugin.
164         3,
165       ],
166       // Test data for scalar to array.
167       [
168         // Source ID of the migration map.
169         [1],
170         // Destination IDs of the migration map.
171         [3, 'foo'],
172         // Input value for the migration plugin.
173         1,
174         // Expected output values of the migration plugin.
175         [3, 'foo'],
176       ],
177       // Test data for array to scalar.
178       [
179         // Source IDs of the migration map.
180         [1, 3],
181         // Destination ID of the migration map.
182         ['foo'],
183         // Input values for the migration plugin.
184         [1, 3],
185         // Expected output value of the migration plugin.
186         'foo',
187       ],
188       // Test data for array to array.
189       [
190         // Source IDs of the migration map.
191         [1, 3],
192         // Destination IDs of the migration map.
193         [3, 'foo'],
194         // Input values for the migration plugin.
195         [1, 3],
196         // Expected output values of the migration plugin.
197         [3, 'foo'],
198       ],
199     ];
200   }
201
202 }