cade905a4d4d3fc84f34f541aa4d7e3c1f2fb093
[yaffs-website] / web / core / modules / migrate / tests / src / Unit / process / MigrationTest.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\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;
15
16 /**
17  * @coversDefaultClass \Drupal\migrate\Plugin\migrate\process\Migration
18  * @group migrate
19  * @group legacy
20  */
21 class MigrationTest extends MigrateProcessTestCase {
22
23   /**
24    * @var \Drupal\migrate\Plugin\MigrationInterface
25    */
26   protected $migration_plugin;
27
28   /**
29    * @var \Drupal\migrate\Plugin\MigrationPluginManagerInterface
30    */
31   protected $migration_plugin_manager;
32
33   /**
34    * @var \Drupal\migrate\Plugin\MigratePluginManager
35    */
36   protected $process_plugin_manager;
37
38   /**
39    * {@inheritdoc}
40    */
41   protected function setUp() {
42     parent::setUp();
43
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);
47   }
48
49   /**
50    * @covers ::transform
51    */
52   public function testTransformWithStubSkipping() {
53     $destination_migration = $this->getMigration();
54     $destination_migration->getDestinationPlugin(TRUE)->shouldNotBeCalled();
55
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()]);
59
60     $configuration = [
61       'no_stub' => TRUE,
62       'migration' => 'destination_migration',
63     ];
64
65     $this->migration_plugin->id()->willReturn('actual_migration');
66
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);
70   }
71
72   /**
73    * @covers ::transform
74    */
75   public function testTransformWithStubbing() {
76     $destination_migration = $this->getMigration();
77     $this->migration_plugin_manager->createInstances(['destination_migration'])
78       ->willReturn(['destination_migration' => $destination_migration->reveal()]);
79
80     $configuration = [
81       'no_stub' => FALSE,
82       'migration' => 'destination_migration',
83     ];
84
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([]);
90
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());
97
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);
101   }
102
103   /**
104    * Creates a mock Migration instance.
105    *
106    * @return \Prophecy\Prophecy\ObjectProphecy
107    *   A mock Migration instance.
108    */
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);
113
114     $migration = $this->prophesize(MigrationInterface::class);
115     $migration->getIdMap()->willReturn($id_map->reveal());
116     return $migration;
117   }
118
119   /**
120    * Tests that processing is skipped when the input value is empty.
121    */
122   public function testSkipOnEmpty() {
123     $configuration = [
124       'migration' => 'foobaz',
125     ];
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');
130   }
131
132   /**
133    * Tests a successful lookup.
134    *
135    * @dataProvider successfulLookupDataProvider
136    *
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.
145    */
146   public function testSuccessfulLookup($source_id_values, $destination_id_values, $source_value, $expected_value) {
147     $configuration = [
148       'migration' => 'foobaz',
149     ];
150     $this->migration_plugin->id()->willReturn(uniqid());
151
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());
155
156     $this->migration_plugin_manager->createInstances(['foobaz'])
157       ->willReturn(['foobaz' => $this->migration_plugin->reveal()]);
158
159     $migrationStorage = $this->prophesize(EntityStorageInterface::class);
160     $migrationStorage
161       ->loadMultiple(['foobaz'])
162       ->willReturn([$this->migration_plugin->reveal()]);
163
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'));
166   }
167
168   /**
169    * Provides data for the successful lookup test.
170    *
171    * @return array
172    */
173   public function successfulLookupDataProvider() {
174     return [
175       'scalar_to_scalar' => [
176         'source_ids' => [1],
177         'destination_ids' => [3],
178         'input_value' => 1,
179         'expected_value' => 3,
180       ],
181       'scalar_to_array' => [
182         'source_ids' => [1],
183         'destination_ids' => [3, 'foo'],
184         'input_value' => 1,
185         'expected_value' => [3, 'foo'],
186       ],
187       'array_to_scalar' => [
188         'source_ids' => [1, 3],
189         'destination_ids' => ['foo'],
190         'input_value' => [1, 3],
191         'expected_value' => 'foo',
192       ],
193       'array_to_array' => [
194         'source_ids' => [1, 3],
195         'destination_ids' => [3, 'foo'],
196         'input_value' => [1, 3],
197         'expected_value' => [3, 'foo'],
198       ],
199     ];
200   }
201
202 }