You are here

public function LocalePluralFormatTest::testPluralEditExport in Drupal 8

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

Tests plural editing and export functionality.

File

core/modules/locale/tests/src/Functional/LocalePluralFormatTest.php, line 234

Class

LocalePluralFormatTest
Tests plural handling for various languages.

Namespace

Drupal\Tests\locale\Functional

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(), [
    'langcode' => 'fr',
  ]);
  $this
    ->importPoFile($this
    ->getPoFileWithComplexPlural(), [
    'langcode' => 'hr',
  ]);

  // Get the French translations.
  $this
    ->drupalPostForm('admin/config/regional/translate/export', [
    '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', [
    '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 = [
    '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');
  $connection = Database::getConnection();

  // Edit langcode hr translations and see if that took effect.
  $lid = $connection
    ->select('locales_source', 'ls')
    ->fields('ls', [
    'lid',
  ])
    ->condition('source', "1 hour" . PoItem::DELIMITER . "@count hours")
    ->condition('context', '')
    ->execute()
    ->fetchField();
  $edit = [
    "strings[{$lid}][translations][1]" => '@count sata edited',
  ];
  $this
    ->drupalPostForm($path, $edit, t('Save translations'));
  $search = [
    '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 = [
    "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', [], [
    'langcode' => 'fr',
  ])
    ->render();
  $lid = $connection
    ->select('locales_source', 'ls')
    ->fields('ls', [
    'lid',
  ])
    ->condition('source', "1 day" . PoItem::DELIMITER . "@count days")
    ->condition('context', '')
    ->execute()
    ->fetchField();

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

  // Save complete translations for the string in langcode fr.
  $edit = [
    "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 = [
    'string' => '1 day',
    'langcode' => 'hr',
  ];
  $this
    ->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
  $edit = [
    "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', [
    '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', [
    '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.');
}