Chris@14
|
1 <?php
|
Chris@14
|
2
|
Chris@14
|
3 namespace Drupal\Tests\user\Functional;
|
Chris@14
|
4
|
Chris@14
|
5 use Drupal\Core\Datetime\Entity\DateFormat;
|
Chris@14
|
6 use Drupal\Tests\BrowserTestBase;
|
Chris@14
|
7
|
Chris@14
|
8 /**
|
Chris@14
|
9 * Set a user time zone and verify that dates are displayed in local time.
|
Chris@14
|
10 *
|
Chris@14
|
11 * @group user
|
Chris@14
|
12 */
|
Chris@14
|
13 class UserTimeZoneTest extends BrowserTestBase {
|
Chris@14
|
14
|
Chris@14
|
15 /**
|
Chris@14
|
16 * Modules to enable.
|
Chris@14
|
17 *
|
Chris@14
|
18 * @var array
|
Chris@14
|
19 */
|
Chris@14
|
20 public static $modules = ['node', 'system_test'];
|
Chris@14
|
21
|
Chris@14
|
22 /**
|
Chris@14
|
23 * Tests the display of dates and time when user-configurable time zones are set.
|
Chris@14
|
24 */
|
Chris@14
|
25 public function testUserTimeZone() {
|
Chris@14
|
26 // Setup date/time settings for Los Angeles time.
|
Chris@14
|
27 $this->config('system.date')
|
Chris@14
|
28 ->set('timezone.user.configurable', 1)
|
Chris@14
|
29 ->set('timezone.default', 'America/Los_Angeles')
|
Chris@14
|
30 ->save();
|
Chris@14
|
31
|
Chris@14
|
32 // Load the 'medium' date format, which is the default for node creation
|
Chris@14
|
33 // time, and override it. Since we are testing time zones with Daylight
|
Chris@14
|
34 // Saving Time, and need to future proof against changes to the zoneinfo
|
Chris@14
|
35 // database, we choose the 'I' format placeholder instead of a
|
Chris@14
|
36 // human-readable zone name. With 'I', a 1 means the date is in DST, and 0
|
Chris@14
|
37 // if not.
|
Chris@14
|
38 DateFormat::load('medium')
|
Chris@14
|
39 ->setPattern('Y-m-d H:i I')
|
Chris@14
|
40 ->save();
|
Chris@14
|
41
|
Chris@14
|
42 // Create a user account and login.
|
Chris@14
|
43 $web_user = $this->drupalCreateUser();
|
Chris@14
|
44 $this->drupalLogin($web_user);
|
Chris@14
|
45
|
Chris@14
|
46 // Create some nodes with different authored-on dates.
|
Chris@14
|
47 // Two dates in PST (winter time):
|
Chris@14
|
48 $date1 = '2007-03-09 21:00:00 -0800';
|
Chris@14
|
49 $date2 = '2007-03-11 01:00:00 -0800';
|
Chris@14
|
50 // One date in PDT (summer time):
|
Chris@14
|
51 $date3 = '2007-03-20 21:00:00 -0700';
|
Chris@14
|
52 $this->drupalCreateContentType(['type' => 'article']);
|
Chris@14
|
53 $node1 = $this->drupalCreateNode(['created' => strtotime($date1), 'type' => 'article']);
|
Chris@14
|
54 $node2 = $this->drupalCreateNode(['created' => strtotime($date2), 'type' => 'article']);
|
Chris@14
|
55 $node3 = $this->drupalCreateNode(['created' => strtotime($date3), 'type' => 'article']);
|
Chris@14
|
56
|
Chris@14
|
57 // Confirm date format and time zone.
|
Chris@14
|
58 $this->drupalGet('node/' . $node1->id());
|
Chris@14
|
59 $this->assertText('2007-03-09 21:00 0', 'Date should be PST.');
|
Chris@14
|
60 $this->drupalGet('node/' . $node2->id());
|
Chris@14
|
61 $this->assertText('2007-03-11 01:00 0', 'Date should be PST.');
|
Chris@14
|
62 $this->drupalGet('node/' . $node3->id());
|
Chris@14
|
63 $this->assertText('2007-03-20 21:00 1', 'Date should be PDT.');
|
Chris@14
|
64
|
Chris@14
|
65 // Change user time zone to Santiago time.
|
Chris@14
|
66 $edit = [];
|
Chris@14
|
67 $edit['mail'] = $web_user->getEmail();
|
Chris@14
|
68 $edit['timezone'] = 'America/Santiago';
|
Chris@14
|
69 $this->drupalPostForm("user/" . $web_user->id() . "/edit", $edit, t('Save'));
|
Chris@14
|
70 $this->assertText(t('The changes have been saved.'), 'Time zone changed to Santiago time.');
|
Chris@14
|
71
|
Chris@14
|
72 // Confirm date format and time zone.
|
Chris@14
|
73 $this->drupalGet('node/' . $node1->id());
|
Chris@14
|
74 $this->assertText('2007-03-10 02:00 1', 'Date should be Chile summer time; five hours ahead of PST.');
|
Chris@14
|
75 $this->drupalGet('node/' . $node2->id());
|
Chris@14
|
76 $this->assertText('2007-03-11 05:00 0', 'Date should be Chile time; four hours ahead of PST');
|
Chris@14
|
77 $this->drupalGet('node/' . $node3->id());
|
Chris@14
|
78 $this->assertText('2007-03-21 00:00 0', 'Date should be Chile time; three hours ahead of PDT.');
|
Chris@14
|
79
|
Chris@14
|
80 // Ensure that anonymous users also use the default timezone.
|
Chris@14
|
81 $this->drupalLogout();
|
Chris@14
|
82 $this->drupalGet('node/' . $node1->id());
|
Chris@14
|
83 $this->assertText('2007-03-09 21:00 0', 'Date should be PST.');
|
Chris@14
|
84 $this->drupalGet('node/' . $node2->id());
|
Chris@14
|
85 $this->assertText('2007-03-11 01:00 0', 'Date should be PST.');
|
Chris@14
|
86 $this->drupalGet('node/' . $node3->id());
|
Chris@14
|
87 $this->assertText('2007-03-20 21:00 1', 'Date should be PDT.');
|
Chris@14
|
88
|
Chris@14
|
89 // Format a date without accessing the current user at all and
|
Chris@14
|
90 // ensure that it uses the default timezone.
|
Chris@14
|
91 $this->drupalGet('/system-test/date');
|
Chris@14
|
92 $this->assertText('2016-01-13 08:29 0', 'Date should be PST.');
|
Chris@14
|
93 }
|
Chris@14
|
94
|
Chris@14
|
95 }
|