View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\migrate\Unit;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\migrate\Plugin\MigrateDestinationPluginManager;
use Drupal\migrate\Plugin\MigratePluginManagerInterface;
use Drupal\migrate\Plugin\MigrateSourcePluginManager;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Plugin\Migration;
use Drupal\migrate\Exception\RequirementsException;
use Drupal\migrate\Plugin\MigrateDestinationInterface;
use Drupal\migrate\Plugin\MigrateSourceInterface;
use Drupal\migrate\Plugin\MigrationPluginManagerInterface;
use Drupal\migrate\Plugin\RequirementsInterface;
use Drupal\Tests\UnitTestCase;
class MigrationTest extends UnitTestCase {
public function testMigrationDependenciesInConstructor(array $dependencies) {
$configuration = [
'migration_dependencies' => $dependencies,
];
$plugin_id = 'test_migration';
$migration_plugin_manager = $this
->createMock('\\Drupal\\migrate\\Plugin\\MigrationPluginManagerInterface');
$source_plugin_manager = $this
->createMock('\\Drupal\\migrate\\Plugin\\MigratePluginManagerInterface');
$process_plugin_manager = $this
->createMock('\\Drupal\\migrate\\Plugin\\MigratePluginManagerInterface');
$destination_plugin_manager = $this
->createMock('\\Drupal\\migrate\\Plugin\\MigrateDestinationPluginManager');
$id_map_plugin_manager = $this
->createMock('\\Drupal\\migrate\\Plugin\\MigratePluginManagerInterface');
$this
->expectDeprecation("Invalid migration dependencies for {$plugin_id} is deprecated in drupal:10.1.0 and will cause an error in drupal:11.0.0. See https://www.drupal.org/node/3266691");
new Migration($configuration, $plugin_id, [], $migration_plugin_manager, $source_plugin_manager, $process_plugin_manager, $destination_plugin_manager, $id_map_plugin_manager);
}
public function testRequirementsForSourcePlugin() {
$migration = new TestMigration();
$source_plugin = $this
->createMock('Drupal\\Tests\\migrate\\Unit\\RequirementsAwareSourceInterface');
$source_plugin
->expects($this
->once())
->method('checkRequirements')
->willThrowException(new RequirementsException('Missing source requirement', [
'key' => 'value',
]));
$destination_plugin = $this
->createMock('Drupal\\Tests\\migrate\\Unit\\RequirementsAwareDestinationInterface');
$migration
->setSourcePlugin($source_plugin);
$migration
->setDestinationPlugin($destination_plugin);
$this
->expectException(RequirementsException::class);
$this
->expectExceptionMessage('Missing source requirement');
$migration
->checkRequirements();
}
public function testRequirementsForDestinationPlugin() {
$migration = new TestMigration();
$source_plugin = $this
->createMock('Drupal\\migrate\\Plugin\\MigrateSourceInterface');
$destination_plugin = $this
->createMock('Drupal\\Tests\\migrate\\Unit\\RequirementsAwareDestinationInterface');
$destination_plugin
->expects($this
->once())
->method('checkRequirements')
->willThrowException(new RequirementsException('Missing destination requirement', [
'key' => 'value',
]));
$migration
->setSourcePlugin($source_plugin);
$migration
->setDestinationPlugin($destination_plugin);
$this
->expectException(RequirementsException::class);
$this
->expectExceptionMessage('Missing destination requirement');
$migration
->checkRequirements();
}
public function testRequirementsForMigrations() {
$migration = new TestMigration();
$source_plugin = $this
->createMock('Drupal\\migrate\\Plugin\\MigrateSourceInterface');
$destination_plugin = $this
->createMock('Drupal\\migrate\\Plugin\\MigrateDestinationInterface');
$migration
->setSourcePlugin($source_plugin);
$migration
->setDestinationPlugin($destination_plugin);
$plugin_manager = $this
->createMock('Drupal\\migrate\\Plugin\\MigrationPluginManagerInterface');
$migration
->setMigrationPluginManager($plugin_manager);
$migration
->setRequirements([
'test_a',
'test_b',
'test_c',
'test_d',
]);
$migration_b = $this
->createMock(MigrationInterface::class);
$migration_c = $this
->createMock(MigrationInterface::class);
$migration_d = $this
->createMock(MigrationInterface::class);
$migration_b
->expects($this
->once())
->method('allRowsProcessed')
->willReturn(TRUE);
$migration_c
->expects($this
->once())
->method('allRowsProcessed')
->willReturn(FALSE);
$migration_d
->expects($this
->once())
->method('allRowsProcessed')
->willReturn(TRUE);
$plugin_manager
->expects($this
->once())
->method('createInstances')
->with([
'test_a',
'test_b',
'test_c',
'test_d',
])
->willReturn([
'test_b' => $migration_b,
'test_c' => $migration_c,
'test_d' => $migration_d,
]);
$this
->expectException(RequirementsException::class);
$this
->expectExceptionMessage('Missing migrations test_a, test_c');
$migration
->checkRequirements();
}
public function testGetMigrations() {
$migration = new TestMigration();
$requirements = [
'test_a',
'test_b',
'test_c',
'test_d',
];
$migration
->setRequirements($requirements);
$this
->assertEquals($requirements, $migration
->getRequirements());
}
public function testMigrationDependenciesWithValidConfig($source, array $expected_value) {
$migration = new TestMigration();
$plugin_manager = $this
->createMock('Drupal\\migrate\\Plugin\\MigrationPluginManagerInterface');
$migration
->setMigrationPluginManager($plugin_manager);
$plugin_manager
->expects($this
->exactly(2))
->method('expandPluginIds')
->willReturnArgument(0);
if (!is_null($source)) {
$migration
->set('migration_dependencies', $source);
}
$this
->assertSame($migration
->getMigrationDependencies(TRUE), $expected_value);
}
public function testMigrationDependenciesWithInvalidConfig(array $dependencies) {
$migration = new TestMigration();
$plugin_id = 'test_migration';
$migration
->setPluginId($plugin_id);
$this
->expectDeprecation("Invalid migration dependencies for {$plugin_id} is deprecated in drupal:10.1.0 and will cause an error in drupal:11.0.0. See https://www.drupal.org/node/3266691");
$migration
->set('migration_dependencies', $dependencies);
$this
->expectException(InvalidPluginDefinitionException::class);
$this
->expectExceptionMessage("Invalid migration dependencies configuration for migration {$plugin_id}");
$migration
->getMigrationDependencies(TRUE);
}
public static function getValidMigrationDependenciesProvider() {
return [
[
'source' => NULL,
'expected_value' => [
'required' => [],
'optional' => [],
],
],
[
'source' => [],
'expected_value' => [
'required' => [],
'optional' => [],
],
],
[
'source' => [
'required' => [
'test_migration',
],
],
'expected_value' => [
'required' => [
'test_migration',
],
'optional' => [],
],
],
[
'source' => [
'optional' => [
'test_migration',
],
],
'expected_value' => [
'optional' => [
'test_migration',
],
'required' => [],
],
],
[
'source' => [
'required' => [
'req_test_migration',
],
'optional' => [
'opt_test_migration',
],
],
'expected_value' => [
'required' => [
'req_test_migration',
],
'optional' => [
'opt_test_migration',
],
],
],
];
}
public static function getInvalidMigrationDependenciesProvider() {
return [
'invalid key' => [
'dependencies' => [
'bogus' => [],
],
],
'required not array' => [
'dependencies' => [
'required' => 17,
'optional' => [],
],
],
'optional not array' => [
'dependencies' => [
'required' => [],
'optional' => 17,
],
],
];
}
public function testTrackLastImportedDeprecation() {
$this
->expectDeprecation("The key 'trackLastImported' is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. There is no replacement. See https://www.drupal.org/node/3282894");
$migration_plugin_manager = $this
->createMock(MigrationPluginManagerInterface::class);
$source_plugin_manager = $this
->createMock(MigrateSourcePluginManager::class);
$process_Plugin_manager = $this
->createMock(MigratePluginManagerInterface::class);
$destination_plugin_manager = $this
->createMock(MigrateDestinationPluginManager::class);
$id_map_plugin_manager = $this
->createMock(MigratePluginManagerInterface::class);
new Migration([], 'test', [
'trackLastImported' => TRUE,
], $migration_plugin_manager, $source_plugin_manager, $process_Plugin_manager, $destination_plugin_manager, $id_map_plugin_manager);
}
public function testGetMigrationDependencies() {
$migration = new TestMigration();
$this
->expectDeprecation('Calling Migration::getMigrationDependencies() without expanding the plugin IDs is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. In most cases, use getMigrationDependencies(TRUE). See https://www.drupal.org/node/3266691');
$migration
->getMigrationDependencies();
}
}
class TestMigration extends Migration {
public function __construct() {
$this->migration_dependencies = ($this->migration_dependencies ?: []) + [
'required' => [],
'optional' => [],
];
}
public function setPluginId($plugin_id) {
$this->pluginId = $plugin_id;
}
public function setRequirements(array $requirements) {
$this->requirements = $requirements;
}
public function setSourcePlugin(MigrateSourceInterface $source_plugin) {
$this->sourcePlugin = $source_plugin;
}
public function setDestinationPlugin(MigrateDestinationInterface $destination_plugin) {
$this->destinationPlugin = $destination_plugin;
}
public function setMigrationPluginManager(MigrationPluginManagerInterface $plugin_manager) {
$this->migrationPluginManager = $plugin_manager;
}
}
interface RequirementsAwareSourceInterface extends MigrateSourceInterface, RequirementsInterface {
}
interface RequirementsAwareDestinationInterface extends MigrateDestinationInterface, RequirementsInterface {
}