Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[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     $this->migration_plugin_manager->createInstances(['foobaz'])
128       ->willReturn(['foobaz' => $this->migration_plugin->reveal()]);
129     $migration = new Migration($configuration, 'migration', [], $this->migration_plugin->reveal(), $this->migration_plugin_manager->reveal(), $this->process_plugin_manager->reveal());
130     $this->setExpectedException(MigrateSkipProcessException::class);
131     $migration->transform(0, $this->migrateExecutable, $this->row, 'foo');
132   }
133
134   /**
135    * Tests a successful lookup.
136    *
137    * @dataProvider successfulLookupDataProvider
138    *
139    * @param array $source_id_values
140    *   The source id(s) of the migration map.
141    * @param array $destination_id_values
142    *   The destination id(s) of the migration map.
143    * @param string|array $source_value
144    *   The source value(s) for the migration process plugin.
145    * @param string|array $expected_value
146    *   The expected value(s) of the migration process plugin.
147    */
148   public function testSuccessfulLookup($source_id_values, $destination_id_values, $source_value, $expected_value) {
149     $configuration = [
150       'migration' => 'foobaz',
151     ];
152     $this->migration_plugin->id()->willReturn(uniqid());
153
154     $id_map = $this->prophesize(MigrateIdMapInterface::class);
155     $id_map->lookupDestinationId($source_id_values)->willReturn($destination_id_values);
156     $this->migration_plugin->getIdMap()->willReturn($id_map->reveal());
157
158     $this->migration_plugin_manager->createInstances(['foobaz'])
159       ->willReturn(['foobaz' => $this->migration_plugin->reveal()]);
160
161     $migrationStorage = $this->prophesize(EntityStorageInterface::class);
162     $migrationStorage
163       ->loadMultiple(['foobaz'])
164       ->willReturn([$this->migration_plugin->reveal()]);
165
166     $migration = new Migration($configuration, 'migration', [], $this->migration_plugin->reveal(), $this->migration_plugin_manager->reveal(), $this->process_plugin_manager->reveal());
167     $this->assertSame($expected_value, $migration->transform($source_value, $this->migrateExecutable, $this->row, 'foo'));
168   }
169
170   /**
171    * Provides data for the successful lookup test.
172    *
173    * @return array
174    */
175   public function successfulLookupDataProvider() {
176     return [
177       'scalar_to_scalar' => [
178         'source_ids' => [1],
179         'destination_ids' => [3],
180         'input_value' => 1,
181         'expected_value' => 3,
182       ],
183       'scalar_to_array' => [
184         'source_ids' => [1],
185         'destination_ids' => [3, 'foo'],
186         'input_value' => 1,
187         'expected_value' => [3, 'foo'],
188       ],
189       'array_to_scalar' => [
190         'source_ids' => [1, 3],
191         'destination_ids' => ['foo'],
192         'input_value' => [1, 3],
193         'expected_value' => 'foo',
194       ],
195       'array_to_array' => [
196         'source_ids' => [1, 3],
197         'destination_ids' => [3, 'foo'],
198         'input_value' => [1, 3],
199         'expected_value' => [3, 'foo'],
200       ],
201     ];
202   }
203
204 }