You are here

public function LocaleImportFunctionalTest::testStandalonePoFile in Drupal 9

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

Tests import of standalone .po files.

File

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

Class

LocaleImportFunctionalTest
Tests the import of locale files.

Namespace

Drupal\Tests\locale\Functional

Code

public function testStandalonePoFile() {

  // Try importing a .po file.
  $this
    ->importPoFile($this
    ->getPoFile(), [
    'langcode' => 'fr',
  ]);
  $this
    ->config('locale.settings');

  // The import should automatically create the corresponding language.
  $this
    ->assertSession()
    ->pageTextContains("The language French has been created.");

  // The import should have created 8 strings.
  $this
    ->assertSession()
    ->pageTextContains("One translation file imported. 8 translations were added, 0 translations were updated and 0 translations were removed.");

  // This import should have saved plural forms to have 2 variants.
  $locale_plurals = \Drupal::service('locale.plural.formula')
    ->getNumberOfPlurals('fr');
  $this
    ->assertEquals(2, $locale_plurals, 'Plural number initialized.');

  // Ensure we were redirected correctly.
  $this
    ->assertSession()
    ->addressEquals(Url::fromRoute('locale.translate_page'));

  // Try importing a .po file with invalid tags.
  $this
    ->importPoFile($this
    ->getBadPoFile(), [
    'langcode' => 'fr',
  ]);

  // The import should have created 1 string and rejected 2.
  $this
    ->assertSession()
    ->pageTextContains("One translation file imported. 1 translations were added, 0 translations were updated and 0 translations were removed.");
  $this
    ->assertSession()
    ->pageTextContains("2 translation strings were skipped because of disallowed or malformed HTML. See the log for details.");

  // Repeat the process with a user that can access site reports, and this
  // time the different warnings must contain links to the log.
  $this
    ->drupalLogin($this->adminUserAccessSiteReports);

  // Try importing a .po file with invalid tags.
  $this
    ->importPoFile($this
    ->getBadPoFile(), [
    'langcode' => 'fr',
  ]);
  $this
    ->assertSession()
    ->pageTextContains("2 translation strings were skipped because of disallowed or malformed HTML. See the log for details.");

  // Check empty files import with a user that cannot access site reports..
  $this
    ->drupalLogin($this->adminUser);

  // Try importing a zero byte sized .po file.
  $this
    ->importPoFile($this
    ->getEmptyPoFile(), [
    'langcode' => 'fr',
  ]);

  // The import should have created 0 string and rejected 0.
  $this
    ->assertSession()
    ->pageTextContains("One translation file could not be imported. See the log for details.");

  // Repeat the process with a user that can access site reports, and this
  // time the different warnings must contain links to the log.
  $this
    ->drupalLogin($this->adminUserAccessSiteReports);

  // Try importing a zero byte sized .po file.
  $this
    ->importPoFile($this
    ->getEmptyPoFile(), [
    'langcode' => 'fr',
  ]);

  // The import should have created 0 string and rejected 0.
  $this
    ->assertSession()
    ->pageTextContains("One translation file could not be imported. See the log for details.");

  // Try importing a .po file which doesn't exist.
  $name = $this
    ->randomMachineName(16);
  $this
    ->drupalGet('admin/config/regional/translate/import');
  $this
    ->submitForm([
    'langcode' => 'fr',
    'files[file]' => $name,
  ], 'Import');
  $this
    ->assertSession()
    ->addressEquals(Url::fromRoute('locale.translate_import'));
  $this
    ->assertSession()
    ->pageTextContains('File to import not found.');

  // Try importing a .po file with overriding strings, and ensure existing
  // strings are kept.
  $this
    ->importPoFile($this
    ->getOverwritePoFile(), [
    'langcode' => 'fr',
  ]);

  // The import should have created 1 string.
  $this
    ->assertSession()
    ->pageTextContains("One translation file imported. 1 translations were added, 0 translations were updated and 0 translations were removed.");

  // Ensure string wasn't overwritten.
  $search = [
    'string' => 'Montag',
    'langcode' => 'fr',
    'translation' => 'translated',
  ];
  $this
    ->drupalGet('admin/config/regional/translate');
  $this
    ->submitForm($search, 'Filter');
  $this
    ->assertSession()
    ->pageTextContains('No strings available.');

  // This import should not have changed number of plural forms.
  $locale_plurals = \Drupal::service('locale.plural.formula')
    ->getNumberOfPlurals('fr');
  $this
    ->assertEquals(2, $locale_plurals, 'Plural numbers untouched.');

  // Try importing a .po file with overriding strings, and ensure existing
  // strings are overwritten.
  $this
    ->importPoFile($this
    ->getOverwritePoFile(), [
    'langcode' => 'fr',
    'overwrite_options[not_customized]' => TRUE,
  ]);

  // The import should have updated 2 strings.
  $this
    ->assertSession()
    ->pageTextContains("One translation file imported. 0 translations were added, 2 translations were updated and 0 translations were removed.");

  // Ensure string was overwritten.
  $search = [
    'string' => 'Montag',
    'langcode' => 'fr',
    'translation' => 'translated',
  ];
  $this
    ->drupalGet('admin/config/regional/translate');
  $this
    ->submitForm($search, 'Filter');
  $this
    ->assertSession()
    ->pageTextNotContains('No strings available.');

  // This import should have changed number of plural forms.
  $locale_plurals = \Drupal::service('locale.plural.formula')
    ->reset()
    ->getNumberOfPlurals('fr');
  $this
    ->assertEquals(3, $locale_plurals, 'Plural numbers changed.');

  // Importing a .po file and mark its strings as customized strings.
  $this
    ->importPoFile($this
    ->getCustomPoFile(), [
    'langcode' => 'fr',
    'customized' => TRUE,
  ]);

  // The import should have created 6 strings.
  $this
    ->assertSession()
    ->pageTextContains("One translation file imported. 6 translations were added, 0 translations were updated and 0 translations were removed.");

  // The database should now contain 6 customized strings (two imported
  // strings are not translated).
  $count = Database::getConnection()
    ->select('locales_target')
    ->condition('customized', 1)
    ->countQuery()
    ->execute()
    ->fetchField();
  $this
    ->assertEquals(6, $count, 'Customized translations successfully imported.');

  // Try importing a .po file with overriding strings, and ensure existing
  // customized strings are kept.
  $this
    ->importPoFile($this
    ->getCustomOverwritePoFile(), [
    'langcode' => 'fr',
    'overwrite_options[not_customized]' => TRUE,
    'overwrite_options[customized]' => FALSE,
  ]);

  // The import should have created 1 string.
  $this
    ->assertSession()
    ->pageTextContains("One translation file imported. 1 translations were added, 0 translations were updated and 0 translations were removed.");

  // Ensure string wasn't overwritten.
  $search = [
    'string' => 'januari',
    'langcode' => 'fr',
    'translation' => 'translated',
  ];
  $this
    ->drupalGet('admin/config/regional/translate');
  $this
    ->submitForm($search, 'Filter');
  $this
    ->assertSession()
    ->pageTextContains('No strings available.');

  // Try importing a .po file with overriding strings, and ensure existing
  // customized strings are overwritten.
  $this
    ->importPoFile($this
    ->getCustomOverwritePoFile(), [
    'langcode' => 'fr',
    'overwrite_options[not_customized]' => FALSE,
    'overwrite_options[customized]' => TRUE,
  ]);

  // The import should have updated 2 strings.
  $this
    ->assertSession()
    ->pageTextContains("One translation file imported. 0 translations were added, 2 translations were updated and 0 translations were removed.");

  // Ensure string was overwritten.
  $search = [
    'string' => 'januari',
    'langcode' => 'fr',
    'translation' => 'translated',
  ];
  $this
    ->drupalGet('admin/config/regional/translate');
  $this
    ->submitForm($search, 'Filter');
  $this
    ->assertSession()
    ->pageTextNotContains('No strings available.');
}