efb831269329806616c84c5816368c3e7896a585
[yaffs-website] / web / core / modules / migrate / tests / src / Kernel / Plugin / MigrationProvidersExistTest.php
1 <?php
2
3 namespace Drupal\Tests\migrate\Kernel\Plugin;
4
5 use Drupal\KernelTests\FileSystemModuleDiscoveryDataProviderTrait;
6 use Drupal\migrate\Plugin\Exception\BadPluginDefinitionException;
7 use Drupal\migrate_drupal\Plugin\MigrateFieldPluginManager;
8 use Drupal\Tests\migrate_drupal\Kernel\MigrateDrupalTestBase;
9
10 /**
11  * Tests that modules exist for all source and destination plugins.
12  *
13  * @group migrate_drupal_ui
14  */
15 class MigrationProvidersExistTest extends MigrateDrupalTestBase {
16
17   use FileSystemModuleDiscoveryDataProviderTrait;
18
19   /**
20    * Tests that a missing source_module property raises an exception.
21    */
22   public function testSourceProvider() {
23     $this->enableModules(['migration_provider_test']);
24     $this->setExpectedException(BadPluginDefinitionException::class, 'The no_source_module plugin must define the source_module property.');
25     $this->container->get('plugin.manager.migration')->getDefinition('migration_provider_no_annotation');
26   }
27
28   /**
29    * Tests that modules exist for all source plugins.
30    */
31   public function testProvidersExist() {
32     $this->enableAllModules();
33
34     /** @var \Drupal\migrate\Plugin\MigrationPluginManager $plugin_manager */
35     $plugin_manager = $this->container->get('plugin.manager.migration');
36
37     // Instantiate all migrations.
38     $migrations = array_keys($plugin_manager->getDefinitions());
39     $migrations = $plugin_manager->createInstances($migrations);
40
41     /** @var \Drupal\migrate\Plugin\MigrationInterface $migration */
42     foreach ($migrations as $migration) {
43       $this->assertInternalType('string', $migration->getSourcePlugin()->getSourceModule());
44     }
45   }
46
47   /**
48    * Enable all available modules.
49    */
50   protected function enableAllModules() {
51     // Install all available modules.
52     $module_handler = $this->container->get('module_handler');
53     $modules = $this->coreModuleListDataProvider();
54     $modules_enabled = $module_handler->getModuleList();
55     $modules_to_enable = array_keys(array_diff_key($modules, $modules_enabled));
56     $this->enableModules($modules_to_enable);
57   }
58
59   /**
60    * Tests that modules exist for all field plugins.
61    */
62   public function testFieldProvidersExist() {
63     $expected_mappings = [
64       'userreference' => [
65         'source_module' => 'userreference',
66         'destination_module' => 'core',
67       ],
68       'nodereference' => [
69         'source_module' => 'nodereference',
70         'destination_module' => 'core',
71       ],
72       'optionwidgets' => [
73         'source_module' => 'optionwidgets',
74         'destination_module' => 'options',
75       ],
76       'list' => [
77         'source_module' => 'list',
78         'destination_module' => 'options',
79       ],
80       'options' => [
81         'source_module' => 'options',
82         'destination_module' => 'options',
83       ],
84       'filefield' => [
85         'source_module' => 'filefield',
86         'destination_module' => 'file',
87       ],
88       'imagefield' => [
89         'source_module' => 'imagefield',
90         'destination_module' => 'image',
91       ],
92       'file' => [
93         'source_module' => 'file',
94         'destination_module' => 'file',
95       ],
96       'image' => [
97         'source_module' => 'image',
98         'destination_module' => 'image',
99       ],
100       'phone' => [
101         'source_module' => 'phone',
102         'destination_module' => 'telephone',
103       ],
104       'link' => [
105         'source_module' => 'link',
106         'destination_module' => 'link',
107       ],
108       'link_field' => [
109         'source_module' => 'link',
110         'destination_module' => 'link',
111       ],
112       'd6_text' => [
113         'source_module' => 'text',
114         'destination_module' => 'text',
115       ],
116       'd7_text' => [
117         'source_module' => 'text',
118         'destination_module' => 'text',
119       ],
120       'taxonomy_term_reference' => [
121         'source_module' => 'taxonomy',
122         'destination_module' => 'core',
123       ],
124       'date' => [
125         'source_module' => 'date',
126         'destination_module' => 'datetime',
127       ],
128       'datetime' => [
129         'source_module' => 'date',
130         'destination_module' => 'datetime',
131       ],
132       'email' => [
133         'source_module' => 'email',
134         'destination_module' => 'core',
135       ],
136       'number_default' => [
137         'source_module' => 'number',
138         'destination_module' => 'core',
139       ],
140       'entityreference' => [
141         'source_module' => 'entityreference',
142         'destination_module' => 'core',
143       ],
144     ];
145     $this->enableAllModules();
146
147     $definitions = $this->container->get('plugin.manager.migrate.field')->getDefinitions();
148     foreach ($definitions as $key => $definition) {
149       $this->assertArrayHasKey($key, $expected_mappings);
150       $this->assertEquals($expected_mappings[$key]['source_module'], $definition['source_module']);
151       $this->assertEquals($expected_mappings[$key]['destination_module'], $definition['destination_module']);
152     }
153   }
154
155   /**
156    * Test a missing required definition.
157    *
158    * @param array $definitions
159    *   A field plugin definition.
160    * @param string $missing_property
161    *   The name of the property missing from the definition.
162    *
163    * @dataProvider fieldPluginDefinitionsProvider
164    */
165   public function testFieldProviderMissingRequiredProperty(array $definitions, $missing_property) {
166     $discovery = $this->getMockBuilder(MigrateFieldPluginManager::class)
167       ->disableOriginalConstructor()
168       ->setMethods(['getDefinitions'])
169       ->getMock();
170     $discovery->method('getDefinitions')
171       ->willReturn($definitions);
172
173     $plugin_manager = $this->getMockBuilder(MigrateFieldPluginManager::class)
174       ->disableOriginalConstructor()
175       ->setMethods(['getDiscovery'])
176       ->getMock();
177     $plugin_manager->method('getDiscovery')
178       ->willReturn($discovery);
179
180     $this->setExpectedException(BadPluginDefinitionException::class, "The missing_{$missing_property} plugin must define the $missing_property property.");
181     $plugin_manager->getDefinitions();
182   }
183
184   /**
185    * Data provider for field plugin definitions.
186    *
187    * @return array
188    *   Array of plugin definitions.
189    */
190   public function fieldPluginDefinitionsProvider() {
191     return [
192       'missing_core_scenario' => [
193         'definitions' => [
194           'missing_core' => [
195             'source_module' => 'migrate',
196             'destination_module' => 'migrate',
197             'id' => 'missing_core',
198             'class' => 'foo',
199             'provider' => 'foo',
200           ],
201         ],
202         'missing_property' => 'core',
203       ],
204       'missing_source_scenario' => [
205         'definitions' => [
206           'missing_source_module' => [
207             'core' => [6, 7],
208             'destination_module' => 'migrate',
209             'id' => 'missing_source_module',
210             'class' => 'foo',
211             'provider' => 'foo',
212           ],
213         ],
214         'missing_property' => 'source_module',
215       ],
216       'missing_destination_scenario' => [
217         'definitions' => [
218           'missing_destination_module' => [
219             'core' => [6, 7],
220             'source_module' => 'migrate',
221             'id' => 'missing_destination_module',
222             'class' => 'foo',
223             'provider' => 'foo',
224           ],
225         ],
226         'missing_property' => 'destination_module',
227       ],
228     ];
229   }
230
231 }