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