You are here

public function LocaleImportFunctionalTest::testConfigPoFile in Drupal 8

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

Tests .po file import with configuration translation.

File

core/modules/locale/tests/src/Functional/LocaleImportFunctionalTest.php, line 301

Class

LocaleImportFunctionalTest
Tests the import of locale files.

Namespace

Drupal\Tests\locale\Functional

Code

public function testConfigPoFile() {

  // Values for translations to assert. Config key, original string,
  // translation and config property name.
  $config_strings = [
    'system.maintenance' => [
      '@site is currently under maintenance. We should be back shortly. Thank you for your patience.',
      '@site karbantartás alatt áll. Rövidesen visszatérünk. Köszönjük a türelmet.',
      'message',
    ],
    'user.role.anonymous' => [
      'Anonymous user',
      'Névtelen felhasználó',
      'label',
    ],
  ];

  // Add custom language for testing.
  $langcode = 'xx';
  $edit = [
    'predefined_langcode' => 'custom',
    'langcode' => $langcode,
    'label' => $this
      ->randomMachineName(16),
    'direction' => LanguageInterface::DIRECTION_LTR,
  ];
  $this
    ->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));

  // Check for the source strings we are going to translate. Adding the
  // custom language should have made the process to export configuration
  // strings to interface translation executed.
  $locale_storage = $this->container
    ->get('locale.storage');
  foreach ($config_strings as $config_string) {
    $string = $locale_storage
      ->findString([
      'source' => $config_string[0],
      'context' => '',
      'type' => 'configuration',
    ]);
    $this
      ->assertNotEmpty($string, 'Configuration strings have been created upon installation.');
  }

  // Import a .po file to translate.
  $this
    ->importPoFile($this
    ->getPoFileWithConfig(), [
    'langcode' => $langcode,
  ]);

  // Translations got recorded in the interface translation system.
  foreach ($config_strings as $config_string) {
    $search = [
      'string' => $config_string[0],
      'langcode' => $langcode,
      'translation' => 'all',
    ];
    $this
      ->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
    $this
      ->assertText($config_string[1], new FormattableMarkup('Translation of @string found.', [
      '@string' => $config_string[0],
    ]));
  }

  // Test that translations got recorded in the config system.
  $overrides = \Drupal::service('language.config_factory_override');
  foreach ($config_strings as $config_key => $config_string) {
    $override = $overrides
      ->getOverride($langcode, $config_key);
    $this
      ->assertEqual($override
      ->get($config_string[2]), $config_string[1]);
  }
}