Move into nested docroot
This commit is contained in:
parent
83a0d3a149
commit
c8b70abde9
13405 changed files with 0 additions and 0 deletions
|
|
@ -0,0 +1,123 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\locale\Kernel;
|
||||
|
||||
use Drupal\block\Entity\Block;
|
||||
use Drupal\language\Entity\ConfigurableLanguage;
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
|
||||
/**
|
||||
* Tests that the locale config manager operates correctly.
|
||||
*
|
||||
* @group locale
|
||||
*/
|
||||
class LocaleConfigManagerTest extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* A list of modules to install for this test.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('system', 'language', 'locale', 'locale_test', 'block');
|
||||
|
||||
/**
|
||||
* This test creates simple config on the fly breaking schema checking.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $strictConfigSchema = FALSE;
|
||||
|
||||
/**
|
||||
* Tests hasTranslation().
|
||||
*/
|
||||
public function testHasTranslation() {
|
||||
$this->installSchema('locale', array('locales_location', 'locales_source', 'locales_target'));
|
||||
$this->installConfig(array('locale_test'));
|
||||
$locale_config_manager = \Drupal::service('locale.config_manager');
|
||||
|
||||
$language = ConfigurableLanguage::createFromLangcode('de');
|
||||
$language->save();
|
||||
$result = $locale_config_manager->hasTranslation('locale_test.no_translation', $language->getId());
|
||||
$this->assertFalse($result, 'There is no translation for locale_test.no_translation configuration.');
|
||||
|
||||
$result = $locale_config_manager->hasTranslation('locale_test.translation', $language->getId());
|
||||
$this->assertTrue($result, 'There is a translation for locale_test.translation configuration.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests getStringTranslation().
|
||||
*/
|
||||
public function testGetStringTranslation() {
|
||||
$this->installSchema('locale', array('locales_location', 'locales_source', 'locales_target'));
|
||||
$this->installConfig(array('locale_test'));
|
||||
|
||||
$locale_config_manager = \Drupal::service('locale.config_manager');
|
||||
|
||||
$language = ConfigurableLanguage::createFromLangcode('de');
|
||||
$language->save();
|
||||
|
||||
$translation_before = $locale_config_manager->getStringTranslation('locale_test.no_translation', $language->getId(), 'Test', '');
|
||||
$this->assertTrue($translation_before->isNew());
|
||||
$translation_before->setString('translation')->save();
|
||||
|
||||
$translation_after = $locale_config_manager->getStringTranslation('locale_test.no_translation', $language->getId(), 'Test', '');
|
||||
$this->assertFalse($translation_after->isNew());
|
||||
$translation_after->setString('updated_translation')->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests getDefaultConfigLangcode().
|
||||
*/
|
||||
public function testGetDefaultConfigLangcode() {
|
||||
// Install the Language module's configuration so we can use the
|
||||
// module_installer service.
|
||||
$this->installConfig(['language']);
|
||||
$this->assertNull(\Drupal::service('locale.config_manager')->getDefaultConfigLangcode('locale_test_translate.settings'), 'Before installing a module the locale config manager can not access the shipped configuration.');
|
||||
\Drupal::service('module_installer')->install(['locale_test_translate']);
|
||||
$this->assertEqual('en', \Drupal::service('locale.config_manager')->getDefaultConfigLangcode('locale_test_translate.settings'), 'After installing a module the locale config manager can get the shipped configuration langcode.');
|
||||
|
||||
$simple_config = \Drupal::configFactory()->getEditable('locale_test_translate.simple_config_extra');
|
||||
$simple_config->set('foo', 'bar')->save();
|
||||
$this->assertNull(\Drupal::service('locale.config_manager')->getDefaultConfigLangcode($simple_config->getName()), 'Simple config created through the API is not treated as shipped configuration.');
|
||||
|
||||
$block = Block::create(array(
|
||||
'id' => 'test_default_config',
|
||||
'theme' => 'classy',
|
||||
'status' => TRUE,
|
||||
'region' => 'content',
|
||||
'plugin' => 'local_tasks_block',
|
||||
'settings' => [
|
||||
'id' => 'local_tasks_block',
|
||||
'label' => $this->randomMachineName(),
|
||||
'provider' => 'core',
|
||||
'label_display' => FALSE,
|
||||
'primary' => TRUE,
|
||||
'secondary' => TRUE,
|
||||
],
|
||||
));
|
||||
$block->save();
|
||||
|
||||
// Install the theme after creating the block as installing the theme will
|
||||
// install the block provided by the locale_test module.
|
||||
\Drupal::service('theme_installer')->install(['classy']);
|
||||
|
||||
// The test_default_config block provided by the locale_test module will not
|
||||
// be installed because a block with the same ID already exists.
|
||||
$this->installConfig(['locale_test']);
|
||||
$this->assertNull(\Drupal::service('locale.config_manager')->getDefaultConfigLangcode('block.block.test_default_config'), 'The block.block.test_default_config is not shipped configuration.');
|
||||
// Delete the block so we can install the one provided by the locale_test
|
||||
// module.
|
||||
$block->delete();
|
||||
$this->installConfig(['locale_test']);
|
||||
$this->assertEqual('en', \Drupal::service('locale.config_manager')->getDefaultConfigLangcode('block.block.test_default_config'), 'The block.block.test_default_config is shipped configuration.');
|
||||
|
||||
// Test the special case for configurable_language config entities.
|
||||
$fr_language = ConfigurableLanguage::createFromLangcode('fr');
|
||||
$fr_language->save();
|
||||
$this->assertEqual('en', \Drupal::service('locale.config_manager')->getDefaultConfigLangcode('language.entity.fr'), 'The language.entity.fr is treated as shipped configuration because it is a configurable_language config entity and in the standard language list.');
|
||||
$custom_language = ConfigurableLanguage::createFromLangcode('custom');
|
||||
$custom_language->save();
|
||||
$this->assertNull(\Drupal::service('locale.config_manager')->getDefaultConfigLangcode('language.entity.custom'), 'The language.entity.custom is not shipped configuration because it is not in the standard language list.');
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,168 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\locale\Kernel;
|
||||
|
||||
use Drupal\Core\DependencyInjection\ContainerBuilder;
|
||||
use Drupal\Core\Language\Language;
|
||||
use Drupal\language\Entity\ConfigurableLanguage;
|
||||
|
||||
/**
|
||||
* Tests default configuration handling with a foreign default language.
|
||||
*
|
||||
* @group locale
|
||||
*/
|
||||
class LocaleConfigSubscriberForeignTest extends LocaleConfigSubscriberTest {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function register(ContainerBuilder $container) {
|
||||
parent::register($container);
|
||||
|
||||
$language = Language::$defaultValues;
|
||||
$language['id'] = 'hu';
|
||||
$language['name'] = 'Hungarian';
|
||||
$container->setParameter('language.default_values', $language);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUpLanguages() {
|
||||
parent::setUpLanguages();
|
||||
ConfigurableLanguage::createFromLangcode('hu')->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUpLocale() {
|
||||
parent::setUpLocale();
|
||||
$this->setUpTranslation('locale_test.translation', 'test', 'English test', 'Hungarian test', 'hu', TRUE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that the language of default configuration was updated.
|
||||
*/
|
||||
public function testDefaultConfigLanguage() {
|
||||
$this->assertEqual('hu', $this->configFactory->getEditable('locale_test.no_translation')->get('langcode'));
|
||||
$this->assertEqual('hu', $this->configFactory->getEditable('locale_test.translation')->get('langcode'));
|
||||
$this->assertEqual($this->configFactory->getEditable('locale_test.translation')->get('test'), 'Hungarian test');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests creating translations of shipped configuration.
|
||||
*/
|
||||
public function testCreateActiveTranslation() {
|
||||
$config_name = 'locale_test.no_translation';
|
||||
$this->saveLanguageActive($config_name, 'test', 'Test (Hungarian)', 'hu');
|
||||
$this->assertTranslation($config_name, 'Test (Hungarian)', 'hu');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests importing community translations of shipped configuration.
|
||||
*/
|
||||
public function testLocaleCreateActiveTranslation() {
|
||||
$config_name = 'locale_test.no_translation';
|
||||
$this->saveLocaleTranslationData($config_name, 'test', 'Test', 'Test (Hungarian)', 'hu', TRUE);
|
||||
$this->assertTranslation($config_name, 'Test (Hungarian)', 'hu', FALSE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests updating translations of shipped configuration.
|
||||
*/
|
||||
public function testUpdateActiveTranslation() {
|
||||
$config_name = 'locale_test.translation';
|
||||
$this->saveLanguageActive($config_name, 'test', 'Updated Hungarian test', 'hu');
|
||||
$this->assertTranslation($config_name, 'Updated Hungarian test', 'hu');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests updating community translations of shipped configuration.
|
||||
*/
|
||||
public function testLocaleUpdateActiveTranslation() {
|
||||
$config_name = 'locale_test.translation';
|
||||
$this->saveLocaleTranslationData($config_name, 'test', 'English test', 'Updated Hungarian test', 'hu', TRUE);
|
||||
$this->assertTranslation($config_name, 'Updated Hungarian test', 'hu', FALSE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests deleting a translation override.
|
||||
*/
|
||||
public function testDeleteTranslation() {
|
||||
$config_name = 'locale_test.translation';
|
||||
$this->deleteLanguageOverride($config_name, 'test', 'English test', 'de');
|
||||
// The German translation in this case will be forced to the Hungarian
|
||||
// source so its not overwritten with locale data later.
|
||||
$this->assertTranslation($config_name, 'Hungarian test', 'de');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests deleting translations of shipped configuration.
|
||||
*/
|
||||
public function testDeleteActiveTranslation() {
|
||||
$config_name = 'locale_test.translation';
|
||||
$this->configFactory->getEditable($config_name)->delete();
|
||||
// Deleting active configuration should not change the locale translation.
|
||||
$this->assertTranslation($config_name, 'Hungarian test', 'hu', FALSE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests deleting community translations of shipped configuration.
|
||||
*/
|
||||
public function testLocaleDeleteActiveTranslation() {
|
||||
$config_name = 'locale_test.translation';
|
||||
$this->deleteLocaleTranslationData($config_name, 'test', 'English test', 'hu');
|
||||
// Deleting the locale translation should not change active config.
|
||||
$this->assertEqual($this->configFactory->getEditable($config_name)->get('test'), 'Hungarian test');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that adding English creates a translation override.
|
||||
*/
|
||||
public function testEnglish() {
|
||||
$config_name = 'locale_test.translation';
|
||||
ConfigurableLanguage::createFromLangcode('en')->save();
|
||||
// Adding a language on the UI would normally call updateConfigTranslations.
|
||||
$this->localeConfigManager->updateConfigTranslations(array($config_name), array('en'));
|
||||
$this->assertConfigOverride($config_name, 'test', 'English test', 'en');
|
||||
|
||||
$this->configFactory->getEditable('locale.settings')->set('translate_english', TRUE)->save();
|
||||
$this->saveLocaleTranslationData($config_name, 'test', 'English test', 'Updated English test', 'en');
|
||||
$this->assertTranslation($config_name, 'Updated English test', 'en', FALSE);
|
||||
|
||||
$this->saveLanguageOverride($config_name, 'test', 'Updated English', 'en');
|
||||
$this->assertTranslation($config_name, 'Updated English', 'en');
|
||||
|
||||
$this->deleteLocaleTranslationData($config_name, 'test', 'English test', 'en');
|
||||
$this->assertNoConfigOverride($config_name, 'en');
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves a language override.
|
||||
*
|
||||
* This will invoke LocaleConfigSubscriber through the event dispatcher. To
|
||||
* make sure the configuration was persisted correctly, the configuration
|
||||
* value is checked. Because LocaleConfigSubscriber temporarily disables the
|
||||
* override state of the configuration factory we check that the correct value
|
||||
* is restored afterwards.
|
||||
*
|
||||
* @param string $config_name
|
||||
* The configuration name.
|
||||
* @param string $key
|
||||
* The configuration key.
|
||||
* @param string $value
|
||||
* The configuration value to save.
|
||||
* @param string $langcode
|
||||
* The language code.
|
||||
*/
|
||||
protected function saveLanguageActive($config_name, $key, $value, $langcode) {
|
||||
$this
|
||||
->configFactory
|
||||
->getEditable($config_name)
|
||||
->set($key, $value)
|
||||
->save();
|
||||
$this->assertActiveConfig($config_name, $key, $value, $langcode);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,486 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\locale\Kernel;
|
||||
|
||||
use Drupal\language\Entity\ConfigurableLanguage;
|
||||
use Drupal\locale\Locale;
|
||||
use Drupal\locale\StringInterface;
|
||||
use Drupal\locale\TranslationString;
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
|
||||
/**
|
||||
* Tests that shipped configuration translations are updated correctly.
|
||||
*
|
||||
* @group locale
|
||||
*/
|
||||
class LocaleConfigSubscriberTest extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['language', 'locale', 'system', 'locale_test'];
|
||||
|
||||
/**
|
||||
* The configurable language manager used in this test.
|
||||
*
|
||||
* @var \Drupal\language\ConfigurableLanguageManagerInterface
|
||||
*/
|
||||
protected $languageManager;
|
||||
|
||||
/**
|
||||
* The configuration factory used in this test.
|
||||
*
|
||||
* @var \Drupal\Core\Config\ConfigFactoryInterface
|
||||
*/
|
||||
protected $configFactory;
|
||||
|
||||
/**
|
||||
* The string storage used in this test.
|
||||
*
|
||||
* @var \Drupal\locale\StringStorageInterface;
|
||||
*/
|
||||
protected $stringStorage;
|
||||
|
||||
/**
|
||||
* The locale configuration manager used in this test.
|
||||
*
|
||||
* @var \Drupal\locale\LocaleConfigManager
|
||||
*/
|
||||
protected $localeConfigManager;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->setUpDefaultLanguage();
|
||||
|
||||
$this->installSchema('locale', ['locales_source', 'locales_target', 'locales_location']);
|
||||
|
||||
$this->setupLanguages();
|
||||
|
||||
$this->installConfig(['locale_test']);
|
||||
// Simulate this hook invoked which would happen if in a non-kernel test
|
||||
// or normal environment.
|
||||
// @see locale_modules_installed()
|
||||
// @see locale_system_update()
|
||||
locale_system_set_config_langcodes();
|
||||
$langcodes = array_keys(\Drupal::languageManager()->getLanguages());
|
||||
$names = Locale::config()->getComponentNames();
|
||||
Locale::config()->updateConfigTranslations($names, $langcodes);
|
||||
|
||||
$this->configFactory = $this->container->get('config.factory');
|
||||
$this->stringStorage = $this->container->get('locale.storage');
|
||||
$this->localeConfigManager = $this->container->get('locale.config_manager');
|
||||
$this->languageManager = $this->container->get('language_manager');
|
||||
|
||||
$this->setUpLocale();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up default language for this test.
|
||||
*/
|
||||
protected function setUpDefaultLanguage() {
|
||||
// Keep the default English.
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up languages needed for this test.
|
||||
*/
|
||||
protected function setUpLanguages() {
|
||||
ConfigurableLanguage::createFromLangcode('de')->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up the locale storage strings to be in line with configuration.
|
||||
*/
|
||||
protected function setUpLocale() {
|
||||
// Set up the locale database the same way we have in the config samples.
|
||||
$this->setUpNoTranslation('locale_test.no_translation', 'test', 'Test', 'de');
|
||||
$this->setUpTranslation('locale_test.translation', 'test', 'English test', 'German test', 'de');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests creating translations of shipped configuration.
|
||||
*/
|
||||
public function testCreateTranslation() {
|
||||
$config_name = 'locale_test.no_translation';
|
||||
|
||||
$this->saveLanguageOverride($config_name, 'test', 'Test (German)', 'de');
|
||||
$this->assertTranslation($config_name, 'Test (German)', 'de');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests importing community translations of shipped configuration.
|
||||
*/
|
||||
public function testLocaleCreateTranslation() {
|
||||
$config_name = 'locale_test.no_translation';
|
||||
|
||||
$this->saveLocaleTranslationData($config_name, 'test', 'Test', 'Test (German)', 'de');
|
||||
$this->assertTranslation($config_name, 'Test (German)', 'de', FALSE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests updating translations of shipped configuration.
|
||||
*/
|
||||
public function testUpdateTranslation() {
|
||||
$config_name = 'locale_test.translation';
|
||||
|
||||
$this->saveLanguageOverride($config_name, 'test', 'Updated German test', 'de');
|
||||
$this->assertTranslation($config_name, 'Updated German test', 'de');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests updating community translations of shipped configuration.
|
||||
*/
|
||||
public function testLocaleUpdateTranslation() {
|
||||
$config_name = 'locale_test.translation';
|
||||
|
||||
$this->saveLocaleTranslationData($config_name, 'test', 'English test', 'Updated German test', 'de');
|
||||
$this->assertTranslation($config_name, 'Updated German test', 'de', FALSE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests deleting translations of shipped configuration.
|
||||
*/
|
||||
public function testDeleteTranslation() {
|
||||
$config_name = 'locale_test.translation';
|
||||
|
||||
$this->deleteLanguageOverride($config_name, 'test', 'English test', 'de');
|
||||
// Instead of deleting the translation, we need to keep a translation with
|
||||
// the source value and mark it as customized to prevent the deletion being
|
||||
// reverted by importing community translations.
|
||||
$this->assertTranslation($config_name, 'English test', 'de');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests deleting community translations of shipped configuration.
|
||||
*/
|
||||
public function testLocaleDeleteTranslation() {
|
||||
$config_name = 'locale_test.translation';
|
||||
|
||||
$this->deleteLocaleTranslationData($config_name, 'test', 'English test', 'de');
|
||||
$this->assertNoTranslation($config_name, 'de');
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up a configuration string without a translation.
|
||||
*
|
||||
* The actual configuration is already available by installing locale_test
|
||||
* module, as it is done in LocaleConfigSubscriberTest::setUp(). This sets up
|
||||
* the necessary source string and verifies that everything is as expected to
|
||||
* avoid false positives.
|
||||
*
|
||||
* @param string $config_name
|
||||
* The configuration name.
|
||||
* @param string $key
|
||||
* The configuration key.
|
||||
* @param string $source
|
||||
* The source string.
|
||||
* @param string $langcode
|
||||
* The language code.
|
||||
*/
|
||||
protected function setUpNoTranslation($config_name, $key, $source, $langcode) {
|
||||
$this->localeConfigManager->updateConfigTranslations(array($config_name), array($langcode));
|
||||
$this->assertNoConfigOverride($config_name, $key, $source, $langcode);
|
||||
$this->assertNoTranslation($config_name, $langcode);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets up a configuration string with a translation.
|
||||
*
|
||||
* The actual configuration is already available by installing locale_test
|
||||
* module, as it is done in LocaleConfigSubscriberTest::setUp(). This sets up
|
||||
* the necessary source and translation strings and verifies that everything
|
||||
* is as expected to avoid false positives.
|
||||
*
|
||||
* @param string $config_name
|
||||
* The configuration name.
|
||||
* @param string $key
|
||||
* The configuration key.
|
||||
* @param string $source
|
||||
* The source string.
|
||||
* @param string $translation
|
||||
* The translation string.
|
||||
* @param string $langcode
|
||||
* The language code.
|
||||
* @param bool $is_active
|
||||
* Whether the update will affect the active configuration.
|
||||
*/
|
||||
protected function setUpTranslation($config_name, $key, $source, $translation, $langcode, $is_active = FALSE) {
|
||||
// Create source and translation strings for the configuration value and add
|
||||
// the configuration name as a location. This would be performed by
|
||||
// locale_translate_batch_import() invoking
|
||||
// LocaleConfigManager::updateConfigTranslations() normally.
|
||||
$this->localeConfigManager->reset();
|
||||
$this->localeConfigManager
|
||||
->getStringTranslation($config_name, $langcode, $source, '')
|
||||
->setString($translation)
|
||||
->setCustomized(FALSE)
|
||||
->save();
|
||||
$this->configFactory->reset($config_name);
|
||||
$this->localeConfigManager->reset();
|
||||
$this->localeConfigManager->updateConfigTranslations(array($config_name), array($langcode));
|
||||
|
||||
if ($is_active) {
|
||||
$this->assertActiveConfig($config_name, $key, $translation, $langcode);
|
||||
}
|
||||
else {
|
||||
$this->assertConfigOverride($config_name, $key, $translation, $langcode);
|
||||
}
|
||||
$this->assertTranslation($config_name, $translation, $langcode, FALSE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves a language override.
|
||||
*
|
||||
* This will invoke LocaleConfigSubscriber through the event dispatcher. To
|
||||
* make sure the configuration was persisted correctly, the configuration
|
||||
* value is checked. Because LocaleConfigSubscriber temporarily disables the
|
||||
* override state of the configuration factory we check that the correct value
|
||||
* is restored afterwards.
|
||||
*
|
||||
* @param string $config_name
|
||||
* The configuration name.
|
||||
* @param string $key
|
||||
* The configuration key.
|
||||
* @param string $value
|
||||
* The configuration value to save.
|
||||
* @param string $langcode
|
||||
* The language code.
|
||||
*/
|
||||
protected function saveLanguageOverride($config_name, $key, $value, $langcode) {
|
||||
$translation_override = $this->languageManager
|
||||
->getLanguageConfigOverride($langcode, $config_name);
|
||||
$translation_override
|
||||
->set($key, $value)
|
||||
->save();
|
||||
$this->configFactory->reset($config_name);
|
||||
|
||||
$this->assertConfigOverride($config_name, $key, $value, $langcode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves translation data from locale module.
|
||||
*
|
||||
* This will invoke LocaleConfigSubscriber through the event dispatcher. To
|
||||
* make sure the configuration was persisted correctly, the configuration
|
||||
* value is checked. Because LocaleConfigSubscriber temporarily disables the
|
||||
* override state of the configuration factory we check that the correct value
|
||||
* is restored afterwards.
|
||||
*
|
||||
* @param string $config_name
|
||||
* The configuration name.
|
||||
* @param string $key
|
||||
* The configuration key.
|
||||
* @param string $source
|
||||
* The source string.
|
||||
* @param string $translation
|
||||
* The translation string to save.
|
||||
* @param string $langcode
|
||||
* The language code.
|
||||
* @param bool $is_active
|
||||
* Whether the update will affect the active configuration.
|
||||
*/
|
||||
protected function saveLocaleTranslationData($config_name, $key, $source, $translation, $langcode, $is_active = FALSE) {
|
||||
$this->localeConfigManager->reset();
|
||||
$this->localeConfigManager
|
||||
->getStringTranslation($config_name, $langcode, $source, '')
|
||||
->setString($translation)
|
||||
->save();
|
||||
$this->localeConfigManager->reset();
|
||||
$this->localeConfigManager->updateConfigTranslations(array($config_name), array($langcode));
|
||||
$this->configFactory->reset($config_name);
|
||||
|
||||
if ($is_active) {
|
||||
$this->assertActiveConfig($config_name, $key, $translation, $langcode);
|
||||
}
|
||||
else {
|
||||
$this->assertConfigOverride($config_name, $key, $translation, $langcode);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a language override.
|
||||
*
|
||||
* This will invoke LocaleConfigSubscriber through the event dispatcher. To
|
||||
* make sure the configuration was persisted correctly, the configuration
|
||||
* value is checked. Because LocaleConfigSubscriber temporarily disables the
|
||||
* override state of the configuration factory we check that the correct value
|
||||
* is restored afterwards.
|
||||
*
|
||||
* @param string $config_name
|
||||
* The configuration name.
|
||||
* @param string $key
|
||||
* The configuration key.
|
||||
* @param string $source_value
|
||||
* The source configuration value to verify the correct value is returned
|
||||
* from the configuration factory after the deletion.
|
||||
* @param string $langcode
|
||||
* The language code.
|
||||
*/
|
||||
protected function deleteLanguageOverride($config_name, $key, $source_value, $langcode) {
|
||||
$translation_override = $this->languageManager
|
||||
->getLanguageConfigOverride($langcode, $config_name);
|
||||
$translation_override
|
||||
->clear($key)
|
||||
->save();
|
||||
$this->configFactory->reset($config_name);
|
||||
|
||||
$this->assertNoConfigOverride($config_name, $key, $source_value, $langcode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes translation data from locale module.
|
||||
*
|
||||
* This will invoke LocaleConfigSubscriber through the event dispatcher. To
|
||||
* make sure the configuration was persisted correctly, the configuration
|
||||
* value is checked. Because LocaleConfigSubscriber temporarily disables the
|
||||
* override state of the configuration factory we check that the correct value
|
||||
* is restored afterwards.
|
||||
*
|
||||
* @param string $config_name
|
||||
* The configuration name.
|
||||
* @param string $key
|
||||
* The configuration key.
|
||||
* @param string $source_value
|
||||
* The source configuration value to verify the correct value is returned
|
||||
* from the configuration factory after the deletion.
|
||||
* @param string $langcode
|
||||
* The language code.
|
||||
*/
|
||||
protected function deleteLocaleTranslationData($config_name, $key, $source_value, $langcode) {
|
||||
$this->localeConfigManager
|
||||
->getStringTranslation($config_name, $langcode, $source_value, '')
|
||||
->delete();
|
||||
$this->localeConfigManager->reset();
|
||||
$this->localeConfigManager->updateConfigTranslations(array($config_name), array($langcode));
|
||||
$this->configFactory->reset($config_name);
|
||||
|
||||
$this->assertNoConfigOverride($config_name, $key, $source_value, $langcode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures configuration override is not present anymore.
|
||||
*
|
||||
* @param string $config_name
|
||||
* The configuration name.
|
||||
* @param string $langcode
|
||||
* The language code.
|
||||
*
|
||||
* @return bool
|
||||
* TRUE if the assertion succeeded, FALSE otherwise.
|
||||
*/
|
||||
protected function assertNoConfigOverride($config_name, $langcode) {
|
||||
$config_langcode = $this->configFactory->getEditable($config_name)->get('langcode');
|
||||
$override = $this->languageManager->getLanguageConfigOverride($langcode, $config_name);
|
||||
return $this->assertNotEqual($config_langcode, $langcode) && $this->assertEqual($override->isNew(), TRUE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures configuration was saved correctly.
|
||||
*
|
||||
* @param string $config_name
|
||||
* The configuration name.
|
||||
* @param string $key
|
||||
* The configuration key.
|
||||
* @param string $value
|
||||
* The configuration value.
|
||||
* @param string $langcode
|
||||
* The language code.
|
||||
*
|
||||
* @return bool
|
||||
* TRUE if the assertion succeeded, FALSE otherwise.
|
||||
*/
|
||||
protected function assertConfigOverride($config_name, $key, $value, $langcode) {
|
||||
$config_langcode = $this->configFactory->getEditable($config_name)->get('langcode');
|
||||
$override = $this->languageManager->getLanguageConfigOverride($langcode, $config_name);
|
||||
return $this->assertNotEqual($config_langcode, $langcode) && $this->assertEqual($override->get($key), $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures configuration was saved correctly.
|
||||
*
|
||||
* @param string $config_name
|
||||
* The configuration name.
|
||||
* @param string $key
|
||||
* The configuration key.
|
||||
* @param string $value
|
||||
* The configuration value.
|
||||
* @param string $langcode
|
||||
* The language code.
|
||||
*
|
||||
* @return bool
|
||||
* TRUE if the assertion succeeded, FALSE otherwise.
|
||||
*/
|
||||
protected function assertActiveConfig($config_name, $key, $value, $langcode) {
|
||||
$config = $this->configFactory->getEditable($config_name);
|
||||
return
|
||||
$this->assertEqual($config->get('langcode'), $langcode) &&
|
||||
$this->assertIdentical($config->get($key), $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures no translation exists.
|
||||
*
|
||||
* @param string $config_name
|
||||
* The configuration name.
|
||||
* @param string $langcode
|
||||
* The language code.
|
||||
*
|
||||
* @return bool
|
||||
* TRUE if the assertion succeeded, FALSE otherwise.
|
||||
*/
|
||||
protected function assertNoTranslation($config_name, $langcode) {
|
||||
$strings = $this->stringStorage->getTranslations([
|
||||
'type' => 'configuration',
|
||||
'name' => $config_name,
|
||||
'language' => $langcode,
|
||||
'translated' => TRUE,
|
||||
]);
|
||||
return $this->assertIdentical([], $strings);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures a translation exists and is marked as customized.
|
||||
*
|
||||
* @param string $config_name
|
||||
* The configuration name.
|
||||
* @param string $translation
|
||||
* The translation.
|
||||
* @param string $langcode
|
||||
* The language code.
|
||||
* @param bool $customized
|
||||
* Whether or not the string should be asserted to be customized or not
|
||||
* customized.
|
||||
*
|
||||
* @return bool
|
||||
* TRUE if the assertion succeeded, FALSE otherwise.
|
||||
*/
|
||||
protected function assertTranslation($config_name, $translation, $langcode, $customized = TRUE) {
|
||||
// Make sure a string exists.
|
||||
$strings = $this->stringStorage->getTranslations([
|
||||
'type' => 'configuration',
|
||||
'name' => $config_name,
|
||||
'language' => $langcode,
|
||||
'translated' => TRUE,
|
||||
]);
|
||||
$pass = $this->assertIdentical(1, count($strings));
|
||||
$string = reset($strings);
|
||||
if ($this->assertTrue($string instanceof StringInterface)) {
|
||||
/** @var \Drupal\locale\StringInterface $string */
|
||||
$pass = $pass && $this->assertIdentical($translation, $string->getString());
|
||||
$pass = $pass && $this->assertTrue($string->isTranslation());
|
||||
if ($this->assertTrue($string instanceof TranslationString)) {
|
||||
/** @var \Drupal\locale\TranslationString $string */
|
||||
// Make sure the string is marked as customized so that it does not get
|
||||
// overridden when the string translations are updated.
|
||||
return $pass && $this->assertEqual($customized, $string->customized);
|
||||
}
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\locale\Kernel;
|
||||
|
||||
use Drupal\Core\Language\LanguageInterface;
|
||||
use Drupal\language\Entity\ConfigurableLanguage;
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
|
||||
|
||||
/**
|
||||
* Tests that the configurable language manager and locale operate correctly.
|
||||
*
|
||||
* @group locale
|
||||
*/
|
||||
class LocaleConfigurableLanguageManagerTest extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* A list of modules to install for this test.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = ['language', 'locale'];
|
||||
|
||||
public function testGetLanguages() {
|
||||
$this->installSchema('locale', ['locales_source', 'locales_target', 'locales_location']);
|
||||
$default_language = new ConfigurableLanguage(['label' => $this->randomMachineName(), 'id' => 'default', 'weight' => 0], 'configurable_language');
|
||||
$default_language->save();
|
||||
|
||||
// Set new default language.
|
||||
\Drupal::service('language.default')->set($default_language);
|
||||
\Drupal::service('string_translation')->setDefaultLangcode($default_language->getId());
|
||||
|
||||
$languages = \Drupal::service('language_manager')->getLanguages(LanguageInterface::STATE_ALL);
|
||||
$this->assertEqual(['default', 'und', 'zxx'], array_keys($languages));
|
||||
|
||||
$configurableLanguage = new ConfigurableLanguage(['label' => $this->randomMachineName(), 'id' => 'test', 'weight' => 1], 'configurable_language');
|
||||
// Simulate a configuration sync by setting the flag otherwise the locked
|
||||
// language weights would be updated whilst saving.
|
||||
// @see \Drupal\language\Entity\ConfigurableLanguage::postSave()
|
||||
$configurableLanguage->setSyncing(TRUE)->save();
|
||||
|
||||
$languages = \Drupal::service('language_manager')->getLanguages(LanguageInterface::STATE_ALL);
|
||||
$this->assertEqual(['default', 'test', 'und', 'zxx'], array_keys($languages));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,101 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\locale\Kernel;
|
||||
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
|
||||
/**
|
||||
* Tests locale translation safe string handling.
|
||||
*
|
||||
* @group locale
|
||||
*/
|
||||
class LocaleStringIsSafeTest extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = ['locale', 'locale_test'];
|
||||
|
||||
/**
|
||||
* Tests for locale_string_is_safe().
|
||||
*/
|
||||
public function testLocaleStringIsSafe() {
|
||||
// Check a translatable string without HTML.
|
||||
$string = 'Hello world!';
|
||||
$result = locale_string_is_safe($string);
|
||||
$this->assertTrue($result);
|
||||
|
||||
// Check a translatable string which includes trustable HTML.
|
||||
$string = 'Hello <strong>world</strong>!';
|
||||
$result = locale_string_is_safe($string);
|
||||
$this->assertTrue($result);
|
||||
|
||||
// Check an untranslatable string which includes untrustable HTML (according
|
||||
// to the locale_string_is_safe() function definition).
|
||||
$string = 'Hello <img src="world.png" alt="world" />!';
|
||||
$result = locale_string_is_safe($string);
|
||||
$this->assertFalse($result);
|
||||
|
||||
// Check a translatable string which includes a token in an href attribute.
|
||||
$string = 'Hi <a href="[current-user:url]">user</a>';
|
||||
$result = locale_string_is_safe($string);
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if a translated and tokenized string is properly escaped by Twig.
|
||||
*
|
||||
* In each assert* call we add a new line at the expected result to match the
|
||||
* newline at the end of the template file.
|
||||
*/
|
||||
public function testLocalizedTokenizedString() {
|
||||
$tests_to_do = [
|
||||
1 => [
|
||||
'original' => 'Go to the <a href="[locale_test:security_test1]">frontpage</a>',
|
||||
'replaced' => 'Go to the <a href="javascript:alert(&#039;Mooooh!&#039;);">frontpage</a>',
|
||||
],
|
||||
2 => [
|
||||
'original' => 'Hello <strong>[locale_test:security_test2]</strong>!',
|
||||
'replaced' => 'Hello <strong>&lt;script&gt;alert(&#039;Mooooh!&#039;);&lt;/script&gt;</strong>!',
|
||||
],
|
||||
];
|
||||
|
||||
foreach ($tests_to_do as $i => $test) {
|
||||
$original_string = $test['original'];
|
||||
$rendered_original_string = \Drupal::theme()->render('locale_test_tokenized', ['content' => $original_string]);
|
||||
// Twig assumes that strings are unsafe so it escapes them, and so the
|
||||
// original and the rendered version should be different.
|
||||
$this->assertNotEqual(
|
||||
$rendered_original_string,
|
||||
$original_string . "\n",
|
||||
'Security test ' . $i . ' before translation'
|
||||
);
|
||||
|
||||
// Pass the original string to the t() function to get it marked as safe.
|
||||
$safe_string = t($original_string);
|
||||
$rendered_safe_string = \Drupal::theme()->render('locale_test_tokenized', ['content' => $safe_string]);
|
||||
// t() function always marks the string as safe so it won't be escaped,
|
||||
// and should be the same as the original.
|
||||
$this->assertEqual(
|
||||
$rendered_safe_string,
|
||||
$original_string . "\n",
|
||||
'Security test ' . $i . ' after translation before token replacement'
|
||||
);
|
||||
|
||||
// Replace tokens in the safe string to inject it with dangerous content.
|
||||
// @see locale_test_tokens().
|
||||
$unsafe_string = \Drupal::token()->replace($safe_string);
|
||||
$rendered_unsafe_string = \Drupal::theme()->render('locale_test_tokenized', ['content' => $unsafe_string]);
|
||||
// Token replacement changes the string so it is not marked as safe
|
||||
// anymore. Check it is escaped the way we expect.
|
||||
$this->assertEqual(
|
||||
$rendered_unsafe_string,
|
||||
$test['replaced'] . "\n",
|
||||
'Security test ' . $i . ' after translation after token replacement'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,64 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\locale\Kernel;
|
||||
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
|
||||
/**
|
||||
* Tests locale translation project handling.
|
||||
*
|
||||
* @group locale
|
||||
*/
|
||||
class LocaleTranslationProjectsTest extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['locale', 'locale_test', 'system'];
|
||||
|
||||
/**
|
||||
* The module handler used in this test.
|
||||
*
|
||||
* @var \Drupal\Core\Extension\ModuleHandlerInterface
|
||||
*/
|
||||
protected $moduleHandler;
|
||||
|
||||
/**
|
||||
* The locale project storage used in this test.
|
||||
*
|
||||
* @var \Drupal\locale\LocaleProjectStorageInterface
|
||||
*/
|
||||
protected $projectStorage;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->moduleHandler = $this->container->get('module_handler');
|
||||
$this->projectStorage = $this->container->get('locale.project');
|
||||
\Drupal::state()->set('locale.remove_core_project', TRUE);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tests locale_translation_clear_cache_projects().
|
||||
*/
|
||||
public function testLocaleTranslationClearCacheProjects() {
|
||||
$this->moduleHandler->loadInclude('locale', 'inc', 'locale.translation');
|
||||
|
||||
$expected = [];
|
||||
$this->assertIdentical($expected, locale_translation_get_projects());
|
||||
|
||||
$this->projectStorage->set('foo', []);
|
||||
$expected['foo'] = new \stdClass();
|
||||
$this->assertEqual($expected, locale_translation_get_projects());
|
||||
|
||||
$this->projectStorage->set('bar', []);
|
||||
locale_translation_clear_cache_projects();
|
||||
$expected['bar'] = new \stdClass();
|
||||
$this->assertEqual($expected, locale_translation_get_projects());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\locale\Kernel\Migrate;
|
||||
|
||||
use Drupal\config\Tests\SchemaCheckTestTrait;
|
||||
use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase;
|
||||
|
||||
/**
|
||||
* Upgrade variables to locale.settings.yml.
|
||||
*
|
||||
* @group migrate_drupal_6
|
||||
*/
|
||||
class MigrateLocaleConfigsTest extends MigrateDrupal6TestBase {
|
||||
|
||||
use SchemaCheckTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = array('locale', 'language');
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->executeMigration('locale_settings');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests migration of locale variables to locale.settings.yml.
|
||||
*/
|
||||
public function testLocaleSettings() {
|
||||
$config = $this->config('locale.settings');
|
||||
$this->assertIdentical(TRUE, $config->get('cache_strings'));
|
||||
$this->assertIdentical('languages', $config->get('javascript.directory'));
|
||||
$this->assertConfigSchema(\Drupal::service('config.typed'), 'locale.settings', $config->get());
|
||||
}
|
||||
|
||||
}
|
||||
269
web/core/modules/locale/tests/src/Unit/LocaleLookupTest.php
Normal file
269
web/core/modules/locale/tests/src/Unit/LocaleLookupTest.php
Normal file
|
|
@ -0,0 +1,269 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\locale\Unit;
|
||||
|
||||
use Drupal\Core\DependencyInjection\ContainerBuilder;
|
||||
use Drupal\locale\LocaleLookup;
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Drupal\locale\LocaleLookup
|
||||
* @group locale
|
||||
*/
|
||||
class LocaleLookupTest extends UnitTestCase {
|
||||
|
||||
/**
|
||||
* A mocked storage to use when instantiating LocaleTranslation objects.
|
||||
*
|
||||
* @var \Drupal\locale\StringStorageInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $storage;
|
||||
|
||||
/**
|
||||
* A mocked cache object.
|
||||
*
|
||||
* @var \Drupal\Core\Cache\CacheBackendInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $cache;
|
||||
|
||||
/**
|
||||
* A mocked lock object.
|
||||
*
|
||||
* @var \Drupal\Core\Lock\LockBackendInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $lock;
|
||||
|
||||
/**
|
||||
* A mocked user object built from AccountInterface.
|
||||
*
|
||||
* @var \Drupal\Core\Session\AccountInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $user;
|
||||
|
||||
/**
|
||||
* A mocked config factory built with UnitTestCase::getConfigFactoryStub().
|
||||
*
|
||||
* @var \Drupal\Core\Config\ConfigFactory|\PHPUnit_Framework_MockObject_MockBuilder
|
||||
*/
|
||||
protected $configFactory;
|
||||
|
||||
/**
|
||||
* A mocked language manager built from LanguageManagerInterface.
|
||||
*
|
||||
* @var \Drupal\Core\Language\LanguageManagerInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $languageManager;
|
||||
|
||||
/**
|
||||
* The request stack.
|
||||
*
|
||||
* @var \Symfony\Component\HttpFoundation\RequestStack
|
||||
*/
|
||||
protected $requestStack;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
$this->storage = $this->getMock('Drupal\locale\StringStorageInterface');
|
||||
$this->cache = $this->getMock('Drupal\Core\Cache\CacheBackendInterface');
|
||||
$this->lock = $this->getMock('Drupal\Core\Lock\LockBackendInterface');
|
||||
$this->lock->expects($this->never())
|
||||
->method($this->anything());
|
||||
|
||||
$this->user = $this->getMock('Drupal\Core\Session\AccountInterface');
|
||||
$this->user->expects($this->any())
|
||||
->method('getRoles')
|
||||
->will($this->returnValue(array('anonymous')));
|
||||
|
||||
$this->configFactory = $this->getConfigFactoryStub(array('locale.settings' => array('cache_strings' => FALSE)));
|
||||
|
||||
$this->languageManager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface');
|
||||
$this->requestStack = new RequestStack();
|
||||
|
||||
$container = new ContainerBuilder();
|
||||
$container->set('current_user', $this->user);
|
||||
\Drupal::setContainer($container);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests locale lookups without fallback.
|
||||
*
|
||||
* @covers ::resolveCacheMiss
|
||||
*/
|
||||
public function testResolveCacheMissWithoutFallback() {
|
||||
$args = array(
|
||||
'language' => 'en',
|
||||
'source' => 'test',
|
||||
'context' => 'irrelevant',
|
||||
);
|
||||
|
||||
$result = (object) array(
|
||||
'translation' => 'test',
|
||||
);
|
||||
|
||||
$this->cache->expects($this->once())
|
||||
->method('get')
|
||||
->with('locale:en:irrelevant:anonymous', FALSE);
|
||||
|
||||
$this->storage->expects($this->once())
|
||||
->method('findTranslation')
|
||||
->with($this->equalTo($args))
|
||||
->will($this->returnValue($result));
|
||||
|
||||
$locale_lookup = $this->getMockBuilder('Drupal\locale\LocaleLookup')
|
||||
->setConstructorArgs(array('en', 'irrelevant', $this->storage, $this->cache, $this->lock, $this->configFactory, $this->languageManager, $this->requestStack))
|
||||
->setMethods(array('persist'))
|
||||
->getMock();
|
||||
$locale_lookup->expects($this->never())
|
||||
->method('persist');
|
||||
$this->assertSame('test', $locale_lookup->get('test'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests locale lookups with fallback.
|
||||
*
|
||||
* Note that context is irrelevant here. It is not used but it is required.
|
||||
*
|
||||
* @covers ::resolveCacheMiss
|
||||
*
|
||||
* @dataProvider resolveCacheMissWithFallbackProvider
|
||||
*/
|
||||
public function testResolveCacheMissWithFallback($langcode, $string, $context, $expected) {
|
||||
// These are fake words!
|
||||
$translations = array(
|
||||
'en' => array(
|
||||
'test' => 'test',
|
||||
'fake' => 'fake',
|
||||
'missing pl' => 'missing pl',
|
||||
'missing cs' => 'missing cs',
|
||||
'missing both' => 'missing both',
|
||||
),
|
||||
'pl' => array(
|
||||
'test' => 'test po polsku',
|
||||
'fake' => 'ściema',
|
||||
'missing cs' => 'zaginiony czech',
|
||||
),
|
||||
'cs' => array(
|
||||
'test' => 'test v české',
|
||||
'fake' => 'falešný',
|
||||
'missing pl' => 'chybějící pl',
|
||||
),
|
||||
);
|
||||
$this->storage->expects($this->any())
|
||||
->method('findTranslation')
|
||||
->will($this->returnCallback(function ($argument) use ($translations) {
|
||||
if (isset($translations[$argument['language']][$argument['source']])) {
|
||||
return (object) array('translation' => $translations[$argument['language']][$argument['source']]);
|
||||
}
|
||||
return TRUE;
|
||||
}));
|
||||
|
||||
$this->languageManager->expects($this->any())
|
||||
->method('getFallbackCandidates')
|
||||
->will($this->returnCallback(function (array $context = array()) {
|
||||
switch ($context['langcode']) {
|
||||
case 'pl':
|
||||
return array('cs', 'en');
|
||||
|
||||
case 'cs':
|
||||
return array('en');
|
||||
|
||||
default:
|
||||
return array();
|
||||
}
|
||||
}));
|
||||
|
||||
$this->cache->expects($this->once())
|
||||
->method('get')
|
||||
->with('locale:' . $langcode . ':' . $context . ':anonymous', FALSE);
|
||||
|
||||
$locale_lookup = new LocaleLookup($langcode, $context, $this->storage, $this->cache, $this->lock, $this->configFactory, $this->languageManager, $this->requestStack);
|
||||
$this->assertSame($expected, $locale_lookup->get($string));
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides test data for testResolveCacheMissWithFallback().
|
||||
*/
|
||||
public function resolveCacheMissWithFallbackProvider() {
|
||||
return array(
|
||||
array('cs', 'test', 'irrelevant', 'test v české'),
|
||||
array('cs', 'fake', 'irrelevant', 'falešný'),
|
||||
array('cs', 'missing pl', 'irrelevant', 'chybějící pl'),
|
||||
array('cs', 'missing cs', 'irrelevant', 'missing cs'),
|
||||
array('cs', 'missing both', 'irrelevant', 'missing both'),
|
||||
|
||||
// Testing PL with fallback to cs, en.
|
||||
array('pl', 'test', 'irrelevant', 'test po polsku'),
|
||||
array('pl', 'fake', 'irrelevant', 'ściema'),
|
||||
array('pl', 'missing pl', 'irrelevant', 'chybějící pl'),
|
||||
array('pl', 'missing cs', 'irrelevant', 'zaginiony czech'),
|
||||
array('pl', 'missing both', 'irrelevant', 'missing both'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests locale lookups with persistent tracking.
|
||||
*
|
||||
* @covers ::resolveCacheMiss
|
||||
*/
|
||||
public function testResolveCacheMissWithPersist() {
|
||||
$args = array(
|
||||
'language' => 'en',
|
||||
'source' => 'test',
|
||||
'context' => 'irrelevant',
|
||||
);
|
||||
|
||||
$result = (object) array(
|
||||
'translation' => 'test',
|
||||
);
|
||||
|
||||
$this->storage->expects($this->once())
|
||||
->method('findTranslation')
|
||||
->with($this->equalTo($args))
|
||||
->will($this->returnValue($result));
|
||||
|
||||
$this->configFactory = $this->getConfigFactoryStub(array('locale.settings' => array('cache_strings' => TRUE)));
|
||||
$locale_lookup = $this->getMockBuilder('Drupal\locale\LocaleLookup')
|
||||
->setConstructorArgs(array('en', 'irrelevant', $this->storage, $this->cache, $this->lock, $this->configFactory, $this->languageManager, $this->requestStack))
|
||||
->setMethods(array('persist'))
|
||||
->getMock();
|
||||
$locale_lookup->expects($this->once())
|
||||
->method('persist');
|
||||
|
||||
$this->assertSame('test', $locale_lookup->get('test'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests locale lookups without a found translation.
|
||||
*
|
||||
* @covers ::resolveCacheMiss
|
||||
*/
|
||||
public function testResolveCacheMissNoTranslation() {
|
||||
$string = $this->getMock('Drupal\locale\StringInterface');
|
||||
$string->expects($this->once())
|
||||
->method('addLocation')
|
||||
->will($this->returnSelf());
|
||||
$this->storage->expects($this->once())
|
||||
->method('findTranslation')
|
||||
->will($this->returnValue(NULL));
|
||||
$this->storage->expects($this->once())
|
||||
->method('createString')
|
||||
->will($this->returnValue($string));
|
||||
|
||||
$request = Request::create('/test');
|
||||
$this->requestStack->push($request);
|
||||
|
||||
$locale_lookup = $this->getMockBuilder('Drupal\locale\LocaleLookup')
|
||||
->setConstructorArgs(array('en', 'irrelevant', $this->storage, $this->cache, $this->lock, $this->configFactory, $this->languageManager, $this->requestStack))
|
||||
->setMethods(array('persist'))
|
||||
->getMock();
|
||||
$locale_lookup->expects($this->never())
|
||||
->method('persist');
|
||||
|
||||
$this->assertTrue($locale_lookup->get('test'));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,57 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\locale\Unit;
|
||||
|
||||
use Drupal\locale\LocaleTranslation;
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Drupal\locale\LocaleTranslation
|
||||
* @group locale
|
||||
*/
|
||||
class LocaleTranslationTest extends UnitTestCase {
|
||||
|
||||
/**
|
||||
* A mocked storage to use when instantiating LocaleTranslation objects.
|
||||
*
|
||||
* @var \PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $storage;
|
||||
|
||||
/**
|
||||
* A mocked language manager built from LanguageManagerInterface.
|
||||
*
|
||||
* @var \Drupal\Core\Language\LanguageManagerInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $languageManager;
|
||||
|
||||
/**
|
||||
* The request stack.
|
||||
*
|
||||
* @var \Symfony\Component\HttpFoundation\RequestStack
|
||||
*/
|
||||
protected $requestStack;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
$this->storage = $this->getMock('Drupal\locale\StringStorageInterface');
|
||||
$this->cache = $this->getMock('Drupal\Core\Cache\CacheBackendInterface');
|
||||
$this->lock = $this->getMock('Drupal\Core\Lock\LockBackendInterface');
|
||||
$this->languageManager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface');
|
||||
$this->requestStack = new RequestStack();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests for \Drupal\locale\LocaleTranslation::destruct().
|
||||
*/
|
||||
public function testDestruct() {
|
||||
$translation = new LocaleTranslation($this->storage, $this->cache, $this->lock, $this->getConfigFactoryStub(), $this->languageManager, $this->requestStack);
|
||||
// Prove that destruction works without errors when translations are empty.
|
||||
$this->assertAttributeEmpty('translations', $translation);
|
||||
$translation->destruct();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\locale\Unit\Menu;
|
||||
|
||||
use Drupal\Tests\Core\Menu\LocalTaskIntegrationTestBase;
|
||||
|
||||
/**
|
||||
* Tests locale local tasks.
|
||||
*
|
||||
* @group locale
|
||||
*/
|
||||
class LocaleLocalTasksTest extends LocalTaskIntegrationTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
$this->directoryList = array(
|
||||
'locale' => 'core/modules/locale',
|
||||
);
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks locale listing local tasks.
|
||||
*
|
||||
* @dataProvider getLocalePageRoutes
|
||||
*/
|
||||
public function testLocalePageLocalTasks($route) {
|
||||
$tasks = array(
|
||||
0 => array('locale.translate_page', 'locale.translate_import', 'locale.translate_export', 'locale.settings'),
|
||||
);
|
||||
$this->assertLocalTasks($route, $tasks);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides a list of routes to test.
|
||||
*/
|
||||
public function getLocalePageRoutes() {
|
||||
return array(
|
||||
array('locale.translate_page'),
|
||||
array('locale.translate_import'),
|
||||
array('locale.translate_export'),
|
||||
array('locale.settings'),
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
35
web/core/modules/locale/tests/src/Unit/StringBaseTest.php
Normal file
35
web/core/modules/locale/tests/src/Unit/StringBaseTest.php
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\locale\Unit;
|
||||
|
||||
use Drupal\locale\SourceString;
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Drupal\locale\StringBase
|
||||
* @group locale
|
||||
*/
|
||||
class StringBaseTest extends UnitTestCase {
|
||||
|
||||
/**
|
||||
* @covers ::save
|
||||
* @expectedException \Drupal\locale\StringStorageException
|
||||
* @expectedExceptionMessage The string cannot be saved because its not bound to a storage: test
|
||||
*/
|
||||
public function testSaveWithoutStorage() {
|
||||
$string = new SourceString(['source' => 'test']);
|
||||
$string->save();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @covers ::delete
|
||||
* @expectedException \Drupal\locale\StringStorageException
|
||||
* @expectedExceptionMessage The string cannot be deleted because its not bound to a storage: test
|
||||
*/
|
||||
public function testDeleteWithoutStorage() {
|
||||
$string = new SourceString(['lid' => 1, 'source' => 'test']);
|
||||
$string->delete();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in a new issue