You are here

public function ConfigTranslationUiTest::testDateFormatTranslation in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/config_translation/tests/src/Functional/ConfigTranslationUiTest.php \Drupal\Tests\config_translation\Functional\ConfigTranslationUiTest::testDateFormatTranslation()
  2. 9 core/modules/config_translation/tests/src/Functional/ConfigTranslationUiTest.php \Drupal\Tests\config_translation\Functional\ConfigTranslationUiTest::testDateFormatTranslation()

Tests date format translation.

File

core/modules/config_translation/tests/src/Functional/ConfigTranslationUiTest.php, line 452

Class

ConfigTranslationUiTest
Translate settings and entities to various languages.

Namespace

Drupal\Tests\config_translation\Functional

Code

public function testDateFormatTranslation() {
  $this
    ->drupalLogin($this->adminUser);
  $this
    ->drupalGet('admin/config/regional/date-time');

  // Check for medium format.
  $this
    ->assertSession()
    ->linkByHrefExists('admin/config/regional/date-time/formats/manage/medium');

  // Save default language configuration for a new format.
  $edit = [
    'label' => 'Custom medium date',
    'id' => 'custom_medium',
    'date_format_pattern' => 'Y. m. d. H:i',
  ];
  $this
    ->drupalGet('admin/config/regional/date-time/formats/add');
  $this
    ->submitForm($edit, 'Add format');

  // Test translating a default shipped format and our custom format.
  $formats = [
    'medium' => 'Default medium date',
    'custom_medium' => 'Custom medium date',
  ];
  foreach ($formats as $id => $label) {
    $translation_base_url = 'admin/config/regional/date-time/formats/manage/' . $id . '/translate';
    $this
      ->drupalGet($translation_base_url);

    // 'Add' link should be present for French translation.
    $translation_page_url = "{$translation_base_url}/fr/add";
    $this
      ->assertSession()
      ->linkByHrefExists($translation_page_url);

    // Make sure original text is present on this page.
    $this
      ->drupalGet($translation_page_url);
    $this
      ->assertSession()
      ->pageTextContains($label);

    // Make sure that the date library is added.
    $this
      ->assertSession()
      ->responseContains('core/modules/system/js/system.date.js');

    // Update translatable fields.
    $edit = [
      'translation[config_names][core.date_format.' . $id . '][label]' => $id . ' - FR',
      'translation[config_names][core.date_format.' . $id . '][pattern]' => 'D',
    ];

    // Save language specific version of form.
    $this
      ->drupalGet($translation_page_url);
    $this
      ->submitForm($edit, 'Save translation');

    // Get translation and check we've got the right value.
    $override = \Drupal::languageManager()
      ->getLanguageConfigOverride('fr', 'core.date_format.' . $id);
    $expected = [
      'label' => $id . ' - FR',
      'pattern' => 'D',
    ];
    $this
      ->assertEquals($expected, $override
      ->get());

    // Formatting the date 8 / 27 / 1985 @ 13:37 EST with pattern D should
    // display "Tue".
    $formatted_date = $this->container
      ->get('date.formatter')
      ->format(494015820, $id, NULL, 'America/New_York', 'fr');
    $this
      ->assertEquals('Tue', $formatted_date, 'Got the right formatted date using the date format translation pattern.');
  }
}