3 namespace Drupal\Tests\config_translation\Functional;
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\field\Entity\FieldStorageConfig;
7 use Drupal\filter\Entity\FilterFormat;
8 use Drupal\language\Entity\ConfigurableLanguage;
9 use Drupal\Tests\BrowserTestBase;
12 * Translate settings and entities to various languages.
14 * @group config_translation
16 class ConfigTranslationCacheTest extends BrowserTestBase {
23 public static $modules = [
26 'config_translation_test',
41 * Languages to enable.
45 protected $langcodes = ['fr', 'ta'];
48 * Administrator user for tests.
50 * @var \Drupal\user\UserInterface
55 * Translator user for tests.
57 * @var \Drupal\user\UserInterface
59 protected $translatorUser;
62 * String translation storage object.
64 * @var \Drupal\locale\StringStorageInterface
66 protected $localeStorage;
68 protected function setUp() {
70 $translator_permissions = [
71 'translate configuration',
74 /** @var \Drupal\filter\FilterFormatInterface $filter_test_format */
75 $filter_test_format = FilterFormat::load('filter_test');
76 /** @var \Drupal\filter\FilterFormatInterface $filtered_html_format */
77 $filtered_html_format = FilterFormat::load('filtered_html');
78 /** @var \Drupal\filter\FilterFormatInterface $full_html_format */
79 $full_html_format = FilterFormat::load('full_html');
81 $admin_permissions = array_merge($translator_permissions, [
82 'administer languages',
83 'administer site configuration',
85 'administer contact forms',
87 $filtered_html_format->getPermissionName(),
88 $full_html_format->getPermissionName(),
89 $filter_test_format->getPermissionName(),
90 'access site-wide contact form',
91 'access contextual links',
92 'administer account settings',
95 'administer content types',
96 'translate interface',
97 'administer entity_test fields',
99 // Create and login user.
100 $this->translatorUser = $this->drupalCreateUser($translator_permissions);
101 $this->adminUser = $this->drupalCreateUser($admin_permissions);
104 foreach ($this->langcodes as $langcode) {
105 ConfigurableLanguage::createFromLangcode($langcode)->save();
107 $this->drupalPlaceBlock('local_tasks_block');
108 $this->drupalPlaceBlock('page_title_block');
112 * Tests the translation of field and field storage configuration.
114 public function testFieldConfigTranslation() {
115 // Add a test field which has a translatable field setting and a
116 // translatable field storage setting.
117 $field_name = strtolower($this->randomMachineName());
118 $field_storage = FieldStorageConfig::create([
119 'field_name' => $field_name,
120 'entity_type' => 'entity_test',
121 'type' => 'test_field',
124 $translatable_storage_setting = $this->randomString();
125 $field_storage->setSetting('translatable_storage_setting', $translatable_storage_setting);
126 $field_storage->save();
128 $bundle = strtolower($this->randomMachineName());
129 entity_test_create_bundle($bundle);
130 $field = FieldConfig::create([
131 'field_name' => $field_name,
132 'entity_type' => 'entity_test',
136 $translatable_field_setting = $this->randomString();
137 $field->setSetting('translatable_field_setting', $translatable_field_setting);
140 $this->drupalLogin($this->translatorUser);
142 $this->drupalGet("/entity_test/structure/$bundle/fields/entity_test.$bundle.$field_name/translate");
143 $this->clickLink('Add');
145 $this->assertText('Translatable field setting');
146 $this->assertEscaped($translatable_field_setting);
147 $this->assertText('Translatable storage setting');
148 $this->assertEscaped($translatable_storage_setting);
150 // Add translation for label.
151 $field_label_fr = $this->randomString();
153 "translation[config_names][field.field.entity_test.$bundle.$field_name][label]" => $field_label_fr,
155 $this->drupalPostForm(NULL, $edit, 'Save translation');
156 $this->drupalLogout();
158 // Check if the translated label appears.
159 $this->drupalLogin($this->adminUser);
160 $this->drupalGet("/fr/entity_test/structure/$bundle/fields");
161 $this->assertEscaped($field_label_fr);
163 // Clear cache on French version and check for translated label.
164 $this->drupalPostForm('/fr/admin/config/development/performance', [], 'Clear all caches');
165 $this->drupalGet("/fr/entity_test/structure/$bundle/fields");
166 // Check if the translation is still there.
167 $this->assertEscaped($field_label_fr);
169 // Clear cache on default version and check for translated label.
170 $this->drupalPostForm('/admin/config/development/performance', [], 'Clear all caches');
171 $this->drupalGet("/fr/entity_test/structure/$bundle/fields");
172 // Check if the translation is still there.
173 $this->assertEscaped($field_label_fr);