function LocaleImportFunctionalTest::testStandalonePoFile

Same name in other branches
  1. 7.x modules/locale/locale.test \LocaleImportFunctionalTest::testStandalonePoFile()
  2. 9 core/modules/locale/tests/src/Functional/LocaleImportFunctionalTest.php \Drupal\Tests\locale\Functional\LocaleImportFunctionalTest::testStandalonePoFile()
  3. 10 core/modules/locale/tests/src/Functional/LocaleImportFunctionalTest.php \Drupal\Tests\locale\Functional\LocaleImportFunctionalTest::testStandalonePoFile()
  4. 11.x core/modules/locale/tests/src/Functional/LocaleImportFunctionalTest.php \Drupal\Tests\locale\Functional\LocaleImportFunctionalTest::testStandalonePoFile()

Test import of standalone .po files.

File

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

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->assertRaw(t('The language %language has been created.', [
        '%language' => 'French',
    ]), 'The language has been automatically created.');
    // The import should have created 8 strings.
    $this->assertRaw(t('One translation file imported. %number translations were added, %update translations were updated and %delete translations were removed.', [
        '%number' => 8,
        '%update' => 0,
        '%delete' => 0,
    ]), 'The translation file was successfully imported.');
    // This import should have saved plural forms to have 2 variants.
    $locale_plurals = \Drupal::service('locale.plural.formula')->getNumberOfPlurals('fr');
    $this->assertEqual(2, $locale_plurals, 'Plural number initialized.');
    // Ensure we were redirected correctly.
    $this->assertUrl(Url::fromRoute('locale.translate_page', [], [
        'absolute' => TRUE,
    ])->toString(), [], 'Correct page redirection.');
    // 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->assertRaw(t('One translation file imported. %number translations were added, %update translations were updated and %delete translations were removed.', [
        '%number' => 1,
        '%update' => 0,
        '%delete' => 0,
    ]), 'The translation file was successfully imported.');
    $skip_message = \Drupal::translation()->formatPlural(2, 'One translation string was skipped because of disallowed or malformed HTML. <a href=":url">See the log</a> for details.', '@count translation strings were skipped because of disallowed or malformed HTML. See the log for details.', [
        ':url' => Url::fromRoute('dblog.overview')->toString(),
    ]);
    $this->assertRaw($skip_message, 'Unsafe strings were skipped.');
    // 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',
    ]);
    $skip_message = \Drupal::translation()->formatPlural(2, 'One translation string was skipped because of disallowed or malformed HTML. <a href=":url">See the log</a> for details.', '@count translation strings were skipped because of disallowed or malformed HTML. <a href=":url">See the log</a> for details.', [
        ':url' => Url::fromRoute('dblog.overview')->toString(),
    ]);
    $this->assertRaw($skip_message, 'Unsafe strings were skipped.');
    // 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->assertRaw(t('One translation file could not be imported. See the log for details.'), 'The empty translation file import reported no translations imported.');
    // 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->assertRaw(t('One translation file could not be imported. <a href=":url">See the log</a> for details.', [
        ':url' => Url::fromRoute('dblog.overview')->toString(),
    ]), 'The empty translation file import reported no translations imported.');
    // Try importing a .po file which doesn't exist.
    $name = $this->randomMachineName(16);
    $this->drupalPostForm('admin/config/regional/translate/import', [
        'langcode' => 'fr',
        'files[file]' => $name,
    ], t('Import'));
    $this->assertUrl(Url::fromRoute('locale.translate_import', [], [
        'absolute' => TRUE,
    ])->toString(), [], 'Correct page redirection.');
    $this->assertText(t('File to import not found.'), 'File to import not found message.');
    // 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->assertRaw(t('One translation file imported. %number translations were added, %update translations were updated and %delete translations were removed.', [
        '%number' => 1,
        '%update' => 0,
        '%delete' => 0,
    ]), 'The translation file was successfully imported.');
    // Ensure string wasn't overwritten.
    $search = [
        'string' => 'Montag',
        'langcode' => 'fr',
        'translation' => 'translated',
    ];
    $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
    $this->assertText(t('No strings available.'), 'String not overwritten by imported string.');
    // This import should not have changed number of plural forms.
    $locale_plurals = \Drupal::service('locale.plural.formula')->getNumberOfPlurals('fr');
    $this->assertEqual(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->assertRaw(t('One translation file imported. %number translations were added, %update translations were updated and %delete translations were removed.', [
        '%number' => 0,
        '%update' => 2,
        '%delete' => 0,
    ]), 'The translation file was successfully imported.');
    // Ensure string was overwritten.
    $search = [
        'string' => 'Montag',
        'langcode' => 'fr',
        'translation' => 'translated',
    ];
    $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
    $this->assertNoText(t('No strings available.'), 'String overwritten by imported string.');
    // This import should have changed number of plural forms.
    $locale_plurals = \Drupal::service('locale.plural.formula')->reset()
        ->getNumberOfPlurals('fr');
    $this->assertEqual(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->assertRaw(t('One translation file imported. %number translations were added, %update translations were updated and %delete translations were removed.', [
        '%number' => 6,
        '%update' => 0,
        '%delete' => 0,
    ]), 'The customized translation file was successfully imported.');
    // 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->assertEqual($count, 6, '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->assertRaw(t('One translation file imported. %number translations were added, %update translations were updated and %delete translations were removed.', [
        '%number' => 1,
        '%update' => 0,
        '%delete' => 0,
    ]), 'The customized translation file was successfully imported.');
    // Ensure string wasn't overwritten.
    $search = [
        'string' => 'januari',
        'langcode' => 'fr',
        'translation' => 'translated',
    ];
    $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
    $this->assertText(t('No strings available.'), 'Customized string not overwritten by imported string.');
    // 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->assertRaw(t('One translation file imported. %number translations were added, %update translations were updated and %delete translations were removed.', [
        '%number' => 0,
        '%update' => 2,
        '%delete' => 0,
    ]), 'The customized translation file was successfully imported.');
    // Ensure string was overwritten.
    $search = [
        'string' => 'januari',
        'langcode' => 'fr',
        'translation' => 'translated',
    ];
    $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
    $this->assertNoText(t('No strings available.'), 'Customized string overwritten by imported string.');
}

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