ConfigTranslationUiTest.php

Same filename in this branch
  1. 11.x core/modules/config_translation/tests/src/FunctionalJavascript/ConfigTranslationUiTest.php
Same filename and directory in other branches
  1. 9 core/modules/config_translation/tests/src/FunctionalJavascript/ConfigTranslationUiTest.php
  2. 9 core/modules/config_translation/tests/src/Functional/ConfigTranslationUiTest.php
  3. 8.9.x core/modules/config_translation/tests/src/FunctionalJavascript/ConfigTranslationUiTest.php
  4. 8.9.x core/modules/config_translation/tests/src/Functional/ConfigTranslationUiTest.php
  5. 10 core/modules/config_translation/tests/src/FunctionalJavascript/ConfigTranslationUiTest.php
  6. 10 core/modules/config_translation/tests/src/Functional/ConfigTranslationUiTest.php

Namespace

Drupal\Tests\config_translation\Functional

File

core/modules/config_translation/tests/src/Functional/ConfigTranslationUiTest.php

View source
<?php

declare (strict_types=1);
namespace Drupal\Tests\config_translation\Functional;

use Drupal\Core\Language\Language;
use Drupal\Core\Language\LanguageInterface;
// cspell:ignore anonyme viewsviewfiles

/**
 * Translate settings and entities to various languages.
 *
 * @group config_translation
 * @group #slow
 */
class ConfigTranslationUiTest extends ConfigTranslationUiTestBase {
    
    /**
     * Tests the account settings translation interface.
     *
     * This is the only special case so far where we have multiple configuration
     * names involved building up one configuration translation form. Test that
     * the translations are saved for all configuration names properly.
     */
    public function testAccountSettingsConfigurationTranslation() : void {
        $this->drupalLogin($this->adminUser);
        $this->drupalGet('admin/config/people/accounts');
        $this->assertSession()
            ->linkExists('Translate account settings');
        $this->drupalGet('admin/config/people/accounts/translate');
        $this->assertSession()
            ->linkExists('Translate account settings');
        $this->assertSession()
            ->linkByHrefExists('admin/config/people/accounts/translate/fr/add');
        // Update account settings fields for French.
        $edit = [
            'translation[config_names][user.settings][anonymous]' => 'Anonyme',
            'translation[config_names][user.mail][status_blocked][subject]' => 'Testing, your account is blocked.',
            'translation[config_names][user.mail][status_blocked][body]' => 'Testing account blocked body.',
        ];
        $this->drupalGet('admin/config/people/accounts/translate/fr/add');
        $this->submitForm($edit, 'Save translation');
        // Make sure the changes are saved and loaded back properly.
        $this->drupalGet('admin/config/people/accounts/translate/fr/edit');
        foreach ($edit as $key => $value) {
            // Check the translations appear in the right field type as well.
            $this->assertSession()
                ->fieldValueEquals($key, $value);
        }
        // Check that labels for email settings appear.
        $this->assertSession()
            ->pageTextContains('Account cancellation confirmation');
        $this->assertSession()
            ->pageTextContains('Password recovery');
    }
    
    /**
     * Tests source and target language edge cases.
     */
    public function testSourceAndTargetLanguage() : void {
        $this->drupalLogin($this->adminUser);
        // Loading translation page for not-specified language (und)
        // should return 403.
        $this->drupalGet('admin/config/system/site-information/translate/und/add');
        $this->assertSession()
            ->statusCodeEquals(403);
        // Check the source language doesn't have 'Add' or 'Delete' link and
        // make sure source language edit goes to original configuration page
        // not the translation specific edit page.
        $this->drupalGet('admin/config/system/site-information/translate');
        $this->assertSession()
            ->linkByHrefNotExists('admin/config/system/site-information/translate/en/edit');
        $this->assertSession()
            ->linkByHrefNotExists('admin/config/system/site-information/translate/en/add');
        $this->assertSession()
            ->linkByHrefNotExists('admin/config/system/site-information/translate/en/delete');
        $this->assertSession()
            ->linkByHrefExists('admin/config/system/site-information');
        // Translation addition to source language should return 403.
        $this->drupalGet('admin/config/system/site-information/translate/en/add');
        $this->assertSession()
            ->statusCodeEquals(403);
        // Translation editing in source language should return 403.
        $this->drupalGet('admin/config/system/site-information/translate/en/edit');
        $this->assertSession()
            ->statusCodeEquals(403);
        // Translation deletion in source language should return 403.
        $this->drupalGet('admin/config/system/site-information/translate/en/delete');
        $this->assertSession()
            ->statusCodeEquals(403);
        // Set default language of site information to not-specified language (und).
        $this->config('system.site')
            ->set('langcode', LanguageInterface::LANGCODE_NOT_SPECIFIED)
            ->save();
        // Make sure translation tab does not exist on the configuration page.
        $this->drupalGet('admin/config/system/site-information');
        $this->assertSession()
            ->linkByHrefNotExists('admin/config/system/site-information/translate');
        // If source language is not specified, translation page should be 403.
        $this->drupalGet('admin/config/system/site-information/translate');
        $this->assertSession()
            ->statusCodeEquals(403);
    }
    
    /**
     * Tests plural source elements in configuration translation forms.
     */
    public function testPluralConfigStringsSourceElements() : void {
        $this->drupalLogin($this->adminUser);
        // Languages to test, with various number of plural forms.
        $languages = [
            'vi' => [
                'plurals' => 1,
                'expected' => [
                    TRUE,
                    FALSE,
                    FALSE,
                    FALSE,
                ],
            ],
            'fr' => [
                'plurals' => 2,
                'expected' => [
                    TRUE,
                    TRUE,
                    FALSE,
                    FALSE,
                ],
            ],
            'sl' => [
                'plurals' => 4,
                'expected' => [
                    TRUE,
                    TRUE,
                    TRUE,
                    TRUE,
                ],
            ],
        ];
        foreach ($languages as $langcode => $data) {
            // Import a .po file to add a new language with a given number of plural forms
            $name = \Drupal::service('file_system')->tempnam('temporary://', $langcode . '_') . '.po';
            file_put_contents($name, $this->getPoFile($data['plurals']));
            $this->drupalGet('admin/config/regional/translate/import');
            $this->submitForm([
                'langcode' => $langcode,
                'files[file]' => $name,
            ], 'Import');
            // Change the config langcode of the 'files' view.
            $config = \Drupal::service('config.factory')->getEditable('views.view.files');
            $config->set('langcode', $langcode);
            $config->save();
            // Go to the translation page of the 'files' view.
            $translation_url = 'admin/structure/views/view/files/translate/en/add';
            $this->drupalGet($translation_url);
            // Check if the expected number of source elements are present.
            foreach ($data['expected'] as $index => $expected) {
                if ($expected) {
                    $this->assertSession()
                        ->responseContains('edit-source-config-names-viewsviewfiles-display-default-display-options-fields-count-format-plural-string-' . $index);
                }
                else {
                    $this->assertSession()
                        ->responseNotContains('edit-source-config-names-viewsviewfiles-display-default-display-options-fields-count-format-plural-string-' . $index);
                }
            }
        }
    }
    
    /**
     * Tests translation of plural strings with multiple plural forms in config.
     */
    public function testPluralConfigStrings() : void {
        $this->drupalLogin($this->adminUser);
        // First import a .po file with multiple plural forms.
        // This will also automatically add the 'sl' language.
        $name = \Drupal::service('file_system')->tempnam('temporary://', "sl_") . '.po';
        file_put_contents($name, $this->getPoFile(4));
        $this->drupalGet('admin/config/regional/translate/import');
        $this->submitForm([
            'langcode' => 'sl',
            'files[file]' => $name,
        ], 'Import');
        // Translate the files view, as this one uses numeric formatters.
        $description = 'Singular form';
        $field_value = '1 place';
        $field_value_plural = '@count places';
        $translation_url = 'admin/structure/views/view/files/translate/sl/add';
        $this->drupalGet($translation_url);
        // Make sure original text is present on this page, in addition to 2 new
        // empty fields.
        $this->assertSession()
            ->pageTextContains($description);
        $this->assertSession()
            ->fieldValueEquals('translation[config_names][views.view.files][display][default][display_options][fields][count][format_plural_string][0]', $field_value);
        $this->assertSession()
            ->fieldValueEquals('translation[config_names][views.view.files][display][default][display_options][fields][count][format_plural_string][1]', $field_value_plural);
        $this->assertSession()
            ->fieldValueEquals('translation[config_names][views.view.files][display][default][display_options][fields][count][format_plural_string][2]', '');
        $this->assertSession()
            ->fieldValueEquals('translation[config_names][views.view.files][display][default][display_options][fields][count][format_plural_string][3]', '');
        // Then make sure it also works.
        $edit = [
            'translation[config_names][views.view.files][display][default][display_options][fields][count][format_plural_string][0]' => $field_value . ' SL',
            'translation[config_names][views.view.files][display][default][display_options][fields][count][format_plural_string][1]' => $field_value_plural . ' 1 SL',
            'translation[config_names][views.view.files][display][default][display_options][fields][count][format_plural_string][2]' => $field_value_plural . ' 2 SL',
            'translation[config_names][views.view.files][display][default][display_options][fields][count][format_plural_string][3]' => $field_value_plural . ' 3 SL',
        ];
        $this->drupalGet($translation_url);
        $this->submitForm($edit, 'Save translation');
        // Make sure the values have changed.
        $this->drupalGet($translation_url);
        $this->assertSession()
            ->fieldValueEquals('translation[config_names][views.view.files][display][default][display_options][fields][count][format_plural_string][0]', "{$field_value} SL");
        $this->assertSession()
            ->fieldValueEquals('translation[config_names][views.view.files][display][default][display_options][fields][count][format_plural_string][1]', "{$field_value_plural} 1 SL");
        $this->assertSession()
            ->fieldValueEquals('translation[config_names][views.view.files][display][default][display_options][fields][count][format_plural_string][2]', "{$field_value_plural} 2 SL");
        $this->assertSession()
            ->fieldValueEquals('translation[config_names][views.view.files][display][default][display_options][fields][count][format_plural_string][3]', "{$field_value_plural} 3 SL");
    }
    
    /**
     * Tests translation storage in locale storage.
     */
    public function testLocaleDBStorage() : void {
        // Enable import of translations. By default this is disabled for automated
        // tests.
        $this->config('locale.settings')
            ->set('translation.import_enabled', TRUE)
            ->set('translation.use_source', LOCALE_TRANSLATION_USE_SOURCE_LOCAL)
            ->save();
        $this->drupalLogin($this->adminUser);
        $langcode = 'xx';
        $name = $this->randomMachineName(16);
        $edit = [
            'predefined_langcode' => 'custom',
            'langcode' => $langcode,
            'label' => $name,
            'direction' => Language::DIRECTION_LTR,
        ];
        $this->drupalGet('admin/config/regional/language/add');
        $this->submitForm($edit, 'Add custom language');
        // Make sure there is no translation stored in locale storage before edit.
        $translation = $this->getTranslation('user.settings', 'anonymous', 'fr');
        $this->assertEmpty($translation);
        // Add custom translation.
        $edit = [
            'translation[config_names][user.settings][anonymous]' => 'Anonyme',
        ];
        $this->drupalGet('admin/config/people/accounts/translate/fr/add');
        $this->submitForm($edit, 'Save translation');
        // Make sure translation stored in locale storage after saved language
        // specific configuration translation.
        $translation = $this->getTranslation('user.settings', 'anonymous', 'fr');
        $this->assertEquals('Anonyme', $translation->getString());
        // revert custom translations to base translation.
        $edit = [
            'translation[config_names][user.settings][anonymous]' => 'Anonymous',
        ];
        $this->drupalGet('admin/config/people/accounts/translate/fr/edit');
        $this->submitForm($edit, 'Save translation');
        // Make sure there is no translation stored in locale storage after revert.
        $translation = $this->getTranslation('user.settings', 'anonymous', 'fr');
        $this->assertEquals('Anonymous', $translation->getString());
    }
    
    /**
     * Tests the single language existing.
     */
    public function testSingleLanguageUI() : void {
        $this->drupalLogin($this->adminUser);
        // Delete French language
        $this->drupalGet('admin/config/regional/language/delete/fr');
        $this->submitForm([], 'Delete');
        $this->assertSession()
            ->pageTextContains('The French (fr) language has been removed.');
        // Change default language to Tamil.
        $edit = [
            'site_default_language' => 'ta',
        ];
        $this->drupalGet('admin/config/regional/language');
        $this->submitForm($edit, 'Save configuration');
        $this->assertSession()
            ->pageTextContains('Configuration saved.');
        // Delete English language
        $this->drupalGet('admin/config/regional/language/delete/en');
        $this->submitForm([], 'Delete');
        $this->assertSession()
            ->pageTextContains('The English (en) language has been removed.');
        // Visit account setting translation page, this should not
        // throw any notices.
        $this->drupalGet('admin/config/people/accounts/translate');
        $this->assertSession()
            ->statusCodeEquals(200);
    }
    
    /**
     * Tests the config_translation_info_alter() hook.
     */
    public function testAlterInfo() : void {
        $this->drupalLogin($this->adminUser);
        $this->container
            ->get('state')
            ->set('config_translation_test_config_translation_info_alter', TRUE);
        $this->container
            ->get('plugin.manager.config_translation.mapper')
            ->clearCachedDefinitions();
        // Check if the translation page does not have the altered out settings.
        $this->drupalGet('admin/config/people/accounts/translate/fr/add');
        $this->assertSession()
            ->pageTextContains('Name');
        $this->assertSession()
            ->pageTextNotContains('Account cancellation confirmation');
        $this->assertSession()
            ->pageTextNotContains('Password recovery');
    }
    
    /**
     * Tests the sequence data type translation.
     */
    public function testSequenceTranslation() : void {
        $this->drupalLogin($this->adminUser);
        
        /** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */
        $config_factory = $this->container
            ->get('config.factory');
        $expected = [
            'kitten',
            'llama',
            'elephant',
        ];
        $actual = $config_factory->getEditable('config_translation_test.content')
            ->get('animals');
        $this->assertEquals($expected, $actual);
        $edit = [
            'translation[config_names][config_translation_test.content][content][value]' => '<p><strong>Hello World</strong> - FR</p>',
            'translation[config_names][config_translation_test.content][animals][0]' => 'kitten - FR',
            'translation[config_names][config_translation_test.content][animals][1]' => 'llama - FR',
            'translation[config_names][config_translation_test.content][animals][2]' => 'elephant - FR',
        ];
        $this->drupalGet('admin/config/media/file-system/translate/fr/add');
        $this->submitForm($edit, 'Save translation');
        $this->container
            ->get('language.config_factory_override')
            ->setLanguage(new Language([
            'id' => 'fr',
        ]));
        $expected = [
            'kitten - FR',
            'llama - FR',
            'elephant - FR',
        ];
        $actual = $config_factory->get('config_translation_test.content')
            ->get('animals');
        $this->assertEquals($expected, $actual);
    }

}

Classes

Title Deprecated Summary
ConfigTranslationUiTest Translate settings and entities to various languages.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.