3 namespace Drupal\Tests\locale\Kernel;
5 use Drupal\block\Entity\Block;
6 use Drupal\language\Entity\ConfigurableLanguage;
7 use Drupal\KernelTests\KernelTestBase;
10 * Tests that the locale config manager operates correctly.
14 class LocaleConfigManagerTest extends KernelTestBase {
17 * A list of modules to install for this test.
21 public static $modules = ['system', 'language', 'locale', 'locale_test', 'block'];
24 * This test creates simple config on the fly breaking schema checking.
28 protected $strictConfigSchema = FALSE;
31 * Tests hasTranslation().
33 public function testHasTranslation() {
34 $this->installSchema('locale', ['locales_location', 'locales_source', 'locales_target']);
35 $this->installConfig(['locale_test']);
36 $locale_config_manager = \Drupal::service('locale.config_manager');
38 $language = ConfigurableLanguage::createFromLangcode('de');
40 $result = $locale_config_manager->hasTranslation('locale_test.no_translation', $language->getId());
41 $this->assertFalse($result, 'There is no translation for locale_test.no_translation configuration.');
43 $result = $locale_config_manager->hasTranslation('locale_test.translation', $language->getId());
44 $this->assertTrue($result, 'There is a translation for locale_test.translation configuration.');
48 * Tests getStringTranslation().
50 public function testGetStringTranslation() {
51 $this->installSchema('locale', ['locales_location', 'locales_source', 'locales_target']);
52 $this->installConfig(['locale_test']);
54 $locale_config_manager = \Drupal::service('locale.config_manager');
56 $language = ConfigurableLanguage::createFromLangcode('de');
59 $translation_before = $locale_config_manager->getStringTranslation('locale_test.no_translation', $language->getId(), 'Test', '');
60 $this->assertTrue($translation_before->isNew());
61 $translation_before->setString('translation')->save();
63 $translation_after = $locale_config_manager->getStringTranslation('locale_test.no_translation', $language->getId(), 'Test', '');
64 $this->assertFalse($translation_after->isNew());
65 $translation_after->setString('updated_translation')->save();
69 * Tests getDefaultConfigLangcode().
71 public function testGetDefaultConfigLangcode() {
72 // Install the Language module's configuration so we can use the
73 // module_installer service.
74 $this->installConfig(['language']);
75 $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.');
76 \Drupal::service('module_installer')->install(['locale_test_translate']);
77 $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.');
79 $simple_config = \Drupal::configFactory()->getEditable('locale_test_translate.simple_config_extra');
80 $simple_config->set('foo', 'bar')->save();
81 $this->assertNull(\Drupal::service('locale.config_manager')->getDefaultConfigLangcode($simple_config->getName()), 'Simple config created through the API is not treated as shipped configuration.');
83 $block = Block::create([
84 'id' => 'test_default_config',
87 'region' => 'content',
88 'plugin' => 'local_tasks_block',
90 'id' => 'local_tasks_block',
91 'label' => $this->randomMachineName(),
93 'label_display' => FALSE,
100 // Install the theme after creating the block as installing the theme will
101 // install the block provided by the locale_test module.
102 \Drupal::service('theme_installer')->install(['classy']);
104 // The test_default_config block provided by the locale_test module will not
105 // be installed because a block with the same ID already exists.
106 $this->installConfig(['locale_test']);
107 $this->assertNull(\Drupal::service('locale.config_manager')->getDefaultConfigLangcode('block.block.test_default_config'), 'The block.block.test_default_config is not shipped configuration.');
108 // Delete the block so we can install the one provided by the locale_test
111 $this->installConfig(['locale_test']);
112 $this->assertEqual('en', \Drupal::service('locale.config_manager')->getDefaultConfigLangcode('block.block.test_default_config'), 'The block.block.test_default_config is shipped configuration.');
114 // Test the special case for configurable_language config entities.
115 $fr_language = ConfigurableLanguage::createFromLangcode('fr');
116 $fr_language->save();
117 $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.');
118 $custom_language = ConfigurableLanguage::createFromLangcode('custom');
119 $custom_language->save();
120 $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.');