function LocalePluralFormatTest::testPluralEditExport

Tests plural editing and export functionality.

File

drupal/core/modules/locale/lib/Drupal/locale/Tests/LocalePluralFormatTest.php, line 137
Definition of Drupal\locale\Tests\LocalePluralFormatTest.

Class

LocalePluralFormatTest
Tests plural format handling functionality.

Namespace

Drupal\locale\Tests

Code

function testPluralEditExport() {

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

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

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

  // Ensure our imported translations exist in the file.
  $this
    ->assertRaw("msgid \"Monday\"\nmsgstr \"lundi\"", t('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
    ->drupalPost('admin/config/regional/translate/export', array(
    'langcode' => 'hr',
  ), t('Export'));

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

  // Ensure our imported translations exist in the file.
  $this
    ->assertRaw("msgid \"Monday\"\nmsgstr \"Ponedjeljak\"", t('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\"", t('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/';
  $this
    ->drupalGet($path);

  // 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.
  $edit = array(
    'strings[10][translations][1]' => '@count sata edited',
  );
  $this
    ->drupalPost($path, $edit, t('Save translations'));
  $search = array(
    'langcode' => 'fr',
  );
  $this
    ->drupalPost('admin/config/regional/translate/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[10][translations][0]' => '@count heure edited',
  );
  $this
    ->drupalPost($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.
  format_plural(1, '1 day', '@count days', array(), array(
    'langcode' => 'fr',
  ));
  $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
    ->drupalPost('admin/config/regional/translate/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
    ->drupalPost($path, $edit, t('Save translations'));

  // Save complete translations for the string in langcode hr.
  $search = array(
    'string' => '1 day',
    'langcode' => 'hr',
  );
  $this
    ->drupalPost('admin/config/regional/translate/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
    ->drupalPost($path, $edit, t('Save translations'));

  // Get the French translations.
  $this
    ->drupalPost('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\"", t('Added French plural translations for days exported properly.'));

  // Get the Croatian translations.
  $this
    ->drupalPost('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\"", t('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\"", t('Added Croatian plural translations exported properly.'));
}