3 namespace Drupal\system\Tests\Common;
5 use Drupal\language\Entity\ConfigurableLanguage;
6 use Drupal\simpletest\WebTestBase;
9 * Tests the format_date() function.
13 class FormatDateTest extends WebTestBase {
20 public static $modules = ['language'];
23 * Arbitrary langcode for a custom language.
25 const LANGCODE = 'xx';
27 protected function setUp() {
28 parent::setUp('language');
30 $this->config('system.date')
31 ->set('timezone.user.configurable', 1)
33 $formats = $this->container->get('entity.manager')
34 ->getStorage('date_format')
35 ->loadMultiple(['long', 'medium', 'short']);
36 $formats['long']->setPattern('l, j. F Y - G:i')->save();
37 $formats['medium']->setPattern('j. F Y - G:i')->save();
38 $formats['short']->setPattern('Y M j - g:ia')->save();
39 $this->refreshVariables();
41 $this->settingsSet('locale_custom_strings_' . self::LANGCODE, [
42 '' => ['Sunday' => 'domingo'],
43 'Long month name' => ['March' => 'marzo'],
46 ConfigurableLanguage::createFromLangcode(static::LANGCODE)->save();
51 * Tests admin-defined formats in format_date().
53 public function testAdminDefinedFormatDate() {
54 // Create and log in an admin user.
55 $this->drupalLogin($this->drupalCreateUser(['administer site configuration']));
57 // Add new date format.
59 'id' => 'example_style',
60 'label' => 'Example Style',
61 'date_format_pattern' => 'j M y',
63 $this->drupalPostForm('admin/config/regional/date-time/formats/add', $edit, t('Add format'));
65 // Add a second date format with a different case than the first.
67 'id' => 'example_style_uppercase',
68 'label' => 'Example Style Uppercase',
69 'date_format_pattern' => 'j M Y',
71 $this->drupalPostForm('admin/config/regional/date-time/formats/add', $edit, t('Add format'));
72 $this->assertText(t('Custom date format added.'));
74 $timestamp = strtotime('2007-03-10T00:00:00+00:00');
75 $this->assertIdentical(format_date($timestamp, 'example_style', '', 'America/Los_Angeles'), '9 Mar 07');
76 $this->assertIdentical(format_date($timestamp, 'example_style_uppercase', '', 'America/Los_Angeles'), '9 Mar 2007');
77 $this->assertIdentical(format_date($timestamp, 'undefined_style'), format_date($timestamp, 'fallback'), 'Test format_date() defaulting to `fallback` when $type not found.');