function ConfigTranslationViewListUiTest::testTimestampFormatterTranslation

Same name and namespace in other branches
  1. 9 core/modules/config_translation/tests/src/Functional/ConfigTranslationViewListUiTest.php \Drupal\Tests\config_translation\Functional\ConfigTranslationViewListUiTest::testTimestampFormatterTranslation()
  2. 11.x core/modules/config_translation/tests/src/Functional/ConfigTranslationViewListUiTest.php \Drupal\Tests\config_translation\Functional\ConfigTranslationViewListUiTest::testTimestampFormatterTranslation()

Test to ensure that TimestampFormatter translation works.

File

core/modules/config_translation/tests/src/Functional/ConfigTranslationViewListUiTest.php, line 75

Class

ConfigTranslationViewListUiTest
Visit view list and test if translate is available.

Namespace

Drupal\Tests\config_translation\Functional

Code

public function testTimestampFormatterTranslation() : void {
  ConfigurableLanguage::createFromLangcode('de')->save();
  $this->drupalCreateContentType([
    'type' => 'article',
  ]);
  $node = $this->drupalCreateNode([
    'type' => 'article',
    'title' => $this->randomMachineName(),
  ]);
  // Update the view to set the field formatter.
  $view = Views::getView('content');
  $display =& $view->storage
    ->getDisplay('default');
  $display['display_options']['fields']['changed']['type'] = 'timestamp_ago';
  $display['display_options']['fields']['changed']['settings'] = [
    'future_format' => '@interval hence',
    'past_format' => '@interval ago',
    'granularity' => 1,
  ];
  $view->save();
  // Add a translation to the views configuration for the past and future
  // formats.
  $this->drupalGet('admin/structure/views/view/content/translate/de/edit');
  $edit = [
    'translation[config_names][views.view.content][display][default][display_options][fields][changed][settings][future_format]' => '@interval später',
    'translation[config_names][views.view.content][display][default][display_options][fields][changed][settings][past_format]' => 'vor @interval',
  ];
  $this->submitForm($edit, 'Save translation');
  // Create a timestamp just over an hour in the past and set the nodes update
  // time to this.
  $past_timestamp = \Drupal::time()->getCurrentTime() - 3700;
  $node->setChangedTime($past_timestamp);
  $node->save();
  $this->drupalGet('/de/admin/content');
  // Not all normal string translations are available, so 'hour' is still in
  // English.
  $this->assertSession()
    ->pageTextContains('vor 1 hour');
  // Create a timestamp just over an hour in the future and set the nodes
  // update time to this.
  $past_timestamp = \Drupal::time()->getCurrentTime() + 3700;
  $node->setChangedTime($past_timestamp);
  $node->save();
  $this->drupalGet('/de/admin/content');
  // Not all normal string translations are available, so 'hour' is still in
  // English.
  $this->assertSession()
    ->pageTextContains('1 hour später');
}

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