Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\locale\Kernel;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\block\Entity\Block;
|
Chris@0
|
6 use Drupal\language\Entity\ConfigurableLanguage;
|
Chris@0
|
7 use Drupal\KernelTests\KernelTestBase;
|
Chris@0
|
8
|
Chris@0
|
9 /**
|
Chris@0
|
10 * Tests that the locale config manager operates correctly.
|
Chris@0
|
11 *
|
Chris@0
|
12 * @group locale
|
Chris@0
|
13 */
|
Chris@0
|
14 class LocaleConfigManagerTest extends KernelTestBase {
|
Chris@0
|
15
|
Chris@0
|
16 /**
|
Chris@0
|
17 * A list of modules to install for this test.
|
Chris@0
|
18 *
|
Chris@0
|
19 * @var array
|
Chris@0
|
20 */
|
Chris@0
|
21 public static $modules = ['system', 'language', 'locale', 'locale_test', 'block'];
|
Chris@0
|
22
|
Chris@0
|
23 /**
|
Chris@0
|
24 * This test creates simple config on the fly breaking schema checking.
|
Chris@0
|
25 *
|
Chris@0
|
26 * @var bool
|
Chris@0
|
27 */
|
Chris@0
|
28 protected $strictConfigSchema = FALSE;
|
Chris@0
|
29
|
Chris@0
|
30 /**
|
Chris@0
|
31 * Tests hasTranslation().
|
Chris@0
|
32 */
|
Chris@0
|
33 public function testHasTranslation() {
|
Chris@0
|
34 $this->installSchema('locale', ['locales_location', 'locales_source', 'locales_target']);
|
Chris@0
|
35 $this->installConfig(['locale_test']);
|
Chris@0
|
36 $locale_config_manager = \Drupal::service('locale.config_manager');
|
Chris@0
|
37
|
Chris@0
|
38 $language = ConfigurableLanguage::createFromLangcode('de');
|
Chris@0
|
39 $language->save();
|
Chris@0
|
40 $result = $locale_config_manager->hasTranslation('locale_test.no_translation', $language->getId());
|
Chris@0
|
41 $this->assertFalse($result, 'There is no translation for locale_test.no_translation configuration.');
|
Chris@0
|
42
|
Chris@0
|
43 $result = $locale_config_manager->hasTranslation('locale_test.translation', $language->getId());
|
Chris@0
|
44 $this->assertTrue($result, 'There is a translation for locale_test.translation configuration.');
|
Chris@0
|
45 }
|
Chris@0
|
46
|
Chris@0
|
47 /**
|
Chris@0
|
48 * Tests getStringTranslation().
|
Chris@0
|
49 */
|
Chris@0
|
50 public function testGetStringTranslation() {
|
Chris@0
|
51 $this->installSchema('locale', ['locales_location', 'locales_source', 'locales_target']);
|
Chris@0
|
52 $this->installConfig(['locale_test']);
|
Chris@0
|
53
|
Chris@0
|
54 $locale_config_manager = \Drupal::service('locale.config_manager');
|
Chris@0
|
55
|
Chris@0
|
56 $language = ConfigurableLanguage::createFromLangcode('de');
|
Chris@0
|
57 $language->save();
|
Chris@0
|
58
|
Chris@0
|
59 $translation_before = $locale_config_manager->getStringTranslation('locale_test.no_translation', $language->getId(), 'Test', '');
|
Chris@0
|
60 $this->assertTrue($translation_before->isNew());
|
Chris@0
|
61 $translation_before->setString('translation')->save();
|
Chris@0
|
62
|
Chris@0
|
63 $translation_after = $locale_config_manager->getStringTranslation('locale_test.no_translation', $language->getId(), 'Test', '');
|
Chris@0
|
64 $this->assertFalse($translation_after->isNew());
|
Chris@0
|
65 $translation_after->setString('updated_translation')->save();
|
Chris@0
|
66 }
|
Chris@0
|
67
|
Chris@0
|
68 /**
|
Chris@0
|
69 * Tests getDefaultConfigLangcode().
|
Chris@0
|
70 */
|
Chris@0
|
71 public function testGetDefaultConfigLangcode() {
|
Chris@0
|
72 // Install the Language module's configuration so we can use the
|
Chris@0
|
73 // module_installer service.
|
Chris@0
|
74 $this->installConfig(['language']);
|
Chris@0
|
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.');
|
Chris@0
|
76 \Drupal::service('module_installer')->install(['locale_test_translate']);
|
Chris@0
|
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.');
|
Chris@0
|
78
|
Chris@0
|
79 $simple_config = \Drupal::configFactory()->getEditable('locale_test_translate.simple_config_extra');
|
Chris@0
|
80 $simple_config->set('foo', 'bar')->save();
|
Chris@0
|
81 $this->assertNull(\Drupal::service('locale.config_manager')->getDefaultConfigLangcode($simple_config->getName()), 'Simple config created through the API is not treated as shipped configuration.');
|
Chris@0
|
82
|
Chris@0
|
83 $block = Block::create([
|
Chris@0
|
84 'id' => 'test_default_config',
|
Chris@0
|
85 'theme' => 'classy',
|
Chris@0
|
86 'status' => TRUE,
|
Chris@0
|
87 'region' => 'content',
|
Chris@0
|
88 'plugin' => 'local_tasks_block',
|
Chris@0
|
89 'settings' => [
|
Chris@0
|
90 'id' => 'local_tasks_block',
|
Chris@0
|
91 'label' => $this->randomMachineName(),
|
Chris@0
|
92 'provider' => 'core',
|
Chris@0
|
93 'label_display' => FALSE,
|
Chris@0
|
94 'primary' => TRUE,
|
Chris@0
|
95 'secondary' => TRUE,
|
Chris@0
|
96 ],
|
Chris@0
|
97 ]);
|
Chris@0
|
98 $block->save();
|
Chris@0
|
99
|
Chris@0
|
100 // Install the theme after creating the block as installing the theme will
|
Chris@0
|
101 // install the block provided by the locale_test module.
|
Chris@0
|
102 \Drupal::service('theme_installer')->install(['classy']);
|
Chris@0
|
103
|
Chris@0
|
104 // The test_default_config block provided by the locale_test module will not
|
Chris@0
|
105 // be installed because a block with the same ID already exists.
|
Chris@0
|
106 $this->installConfig(['locale_test']);
|
Chris@0
|
107 $this->assertNull(\Drupal::service('locale.config_manager')->getDefaultConfigLangcode('block.block.test_default_config'), 'The block.block.test_default_config is not shipped configuration.');
|
Chris@0
|
108 // Delete the block so we can install the one provided by the locale_test
|
Chris@0
|
109 // module.
|
Chris@0
|
110 $block->delete();
|
Chris@0
|
111 $this->installConfig(['locale_test']);
|
Chris@0
|
112 $this->assertEqual('en', \Drupal::service('locale.config_manager')->getDefaultConfigLangcode('block.block.test_default_config'), 'The block.block.test_default_config is shipped configuration.');
|
Chris@0
|
113
|
Chris@0
|
114 // Test the special case for configurable_language config entities.
|
Chris@0
|
115 $fr_language = ConfigurableLanguage::createFromLangcode('fr');
|
Chris@0
|
116 $fr_language->save();
|
Chris@0
|
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.');
|
Chris@0
|
118 $custom_language = ConfigurableLanguage::createFromLangcode('custom');
|
Chris@0
|
119 $custom_language->save();
|
Chris@0
|
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.');
|
Chris@0
|
121 }
|
Chris@0
|
122
|
Chris@0
|
123 }
|