You are here

public function LocaleConfigTranslationImportTest::testConfigTranslationImport in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/locale/tests/src/Functional/LocaleConfigTranslationImportTest.php \Drupal\Tests\locale\Functional\LocaleConfigTranslationImportTest::testConfigTranslationImport()
  2. 9 core/modules/locale/tests/src/Functional/LocaleConfigTranslationImportTest.php \Drupal\Tests\locale\Functional\LocaleConfigTranslationImportTest::testConfigTranslationImport()

Tests update changes configuration translations if enabled after language.

File

core/modules/locale/tests/src/Functional/LocaleConfigTranslationImportTest.php, line 38

Class

LocaleConfigTranslationImportTest
Tests translation update's effects on configuration translations.

Namespace

Drupal\Tests\locale\Functional

Code

public function testConfigTranslationImport() {
  $admin_user = $this
    ->drupalCreateUser([
    'administer modules',
    'administer site configuration',
    'administer languages',
    'access administration pages',
    'administer permissions',
  ]);
  $this
    ->drupalLogin($admin_user);

  // Add a language. The Afrikaans translation file of locale_test_translate
  // (test.af.po) has been prepared with a configuration translation.
  ConfigurableLanguage::createFromLangcode('af')
    ->save();

  // Enable locale module.
  $this->container
    ->get('module_installer')
    ->install([
    'locale',
  ]);
  $this
    ->resetAll();

  // 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();

  // Add translation permissions now that the locale module has been enabled.
  $edit = [
    'authenticated[translate interface]' => 'translate interface',
  ];
  $this
    ->drupalGet('admin/people/permissions');
  $this
    ->submitForm($edit, 'Save permissions');

  // Check and update the translation status. This will import the Afrikaans
  // translations of locale_test_translate module.
  $this
    ->drupalGet('admin/reports/translations/check');

  // Override the Drupal core translation status to be up to date.
  // Drupal core should not be a subject in this test.
  $status = locale_translation_get_status();
  $status['drupal']['af']->type = 'current';
  \Drupal::state()
    ->set('locale.translation_status', $status);
  $this
    ->drupalGet('admin/reports/translations');
  $this
    ->submitForm([], 'Update translations');

  // Check if configuration translations have been imported.
  $override = \Drupal::languageManager()
    ->getLanguageConfigOverride('af', 'system.maintenance');

  // cSpell:disable-next-line
  $this
    ->assertEquals('Ons is tans besig met onderhoud op @site. Wees asseblief geduldig, ons sal binnekort weer terug wees.', $override
    ->get('message'));

  // Ensure that \Drupal\locale\LocaleConfigSubscriber::onConfigSave() works
  // as expected during a configuration install that installs locale.

  /** @var \Drupal\Core\Config\FileStorage $sync */
  $sync = $this->container
    ->get('config.storage.sync');
  $this
    ->copyConfig($this->container
    ->get('config.storage'), $sync);

  // Add our own translation to the config that will be imported.
  $af_sync = $sync
    ->createCollection('language.af');
  $data = $af_sync
    ->read('system.maintenance');
  $data['message'] = 'Test af message';
  $af_sync
    ->write('system.maintenance', $data);

  // Uninstall locale module.
  $this->container
    ->get('module_installer')
    ->uninstall([
    'locale_test_translate',
  ]);
  $this->container
    ->get('module_installer')
    ->uninstall([
    'locale',
  ]);
  $this
    ->resetAll();
  $this
    ->configImporter()
    ->import();
  $this
    ->drupalGet('admin/reports/translations/check');
  $status = locale_translation_get_status();
  $status['drupal']['af']->type = 'current';
  \Drupal::state()
    ->set('locale.translation_status', $status);
  $this
    ->drupalGet('admin/reports/translations');
  $this
    ->submitForm([], 'Update translations');

  // Check if configuration translations have been imported.
  $override = \Drupal::languageManager()
    ->getLanguageConfigOverride('af', 'system.maintenance');
  $this
    ->assertEquals('Test af message', $override
    ->get('message'));
}