You are here

public function LocaleImportFunctionalTest::testConfigPoFile in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/locale/tests/src/Functional/LocaleImportFunctionalTest.php \Drupal\Tests\locale\Functional\LocaleImportFunctionalTest::testConfigPoFile()
  2. 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 304

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.',
      // cSpell:disable-next-line
      '@site karbantartás alatt áll. Rövidesen visszatérünk. Köszönjük a türelmet.',
      'message',
    ],
    'user.role.anonymous' => [
      'Anonymous user',
      // cSpell:disable-next-line
      '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
    ->drupalGet('admin/config/regional/language/add');
  $this
    ->submitForm($edit, '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
      ->drupalGet('admin/config/regional/translate');
    $this
      ->submitForm($search, 'Filter');
    $this
      ->assertSession()
      ->pageTextContains($config_string[1]);
  }

  // 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
      ->assertEquals($override
      ->get($config_string[2]), $config_string[1]);
  }
}