Chris@0: storage = $this->container->get('locale.storage');
Chris@0:
Chris@0: // Enable import of translations. By default this is disabled for automated
Chris@0: // tests.
Chris@0: $this->config('locale.settings')
Chris@0: ->set('translation.import_enabled', TRUE)
Chris@16: ->set('translation.use_source', LOCALE_TRANSLATION_USE_SOURCE_LOCAL)
Chris@0: ->save();
Chris@0:
Chris@0: // Add custom language.
Chris@0: $this->langcode = 'xx';
Chris@0: $admin_user = $this->drupalCreateUser(['administer languages', 'access administration pages', 'translate interface', 'administer modules', 'access site-wide contact form', 'administer contact forms', 'administer site configuration']);
Chris@0: $this->drupalLogin($admin_user);
Chris@0: $name = $this->randomMachineName(16);
Chris@0: $edit = [
Chris@0: 'predefined_langcode' => 'custom',
Chris@0: 'langcode' => $this->langcode,
Chris@0: 'label' => $name,
Chris@0: 'direction' => LanguageInterface::DIRECTION_LTR,
Chris@0: ];
Chris@0: $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
Chris@0: // Set path prefix.
Chris@0: $edit = ["prefix[$this->langcode]" => $this->langcode];
Chris@0: $this->drupalPostForm('admin/config/regional/language/detection/url', $edit, t('Save configuration'));
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Tests basic configuration translation.
Chris@0: */
Chris@0: public function testConfigTranslation() {
Chris@0: // Check that the maintenance message exists and create translation for it.
Chris@0: $source = '@site is currently under maintenance. We should be back shortly. Thank you for your patience.';
Chris@0: $string = $this->storage->findString(['source' => $source, 'context' => '', 'type' => 'configuration']);
Chris@0: $this->assertTrue($string, 'Configuration strings have been created upon installation.');
Chris@0:
Chris@0: // Translate using the UI so configuration is refreshed.
Chris@0: $message = $this->randomMachineName(20);
Chris@0: $search = [
Chris@0: 'string' => $string->source,
Chris@0: 'langcode' => $this->langcode,
Chris@0: 'translation' => 'all',
Chris@0: ];
Chris@0: $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
Chris@0: $textareas = $this->xpath('//textarea');
Chris@0: $textarea = current($textareas);
Chris@0: $lid = $textarea->getAttribute('name');
Chris@0: $edit = [
Chris@0: $lid => $message,
Chris@0: ];
Chris@0: $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
Chris@0:
Chris@0: // Get translation and check we've only got the message.
Chris@0: $translation = \Drupal::languageManager()->getLanguageConfigOverride($this->langcode, 'system.maintenance')->get();
Chris@0: $this->assertEqual(count($translation), 1, 'Got the right number of properties after translation.');
Chris@0: $this->assertEqual($translation['message'], $message);
Chris@0:
Chris@0: // Check default medium date format exists and create a translation for it.
Chris@0: $string = $this->storage->findString(['source' => 'D, m/d/Y - H:i', 'context' => 'PHP date format', 'type' => 'configuration']);
Chris@0: $this->assertTrue($string, 'Configuration date formats have been created upon installation.');
Chris@0:
Chris@0: // Translate using the UI so configuration is refreshed.
Chris@0: $search = [
Chris@0: 'string' => $string->source,
Chris@0: 'langcode' => $this->langcode,
Chris@0: 'translation' => 'all',
Chris@0: ];
Chris@0: $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
Chris@0: $textareas = $this->xpath('//textarea');
Chris@0: $textarea = current($textareas);
Chris@0: $lid = $textarea->getAttribute('name');
Chris@0: $edit = [
Chris@0: $lid => 'D',
Chris@0: ];
Chris@0: $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
Chris@0:
Chris@0: $translation = \Drupal::languageManager()->getLanguageConfigOverride($this->langcode, 'core.date_format.medium')->get();
Chris@0: $this->assertEqual($translation['pattern'], 'D', 'Got the right date format pattern after translation.');
Chris@0:
Chris@0: // Formatting the date 8 / 27 / 1985 @ 13:37 EST with pattern D should
Chris@0: // display "Tue".
Chris@18: $formatted_date = $this->container->get('date.formatter')->format(494015820, $type = 'medium', NULL, 'America/New_York', $this->langcode);
Chris@0: $this->assertEqual($formatted_date, 'Tue', 'Got the right formatted date using the date format translation pattern.');
Chris@0:
Chris@0: // Assert strings from image module config are not available.
Chris@0: $string = $this->storage->findString(['source' => 'Medium (220×220)', 'context' => '', 'type' => 'configuration']);
Chris@0: $this->assertFalse($string, 'Configuration strings have been created upon installation.');
Chris@0:
Chris@0: // Enable the image module.
Chris@0: $this->drupalPostForm('admin/modules', ['modules[image][enable]' => "1"], t('Install'));
Chris@0: $this->rebuildContainer();
Chris@0:
Chris@0: $string = $this->storage->findString(['source' => 'Medium (220×220)', 'context' => '', 'type' => 'configuration']);
Chris@0: $this->assertTrue($string, 'Configuration strings have been created upon installation.');
Chris@0: $locations = $string->getLocations();
Chris@0: $this->assertTrue(isset($locations['configuration']) && isset($locations['configuration']['image.style.medium']), 'Configuration string has been created with the right location');
Chris@0:
Chris@0: // Check the string is unique and has no translation yet.
Chris@0: $translations = $this->storage->getTranslations(['language' => $this->langcode, 'type' => 'configuration', 'name' => 'image.style.medium']);
Chris@0: $this->assertEqual(count($translations), 1);
Chris@0: $translation = reset($translations);
Chris@0: $this->assertEqual($translation->source, $string->source);
Chris@0: $this->assertTrue(empty($translation->translation));
Chris@0:
Chris@0: // Translate using the UI so configuration is refreshed.
Chris@0: $image_style_label = $this->randomMachineName(20);
Chris@0: $search = [
Chris@0: 'string' => $string->source,
Chris@0: 'langcode' => $this->langcode,
Chris@0: 'translation' => 'all',
Chris@0: ];
Chris@0: $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
Chris@0: $textarea = current($this->xpath('//textarea'));
Chris@0: $lid = $textarea->getAttribute('name');
Chris@0: $edit = [
Chris@0: $lid => $image_style_label,
Chris@0: ];
Chris@0: $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
Chris@0:
Chris@0: // Check the right single translation has been created.
Chris@0: $translations = $this->storage->getTranslations(['language' => $this->langcode, 'type' => 'configuration', 'name' => 'image.style.medium']);
Chris@0: $translation = reset($translations);
Chris@0: $this->assertTrue(count($translations) == 1 && $translation->source == $string->source && $translation->translation == $image_style_label, 'Got only one translation for image configuration.');
Chris@0:
Chris@0: // Try more complex configuration data.
Chris@0: $translation = \Drupal::languageManager()->getLanguageConfigOverride($this->langcode, 'image.style.medium')->get();
Chris@0: $this->assertEqual($translation['label'], $image_style_label, 'Got the right translation for image style name after translation');
Chris@0:
Chris@0: // Uninstall the module.
Chris@0: $this->drupalPostForm('admin/modules/uninstall', ['uninstall[image]' => "image"], t('Uninstall'));
Chris@0: $this->drupalPostForm(NULL, [], t('Uninstall'));
Chris@0:
Chris@0: // Ensure that the translated configuration has been removed.
Chris@0: $override = \Drupal::languageManager()->getLanguageConfigOverride('xx', 'image.style.medium');
Chris@0: $this->assertTrue($override->isNew(), 'Translated configuration for image module removed.');
Chris@0:
Chris@0: // Translate default category using the UI so configuration is refreshed.
Chris@0: $category_label = $this->randomMachineName(20);
Chris@0: $search = [
Chris@0: 'string' => 'Website feedback',
Chris@0: 'langcode' => $this->langcode,
Chris@0: 'translation' => 'all',
Chris@0: ];
Chris@0: $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
Chris@0: $textarea = current($this->xpath('//textarea'));
Chris@0: $lid = $textarea->getAttribute('name');
Chris@0: $edit = [
Chris@0: $lid => $category_label,
Chris@0: ];
Chris@0: $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
Chris@0:
Chris@0: // Check if this category displayed in this language will use the
Chris@0: // translation. This test ensures the entity loaded from the request
Chris@0: // upcasting will already work.
Chris@0: $this->drupalGet($this->langcode . '/contact/feedback');
Chris@0: $this->assertText($category_label);
Chris@0:
Chris@0: // Check if the UI does not show the translated String.
Chris@0: $this->drupalGet('admin/structure/contact/manage/feedback');
Chris@0: $this->assertFieldById('edit-label', 'Website feedback', 'Translation is not loaded for Edit Form.');
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Test translatability of optional configuration in locale.
Chris@0: */
Chris@0: public function testOptionalConfiguration() {
Chris@0: $this->assertNodeConfig(FALSE, FALSE);
Chris@0: // Enable the node module.
Chris@0: $this->drupalPostForm('admin/modules', ['modules[node][enable]' => "1"], t('Install'));
Chris@0: $this->drupalPostForm(NULL, [], t('Continue'));
Chris@0: $this->rebuildContainer();
Chris@0: $this->assertNodeConfig(TRUE, FALSE);
Chris@0: // Enable the views module (which node provides some optional config for).
Chris@0: $this->drupalPostForm('admin/modules', ['modules[views][enable]' => "1"], t('Install'));
Chris@0: $this->rebuildContainer();
Chris@0: $this->assertNodeConfig(TRUE, TRUE);
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Check that node configuration source strings are made available in locale.
Chris@0: *
Chris@0: * @param bool $required
Chris@0: * Whether to assume a sample of the required default configuration is
Chris@0: * present.
Chris@0: * @param bool $optional
Chris@0: * Whether to assume a sample of the optional default configuration is
Chris@0: * present.
Chris@0: */
Chris@0: protected function assertNodeConfig($required, $optional) {
Chris@0: // Check the required default configuration in node module.
Chris@0: $string = $this->storage->findString(['source' => 'Make content sticky', 'context' => '', 'type' => 'configuration']);
Chris@0: if ($required) {
Chris@0: $this->assertFalse($this->config('system.action.node_make_sticky_action')->isNew());
Chris@0: $this->assertTrue($string, 'Node action text can be found with node module.');
Chris@0: }
Chris@0: else {
Chris@0: $this->assertTrue($this->config('system.action.node_make_sticky_action')->isNew());
Chris@0: $this->assertFalse($string, 'Node action text can not be found without node module.');
Chris@0: }
Chris@0:
Chris@0: // Check the optional default configuration in node module.
Chris@14: $string = $this->storage->findString(['source' => 'No front page content has been created yet.
Follow the User Guide to start building your site.', 'context' => '', 'type' => 'configuration']);
Chris@0: if ($optional) {
Chris@0: $this->assertFalse($this->config('views.view.frontpage')->isNew());
Chris@0: $this->assertTrue($string, 'Node view text can be found with node and views modules.');
Chris@0: }
Chris@0: else {
Chris@0: $this->assertTrue($this->config('views.view.frontpage')->isNew());
Chris@0: $this->assertFalse($string, 'Node view text can not be found without node and/or views modules.');
Chris@0: }
Chris@0: }
Chris@0:
Chris@0: }