dce4264bc3993bd83191ba7fd3b89ffbfa85a462
[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\MigrateSourcePluginManager $plugin_manager */
35     $plugin_manager = $this->container->get('plugin.manager.migrate.source');
36
37     foreach ($plugin_manager->getDefinitions() as $definition) {
38       $id = $definition['id'];
39       $this->assertArrayHasKey('source_module', $definition, "No source_module property in '$id'");
40     }
41   }
42
43   /**
44    * Enable all available modules.
45    */
46   protected function enableAllModules() {
47     // Install all available modules.
48     $module_handler = $this->container->get('module_handler');
49     $modules = $this->coreModuleListDataProvider();
50     $modules_enabled = $module_handler->getModuleList();
51     $modules_to_enable = array_keys(array_diff_key($modules, $modules_enabled));
52     $this->enableModules($modules_to_enable);
53   }
54
55   /**
56    * Tests that modules exist for all field plugins.
57    */
58   public function testFieldProvidersExist() {
59     $expected_mappings = [
60       'userreference' => [
61         'source_module' => 'userreference',
62         'destination_module' => 'core',
63       ],
64       'nodereference' => [
65         'source_module' => 'nodereference',
66         'destination_module' => 'core',
67       ],
68       'optionwidgets' => [
69         'source_module' => 'optionwidgets',
70         'destination_module' => 'options',
71       ],
72       'list' => [
73         'source_module' => 'list',
74         'destination_module' => 'options',
75       ],
76       'options' => [
77         'source_module' => 'options',
78         'destination_module' => 'options',
79       ],
80       'filefield' => [
81         'source_module' => 'filefield',
82         'destination_module' => 'file',
83       ],
84       'imagefield' => [
85         'source_module' => 'imagefield',
86         'destination_module' => 'image',
87       ],
88       'file' => [
89         'source_module' => 'file',
90         'destination_module' => 'file',
91       ],
92       'image' => [
93         'source_module' => 'image',
94         'destination_module' => 'image',
95       ],
96       'phone' => [
97         'source_module' => 'phone',
98         'destination_module' => 'telephone',
99       ],
100       'link' => [
101         'source_module' => 'link',
102         'destination_module' => 'link',
103       ],
104       'link_field' => [
105         'source_module' => 'link',
106         'destination_module' => 'link',
107       ],
108       'd6_text' => [
109         'source_module' => 'text',
110         'destination_module' => 'text',
111       ],
112       'd7_text' => [
113         'source_module' => 'text',
114         'destination_module' => 'text',
115       ],
116       'taxonomy_term_reference' => [
117         'source_module' => 'taxonomy',
118         'destination_module' => 'core',
119       ],
120       'date' => [
121         'source_module' => 'date',
122         'destination_module' => 'datetime',
123       ],
124       'datetime' => [
125         'source_module' => 'date',
126         'destination_module' => 'datetime',
127       ],
128       'email' => [
129         'source_module' => 'email',
130         'destination_module' => 'core',
131       ],
132       'number_default' => [
133         'source_module' => 'number',
134         'destination_module' => 'core',
135       ],
136       'entityreference' => [
137         'source_module' => 'entityreference',
138         'destination_module' => 'core',
139       ],
140     ];
141     $this->enableAllModules();
142
143     $definitions = $this->container->get('plugin.manager.migrate.field')->getDefinitions();
144     foreach ($definitions as $key => $definition) {
145       $this->assertArrayHasKey($key, $expected_mappings);
146       $this->assertEquals($expected_mappings[$key]['source_module'], $definition['source_module']);
147       $this->assertEquals($expected_mappings[$key]['destination_module'], $definition['destination_module']);
148     }
149   }
150
151   /**
152    * Test a missing required definition.
153    *
154    * @param array $definitions
155    *   A field plugin definition.
156    * @param string $missing_property
157    *   The name of the property missing from the definition.
158    *
159    * @dataProvider fieldPluginDefinitionsProvider
160    */
161   public function testFieldProviderMissingRequiredProperty(array $definitions, $missing_property) {
162     $discovery = $this->getMockBuilder(MigrateFieldPluginManager::class)
163       ->disableOriginalConstructor()
164       ->setMethods(['getDefinitions'])
165       ->getMock();
166     $discovery->method('getDefinitions')
167       ->willReturn($definitions);
168
169     $plugin_manager = $this->getMockBuilder(MigrateFieldPluginManager::class)
170       ->disableOriginalConstructor()
171       ->setMethods(['getDiscovery'])
172       ->getMock();
173     $plugin_manager->method('getDiscovery')
174       ->willReturn($discovery);
175
176     $this->setExpectedException(BadPluginDefinitionException::class, "The missing_{$missing_property} plugin must define the $missing_property property.");
177     $plugin_manager->getDefinitions();
178   }
179
180   /**
181    * Data provider for field plugin definitions.
182    *
183    * @return array
184    *   Array of plugin definitions.
185    */
186   public function fieldPluginDefinitionsProvider() {
187     return [
188       'missing_core_scenario' => [
189         'definitions' => [
190           'missing_core' => [
191             'source_module' => 'migrate',
192             'destination_module' => 'migrate',
193             'id' => 'missing_core',
194             'class' => 'foo',
195             'provider' => 'foo',
196           ],
197         ],
198         'missing_property' => 'core',
199       ],
200       'missing_source_scenario' => [
201         'definitions' => [
202           'missing_source_module' => [
203             'core' => [6, 7],
204             'destination_module' => 'migrate',
205             'id' => 'missing_source_module',
206             'class' => 'foo',
207             'provider' => 'foo',
208           ],
209         ],
210         'missing_property' => 'source_module',
211       ],
212       'missing_destination_scenario' => [
213         'definitions' => [
214           'missing_destination_module' => [
215             'core' => [6, 7],
216             'source_module' => 'migrate',
217             'id' => 'missing_destination_module',
218             'class' => 'foo',
219             'provider' => 'foo',
220           ],
221         ],
222         'missing_property' => 'destination_module',
223       ],
224     ];
225   }
226
227 }