function LocaleImportFunctionalTest::testStandalonePoFile

Test import of standalone .po files.

File

drupal/core/modules/locale/lib/Drupal/locale/Tests/LocaleImportFunctionalTest.php, line 51
Definition of Drupal\locale\Tests\LocaleImportFunctionalTest.

Class

LocaleImportFunctionalTest
Functional tests for the import of translation files.

Namespace

Drupal\locale\Tests

Code

function testStandalonePoFile() {

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

  // The import should automatically create the corresponding language.
  $this
    ->assertRaw(t('The language %language has been created.', array(
    '%language' => 'French',
  )), t('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.', array(
    '%number' => 8,
    '%update' => 0,
    '%delete' => 0,
  )), t('The translation file was successfully imported.'));

  // This import should have saved plural forms to have 2 variants.
  $locale_plurals = \Drupal::state()
    ->get('locale.translation.plurals') ?: array();
  $this
    ->assert($locale_plurals['fr']['plurals'] == 2, t('Plural number initialized.'));

  // Ensure we were redirected correctly.
  $this
    ->assertEqual($this
    ->getUrl(), url('admin/config/regional/translate', array(
    'absolute' => TRUE,
  )), t('Correct page redirection.'));

  // Try importing a .po file with invalid tags.
  $this
    ->importPoFile($this
    ->getBadPoFile(), array(
    '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.', array(
    '%number' => 1,
    '%update' => 0,
    '%delete' => 0,
  )), t('The translation file was successfully imported.'));
  $skip_message = format_plural(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.', array(
    '@url' => url('admin/reports/dblog'),
  ));
  $this
    ->assertRaw($skip_message, t('Unsafe strings were skipped.'));

  // Try importing a zero byte sized .po file.
  $this
    ->importPoFile($this
    ->getEmptyPoFile(), array(
    '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.', array(
    '@url' => url('admin/reports/dblog'),
  )), 'The empty translation file was successfully imported.');

  // Try importing a .po file which doesn't exist.
  $name = $this
    ->randomName(16);
  $this
    ->drupalPost('admin/config/regional/translate/import', array(
    'langcode' => 'fr',
    'files[file]' => $name,
  ), t('Import'));
  $this
    ->assertEqual($this
    ->getUrl(), url('admin/config/regional/translate/import', array(
    'absolute' => TRUE,
  )), t('Correct page redirection.'));
  $this
    ->assertText(t('File to import not found.'), t('File to import not found message.'));

  // Try importing a .po file with overriding strings, and ensure existing
  // strings are kept.
  $this
    ->importPoFile($this
    ->getOverwritePoFile(), array(
    '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.', array(
    '%number' => 1,
    '%update' => 0,
    '%delete' => 0,
  )), t('The translation file was successfully imported.'));

  // Ensure string wasn't overwritten.
  $search = array(
    'string' => 'Montag',
    'langcode' => 'fr',
    'translation' => 'translated',
  );
  $this
    ->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  $this
    ->assertText(t('No strings available.'), t('String not overwritten by imported string.'));

  // This import should not have changed number of plural forms.
  $locale_plurals = \Drupal::state()
    ->get('locale.translation.plurals') ?: array();
  $this
    ->assert($locale_plurals['fr']['plurals'] == 2, t('Plural numbers untouched.'));

  // Try importing a .po file with overriding strings, and ensure existing
  // strings are overwritten.
  $this
    ->importPoFile($this
    ->getOverwritePoFile(), array(
    '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.', array(
    '%number' => 0,
    '%update' => 2,
    '%delete' => 0,
  )), t('The translation file was successfully imported.'));

  // Ensure string was overwritten.
  $search = array(
    'string' => 'Montag',
    'langcode' => 'fr',
    'translation' => 'translated',
  );
  $this
    ->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  $this
    ->assertNoText(t('No strings available.'), t('String overwritten by imported string.'));

  // This import should have changed number of plural forms.
  $locale_plurals = \Drupal::state()
    ->get('locale.translation.plurals') ?: array();
  $this
    ->assert($locale_plurals['fr']['plurals'] == 3, t('Plural numbers changed.'));

  // Importing a .po file and mark its strings as customized strings.
  $this
    ->importPoFile($this
    ->getCustomPoFile(), array(
    '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.', array(
    '%number' => 6,
    '%update' => 0,
    '%delete' => 0,
  )), t('The customized translation file was successfully imported.'));

  // The database should now contain 6 customized strings (two imported
  // strings are not translated).
  $count = db_query('SELECT lid FROM {locales_target} WHERE customized = :custom', array(
    ':custom' => 1,
  ))
    ->rowCount();
  $this
    ->assertEqual($count, 6, t('Customized translations succesfully imported.'));

  // Try importing a .po file with overriding strings, and ensure existing
  // customized strings are kept.
  $this
    ->importPoFile($this
    ->getCustomOverwritePoFile(), array(
    '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.', array(
    '%number' => 1,
    '%update' => 0,
    '%delete' => 0,
  )), t('The customized translation file was successfully imported.'));

  // Ensure string wasn't overwritten.
  $search = array(
    'string' => 'januari',
    'langcode' => 'fr',
    'translation' => 'translated',
  );
  $this
    ->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  $this
    ->assertText(t('No strings available.'), t('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(), array(
    '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.', array(
    '%number' => 0,
    '%update' => 2,
    '%delete' => 0,
  )), t('The customized translation file was successfully imported.'));

  // Ensure string was overwritten.
  $search = array(
    'string' => 'januari',
    'langcode' => 'fr',
    'translation' => 'translated',
  );
  $this
    ->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  $this
    ->assertNoText(t('No strings available.'), t('Customized string overwritten by imported string.'));
}