annotate core/modules/user/src/Tests/UserTimeZoneTest.php @ 0:4c8ae668cc8c

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