UserTimeZoneTest.php in Drupal 9
File
core/modules/user/tests/src/Functional/UserTimeZoneTest.php
View source
<?php
namespace Drupal\Tests\user\Functional;
use Drupal\Core\Datetime\Entity\DateFormat;
use Drupal\Tests\BrowserTestBase;
class UserTimeZoneTest extends BrowserTestBase {
protected static $modules = [
'node',
'system_test',
];
protected $defaultTheme = 'stark';
public function testUserTimeZone() {
$this
->config('system.date')
->set('timezone.user.configurable', 1)
->set('timezone.default', 'America/Los_Angeles')
->save();
DateFormat::load('medium')
->setPattern('Y-m-d H:i I')
->save();
$web_user = $this
->drupalCreateUser();
$this
->drupalLogin($web_user);
$date1 = '2007-03-09 21:00:00 -0800';
$date2 = '2007-03-11 01:00:00 -0800';
$date3 = '2007-03-20 21:00:00 -0700';
$this
->drupalCreateContentType([
'type' => 'article',
]);
$node1 = $this
->drupalCreateNode([
'created' => strtotime($date1),
'type' => 'article',
]);
$node2 = $this
->drupalCreateNode([
'created' => strtotime($date2),
'type' => 'article',
]);
$node3 = $this
->drupalCreateNode([
'created' => strtotime($date3),
'type' => 'article',
]);
$this
->drupalGet('node/' . $node1
->id());
$this
->assertSession()
->pageTextContains('2007-03-09 21:00 0');
$this
->drupalGet('node/' . $node2
->id());
$this
->assertSession()
->pageTextContains('2007-03-11 01:00 0');
$this
->drupalGet('node/' . $node3
->id());
$this
->assertSession()
->pageTextContains('2007-03-20 21:00 1');
$edit = [];
$edit['mail'] = $web_user
->getEmail();
$edit['timezone'] = 'America/Santiago';
$this
->drupalGet("user/" . $web_user
->id() . "/edit");
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains('The changes have been saved.');
$this
->drupalGet('node/' . $node1
->id());
$this
->assertSession()
->pageTextContains('2007-03-10 02:00 1');
$this
->drupalGet('node/' . $node2
->id());
$this
->assertSession()
->pageTextContains('2007-03-11 05:00 0');
$this
->drupalGet('node/' . $node3
->id());
$this
->assertSession()
->pageTextContains('2007-03-21 00:00 0');
$this
->drupalLogout();
$this
->drupalGet('node/' . $node1
->id());
$this
->assertSession()
->pageTextContains('2007-03-09 21:00 0');
$this
->drupalGet('node/' . $node2
->id());
$this
->assertSession()
->pageTextContains('2007-03-11 01:00 0');
$this
->drupalGet('node/' . $node3
->id());
$this
->assertSession()
->pageTextContains('2007-03-20 21:00 1');
$this
->drupalGet('/system-test/date');
$this
->assertSession()
->pageTextContains('2016-01-13 08:29 0');
}
}
Classes
Name |
Description |
UserTimeZoneTest |
Set a user time zone and verify that dates are displayed in local time. |