View source
<?php
namespace Drupal\Tests\commerce_migrate_ubercart\Kernel\Traits;
use Drupal\commerce_migrate\Utility;
use Drupal\commerce_migrate_ubercart\Plugin\migrate\source\uc6\Product as Uc6Product;
use Drupal\commerce_migrate_ubercart\Plugin\migrate\source\uc7\Product as Uc7Product;
use Drupal\Component\Discovery\YamlDiscovery;
use Drupal\KernelTests\FileSystemModuleDiscoveryDataProviderTrait;
use Drupal\migrate_drupal\MigrationConfigurationTrait;
use Drupal\migrate_drupal\MigrationState;
trait ValidateMigrationStateTestTrait {
use FileSystemModuleDiscoveryDataProviderTrait;
use MigrationConfigurationTrait;
public function testMigrationState() {
$separator = ',';
$this
->enableAllModules();
$version = (string) $this
->getLegacyDrupalVersion($this->sourceDatabase);
$discovered = [];
$plugin_manager = $this->container
->get('plugin.manager.migration');
$migrations = $plugin_manager
->createInstancesByTag('Drupal ' . $version);
foreach ($migrations as $migration) {
$definition = $migration
->getPluginDefinition();
if (is_array($definition['provider'])) {
$provider = reset($definition['provider']);
}
else {
$provider = $definition['provider'];
}
$source_module = $migration
->getSourcePlugin()
->getSourceModule();
$destination_module = $migration
->getDestinationPlugin()
->getDestinationModule();
$source_plugin = $migration
->getSourcePlugin();
if (Utility::classInArray($source_plugin, [
Uc6Product::class,
Uc7Product::class,
])) {
$provider = 'commerce_migrate_ubercart';
}
$discovered[] = implode($separator, [
$provider,
$source_module,
$destination_module,
]);
}
$definitions = $this->container
->get('plugin.manager.migrate.field')
->getDefinitions();
foreach ($definitions as $key => $definition) {
if (isset($definition['core'][$version])) {
$discovered[] = implode($separator, [
$definition['provider'],
$definition['source_module'],
$definition['destination_module'],
]);
}
}
$system_info = (new YamlDiscovery('migrate_drupal', array_map(function (&$value) {
return $value . '/migrations/state/';
}, \Drupal::moduleHandler()
->getModuleDirectories())))
->findAll();
$declared = [];
$declared[MigrationState::FINISHED] = [];
$declared[MigrationState::NOT_FINISHED] = [];
$states = [
MigrationState::FINISHED,
MigrationState::NOT_FINISHED,
];
foreach ($system_info as $module => $info) {
foreach ($states as $state) {
if (isset($info[$state][$version])) {
foreach ($info[$state][$version] as $source => $destination) {
if ($source !== 'i18nstrings' && $source !== 'i18n_string') {
foreach ((array) $destination as $dest) {
$key = [
$module,
$source,
trim($dest),
];
$declared[$state][] = implode($separator, $key);
}
}
}
}
}
}
sort($declared[MigrationState::FINISHED]);
sort($declared[MigrationState::NOT_FINISHED]);
$declared_unique[MigrationState::FINISHED] = array_unique($declared[MigrationState::FINISHED]);
$declared_unique[MigrationState::NOT_FINISHED] = array_unique($declared[MigrationState::NOT_FINISHED]);
sort($discovered);
$discovered_unique = array_unique($discovered);
foreach ($discovered_unique as $datum) {
$data = str_getcsv($datum);
$in_finished = in_array($datum, $declared_unique[MigrationState::FINISHED]);
$in_not_finished = in_array($datum, $declared_unique[MigrationState::NOT_FINISHED]);
$found = $in_finished || $in_not_finished;
$this
->assertTrue($found, sprintf("No migration state found for version '%s' with source_module '%s' and destination_module '%s' declared in module '%s'", $version, $data[1], $data[2], $data[0]));
}
$discovered_not_finished = array_diff($discovered_unique, $declared_unique[MigrationState::FINISHED]);
foreach ($discovered_not_finished as $datum) {
$data = str_getcsv($datum);
$this
->assertContains($datum, $declared_unique[MigrationState::NOT_FINISHED], sprintf("No migration found for version '%s' with source_module '%s' and destination_module '%s' declared in module '%s'", $version, $data[1], $data[2], $data[0]));
}
}
protected function enableAllModules() {
$module_handler = $this->container
->get('module_handler');
$modules = $this
->coreModuleListDataProvider();
$modules_enabled = $module_handler
->getModuleList();
$modules_to_enable = array_keys(array_diff_key($modules, $modules_enabled));
$this
->enableModules($modules_to_enable);
return $modules;
}
}