You are here

public function LocalePluralFormatTest::testPluralEditExport in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/locale/src/Tests/LocalePluralFormatTest.php \Drupal\locale\Tests\LocalePluralFormatTest::testPluralEditExport()

Tests plural editing and export functionality.

File

core/modules/locale/src/Tests/LocalePluralFormatTest.php, line 210
Contains \Drupal\locale\Tests\LocalePluralFormatTest.

Class

LocalePluralFormatTest
Tests plural handling for various languages.

Namespace

Drupal\locale\Tests

Code

public function testPluralEditExport() {

  // Import some .po files with formulas to set up the environment.
  // These will also add the languages to the system.
  $this
    ->importPoFile($this
    ->getPoFileWithSimplePlural(), array(
    'langcode' => 'fr',
  ));
  $this
    ->importPoFile($this
    ->getPoFileWithComplexPlural(), array(
    'langcode' => 'hr',
  ));

  // Get the French translations.
  $this
    ->drupalPostForm('admin/config/regional/translate/export', array(
    'langcode' => 'fr',
  ), t('Export'));

  // Ensure we have a translation file.
  $this
    ->assertRaw('# French translation of Drupal', 'Exported French translation file.');

  // Ensure our imported translations exist in the file.
  $this
    ->assertRaw("msgid \"Monday\"\nmsgstr \"lundi\"", 'French translations present in exported file.');

  // Check for plural export specifically.
  $this
    ->assertRaw("msgid \"1 hour\"\nmsgid_plural \"@count hours\"\nmsgstr[0] \"@count heure\"\nmsgstr[1] \"@count heures\"", 'Plural translations exported properly.');

  // Get the Croatian translations.
  $this
    ->drupalPostForm('admin/config/regional/translate/export', array(
    'langcode' => 'hr',
  ), t('Export'));

  // Ensure we have a translation file.
  $this
    ->assertRaw('# Croatian translation of Drupal', 'Exported Croatian translation file.');

  // Ensure our imported translations exist in the file.
  $this
    ->assertRaw("msgid \"Monday\"\nmsgstr \"Ponedjeljak\"", 'Croatian translations present in exported file.');

  // Check for plural export specifically.
  $this
    ->assertRaw("msgid \"1 hour\"\nmsgid_plural \"@count hours\"\nmsgstr[0] \"@count sat\"\nmsgstr[1] \"@count sata\"\nmsgstr[2] \"@count sati\"", 'Plural translations exported properly.');

  // Check if the source appears on the translation page.
  $this
    ->drupalGet('admin/config/regional/translate');
  $this
    ->assertText("1 hour");
  $this
    ->assertText("@count hours");

  // Look up editing page for this plural string and check fields.
  $path = 'admin/config/regional/translate/';
  $search = array(
    'langcode' => 'hr',
  );
  $this
    ->drupalPostForm($path, $search, t('Filter'));

  // Labels for plural editing elements.
  $this
    ->assertText('Singular form');
  $this
    ->assertText('First plural form');
  $this
    ->assertText('2. plural form');
  $this
    ->assertNoText('3. plural form');

  // Plural values for langcode hr.
  $this
    ->assertText('@count sat');
  $this
    ->assertText('@count sata');
  $this
    ->assertText('@count sati');

  // Edit langcode hr translations and see if that took effect.
  $lid = db_query("SELECT lid FROM {locales_source} WHERE source = :source AND context = ''", array(
    ':source' => "1 hour" . LOCALE_PLURAL_DELIMITER . "@count hours",
  ))
    ->fetchField();
  $edit = array(
    "strings[{$lid}][translations][1]" => '@count sata edited',
  );
  $this
    ->drupalPostForm($path, $edit, t('Save translations'));
  $search = array(
    'langcode' => 'fr',
  );
  $this
    ->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));

  // Plural values for the langcode fr.
  $this
    ->assertText('@count heure');
  $this
    ->assertText('@count heures');
  $this
    ->assertNoText('2. plural form');

  // Edit langcode fr translations and see if that took effect.
  $edit = array(
    "strings[{$lid}][translations][0]" => '@count heure edited',
  );
  $this
    ->drupalPostForm($path, $edit, t('Save translations'));

  // Inject a plural source string to the database. We need to use a specific
  // langcode here because the language will be English by default and will
  // not save our source string for performance optimization if we do not ask
  // specifically for a language.
  \Drupal::translation()
    ->formatPlural(1, '1 day', '@count days', array(), array(
    'langcode' => 'fr',
  ))
    ->render();
  $lid = db_query("SELECT lid FROM {locales_source} WHERE source = :source AND context = ''", array(
    ':source' => "1 day" . LOCALE_PLURAL_DELIMITER . "@count days",
  ))
    ->fetchField();

  // Look up editing page for this plural string and check fields.
  $search = array(
    'string' => '1 day',
    'langcode' => 'fr',
  );
  $this
    ->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));

  // Save complete translations for the string in langcode fr.
  $edit = array(
    "strings[{$lid}][translations][0]" => '1 jour',
    "strings[{$lid}][translations][1]" => '@count jours',
  );
  $this
    ->drupalPostForm($path, $edit, t('Save translations'));

  // Save complete translations for the string in langcode hr.
  $search = array(
    'string' => '1 day',
    'langcode' => 'hr',
  );
  $this
    ->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
  $edit = array(
    "strings[{$lid}][translations][0]" => '@count dan',
    "strings[{$lid}][translations][1]" => '@count dana',
    "strings[{$lid}][translations][2]" => '@count dana',
  );
  $this
    ->drupalPostForm($path, $edit, t('Save translations'));

  // Get the French translations.
  $this
    ->drupalPostForm('admin/config/regional/translate/export', array(
    'langcode' => 'fr',
  ), t('Export'));

  // Check for plural export specifically.
  $this
    ->assertRaw("msgid \"1 hour\"\nmsgid_plural \"@count hours\"\nmsgstr[0] \"@count heure edited\"\nmsgstr[1] \"@count heures\"", 'Edited French plural translations for hours exported properly.');
  $this
    ->assertRaw("msgid \"1 day\"\nmsgid_plural \"@count days\"\nmsgstr[0] \"1 jour\"\nmsgstr[1] \"@count jours\"", 'Added French plural translations for days exported properly.');

  // Get the Croatian translations.
  $this
    ->drupalPostForm('admin/config/regional/translate/export', array(
    'langcode' => 'hr',
  ), t('Export'));

  // Check for plural export specifically.
  $this
    ->assertRaw("msgid \"1 hour\"\nmsgid_plural \"@count hours\"\nmsgstr[0] \"@count sat\"\nmsgstr[1] \"@count sata edited\"\nmsgstr[2] \"@count sati\"", 'Edited Croatian plural translations exported properly.');
  $this
    ->assertRaw("msgid \"1 day\"\nmsgid_plural \"@count days\"\nmsgstr[0] \"@count dan\"\nmsgstr[1] \"@count dana\"\nmsgstr[2] \"@count dana\"", 'Added Croatian plural translations exported properly.');
}