Issue #2510150 by tduong, Berdir: AccountProxy is not calling date_set_default_timezone() for anonymous users

8.1.x
Alex Pott 2016-01-19 14:57:43 +00:00
parent 6833754d64
commit af54540567
9 changed files with 106 additions and 9 deletions

View File

@ -83,8 +83,11 @@ class AuthenticationSubscriber implements EventSubscriberInterface {
$account = $this->authenticationProvider->authenticate($request); $account = $this->authenticationProvider->authenticate($request);
if ($account) { if ($account) {
$this->accountProxy->setAccount($account); $this->accountProxy->setAccount($account);
return;
} }
} }
// No account has been set explicitly, initialize the timezone here.
date_default_timezone_set(drupal_get_user_timezone());
} }
} }

View File

@ -93,9 +93,17 @@ class LoggerChannel implements LoggerChannelInterface {
$context['request_uri'] = $request->getUri(); $context['request_uri'] = $request->getUri();
$context['referer'] = $request->headers->get('Referer', ''); $context['referer'] = $request->headers->get('Referer', '');
$context['ip'] = $request->getClientIP(); $context['ip'] = $request->getClientIP();
if ($this->currentUser) { try {
$context['user'] = $this->currentUser; if ($this->currentUser) {
$context['uid'] = $this->currentUser->id(); $context['user'] = $this->currentUser;
$context['uid'] = $this->currentUser->id();
}
}
catch (\Exception $e) {
// An exception might be thrown if the database connection is not
// available or due to another unexpected reason. It is more important
// to log the error that we already have so any additional exceptions
// are ignored.
} }
} }

View File

@ -56,7 +56,7 @@ class AccountProxy implements AccountProxyInterface {
// After the container is rebuilt, DrupalKernel sets the initial // After the container is rebuilt, DrupalKernel sets the initial
// account to the id of the logged in user. This is necessary in order // account to the id of the logged in user. This is necessary in order
// to refresh the user account reference here. // to refresh the user account reference here.
$this->account = $this->loadUserEntity($this->initialAccountId); $this->setAccount($this->loadUserEntity($this->initialAccountId));
} }
else { else {
$this->account = new AnonymousUserSession(); $this->account = new AnonymousUserSession();

View File

@ -80,17 +80,17 @@ class NodeAttributesTest extends NodeTestBase {
'lang' => 'en', 'lang' => 'en',
); );
$this->assertTrue($graph->hasProperty($node_uri, 'http://purl.org/dc/terms/title', $expected_value), 'Node title found in RDF output (dc:title).'); $this->assertTrue($graph->hasProperty($node_uri, 'http://purl.org/dc/terms/title', $expected_value), 'Node title found in RDF output (dc:title).');
// Node date. // Node date (date format must be UTC).
$expected_value = array( $expected_value = array(
'type' => 'literal', 'type' => 'literal',
'value' => date('c', $node->getCreatedTime()), 'value' => \Drupal::service('date.formatter')->format($node->getCreatedTime(), 'custom', 'c', 'UTC'),
'datatype' => 'http://www.w3.org/2001/XMLSchema#dateTime', 'datatype' => 'http://www.w3.org/2001/XMLSchema#dateTime',
); );
$this->assertTrue($graph->hasProperty($node_uri, 'http://purl.org/dc/terms/date', $expected_value), 'Node date found in RDF output (dc:date).'); $this->assertTrue($graph->hasProperty($node_uri, 'http://purl.org/dc/terms/date', $expected_value), 'Node date found in RDF output (dc:date).');
// Node date. // Node date (date format must be UTC).
$expected_value = array( $expected_value = array(
'type' => 'literal', 'type' => 'literal',
'value' => date('c', $node->getCreatedTime()), 'value' => \Drupal::service('date.formatter')->format($node->getCreatedTime(), 'custom', 'c', 'UTC'),
'datatype' => 'http://www.w3.org/2001/XMLSchema#dateTime', 'datatype' => 'http://www.w3.org/2001/XMLSchema#dateTime',
); );
$this->assertTrue($graph->hasProperty($node_uri, 'http://purl.org/dc/terms/created', $expected_value), 'Node date found in RDF output (dc:created).'); $this->assertTrue($graph->hasProperty($node_uri, 'http://purl.org/dc/terms/created', $expected_value), 'Node date found in RDF output (dc:created).');

View File

@ -0,0 +1,42 @@
<?php
/**
* @file
* Contains \Drupal\simpletest\Tests\TimeZoneTest.
*/
namespace Drupal\simpletest\Tests;
use Drupal\simpletest\WebTestBase;
/**
* This test will check SimpleTest's default time zone handling.
*
* @group simpletest
*/
class TimeZoneTest extends WebTestBase {
/**
* A user with administrative privileges.
*/
protected $adminUser;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->adminUser = $this->drupalCreateUser(['administer site configuration']);
}
/**
* Tests that user accounts have the default time zone set.
*/
function testAccountTimeZones() {
$expected = 'Australia/Sydney';
$this->assertEqual($this->rootUser->getTimeZone(), $expected, 'Root user has correct time zone.');
$this->assertEqual($this->adminUser->getTimeZone(), $expected, 'Admin user has correct time zone.');
}
}

View File

@ -528,6 +528,14 @@ abstract class WebTestBase extends TestBase {
* being executed. * being executed.
*/ */
protected function setUp() { protected function setUp() {
// Set an explicit time zone to not rely on the system one, which may vary
// from setup to setup. The Australia/Sydney time zone is chosen so all
// tests are run using an edge case scenario (UTC+10 and DST). This choice
// is made to prevent time zone related regressions and reduce the
// fragility of the testing system in general. This is also set in config in
// \Drupal\simpletest\WebTestBase::initConfig().
date_default_timezone_set('Australia/Sydney');
// Preserve original batch for later restoration. // Preserve original batch for later restoration.
$this->setBatch(); $this->setBatch();
@ -837,6 +845,7 @@ abstract class WebTestBase extends TestBase {
'name' => 'admin', 'name' => 'admin',
'mail' => 'admin@example.com', 'mail' => 'admin@example.com',
'pass_raw' => $this->randomMachineName(), 'pass_raw' => $this->randomMachineName(),
'timezone' => date_default_timezone_get(),
)); ));
// The child site derives its session name from the database prefix when // The child site derives its session name from the database prefix when

View File

@ -341,4 +341,16 @@ class SystemTestController extends ControllerBase {
return $build; return $build;
} }
/**
* Returns the current date.
*
* @return \Symfony\Component\HttpFoundation\Response $response
* A Response object containing the current date.
*/
public function getCurrentDate() {
// Uses specific time to test that the right timezone is used.
$response = new Response(\Drupal::service('date.formatter')->format(1452702549));
return $response;
}
} }

View File

@ -150,3 +150,12 @@ system_test.respond_cacheable_response:
_controller: '\Drupal\system_test\Controller\SystemTestController::respondWithCacheableReponse' _controller: '\Drupal\system_test\Controller\SystemTestController::respondWithCacheableReponse'
requirements: requirements:
_access: 'TRUE' _access: 'TRUE'
system_test.date:
path: '/system-test/date'
defaults:
_controller: '\Drupal\system_test\Controller\SystemTestController::getCurrentDate'
options:
no_cache: 'TRUE'
requirements:
_access: 'TRUE'

View File

@ -21,7 +21,7 @@ class UserTimeZoneTest extends WebTestBase {
* *
* @var array * @var array
*/ */
public static $modules = array('node'); public static $modules = array('node', 'system_test');
/** /**
* Tests the display of dates and time when user-configurable time zones are set. * Tests the display of dates and time when user-configurable time zones are set.
@ -73,5 +73,19 @@ class UserTimeZoneTest extends WebTestBase {
$this->assertText('2007-03-11 05:00 CLT', 'Date should be Chile time; four hours ahead of PST'); $this->assertText('2007-03-11 05:00 CLT', 'Date should be Chile time; four hours ahead of PST');
$this->drupalGet('node/' . $node3->id()); $this->drupalGet('node/' . $node3->id());
$this->assertText('2007-03-21 00:00 CLT', 'Date should be Chile time; three hours ahead of PDT.'); $this->assertText('2007-03-21 00:00 CLT', 'Date should be Chile time; three hours ahead of PDT.');
// Ensure that anonymous users also use the default timezone.
$this->drupalLogout();
$this->drupalGet('node/' . $node1->id());
$this->assertText('2007-03-09 21:00 PST', 'Date should be PST.');
$this->drupalGet('node/' . $node2->id());
$this->assertText('2007-03-11 01:00 PST', 'Date should be PST.');
$this->drupalGet('node/' . $node3->id());
$this->assertText('2007-03-20 21:00 PDT', 'Date should be PDT.');
// Format a date without accessing the current user at all and
// ensure that it uses the default timezone.
$this->drupalGet('/system-test/date');
$this->assertText('2016-01-13 08:29 PST', 'Date should be PST.');
} }
} }