Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / system / src / Tests / Common / FormatDateTest.php
1 <?php
2
3 namespace Drupal\system\Tests\Common;
4
5 use Drupal\language\Entity\ConfigurableLanguage;
6 use Drupal\simpletest\WebTestBase;
7
8 /**
9  * Tests the format_date() function.
10  *
11  * @group Common
12  */
13 class FormatDateTest extends WebTestBase {
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = ['language'];
21
22   /**
23    * Arbitrary langcode for a custom language.
24    */
25   const LANGCODE = 'xx';
26
27   protected function setUp() {
28     parent::setUp('language');
29
30     $this->config('system.date')
31       ->set('timezone.user.configurable', 1)
32       ->save();
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();
40
41     $this->settingsSet('locale_custom_strings_' . self::LANGCODE, [
42       '' => ['Sunday' => 'domingo'],
43       'Long month name' => ['March' => 'marzo'],
44     ]);
45
46     ConfigurableLanguage::createFromLangcode(static::LANGCODE)->save();
47     $this->resetAll();
48   }
49
50   /**
51    * Tests admin-defined formats in format_date().
52    */
53   public function testAdminDefinedFormatDate() {
54     // Create and log in an admin user.
55     $this->drupalLogin($this->drupalCreateUser(['administer site configuration']));
56
57     // Add new date format.
58     $edit = [
59       'id' => 'example_style',
60       'label' => 'Example Style',
61       'date_format_pattern' => 'j M y',
62     ];
63     $this->drupalPostForm('admin/config/regional/date-time/formats/add', $edit, t('Add format'));
64
65     // Add a second date format with a different case than the first.
66     $edit = [
67       'id' => 'example_style_uppercase',
68       'label' => 'Example Style Uppercase',
69       'date_format_pattern' => 'j M Y',
70     ];
71     $this->drupalPostForm('admin/config/regional/date-time/formats/add', $edit, t('Add format'));
72     $this->assertText(t('Custom date format added.'));
73
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.');
78   }
79
80 }