View source
<?php
namespace Drupal\Tests\migrate\Kernel;
use Drupal\migrate\MigrateExecutable;
class MigrateConfigRollbackTest extends MigrateTestBase {
protected static $modules = [
'system',
'language',
'config_translation',
];
public function testConfigRollback() {
$variable = [
[
'id' => 'site_name',
'site_name' => 'Some site',
'site_slogan' => 'Awesome slogan',
],
];
$ids = [
'id' => [
'type' => 'string',
],
];
$definition = [
'id' => 'config',
'migration_tags' => [
'Import and rollback test',
],
'source' => [
'plugin' => 'embedded_data',
'data_rows' => $variable,
'ids' => $ids,
],
'process' => [
'name' => 'site_name',
'slogan' => 'site_slogan',
],
'destination' => [
'plugin' => 'config',
'config_name' => 'system.site',
],
];
$config_migration = \Drupal::service('plugin.manager.migration')
->createStubMigration($definition);
$config_id_map = $config_migration
->getIdMap();
$this
->assertFalse($config_migration
->getDestinationPlugin()
->supportsRollback());
$config_executable = new MigrateExecutable($config_migration, $this);
$config_executable
->import();
$config = $this
->config('system.site');
$this
->assertSame('Some site', $config
->get('name'));
$this
->assertSame('Awesome slogan', $config
->get('slogan'));
$map_row = $config_id_map
->getRowBySource([
'id' => $variable[0]['id'],
]);
$this
->assertNotNull($map_row['destid1']);
$config_executable
->rollback();
$config = $this
->config('system.site');
$this
->assertSame('Some site', $config
->get('name'));
$this
->assertSame('Awesome slogan', $config
->get('slogan'));
$this
->assertFalse($config_id_map
->getRowBySource([
'id' => $variable[0]['id'],
]));
$i18n_variable = [
[
'id' => 'site_name',
'language' => 'fr',
'site_name' => 'fr - Some site',
'site_slogan' => 'fr - Awesome slogan',
],
[
'id' => 'site_name',
'language' => 'is',
'site_name' => 'is - Some site',
'site_slogan' => 'is - Awesome slogan',
],
];
$ids = [
'id' => [
'type' => 'string',
],
'language' => [
'type' => 'string',
],
];
$definition = [
'id' => 'i18n_config',
'migration_tags' => [
'Import and rollback test',
],
'source' => [
'plugin' => 'embedded_data',
'data_rows' => $i18n_variable,
'ids' => $ids,
],
'process' => [
'langcode' => 'language',
'name' => 'site_name',
'slogan' => 'site_slogan',
],
'destination' => [
'plugin' => 'config',
'config_name' => 'system.site',
'translations' => 'true',
],
];
$config_migration = \Drupal::service('plugin.manager.migration')
->createStubMigration($definition);
$config_id_map = $config_migration
->getIdMap();
$this
->assertTrue($config_migration
->getDestinationPlugin()
->supportsRollback());
$config_executable = new MigrateExecutable($config_migration, $this);
$config_executable
->import();
$language_manager = \Drupal::service('language_manager');
foreach ($i18n_variable as $row) {
$langcode = $row['language'];
$config_translation = $language_manager
->getLanguageConfigOverride($langcode, 'system.site');
$this
->assertSame($row['site_name'], $config_translation
->get('name'));
$this
->assertSame($row['site_slogan'], $config_translation
->get('slogan'));
$map_row = $config_id_map
->getRowBySource([
'id' => $row['id'],
'language' => $row['language'],
]);
$this
->assertNotNull($map_row['destid1']);
}
$config_executable
->rollback();
foreach ($i18n_variable as $row) {
$langcode = $row['language'];
$config_translation = $language_manager
->getLanguageConfigOverride($langcode, 'system.site');
$this
->assertNull($config_translation
->get('name'));
$this
->assertNull($config_translation
->get('slogan'));
$map_row = $config_id_map
->getRowBySource([
'id' => $row['id'],
'language' => $langcode,
]);
$this
->assertFalse($map_row);
}
$config = $this
->config('system.site');
$this
->assertSame('Some site', $config
->get('name'));
$this
->assertSame('Awesome slogan', $config
->get('slogan'));
}
}