Mercurial > hg > isophonics-drupal-site
comparison core/modules/locale/tests/src/Kernel/LocaleConfigManagerTest.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\Tests\locale\Kernel; | |
4 | |
5 use Drupal\block\Entity\Block; | |
6 use Drupal\language\Entity\ConfigurableLanguage; | |
7 use Drupal\KernelTests\KernelTestBase; | |
8 | |
9 /** | |
10 * Tests that the locale config manager operates correctly. | |
11 * | |
12 * @group locale | |
13 */ | |
14 class LocaleConfigManagerTest extends KernelTestBase { | |
15 | |
16 /** | |
17 * A list of modules to install for this test. | |
18 * | |
19 * @var array | |
20 */ | |
21 public static $modules = ['system', 'language', 'locale', 'locale_test', 'block']; | |
22 | |
23 /** | |
24 * This test creates simple config on the fly breaking schema checking. | |
25 * | |
26 * @var bool | |
27 */ | |
28 protected $strictConfigSchema = FALSE; | |
29 | |
30 /** | |
31 * Tests hasTranslation(). | |
32 */ | |
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'); | |
37 | |
38 $language = ConfigurableLanguage::createFromLangcode('de'); | |
39 $language->save(); | |
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.'); | |
42 | |
43 $result = $locale_config_manager->hasTranslation('locale_test.translation', $language->getId()); | |
44 $this->assertTrue($result, 'There is a translation for locale_test.translation configuration.'); | |
45 } | |
46 | |
47 /** | |
48 * Tests getStringTranslation(). | |
49 */ | |
50 public function testGetStringTranslation() { | |
51 $this->installSchema('locale', ['locales_location', 'locales_source', 'locales_target']); | |
52 $this->installConfig(['locale_test']); | |
53 | |
54 $locale_config_manager = \Drupal::service('locale.config_manager'); | |
55 | |
56 $language = ConfigurableLanguage::createFromLangcode('de'); | |
57 $language->save(); | |
58 | |
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(); | |
62 | |
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(); | |
66 } | |
67 | |
68 /** | |
69 * Tests getDefaultConfigLangcode(). | |
70 */ | |
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.'); | |
78 | |
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.'); | |
82 | |
83 $block = Block::create([ | |
84 'id' => 'test_default_config', | |
85 'theme' => 'classy', | |
86 'status' => TRUE, | |
87 'region' => 'content', | |
88 'plugin' => 'local_tasks_block', | |
89 'settings' => [ | |
90 'id' => 'local_tasks_block', | |
91 'label' => $this->randomMachineName(), | |
92 'provider' => 'core', | |
93 'label_display' => FALSE, | |
94 'primary' => TRUE, | |
95 'secondary' => TRUE, | |
96 ], | |
97 ]); | |
98 $block->save(); | |
99 | |
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']); | |
103 | |
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 | |
109 // module. | |
110 $block->delete(); | |
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.'); | |
113 | |
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.'); | |
121 } | |
122 | |
123 } |