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\Migration;
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\Migration
21 class MigrationTest extends MigrateProcessTestCase {
24 * @var \Drupal\migrate\Plugin\MigrationInterface
26 protected $migration_plugin;
29 * @var \Drupal\migrate\Plugin\MigrationPluginManagerInterface
31 protected $migration_plugin_manager;
34 * @var \Drupal\migrate\Plugin\MigratePluginManager
36 protected $process_plugin_manager;
41 protected function setUp() {
44 $this->migration_plugin = $this->prophesize(MigrationInterface::class);
45 $this->migration_plugin_manager = $this->prophesize(MigrationPluginManagerInterface::class);
46 $this->process_plugin_manager = $this->prophesize(MigratePluginManager::class);
52 public function testTransformWithStubSkipping() {
53 $destination_migration = $this->getMigration();
54 $destination_migration->getDestinationPlugin(TRUE)->shouldNotBeCalled();
56 // Ensure the migration plugin manager returns our migration.
57 $this->migration_plugin_manager->createInstances(Argument::exact(['destination_migration']))
58 ->willReturn(['destination_migration' => $destination_migration->reveal()]);
62 'migration' => 'destination_migration',
65 $this->migration_plugin->id()->willReturn('actual_migration');
67 $migration = new Migration($configuration, '', [], $this->migration_plugin->reveal(), $this->migration_plugin_manager->reveal(), $this->process_plugin_manager->reveal());
68 $result = $migration->transform(1, $this->migrateExecutable, $this->row, '');
69 $this->assertNull($result);
75 public function testTransformWithStubbing() {
76 $destination_migration = $this->getMigration();
77 $this->migration_plugin_manager->createInstances(['destination_migration'])
78 ->willReturn(['destination_migration' => $destination_migration->reveal()]);
82 'migration' => 'destination_migration',
85 $this->migration_plugin->id()->willReturn('actual_migration');
86 $destination_migration->id()->willReturn('destination_migration');
87 $destination_migration->getDestinationPlugin(TRUE)->shouldBeCalled();
88 $destination_migration->getProcess()->willReturn([]);
89 $destination_migration->getSourceConfiguration()->willReturn([]);
91 $source_plugin = $this->prophesize(MigrateSourceInterface::class);
92 $source_plugin->getIds()->willReturn(['nid']);
93 $destination_migration->getSourcePlugin()->willReturn($source_plugin->reveal());
94 $destination_plugin = $this->prophesize(MigrateDestinationInterface::class);
95 $destination_plugin->import(Argument::any())->willReturn([2]);
96 $destination_migration->getDestinationPlugin(TRUE)->willReturn($destination_plugin->reveal());
98 $migration = new Migration($configuration, '', [], $this->migration_plugin->reveal(), $this->migration_plugin_manager->reveal(), $this->process_plugin_manager->reveal());
99 $result = $migration->transform(1, $this->migrateExecutable, $this->row, '');
100 $this->assertEquals(2, $result);
104 * Creates a mock Migration instance.
106 * @return \Prophecy\Prophecy\ObjectProphecy
107 * A mock Migration instance.
109 protected function getMigration() {
110 $id_map = $this->prophesize(MigrateIdMapInterface::class);
111 $id_map->lookupDestinationId([1])->willReturn(NULL);
112 $id_map->saveIdMapping(Argument::any(), Argument::any(), MigrateIdMapInterface::STATUS_NEEDS_UPDATE)->willReturn(NULL);
114 $migration = $this->prophesize(MigrationInterface::class);
115 $migration->getIdMap()->willReturn($id_map->reveal());
120 * Tests that processing is skipped when the input value is empty.
122 public function testSkipOnEmpty() {
124 'migration' => 'foobaz',
126 $this->migration_plugin->id()->willReturn(uniqid());
127 $migration = new Migration($configuration, 'migration', [], $this->migration_plugin->reveal(), $this->migration_plugin_manager->reveal(), $this->process_plugin_manager->reveal());
128 $this->setExpectedException(MigrateSkipProcessException::class);
129 $migration->transform(0, $this->migrateExecutable, $this->row, 'foo');
133 * Tests a successful lookup.
135 * @dataProvider successfulLookupDataProvider
137 * @param array $source_id_values
138 * The source id(s) of the migration map.
139 * @param array $destination_id_values
140 * The destination id(s) of the migration map.
141 * @param string|array $source_value
142 * The source value(s) for the migration process plugin.
143 * @param string|array $expected_value
144 * The expected value(s) of the migration process plugin.
146 public function testSuccessfulLookup($source_id_values, $destination_id_values, $source_value, $expected_value) {
148 'migration' => 'foobaz',
150 $this->migration_plugin->id()->willReturn(uniqid());
152 $id_map = $this->prophesize(MigrateIdMapInterface::class);
153 $id_map->lookupDestinationId($source_id_values)->willReturn($destination_id_values);
154 $this->migration_plugin->getIdMap()->willReturn($id_map->reveal());
156 $this->migration_plugin_manager->createInstances(['foobaz'])
157 ->willReturn(['foobaz' => $this->migration_plugin->reveal()]);
159 $migrationStorage = $this->prophesize(EntityStorageInterface::class);
161 ->loadMultiple(['foobaz'])
162 ->willReturn([$this->migration_plugin->reveal()]);
164 $migration = new Migration($configuration, 'migration', [], $this->migration_plugin->reveal(), $this->migration_plugin_manager->reveal(), $this->process_plugin_manager->reveal());
165 $this->assertSame($expected_value, $migration->transform($source_value, $this->migrateExecutable, $this->row, 'foo'));
169 * Provides data for the successful lookup test.
173 public function successfulLookupDataProvider() {
175 'scalar_to_scalar' => [
177 'destination_ids' => [3],
179 'expected_value' => 3,
181 'scalar_to_array' => [
183 'destination_ids' => [3, 'foo'],
185 'expected_value' => [3, 'foo'],
187 'array_to_scalar' => [
188 'source_ids' => [1, 3],
189 'destination_ids' => ['foo'],
190 'input_value' => [1, 3],
191 'expected_value' => 'foo',
193 'array_to_array' => [
194 'source_ids' => [1, 3],
195 'destination_ids' => [3, 'foo'],
196 'input_value' => [1, 3],
197 'expected_value' => [3, 'foo'],