PageRenderTime 28ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/web/core/modules/migrate/tests/src/Unit/MigrationTest.php

https://gitlab.com/mohamed_hussein/prodt
PHP | 271 lines | 135 code | 39 blank | 97 comment | 2 complexity | 4c2ab2126b5d7754713e4e7159a996ae MD5 | raw file
  1. <?php
  2. /**
  3. * @file
  4. * Contains \Drupal\Tests\migrate\Unit\MigrationTest.
  5. */
  6. namespace Drupal\Tests\migrate\Unit;
  7. use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
  8. use Drupal\migrate\Plugin\MigrationInterface;
  9. use Drupal\migrate\Plugin\Migration;
  10. use Drupal\migrate\Exception\RequirementsException;
  11. use Drupal\migrate\Plugin\MigrateDestinationInterface;
  12. use Drupal\migrate\Plugin\MigrateSourceInterface;
  13. use Drupal\migrate\Plugin\MigrationPluginManagerInterface;
  14. use Drupal\migrate\Plugin\RequirementsInterface;
  15. use Drupal\Tests\UnitTestCase;
  16. /**
  17. * @coversDefaultClass \Drupal\migrate\Plugin\Migration
  18. *
  19. * @group migrate
  20. */
  21. class MigrationTest extends UnitTestCase {
  22. /**
  23. * Tests checking requirements for source plugins.
  24. *
  25. * @covers ::checkRequirements
  26. */
  27. public function testRequirementsForSourcePlugin() {
  28. $migration = new TestMigration();
  29. $source_plugin = $this->createMock('Drupal\Tests\migrate\Unit\RequirementsAwareSourceInterface');
  30. $source_plugin->expects($this->once())
  31. ->method('checkRequirements')
  32. ->willThrowException(new RequirementsException('Missing source requirement', ['key' => 'value']));
  33. $destination_plugin = $this->createMock('Drupal\Tests\migrate\Unit\RequirementsAwareDestinationInterface');
  34. $migration->setSourcePlugin($source_plugin);
  35. $migration->setDestinationPlugin($destination_plugin);
  36. $this->expectException(RequirementsException::class);
  37. $this->expectExceptionMessage('Missing source requirement');
  38. $migration->checkRequirements();
  39. }
  40. /**
  41. * Tests checking requirements for destination plugins.
  42. *
  43. * @covers ::checkRequirements
  44. */
  45. public function testRequirementsForDestinationPlugin() {
  46. $migration = new TestMigration();
  47. $source_plugin = $this->createMock('Drupal\migrate\Plugin\MigrateSourceInterface');
  48. $destination_plugin = $this->createMock('Drupal\Tests\migrate\Unit\RequirementsAwareDestinationInterface');
  49. $destination_plugin->expects($this->once())
  50. ->method('checkRequirements')
  51. ->willThrowException(new RequirementsException('Missing destination requirement', ['key' => 'value']));
  52. $migration->setSourcePlugin($source_plugin);
  53. $migration->setDestinationPlugin($destination_plugin);
  54. $this->expectException(RequirementsException::class);
  55. $this->expectExceptionMessage('Missing destination requirement');
  56. $migration->checkRequirements();
  57. }
  58. /**
  59. * Tests checking requirements for destination plugins.
  60. *
  61. * @covers ::checkRequirements
  62. */
  63. public function testRequirementsForMigrations() {
  64. $migration = new TestMigration();
  65. // Setup source and destination plugins without any requirements.
  66. $source_plugin = $this->createMock('Drupal\migrate\Plugin\MigrateSourceInterface');
  67. $destination_plugin = $this->createMock('Drupal\migrate\Plugin\MigrateDestinationInterface');
  68. $migration->setSourcePlugin($source_plugin);
  69. $migration->setDestinationPlugin($destination_plugin);
  70. $plugin_manager = $this->createMock('Drupal\migrate\Plugin\MigrationPluginManagerInterface');
  71. $migration->setMigrationPluginManager($plugin_manager);
  72. // We setup the requirements that test_a doesn't exist and test_c is not
  73. // completed yet.
  74. $migration->setRequirements(['test_a', 'test_b', 'test_c', 'test_d']);
  75. $migration_b = $this->createMock(MigrationInterface::class);
  76. $migration_c = $this->createMock(MigrationInterface::class);
  77. $migration_d = $this->createMock(MigrationInterface::class);
  78. $migration_b->expects($this->once())
  79. ->method('allRowsProcessed')
  80. ->willReturn(TRUE);
  81. $migration_c->expects($this->once())
  82. ->method('allRowsProcessed')
  83. ->willReturn(FALSE);
  84. $migration_d->expects($this->once())
  85. ->method('allRowsProcessed')
  86. ->willReturn(TRUE);
  87. $plugin_manager->expects($this->once())
  88. ->method('createInstances')
  89. ->with(['test_a', 'test_b', 'test_c', 'test_d'])
  90. ->willReturn(['test_b' => $migration_b, 'test_c' => $migration_c, 'test_d' => $migration_d]);
  91. $this->expectException(RequirementsException::class);
  92. $this->expectExceptionMessage('Missing migrations test_a, test_c');
  93. $migration->checkRequirements();
  94. }
  95. /**
  96. * Tests getting requirement list.
  97. *
  98. * @covers ::getRequirements
  99. */
  100. public function testGetMigrations() {
  101. $migration = new TestMigration();
  102. $requirements = ['test_a', 'test_b', 'test_c', 'test_d'];
  103. $migration->setRequirements($requirements);
  104. $this->assertEquals($requirements, $migration->getRequirements());
  105. }
  106. /**
  107. * Tests valid migration dependencies configuration returns expected values.
  108. *
  109. * @param array|null $source
  110. * The migration dependencies configuration being tested.
  111. * @param array $expected_value
  112. * The migration dependencies configuration array expected.
  113. *
  114. * @covers ::getMigrationDependencies
  115. * @dataProvider getValidMigrationDependenciesProvider
  116. *
  117. * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
  118. */
  119. public function testMigrationDependenciesWithValidConfig($source, array $expected_value) {
  120. $migration = new TestMigration();
  121. if (!is_null($source)) {
  122. $migration->set('migration_dependencies', $source);
  123. }
  124. $this->assertSame($migration->getMigrationDependencies(), $expected_value);
  125. }
  126. /**
  127. * Tests that getting migration dependencies fails with invalid configuration.
  128. *
  129. * @covers ::getMigrationDependencies
  130. */
  131. public function testMigrationDependenciesWithInvalidConfig() {
  132. $migration = new TestMigration();
  133. // Set the plugin ID to test the returned message.
  134. $plugin_id = 'test_migration';
  135. $migration->setPluginId($plugin_id);
  136. // Migration dependencies expects ['optional' => []] or ['required' => []]].
  137. $migration->set('migration_dependencies', ['test_migration_dependency']);
  138. $this->expectException(InvalidPluginDefinitionException::class);
  139. $this->expectExceptionMessage("Invalid migration dependencies configuration for migration {$plugin_id}");
  140. $migration->getMigrationDependencies();
  141. }
  142. /**
  143. * Provides data for valid migration configuration test.
  144. */
  145. public function getValidMigrationDependenciesProvider() {
  146. return [
  147. [
  148. 'source' => NULL,
  149. 'expected_value' => ['required' => [], 'optional' => []],
  150. ],
  151. [
  152. 'source' => [],
  153. 'expected_value' => ['required' => [], 'optional' => []],
  154. ],
  155. [
  156. 'source' => ['required' => ['test_migration']],
  157. 'expected_value' => ['required' => ['test_migration'], 'optional' => []],
  158. ],
  159. [
  160. 'source' => ['optional' => ['test_migration']],
  161. 'expected_value' => ['optional' => ['test_migration'], 'required' => []],
  162. ],
  163. [
  164. 'source' => ['required' => ['req_test_migration'], 'optional' => ['opt_test_migration']],
  165. 'expected_value' => ['required' => ['req_test_migration'], 'optional' => ['opt_test_migration']],
  166. ],
  167. ];
  168. }
  169. }
  170. /**
  171. * Defines the TestMigration class.
  172. */
  173. class TestMigration extends Migration {
  174. /**
  175. * Constructs an instance of TestMigration object.
  176. */
  177. public function __construct() {
  178. }
  179. /**
  180. * Sets the migration ID (machine name).
  181. *
  182. * @param string $plugin_id
  183. * The plugin_id of the plugin instance.
  184. */
  185. public function setPluginId($plugin_id) {
  186. $this->pluginId = $plugin_id;
  187. }
  188. /**
  189. * Sets the requirements values.
  190. *
  191. * @param array $requirements
  192. * The array of requirement values.
  193. */
  194. public function setRequirements(array $requirements) {
  195. $this->requirements = $requirements;
  196. }
  197. /**
  198. * Sets the source Plugin.
  199. *
  200. * @param \Drupal\migrate\Plugin\MigrateSourceInterface $source_plugin
  201. * The source Plugin.
  202. */
  203. public function setSourcePlugin(MigrateSourceInterface $source_plugin) {
  204. $this->sourcePlugin = $source_plugin;
  205. }
  206. /**
  207. * Sets the destination Plugin.
  208. *
  209. * @param \Drupal\migrate\Plugin\MigrateDestinationInterface $destination_plugin
  210. * The destination Plugin.
  211. */
  212. public function setDestinationPlugin(MigrateDestinationInterface $destination_plugin) {
  213. $this->destinationPlugin = $destination_plugin;
  214. }
  215. /**
  216. * Sets the plugin manager service.
  217. *
  218. * @param \Drupal\migrate\Plugin\MigrationPluginManagerInterface $plugin_manager
  219. * The plugin manager service.
  220. */
  221. public function setMigrationPluginManager(MigrationPluginManagerInterface $plugin_manager) {
  222. $this->migrationPluginManager = $plugin_manager;
  223. }
  224. }
  225. /**
  226. * Defines the RequirementsAwareSourceInterface.
  227. */
  228. interface RequirementsAwareSourceInterface extends MigrateSourceInterface, RequirementsInterface {}
  229. /**
  230. * Defines the RequirementsAwareDestinationInterface.
  231. */
  232. interface RequirementsAwareDestinationInterface extends MigrateDestinationInterface, RequirementsInterface {}