Commit 1380ab9d authored by alexpott's avatar alexpott
Browse files

Issue #2225293 by maxocub, penyaskito, Gábor Hojtsy, quietone, mitrpaka,...

Issue #2225293 by maxocub, penyaskito, Gábor Hojtsy, quietone, mitrpaka, Sonal.Sangale, rvilar, phenaproxima, Ryan Weal: Migrate D6 and D7 language negotiation settings
parent 19006b5e
id: d6_language_negotiation_settings
label: Language negotiation settings
migration_tags:
- Drupal 6
source:
plugin: variable
variables:
- language_negotiation
process:
session/parameter:
plugin: default_value
default_value: 'language'
selected_langcode:
plugin: default_value
default_value: 'site_default'
url/source:
plugin: static_map
source: language_negotiation
default_value: path_prefix
map:
# LANGUAGE_NEGOTIATION_NONE = 0
# LANGUAGE_NEGOTIATION_PATH_DEFAULT = 1
# LANGUAGE_NEGOTIATION_PATH = 2
# LANGUAGE_NEGOTIATION_DOMAIN = 3
0: path_prefix
1: path_prefix
2: path_prefix
3: domain
destination:
plugin: config
config_name: language.negotiation
migration_dependencies:
required:
- language
id: d6_language_types
label: Language types
migration_tags:
- Drupal 6
source:
plugin: variable
variables:
- language_negotiation
process:
all:
plugin: default_value
default_value:
- 'language_interface'
- 'language_content'
- 'language_url'
configurable:
plugin: default_value
default_value:
- 'language_interface'
negotiation/language_content/enabled:
plugin: default_value
default_value:
'language-interface': 0
negotiation/language_url/enabled:
plugin: default_value
default_value:
'language-url': 0
'language-url-fallback': 1
negotiation/language_interface/enabled:
plugin: static_map
source: language_negotiation
map:
# LANGUAGE_NEGOTIATION_NONE = 0
# LANGUAGE_NEGOTIATION_PATH_DEFAULT = 1
# LANGUAGE_NEGOTIATION_PATH = 2
# LANGUAGE_NEGOTIATION_DOMAIN = 3
0:
'language-selected': 0
1:
'language-url': 0
'language-selected': 1
2:
'language-url': 0
'language-user': 1
'language-browser': 2
'language-selected': 3
3:
'language-url': 0
'language-selected': 1
destination:
plugin: config
config_name: language.types
......@@ -8,8 +8,25 @@ source:
- locale_language_negotiation_session_param
- locale_language_negotiation_url_part
process:
'session/parameter': locale_language_negotiation_session_param
'url/source': locale_language_negotiation_url_part
session/parameter:
plugin: default_value
source: locale_language_negotiation_session_param
default_value: 'language'
selected_langcode:
plugin: default_value
default_value: 'site_default'
url/source:
plugin: static_map
source: locale_language_negotiation_url_part
default_value: path_prefix
map:
# LOCALE_LANGUAGE_NEGOTIATION_URL_PREFIX = 0
# LOCALE_LANGUAGE_NEGOTIATION_URL_DOMAIN = 1
0: path_prefix
1: domain
destination:
plugin: config
config_name: language.negotiation
migration_dependencies:
required:
- language
id: language_prefixes_and_domains
label: Language prefixes and domains
migration_tags:
- Drupal 6
- Drupal 7
source:
plugin: language
fetch_all: true
domain_negotiation: true
process:
url/prefixes:
plugin: array_build
source: languages
key: language
value: prefix
url/domains:
plugin: language_domains
source: languages
key: language
value: domain
destination:
plugin: config
config_name: language.negotiation
migration_dependencies:
required:
- language
<?php
namespace Drupal\language\Plugin\migrate\process;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Plugin\migrate\process\ArrayBuild;
use Drupal\migrate\Row;
/**
* This plugin makes sure that no domain is empty if domain negotiation is used.
*
* @MigrateProcessPlugin(
* id = "language_domains",
* handle_multiples = TRUE
* )
*/
class LanguageDomains extends ArrayBuild {
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
if ($row->getSourceProperty('domain_negotiation')) {
global $base_url;
foreach ($value as $old_key => $old_value) {
if (empty($old_value['domain'])) {
// The default language domain might be empty.
// If it is, use the current domain.
$value[$old_key]['domain'] = parse_url($base_url, PHP_URL_HOST);
}
else {
// Ensure we have a protocol when checking for the hostname.
$domain = 'http://' . str_replace(['http://', 'https://'], '', $old_value['domain']);
// Only keep the host part of the domain.
$value[$old_key]['domain'] = parse_url($domain, PHP_URL_HOST);
}
}
}
return parent::transform($value, $migrate_executable, $row, $destination_property);
}
}
......@@ -2,6 +2,7 @@
namespace Drupal\language\Plugin\migrate\source;
use Drupal\migrate\Row;
use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
/**
......@@ -49,4 +50,27 @@ public function query() {
return $this->select('languages')->fields('languages');
}
/**
* {@inheritdoc}
*/
public function prepareRow(Row $row) {
if (!empty($this->configuration['fetch_all'])) {
// Get an array of all languages.
$languages = $this->query()->execute()->fetchAll();
$row->setSourceProperty('languages', $languages);
}
if (!empty($this->configuration['domain_negotiation'])) {
// Check if domain negotiation is used to be able to fill in the default
// language domain, which may be empty. In D6, domain negotiation is used
// when the 'language_negotiation' variable is set to '3', and in D7, when
// the 'locale_language_negotiation_url_part' variable is set to '1'.
if ($this->variableGet('language_negotiation', 0) == 3 || $this->variableGet('locale_language_negotiation_url_part', 0) == 1) {
$row->setSourceProperty('domain_negotiation', TRUE);
}
}
return parent::prepareRow($row);
}
}
<?php
namespace Drupal\Tests\language\Kernel\Migrate\d6;
use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl;
use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase;
/**
* Tests the migration of language negotiation and language types.
*
* @group migrate_drupal_6
*/
class MigrateLanguageNegotiationSettingsTest extends MigrateDrupal6TestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['language'];
/**
* Tests the migration with LANGUAGE_NEGOTIATION_PATH_DEFAULT.
*/
public function testLanguageNegotiationWithDefaultPathPrefix() {
$this->executeMigrations([
'language',
'd6_language_negotiation_settings',
'language_prefixes_and_domains',
'd6_language_types',
]);
$config = $this->config('language.negotiation');
$this->assertSame($config->get('session.parameter'), 'language');
$this->assertSame($config->get('url.source'), LanguageNegotiationUrl::CONFIG_PATH_PREFIX);
$this->assertSame($config->get('selected_langcode'), 'site_default');
$expected_prefixes = [
'en' => '',
'fr' => 'fr',
'zu' => 'zu',
];
$this->assertSame($config->get('url.prefixes'), $expected_prefixes);
$config = $this->config('language.types');
$this->assertSame($config->get('all'), ['language_interface', 'language_content', 'language_url']);
$this->assertSame($config->get('configurable'), ['language_interface']);
$this->assertSame($config->get('negotiation.language_content.enabled'), ['language-interface' => 0]);
$this->assertSame($config->get('negotiation.language_url.enabled'), ['language-url' => 0, 'language-url-fallback' => 1]);
$expected_language_interface = [
'language-url' => 0,
'language-selected' => 1,
];
$this->assertSame($config->get('negotiation.language_interface.enabled'), $expected_language_interface);
}
/**
* Tests the migration with LANGUAGE_NEGOTIATION_NONE.
*/
public function testLanguageNegotiationWithNoNegotiation() {
$this->sourceDatabase->update('variable')
->fields(array('value' => serialize(0)))
->condition('name', 'language_negotiation')
->execute();
$this->executeMigrations([
'language',
'd6_language_negotiation_settings',
'language_prefixes_and_domains',
'd6_language_types',
]);
$config = $this->config('language.negotiation');
$this->assertSame($config->get('session.parameter'), 'language');
$this->assertSame($config->get('url.source'), LanguageNegotiationUrl::CONFIG_PATH_PREFIX);
$this->assertSame($config->get('selected_langcode'), 'site_default');
$config = $this->config('language.types');
$this->assertSame($config->get('all'), ['language_interface', 'language_content', 'language_url']);
$this->assertSame($config->get('configurable'), ['language_interface']);
$this->assertSame($config->get('negotiation.language_content.enabled'), ['language-interface' => 0]);
$this->assertSame($config->get('negotiation.language_url.enabled'), ['language-url' => 0, 'language-url-fallback' => 1]);
$expected_language_interface = [
'language-selected' => 0,
];
$this->assertSame($config->get('negotiation.language_interface.enabled'), $expected_language_interface);
}
/**
* Tests the migration with LANGUAGE_NEGOTIATION_PATH.
*/
public function testLanguageNegotiationWithPathPrefix() {
$this->sourceDatabase->update('variable')
->fields(array('value' => serialize(2)))
->condition('name', 'language_negotiation')
->execute();
$this->executeMigrations([
'language',
'd6_language_negotiation_settings',
'language_prefixes_and_domains',
'd6_language_types',
]);
$config = $this->config('language.negotiation');
$this->assertSame($config->get('session.parameter'), 'language');
$this->assertSame($config->get('url.source'), LanguageNegotiationUrl::CONFIG_PATH_PREFIX);
$this->assertSame($config->get('selected_langcode'), 'site_default');
$expected_prefixes = [
'en' => '',
'fr' => 'fr',
'zu' => 'zu',
];
$this->assertSame($config->get('url.prefixes'), $expected_prefixes);
$config = $this->config('language.types');
$this->assertSame($config->get('all'), ['language_interface', 'language_content', 'language_url']);
$this->assertSame($config->get('configurable'), ['language_interface']);
$this->assertSame($config->get('negotiation.language_content.enabled'), ['language-interface' => 0]);
$this->assertSame($config->get('negotiation.language_url.enabled'), ['language-url' => 0, 'language-url-fallback' => 1]);
$expected_language_interface = [
'language-url' => 0,
'language-user' => 1,
'language-browser' => 2,
'language-selected' => 3,
];
$this->assertSame($config->get('negotiation.language_interface.enabled'), $expected_language_interface);
}
/**
* Tests the migration with LANGUAGE_NEGOTIATION_DOMAIN.
*/
public function testLanguageNegotiationWithDomain() {
$this->sourceDatabase->update('variable')
->fields(array('value' => serialize(3)))
->condition('name', 'language_negotiation')
->execute();
$this->executeMigrations([
'language',
'd6_language_negotiation_settings',
'language_prefixes_and_domains',
'd6_language_types',
]);
global $base_url;
$config = $this->config('language.negotiation');
$this->assertSame($config->get('session.parameter'), 'language');
$this->assertSame($config->get('url.source'), LanguageNegotiationUrl::CONFIG_DOMAIN);
$this->assertSame($config->get('selected_langcode'), 'site_default');
$expected_domains = [
'en' => parse_url($base_url, PHP_URL_HOST),
'fr' => 'fr.drupal.org',
'zu' => 'zu.drupal.org',
];
$this->assertSame($config->get('url.domains'), $expected_domains);
$config = $this->config('language.types');
$this->assertSame($config->get('all'), ['language_interface', 'language_content', 'language_url']);
$this->assertSame($config->get('configurable'), ['language_interface']);
$this->assertSame($config->get('negotiation.language_content.enabled'), ['language-interface' => 0]);
$this->assertSame($config->get('negotiation.language_url.enabled'), ['language-url' => 0, 'language-url-fallback' => 1]);
$expected_language_interface = [
'language-url' => 0,
'language-selected' => 1,
];
$this->assertSame($config->get('negotiation.language_interface.enabled'), $expected_language_interface);
}
}
......@@ -2,46 +2,31 @@
namespace Drupal\Tests\language\Kernel\Migrate\d7;
use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl;
use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
/**
* Tests migration of language negotiation variables.
* Tests the migration of language negotiation.
*
* @group language
* @group migrate_drupal_7
*/
class MigrateLanguageNegotiationSettingsTest extends MigrateDrupal7TestBase {
/**
* Modules to enable.
*
* @var array
* {@inheritdoc}
*/
public static $modules = ['language'];
/**
* {@inheritdoc}
* Tests migration of language types variables to language.types.yml.
*/
protected function setUp() {
parent::setUp();
public function testLanguageTypes() {
$this->executeMigrations([
'language',
'd7_language_negotiation_settings',
'd7_language_types',
]);
}
/**
* Tests migration of language negotiation variables to language.negotiation.yml.
*/
public function testLanguageNegotiation() {
$config = $this->config('language.negotiation');
$this->assertIdentical($config->get('session.parameter'), 'language');
$this->assertIdentical($config->get('url.source'), 'domain');
}
/**
* Tests migration of language types variables to language.types.yml.
*/
public function testLanguageTypes() {
$config = $this->config('language.types');
$this->assertSame($config->get('all'), ['language_content', 'language_url', 'language_interface']);
$this->assertSame($config->get('configurable'), ['language_interface']);
......@@ -64,4 +49,77 @@ public function testLanguageTypes() {
$this->assertSame($config->get('negotiation.language_interface'), $expected_language_interface);
}
/**
* Tests the migration with prefix negotiation.
*/
public function testLanguageNegotiationWithPrefix() {
$this->executeMigrations([
'language',
'd7_language_negotiation_settings',
'language_prefixes_and_domains',
]);
$config = $this->config('language.negotiation');
$this->assertSame($config->get('session.parameter'), 'language');
$this->assertSame($config->get('url.source'), LanguageNegotiationUrl::CONFIG_PATH_PREFIX);
$this->assertSame($config->get('selected_langcode'), 'site_default');
$expected_prefixes = [
'en' => '',
'is' => 'is',
];
$this->assertSame($config->get('url.prefixes'), $expected_prefixes);
}
/**
* Tests the migration with domain negotiation.
*/
public function testLanguageNegotiationWithDomain() {
$this->sourceDatabase->update('variable')
->fields(array('value' => serialize(1)))
->condition('name', 'locale_language_negotiation_url_part')
->execute();
$this->executeMigrations([
'language',
'd7_language_negotiation_settings',
'language_prefixes_and_domains',
]);
global $base_url;
$config = $this->config('language.negotiation');
$this->assertSame($config->get('session.parameter'), 'language');
$this->assertSame($config->get('url.source'), LanguageNegotiationUrl::CONFIG_DOMAIN);
$this->assertSame($config->get('selected_langcode'), 'site_default');
$expected_domains = [
'en' => parse_url($base_url, PHP_URL_HOST),
'is' => 'is.drupal.org',
];
$this->assertSame($config->get('url.domains'), $expected_domains);
}
/**
* Tests the migration with non-existent variables.
*/
public function testLanguageNegotiationWithNonExistentVariables() {
$this->sourceDatabase->delete('variable')
->condition('name', ['local_language_negotiation_url_part', 'local_language_negotiation_session_param'], 'IN')
->execute();
$this->executeMigrations([
'language',
'd6_language_negotiation_settings',
'language_prefixes_and_domains',
]);
$config = $this->config('language.negotiation');
$this->assertSame($config->get('session.parameter'), 'language');
$this->assertSame($config->get('url.source'), LanguageNegotiationUrl::CONFIG_PATH_PREFIX);
$this->assertSame($config->get('selected_langcode'), 'site_default');
$expected_prefixes = [
'en' => '',
'is' => 'is',
];
$this->assertSame($config->get('url.prefixes'), $expected_prefixes);
}
}
<?php
namespace Drupal\Tests\language\Unit\process;
use Drupal\language\Plugin\migrate\process\LanguageDomains;
use Drupal\Tests\migrate\Unit\process\MigrateProcessTestCase;
/**
* @coversDefaultClass \Drupal\language\Plugin\migrate\process\LanguageDomains
* @group language
*/
class LanguageDomainsTest extends MigrateProcessTestCase {
/**
* {@inheritdoc}
*/
protected $backupGlobalsBlacklist = ['base_url'];
/**
* {@inheritdoc}
*/
protected function setUp() {
$configuration = [
'key' => 'language',
'value' => 'domain',
];
$this->plugin = new LanguageDomains($configuration, 'map', []);
parent::setUp();
// The language_domains plugin calls getSourceProperty() to check if domain
// negotiation is used. If it is the values will be processed so we need it
// to return TRUE to be able to test the process.
$this->row->expects($this->once())
->method('getSourceProperty')
->will($this->returnValue(TRUE));
// The language_domains plugin use $base_url to fill empty domains.
global $base_url;
$base_url = 'http://example.com';
}
/**
* @covers ::transform
*/
public function testTransform() {
$source = [
['language' => 'en', 'domain' => ''],
['language' => 'fr', 'domain' => 'fr.example.com'],
['language' => 'es', 'domain' => 'http://es.example.com'],
['language' => 'hu', 'domain' => 'https://hu.example.com'],
];
$expected = [