function DateFormatsLanguageTest::testLocalizeDateFormats

Functional tests for localizing date formats.

File

drupal/core/modules/system/lib/Drupal/system/Tests/System/DateFormatsLanguageTest.php, line 46
Definition of Drupal\system\Tests\System\DateFormatsLanguageTest.

Class

DateFormatsLanguageTest
Functional tests for localizing date formats.

Namespace

Drupal\system\Tests\System

Code

function testLocalizeDateFormats() {

  // Add language.
  $edit = array(
    'predefined_langcode' => 'fr',
  );
  $this
    ->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));

  // Set language negotiation.
  $language_type = LANGUAGE_TYPE_INTERFACE;
  $edit = array(
    "{$language_type}[enabled][language-url]" => TRUE,
  );
  $this
    ->drupalPost('admin/config/regional/language/detection', $edit, t('Save settings'));

  // Add new date format for French.
  $edit = array(
    'date_format_id' => 'example_style_fr',
    'date_format_name' => 'Example Style',
    'date_format_pattern' => 'd.m.Y - H:i',
    'date_langcode[]' => array(
      'fr',
    ),
  );
  $this
    ->drupalPost('admin/config/regional/date-time/formats/add', $edit, t('Add format'));

  // Add new date format for English.
  $edit = array(
    'date_format_id' => 'example_style_en',
    'date_format_name' => 'Example Style',
    'date_format_pattern' => 'j M Y - g:ia',
    'date_langcode[]' => array(
      'en',
    ),
  );
  $this
    ->drupalPost('admin/config/regional/date-time/formats/add', $edit, t('Add format'));

  // Configure date formats.
  $this
    ->drupalGet('admin/config/regional/date-time/locale');
  $this
    ->assertText('French', 'Configured languages appear.');
  $edit = array(
    'date_format_long' => 'example_style_fr',
    'date_format_medium' => 'example_style_fr',
    'date_format_short' => 'example_style_fr',
  );
  $this
    ->drupalPost('admin/config/regional/date-time/locale/fr/edit', $edit, t('Save configuration'));
  $this
    ->assertText(t('Configuration saved.'), 'French date formats updated.');
  $edit = array(
    'date_format_long' => 'example_style_en',
    'date_format_medium' => 'example_style_en',
    'date_format_short' => 'example_style_en',
  );
  $this
    ->drupalPost('admin/config/regional/date-time/locale/en/edit', $edit, t('Save configuration'));
  $this
    ->assertText(t('Configuration saved.'), 'English date formats updated.');

  // Create node content.
  $node = $this
    ->drupalCreateNode(array(
    'type' => 'article',
  ));

  // Configure format for the node posted date changes with the language.
  $this
    ->drupalGet('node/' . $node->nid);
  $english_date = format_date($node->created, 'custom', 'j M Y');
  $this
    ->assertText($english_date, 'English date format appears');
  $this
    ->drupalGet('fr/node/' . $node->nid);
  $french_date = format_date($node->created, 'custom', 'd.m.Y');
  $this
    ->assertText($french_date, 'French date format appears');

  // Make sure we can reset dates back to default.
  $this
    ->drupalPost('admin/config/regional/date-time/locale/en/reset', array(), t('Reset'));
  $this
    ->drupalGet('node/' . $node->nid);
  $this
    ->assertNoText($english_date, 'English date format does not appear');
}