Backup of db before drupal security update
[yaffs-website] / web / core / modules / locale / src / Tests / LocaleConfigTranslationTest.php
1 <?php
2
3 namespace Drupal\locale\Tests;
4
5 use Drupal\simpletest\WebTestBase;
6 use Drupal\Core\Language\LanguageInterface;
7
8 /**
9  * Tests translation of configuration strings.
10  *
11  * @group locale
12  */
13 class LocaleConfigTranslationTest extends WebTestBase {
14
15   /**
16    * The language code used.
17    *
18    * @var string
19    */
20   protected $langcode;
21
22   /**
23    * Modules to enable.
24    *
25    * @var array
26    */
27   public static $modules = ['locale', 'contact', 'contact_test'];
28
29   /**
30    * {@inheritdoc}
31    */
32   protected function setUp() {
33     parent::setUp();
34     // Add a default locale storage for all these tests.
35     $this->storage = $this->container->get('locale.storage');
36
37     // Enable import of translations. By default this is disabled for automated
38     // tests.
39     $this->config('locale.settings')
40       ->set('translation.import_enabled', TRUE)
41       ->save();
42
43     // Add custom language.
44     $this->langcode = 'xx';
45     $admin_user = $this->drupalCreateUser(['administer languages', 'access administration pages', 'translate interface', 'administer modules', 'access site-wide contact form', 'administer contact forms', 'administer site configuration']);
46     $this->drupalLogin($admin_user);
47     $name = $this->randomMachineName(16);
48     $edit = [
49       'predefined_langcode' => 'custom',
50       'langcode' => $this->langcode,
51       'label' => $name,
52       'direction' => LanguageInterface::DIRECTION_LTR,
53     ];
54     $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
55     // Set path prefix.
56     $edit = ["prefix[$this->langcode]" => $this->langcode];
57     $this->drupalPostForm('admin/config/regional/language/detection/url', $edit, t('Save configuration'));
58   }
59
60   /**
61    * Tests basic configuration translation.
62    */
63   public function testConfigTranslation() {
64     // Check that the maintenance message exists and create translation for it.
65     $source = '@site is currently under maintenance. We should be back shortly. Thank you for your patience.';
66     $string = $this->storage->findString(['source' => $source, 'context' => '', 'type' => 'configuration']);
67     $this->assertTrue($string, 'Configuration strings have been created upon installation.');
68
69     // Translate using the UI so configuration is refreshed.
70     $message = $this->randomMachineName(20);
71     $search = [
72       'string' => $string->source,
73       'langcode' => $this->langcode,
74       'translation' => 'all',
75     ];
76     $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
77     $textareas = $this->xpath('//textarea');
78     $textarea = current($textareas);
79     $lid = (string) $textarea[0]['name'];
80     $edit = [
81       $lid => $message,
82     ];
83     $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
84
85     // Get translation and check we've only got the message.
86     $translation = \Drupal::languageManager()->getLanguageConfigOverride($this->langcode, 'system.maintenance')->get();
87     $this->assertEqual(count($translation), 1, 'Got the right number of properties after translation.');
88     $this->assertEqual($translation['message'], $message);
89
90     // Check default medium date format exists and create a translation for it.
91     $string = $this->storage->findString(['source' => 'D, m/d/Y - H:i', 'context' => 'PHP date format', 'type' => 'configuration']);
92     $this->assertTrue($string, 'Configuration date formats have been created upon installation.');
93
94     // Translate using the UI so configuration is refreshed.
95     $search = [
96       'string' => $string->source,
97       'langcode' => $this->langcode,
98       'translation' => 'all',
99     ];
100     $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
101     $textareas = $this->xpath('//textarea');
102     $textarea = current($textareas);
103     $lid = (string) $textarea[0]['name'];
104     $edit = [
105       $lid => 'D',
106     ];
107     $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
108
109     $translation = \Drupal::languageManager()->getLanguageConfigOverride($this->langcode, 'core.date_format.medium')->get();
110     $this->assertEqual($translation['pattern'], 'D', 'Got the right date format pattern after translation.');
111
112     // Formatting the date 8 / 27 / 1985 @ 13:37 EST with pattern D should
113     // display "Tue".
114     $formatted_date = format_date(494015820, $type = 'medium', NULL, 'America/New_York', $this->langcode);
115     $this->assertEqual($formatted_date, 'Tue', 'Got the right formatted date using the date format translation pattern.');
116
117     // Assert strings from image module config are not available.
118     $string = $this->storage->findString(['source' => 'Medium (220×220)', 'context' => '', 'type' => 'configuration']);
119     $this->assertFalse($string, 'Configuration strings have been created upon installation.');
120
121     // Enable the image module.
122     $this->drupalPostForm('admin/modules', ['modules[image][enable]' => "1"], t('Install'));
123     $this->rebuildContainer();
124
125     $string = $this->storage->findString(['source' => 'Medium (220×220)', 'context' => '', 'type' => 'configuration']);
126     $this->assertTrue($string, 'Configuration strings have been created upon installation.');
127     $locations = $string->getLocations();
128     $this->assertTrue(isset($locations['configuration']) && isset($locations['configuration']['image.style.medium']), 'Configuration string has been created with the right location');
129
130     // Check the string is unique and has no translation yet.
131     $translations = $this->storage->getTranslations(['language' => $this->langcode, 'type' => 'configuration', 'name' => 'image.style.medium']);
132     $this->assertEqual(count($translations), 1);
133     $translation = reset($translations);
134     $this->assertEqual($translation->source, $string->source);
135     $this->assertTrue(empty($translation->translation));
136
137     // Translate using the UI so configuration is refreshed.
138     $image_style_label = $this->randomMachineName(20);
139     $search = [
140       'string' => $string->source,
141       'langcode' => $this->langcode,
142       'translation' => 'all',
143     ];
144     $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
145     $textarea = current($this->xpath('//textarea'));
146     $lid = (string) $textarea[0]['name'];
147     $edit = [
148       $lid => $image_style_label,
149     ];
150     $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
151
152     // Check the right single translation has been created.
153     $translations = $this->storage->getTranslations(['language' => $this->langcode, 'type' => 'configuration', 'name' => 'image.style.medium']);
154     $translation = reset($translations);
155     $this->assertTrue(count($translations) == 1 && $translation->source == $string->source && $translation->translation == $image_style_label, 'Got only one translation for image configuration.');
156
157     // Try more complex configuration data.
158     $translation = \Drupal::languageManager()->getLanguageConfigOverride($this->langcode, 'image.style.medium')->get();
159     $this->assertEqual($translation['label'], $image_style_label, 'Got the right translation for image style name after translation');
160
161     // Uninstall the module.
162     $this->drupalPostForm('admin/modules/uninstall', ['uninstall[image]' => "image"], t('Uninstall'));
163     $this->drupalPostForm(NULL, [], t('Uninstall'));
164
165     // Ensure that the translated configuration has been removed.
166     $override = \Drupal::languageManager()->getLanguageConfigOverride('xx', 'image.style.medium');
167     $this->assertTrue($override->isNew(), 'Translated configuration for image module removed.');
168
169     // Translate default category using the UI so configuration is refreshed.
170     $category_label = $this->randomMachineName(20);
171     $search = [
172       'string' => 'Website feedback',
173       'langcode' => $this->langcode,
174       'translation' => 'all',
175     ];
176     $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
177     $textarea = current($this->xpath('//textarea'));
178     $lid = (string) $textarea[0]['name'];
179     $edit = [
180       $lid => $category_label,
181     ];
182     $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
183
184     // Check if this category displayed in this language will use the
185     // translation. This test ensures the entity loaded from the request
186     // upcasting will already work.
187     $this->drupalGet($this->langcode . '/contact/feedback');
188     $this->assertText($category_label);
189
190     // Check if the UI does not show the translated String.
191     $this->drupalGet('admin/structure/contact/manage/feedback');
192     $this->assertFieldById('edit-label', 'Website feedback', 'Translation is not loaded for Edit Form.');
193   }
194
195   /**
196    * Test translatability of optional configuration in locale.
197    */
198   public function testOptionalConfiguration() {
199     $this->assertNodeConfig(FALSE, FALSE);
200     // Enable the node module.
201     $this->drupalPostForm('admin/modules', ['modules[node][enable]' => "1"], t('Install'));
202     $this->drupalPostForm(NULL, [], t('Continue'));
203     $this->rebuildContainer();
204     $this->assertNodeConfig(TRUE, FALSE);
205     // Enable the views module (which node provides some optional config for).
206     $this->drupalPostForm('admin/modules', ['modules[views][enable]' => "1"], t('Install'));
207     $this->rebuildContainer();
208     $this->assertNodeConfig(TRUE, TRUE);
209   }
210
211   /**
212    * Check that node configuration source strings are made available in locale.
213    *
214    * @param bool $required
215    *   Whether to assume a sample of the required default configuration is
216    *   present.
217    * @param bool $optional
218    *   Whether to assume a sample of the optional default configuration is
219    *   present.
220    */
221   protected function assertNodeConfig($required, $optional) {
222     // Check the required default configuration in node module.
223     $string = $this->storage->findString(['source' => 'Make content sticky', 'context' => '', 'type' => 'configuration']);
224     if ($required) {
225       $this->assertFalse($this->config('system.action.node_make_sticky_action')->isNew());
226       $this->assertTrue($string, 'Node action text can be found with node module.');
227     }
228     else {
229       $this->assertTrue($this->config('system.action.node_make_sticky_action')->isNew());
230       $this->assertFalse($string, 'Node action text can not be found without node module.');
231     }
232
233     // Check the optional default configuration in node module.
234     $string = $this->storage->findString(['source' => 'No front page content has been created yet.', 'context' => '', 'type' => 'configuration']);
235     if ($optional) {
236       $this->assertFalse($this->config('views.view.frontpage')->isNew());
237       $this->assertTrue($string, 'Node view text can be found with node and views modules.');
238     }
239     else {
240       $this->assertTrue($this->config('views.view.frontpage')->isNew());
241       $this->assertFalse($string, 'Node view text can not be found without node and/or views modules.');
242     }
243   }
244
245 }