function LocalePluralFormatTest::testPluralEditDateFormatter

Same name and namespace in other branches
  1. 9 core/modules/locale/tests/src/Functional/LocalePluralFormatTest.php \Drupal\Tests\locale\Functional\LocalePluralFormatTest::testPluralEditDateFormatter()
  2. 8.9.x core/modules/locale/tests/src/Functional/LocalePluralFormatTest.php \Drupal\Tests\locale\Functional\LocalePluralFormatTest::testPluralEditDateFormatter()
  3. 11.x core/modules/locale/tests/src/Functional/LocalePluralFormatTest.php \Drupal\Tests\locale\Functional\LocalePluralFormatTest::testPluralEditDateFormatter()

Tests plural editing of DateFormatter strings.

File

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

Class

LocalePluralFormatTest
Tests plural handling for various languages.

Namespace

Drupal\Tests\locale\Functional

Code

public function testPluralEditDateFormatter() : void {
  // 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',
  ]);
  // Set French as the site default language.
  $this->config('system.site')
    ->set('default_langcode', 'fr')
    ->save();
  // Visit User Info page before updating translation strings. Change the
  // created time to ensure that the we're dealing in seconds and it can't be
  // exactly 1 minute.
  $this->adminUser
    ->set('created', time() - 1)
    ->save();
  $this->drupalGet('user');
  // Member for time should be translated.
  $this->assertSession()
    ->pageTextContains("seconde");
  $path = 'admin/config/regional/translate/';
  $search = [
    'langcode' => 'fr',
    // Limit to only translated strings to ensure that database ordering does
    // not break the test.
'translation' => 'translated',
  ];
  $this->drupalGet($path);
  $this->submitForm($search, 'Filter');
  // Plural values for the langcode fr.
  $this->assertSession()
    ->pageTextContains('@count seconde');
  $this->assertSession()
    ->pageTextContains('@count secondes');
  // 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, '@count second', '@count seconds', [], [
    'langcode' => 'fr',
  ])
    ->render();
  $lid = Database::getConnection()->select('locales_source', 'ls')
    ->fields('ls', [
    'lid',
  ])
    ->condition('source', "@count second" . PoItem::DELIMITER . "@count seconds")
    ->condition('context', '')
    ->execute()
    ->fetchField();
  // Look up editing page for this plural string and check fields.
  $search = [
    'string' => '@count second',
    '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]" => '@count seconde updated',
    "strings[{$lid}][translations][1]" => '@count secondes updated',
  ];
  $this->drupalGet($path);
  $this->submitForm($edit, 'Save translations');
  // User interface input for translating seconds should not be duplicated
  $this->assertSession()
    ->pageTextContainsOnce('@count seconds');
  // Member for time should be translated. Change the created time to ensure
  // that the we're dealing in multiple seconds and it can't be exactly 1
  // second or minute.
  $this->adminUser
    ->set('created', time() - 2)
    ->save();
  $this->drupalGet('user');
  $this->assertSession()
    ->pageTextContains("secondes updated");
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.