Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / config_translation / tests / src / Functional / ConfigTranslationCacheTest.php
1 <?php
2
3 namespace Drupal\Tests\config_translation\Functional;
4
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;
10
11 /**
12  * Translate settings and entities to various languages.
13  *
14  * @group config_translation
15  */
16 class ConfigTranslationCacheTest extends BrowserTestBase {
17
18   /**
19    * Modules to enable.
20    *
21    * @var array
22    */
23   public static $modules = [
24     'block',
25     'config_translation',
26     'config_translation_test',
27     'contact',
28     'contact_test',
29     'contextual',
30     'entity_test',
31     'field_test',
32     'field_ui',
33     'filter',
34     'filter_test',
35     'node',
36     'views',
37     'views_ui',
38   ];
39
40   /**
41    * Languages to enable.
42    *
43    * @var array
44    */
45   protected $langcodes = ['fr', 'ta'];
46
47   /**
48    * Administrator user for tests.
49    *
50    * @var \Drupal\user\UserInterface
51    */
52   protected $adminUser;
53
54   /**
55    * Translator user for tests.
56    *
57    * @var \Drupal\user\UserInterface
58    */
59   protected $translatorUser;
60
61   /**
62    * String translation storage object.
63    *
64    * @var \Drupal\locale\StringStorageInterface
65    */
66   protected $localeStorage;
67
68   protected function setUp() {
69     parent::setUp();
70     $translator_permissions = [
71       'translate configuration',
72     ];
73
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');
80
81     $admin_permissions = array_merge($translator_permissions, [
82       'administer languages',
83       'administer site configuration',
84       'link to any page',
85       'administer contact forms',
86       'administer filters',
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',
93       'administer themes',
94       'bypass node access',
95       'administer content types',
96       'translate interface',
97       'administer entity_test fields',
98     ]);
99     // Create and login user.
100     $this->translatorUser = $this->drupalCreateUser($translator_permissions);
101     $this->adminUser = $this->drupalCreateUser($admin_permissions);
102
103     // Add languages.
104     foreach ($this->langcodes as $langcode) {
105       ConfigurableLanguage::createFromLangcode($langcode)->save();
106     }
107     $this->drupalPlaceBlock('local_tasks_block');
108     $this->drupalPlaceBlock('page_title_block');
109   }
110
111   /**
112    * Tests the translation of field and field storage configuration.
113    */
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',
122     ]);
123
124     $translatable_storage_setting = $this->randomString();
125     $field_storage->setSetting('translatable_storage_setting', $translatable_storage_setting);
126     $field_storage->save();
127
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',
133       'bundle' => $bundle,
134     ]);
135
136     $translatable_field_setting = $this->randomString();
137     $field->setSetting('translatable_field_setting', $translatable_field_setting);
138     $field->save();
139
140     $this->drupalLogin($this->translatorUser);
141
142     $this->drupalGet("/entity_test/structure/$bundle/fields/entity_test.$bundle.$field_name/translate");
143     $this->clickLink('Add');
144
145     $this->assertText('Translatable field setting');
146     $this->assertEscaped($translatable_field_setting);
147     $this->assertText('Translatable storage setting');
148     $this->assertEscaped($translatable_storage_setting);
149
150     // Add translation for label.
151     $field_label_fr = $this->randomString();
152     $edit = [
153       "translation[config_names][field.field.entity_test.$bundle.$field_name][label]" => $field_label_fr,
154     ];
155     $this->drupalPostForm(NULL, $edit, 'Save translation');
156     $this->drupalLogout();
157
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);
162
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);
168
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);
174   }
175
176 }