You are here

public function LocalePluralFormatTest::testPluralEditExport in Drupal 9

Same name and namespace in other branches
  1. 8 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 237

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
    ->drupalGet('admin/config/regional/translate/export');
  $this
    ->submitForm([
    'langcode' => 'fr',
  ], 'Export');

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

  // Ensure our imported translations exist in the file.
  $this
    ->assertSession()
    ->responseContains("msgid \"Monday\"\nmsgstr \"lundi\"");

  // Check for plural export specifically.
  $this
    ->assertSession()
    ->responseContains("msgid \"1 hour\"\nmsgid_plural \"@count hours\"\nmsgstr[0] \"@count heure\"\nmsgstr[1] \"@count heures\"");

  // Get the Croatian translations.
  $this
    ->drupalGet('admin/config/regional/translate/export');
  $this
    ->submitForm([
    'langcode' => 'hr',
  ], 'Export');

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

  // Ensure our imported translations exist in the file.
  $this
    ->assertSession()
    ->responseContains("msgid \"Monday\"\nmsgstr \"Ponedjeljak\"");

  // Check for plural export specifically.
  $this
    ->assertSession()
    ->responseContains("msgid \"1 hour\"\nmsgid_plural \"@count hours\"\nmsgstr[0] \"@count sat\"\nmsgstr[1] \"@count sata\"\nmsgstr[2] \"@count sati\"");

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

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

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

  // Plural values for langcode hr.
  $this
    ->assertSession()
    ->pageTextContains('@count sat');
  $this
    ->assertSession()
    ->pageTextContains('@count sata');
  $this
    ->assertSession()
    ->pageTextContains('@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
    ->drupalGet($path);
  $this
    ->submitForm($edit, 'Save translations');
  $search = [
    'langcode' => 'fr',
  ];
  $this
    ->drupalGet('admin/config/regional/translate');
  $this
    ->submitForm($search, 'Filter');

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

  // Edit langcode fr translations and see if that took effect.
  $edit = [
    "strings[{$lid}][translations][0]" => '@count heure edited',
  ];
  $this
    ->drupalGet($path);
  $this
    ->submitForm($edit, '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
    ->drupalGet('admin/config/regional/translate');
  $this
    ->submitForm($search, 'Filter');

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

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

  // Get the French translations.
  $this
    ->drupalGet('admin/config/regional/translate/export');
  $this
    ->submitForm([
    'langcode' => 'fr',
  ], 'Export');

  // Check for plural export specifically.
  $this
    ->assertSession()
    ->responseContains("msgid \"1 hour\"\nmsgid_plural \"@count hours\"\nmsgstr[0] \"@count heure edited\"\nmsgstr[1] \"@count heures\"");
  $this
    ->assertSession()
    ->responseContains("msgid \"1 day\"\nmsgid_plural \"@count days\"\nmsgstr[0] \"1 jour\"\nmsgstr[1] \"@count jours\"");

  // Get the Croatian translations.
  $this
    ->drupalGet('admin/config/regional/translate/export');
  $this
    ->submitForm([
    'langcode' => 'hr',
  ], 'Export');

  // Check for plural export specifically.
  $this
    ->assertSession()
    ->responseContains("msgid \"1 hour\"\nmsgid_plural \"@count hours\"\nmsgstr[0] \"@count sat\"\nmsgstr[1] \"@count sata edited\"\nmsgstr[2] \"@count sati\"");
  $this
    ->assertSession()
    ->responseContains("msgid \"1 day\"\nmsgid_plural \"@count days\"\nmsgstr[0] \"@count dan\"\nmsgstr[1] \"@count dana\"\nmsgstr[2] \"@count dana\"");
}