diff --git a/core/modules/locale/src/Tests/LocaleConfigTranslationTest.php b/core/modules/locale/src/Tests/LocaleConfigTranslationTest.php index d5a7c21..96931ae 100644 --- a/core/modules/locale/src/Tests/LocaleConfigTranslationTest.php +++ b/core/modules/locale/src/Tests/LocaleConfigTranslationTest.php @@ -18,6 +18,13 @@ class LocaleConfigTranslationTest extends WebTestBase { /** + * The language code used. + * + * @var string + */ + protected $this->langcode; + + /** * Modules to enable. * * @var array @@ -37,28 +44,28 @@ protected function setUp() { $this->config('locale.settings') ->set('translation.import_enabled', TRUE) ->save(); - } - /** - * Tests basic configuration translation. - */ - public function testConfigTranslation() { // Add custom language. - $langcode = 'xx'; + $this->langcode = 'xx'; $admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages', 'translate interface', 'administer modules', 'access site-wide contact form', 'administer contact forms')); $this->drupalLogin($admin_user); $name = $this->randomMachineName(16); $edit = array( 'predefined_langcode' => 'custom', - 'langcode' => $langcode, + 'langcode' => $this->langcode, 'label' => $name, 'direction' => LanguageInterface::DIRECTION_LTR, ); $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language')); // Set path prefix. - $edit = array("prefix[$langcode]" => $langcode); + $edit = array("prefix[{$this->langcode}]" => $this->langcode); $this->drupalPostForm('admin/config/regional/language/detection/url', $edit, t('Save configuration')); + } + /** + * Tests basic configuration translation. + */ + public function testConfigTranslation() { // Check site name string exists and create translation for it. $string = $this->storage->findString(array('source' => 'Drupal', 'context' => '', 'type' => 'configuration')); $this->assertTrue($string, 'Configuration strings have been created upon installation.'); @@ -67,7 +74,7 @@ public function testConfigTranslation() { $site_name = $this->randomMachineName(20); $search = array( 'string' => $string->source, - 'langcode' => $langcode, + 'langcode' => $this->langcode, 'translation' => 'all', ); $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter')); @@ -80,12 +87,12 @@ public function testConfigTranslation() { $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations')); // Get translation and check we've only got the site name. - $translation = \Drupal::languageManager()->getLanguageConfigOverride($langcode, 'system.site')->get(); + $translation = \Drupal::languageManager()->getLanguageConfigOverride($this->langcode, 'system.site')->get(); $this->assertEqual(count($translation), 1, 'Got the right number of properties after translation.'); $this->assertEqual($translation['name'], $site_name, 'Got the right translation for the site name.'); // Check the translated site name is displayed. - $this->drupalGet($langcode); + $this->drupalGet($this->langcode); $this->assertText($site_name, 'The translated site name is displayed after translations refreshed.'); // Check default medium date format exists and create a translation for it. @@ -95,7 +102,7 @@ public function testConfigTranslation() { // Translate using the UI so configuration is refreshed. $search = array( 'string' => $string->source, - 'langcode' => $langcode, + 'langcode' => $this->langcode, 'translation' => 'all', ); $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter')); @@ -107,12 +114,12 @@ public function testConfigTranslation() { ); $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations')); - $translation = \Drupal::languageManager()->getLanguageConfigOverride($langcode, 'core.date_format.medium')->get(); + $translation = \Drupal::languageManager()->getLanguageConfigOverride($this->langcode, 'core.date_format.medium')->get(); $this->assertEqual($translation['pattern'], 'D', 'Got the right date format pattern after translation.'); // Formatting the date 8 / 27 / 1985 @ 13:37 EST with pattern D should // display "Tue". - $formatted_date = format_date(494015820, $type = 'medium', NULL, NULL, $langcode); + $formatted_date = format_date(494015820, $type = 'medium', NULL, NULL, $this->langcode); $this->assertEqual($formatted_date, 'Tue', 'Got the right formatted date using the date format translation pattern.'); // Assert strings from image module config are not available. @@ -129,7 +136,7 @@ public function testConfigTranslation() { $this->assertTrue(isset($locations['configuration']) && isset($locations['configuration']['image.style.medium']), 'Configuration string has been created with the right location'); // Check the string is unique and has no translation yet. - $translations = $this->storage->getTranslations(array('language' => $langcode, 'type' => 'configuration', 'name' => 'image.style.medium')); + $translations = $this->storage->getTranslations(array('language' => $this->langcode, 'type' => 'configuration', 'name' => 'image.style.medium')); $this->assertEqual(count($translations), 1); $translation = reset($translations); $this->assertEqual($translation->source, $string->source); @@ -139,7 +146,7 @@ public function testConfigTranslation() { $image_style_label = $this->randomMachineName(20); $search = array( 'string' => $string->source, - 'langcode' => $langcode, + 'langcode' => $this->langcode, 'translation' => 'all', ); $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter')); @@ -151,12 +158,12 @@ public function testConfigTranslation() { $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations')); // Check the right single translation has been created. - $translations = $this->storage->getTranslations(array('language' => $langcode, 'type' => 'configuration', 'name' => 'image.style.medium')); + $translations = $this->storage->getTranslations(array('language' => $this->langcode, 'type' => 'configuration', 'name' => 'image.style.medium')); $translation = reset($translations); $this->assertTrue(count($translations) == 1 && $translation->source == $string->source && $translation->translation == $image_style_label, 'Got only one translation for image configuration.'); // Try more complex configuration data. - $translation = \Drupal::languageManager()->getLanguageConfigOverride($langcode, 'image.style.medium')->get(); + $translation = \Drupal::languageManager()->getLanguageConfigOverride($this->langcode, 'image.style.medium')->get(); $this->assertEqual($translation['label'], $image_style_label, 'Got the right translation for image style name after translation'); // Uninstall the module. @@ -171,7 +178,7 @@ public function testConfigTranslation() { $category_label = $this->randomMachineName(20); $search = array( 'string' => 'Website feedback', - 'langcode' => $langcode, + 'langcode' => $this->langcode, 'translation' => 'all', ); $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter')); @@ -185,7 +192,7 @@ public function testConfigTranslation() { // Check if this category displayed in this language will use the // translation. This test ensures the entity loaded from the request // upcasting will already work. - $this->drupalGet($langcode . '/contact/feedback'); + $this->drupalGet($this->langcode . '/contact/feedback'); $this->assertText($category_label); // Check if the UI does not show the translated String. @@ -193,4 +200,49 @@ public function testConfigTranslation() { $this->assertFieldById('edit-label', 'Website feedback', 'Translation is not loaded for Edit Form.'); } + /** + * Test translatability of optional configuration in locale. + */ + public function testOptionalConfiguration() { + $this->checkNodeStrings(FALSE, FALSE); + // Enable the node module. + $this->drupalPostForm('admin/modules', array('modules[Core][node][enable]' => "1"), t('Save configuration')); + $this->rebuildContainer(); + $this->checkNodeStrings(TRUE, FALSE); + // Enable the views module (which node provides some optional config for). + $this->drupalPostForm('admin/modules', array('modules[Core][views][enable]' => "1"), t('Save configuration')); + $this->rebuildContainer(); + $this->checkNodeStrings(TRUE, TRUE); + } + + /** + * Check that node configuration source strings are made available in locale. + * + * @param bool $required + * Whether to assume a sample of the required default configuration is + * present. + * @param bool $optional + * Whether to assume a sample of the optional default configuration is + * present. + */ + protected function checkNodeStrings($required, $optional) { + // Check the required default configuration string in node. + $string = $this->storage->findString(array('source' => 'Make content sticky', 'context' => '', 'type' => 'configuration')); + if ($required) { + $this->assertTrue($string, 'Node action can be found with node module.'); + } + else { + $this->assertFalse($string, 'Node action can not be found without node module.'); + } + + // Check the optional default configuration string in node. + $string = $this->storage->findString(array('source' => 'No front page content has been created yet.', 'context' => '', 'type' => 'configuration')); + if ($optional) { + $this->assertTrue($string, 'Node view can be found with node and views modules.'); + } + else { + $this->assertFalse($string, 'Node view can not be found without node and/or views modules.'); + } + } + }