Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / system / tests / src / Functional / Datetime / DrupalDateTimeTest.php
1 <?php
2
3 namespace Drupal\Tests\system\Functional\Datetime;
4
5 use Drupal\Core\Datetime\DrupalDateTime;
6 use Drupal\Tests\BrowserTestBase;
7 use Drupal\user\Entity\User;
8
9 /**
10  * Tests DrupalDateTime functionality.
11  *
12  * @group Datetime
13  */
14 class DrupalDateTimeTest extends BrowserTestBase {
15
16   /**
17    * Set up required modules.
18    */
19   public static $modules = [];
20
21   /**
22    * Test setup.
23    */
24   protected function setUp() {
25     parent::setUp();
26
27   }
28
29   /**
30    * Test that the AJAX Timezone Callback can deal with various formats.
31    */
32   public function testSystemTimezone() {
33     $options = [
34       'query' => [
35         'date' => 'Tue+Sep+17+2013+21%3A35%3A31+GMT%2B0100+(BST)#',
36       ],
37     ];
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.');
41   }
42
43   /**
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
47    * stated timezones.
48    */
49   public function testDateTimezone() {
50     $date_string = '2007-01-31 21:00:00';
51
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)
56       ->save();
57
58     // Detect the system timezone.
59     $system_timezone = date_default_timezone_get();
60
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.');
66
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.');
71
72     // Set a site timezone.
73     $this->config('system.date')->set('timezone.default', 'Europe/Warsaw')->save();
74
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.');
80
81     // Create user.
82     $this->config('system.date')->set('timezone.user.configurable', 1)->save();
83     $test_user = $this->drupalCreateUser([]);
84     $this->drupalLogin($test_user);
85
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'));
89
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()));
93
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.');
99   }
100
101   /**
102    * Tests the ability to override the time zone in the format method.
103    */
104   public function testTimezoneFormat() {
105     // Create a date in UTC
106     $date = DrupalDateTime::createFromTimestamp(87654321, 'UTC');
107
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.');
110
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');
113
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');
117   }
118
119 }