View source
<?php
namespace Drupal\Tests\migrate_upgrade\Functional;
use Drupal\Core\Plugin\PluginBase;
use Drupal\KernelTests\FileSystemModuleDiscoveryDataProviderTrait;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate_plus\Entity\Migration;
use Drupal\Tests\migrate_drupal_ui\Functional\MigrateUpgradeTestBase;
use Drush\TestTraits\DrushTestTrait;
class MigrateUpgradeCommandsTest extends MigrateUpgradeTestBase {
use DrushTestTrait;
use FileSystemModuleDiscoveryDataProviderTrait;
protected $defaultTheme = 'stark';
protected function setUp() : void {
self::$modules = array_merge(array_keys($this
->coreModuleListDataProvider()), [
'migrate_plus',
'migrate_upgrade',
]);
self::$modules = array_diff(self::$modules, [
'block_place',
]);
parent::setUp();
}
public function testDrupalConfigureUpgrade($drupal_version) : void {
$this
->loadFixture(drupal_get_path('module', 'migrate_drupal') . "/tests/fixtures/drupal{$drupal_version}.php");
$prefix = 'upgrade_legacy_';
$this
->executeMigrateUpgrade([
'configure-only' => NULL,
'migration-prefix' => $prefix,
]);
$expected = [
"{$prefix}action_settings" => [
'original' => 'action_settings',
'generated' => "{$prefix}action_settings",
],
"{$prefix}book_settings" => [
'original' => 'book_settings',
'generated' => "{$prefix}book_settings",
],
];
$this
->assertEquals($this
->getOutputFromJSON(), array_replace_recursive($expected, $this
->getOutputFromJSON()));
$migrate_plus_migrations = Migration::loadMultiple();
$migrations = $this
->getMigrations($this->sourceDatabase
->getKey(), $drupal_version);
$this
->assertMigrations($prefix, $migrations, $migrate_plus_migrations);
$optional = array_flip($migrate_plus_migrations["{$prefix}d{$drupal_version}_url_alias"]
->toArray()['migration_dependencies']['optional']);
$node_migrations = array_intersect_key([
"{$prefix}d{$drupal_version}_node_translation_page" => TRUE,
"{$prefix}d{$drupal_version}_node_complete_page" => TRUE,
], $optional);
$this
->assertNotEmpty($node_migrations);
}
public function majorDrupalVersionsDataProvider() : array {
$version = [];
$version['drupal 6'] = [
6,
];
$version['drupal 7'] = [
7,
];
return $version;
}
protected function executeMigrateUpgrade(array $options = []) : void {
$options += [
'format' => 'json',
'fields' => '*',
];
$connection_options = $this->sourceDatabase
->getConnectionOptions();
$options['legacy-db-url'] = $this
->convertDbSpecUrl($connection_options);
if (!empty($connection_options['prefix']['default'])) {
$options['legacy-db-prefix'] = $connection_options['prefix']['default'];
}
$this
->drush('migrate:upgrade', [], $options);
}
protected function assertMigrations($prefix, array $migrations, array $migrate_plus_migrations) : void {
$available_migrations = array_flip(array_map(static function (MigrationInterface $migration) use ($prefix) {
if (strpos($migration
->id(), $prefix) === 0) {
return $migration
->id();
}
return $prefix . str_replace(PluginBase::DERIVATIVE_SEPARATOR, '_', $migration
->id());
}, $migrations));
$this
->assertEmpty(array_diff_key($available_migrations, $migrate_plus_migrations));
}
protected function convertDbSpecUrl(array $db_spec) : string {
if ($db_spec['driver'] === 'sqlite') {
return 'sqlite://' . $db_spec['database'];
}
$url = $db_spec['driver'] . '://' . $db_spec['username'] . ':' . $db_spec['password'] . '@' . $db_spec['host'];
if (isset($db_spec['port'])) {
$url .= ':' . $db_spec['port'];
}
return $url . '/' . $db_spec['database'];
}
protected function getSourceBasePath() : string {
return '';
}
protected function getEntityCounts() : array {
return [];
}
protected function getAvailablePaths() : array {
return [];
}
protected function getMissingPaths() : array {
return [];
}
protected function getEntityCountsIncremental() : array {
return [];
}
}