3 namespace Drupal\Tests\system\Functional\Datetime;
5 use Drupal\Core\Datetime\DrupalDateTime;
6 use Drupal\Tests\BrowserTestBase;
7 use Drupal\user\Entity\User;
10 * Tests DrupalDateTime functionality.
14 class DrupalDateTimeTest extends BrowserTestBase {
17 * Set up required modules.
19 public static $modules = [];
24 protected function setUp() {
30 * Test that the AJAX Timezone Callback can deal with various formats.
32 public function testSystemTimezone() {
35 'date' => 'Tue+Sep+17+2013+21%3A35%3A31+GMT%2B0100+(BST)#',
38 // Query the AJAX Timezone Callback with a long-format date.
39 $response = $this->drupalGet('system/timezone/BST/3600/1', $options);
40 $this->assertEqual($response, '"Europe\/London"', 'Timezone AJAX callback successfully identifies and responds to a long-format date.');
44 * Test that DrupalDateTime can detect the right timezone to use.
45 * Test with a variety of less commonly used timezone names to
46 * help ensure that the system timezone will be different than the
49 public function testDateTimezone() {
50 $date_string = '2007-01-31 21:00:00';
52 // Make sure no site timezone has been set.
53 $this->config('system.date')
54 ->set('timezone.user.configurable', 0)
55 ->set('timezone.default', NULL)
58 // Detect the system timezone.
59 $system_timezone = date_default_timezone_get();
61 // Create a date object with an unspecified timezone, which should
62 // end up using the system timezone.
63 $date = new DrupalDateTime($date_string);
64 $timezone = $date->getTimezone()->getName();
65 $this->assertTrue($timezone == $system_timezone, 'DrupalDateTime uses the system timezone when there is no site timezone.');
67 // Create a date object with a specified timezone.
68 $date = new DrupalDateTime($date_string, 'America/Yellowknife');
69 $timezone = $date->getTimezone()->getName();
70 $this->assertTrue($timezone == 'America/Yellowknife', 'DrupalDateTime uses the specified timezone if provided.');
72 // Set a site timezone.
73 $this->config('system.date')->set('timezone.default', 'Europe/Warsaw')->save();
75 // Create a date object with an unspecified timezone, which should
76 // end up using the site timezone.
77 $date = new DrupalDateTime($date_string);
78 $timezone = $date->getTimezone()->getName();
79 $this->assertTrue($timezone == 'Europe/Warsaw', 'DrupalDateTime uses the site timezone if provided.');
82 $this->config('system.date')->set('timezone.user.configurable', 1)->save();
83 $test_user = $this->drupalCreateUser([]);
84 $this->drupalLogin($test_user);
86 // Set up the user with a different timezone than the site.
87 $edit = ['mail' => $test_user->getEmail(), 'timezone' => 'Asia/Manila'];
88 $this->drupalPostForm('user/' . $test_user->id() . '/edit', $edit, t('Save'));
90 // Reload the user and reset the timezone in AccountProxy::setAccount().
91 \Drupal::entityManager()->getStorage('user')->resetCache();
92 $this->container->get('current_user')->setAccount(User::load($test_user->id()));
94 // Create a date object with an unspecified timezone, which should
95 // end up using the user timezone.
96 $date = new DrupalDateTime($date_string);
97 $timezone = $date->getTimezone()->getName();
98 $this->assertTrue($timezone == 'Asia/Manila', 'DrupalDateTime uses the user timezone, if configurable timezones are used and it is set.');
102 * Tests the ability to override the time zone in the format method.
104 public function testTimezoneFormat() {
105 // Create a date in UTC
106 $date = DrupalDateTime::createFromTimestamp(87654321, 'UTC');
108 // Verify that the date format method displays the default time zone.
109 $this->assertEqual($date->format('Y/m/d H:i:s e'), '1972/10/11 12:25:21 UTC', 'Date has default UTC time zone and correct date/time.');
111 // Verify that the format method can override the time zone.
112 $this->assertEqual($date->format('Y/m/d H:i:s e', ['timezone' => 'America/New_York']), '1972/10/11 08:25:21 America/New_York', 'Date displayed overidden time zone and correct date/time');
114 // Verify that the date format method still displays the default time zone
115 // for the date object.
116 $this->assertEqual($date->format('Y/m/d H:i:s e'), '1972/10/11 12:25:21 UTC', 'Date still has default UTC time zone and correct date/time');