diff --git a/core/lib/Drupal/Core/Session/AccountProxy.php b/core/lib/Drupal/Core/Session/AccountProxy.php index a50a289..ba4d880 100644 --- a/core/lib/Drupal/Core/Session/AccountProxy.php +++ b/core/lib/Drupal/Core/Session/AccountProxy.php @@ -23,9 +23,19 @@ class AccountProxy implements AccountProxyInterface { protected $account; /** + * Account id. + * + * @var int + */ + protected $id = 0; + + /** * Initial account id. * * @var int + * + * @deprecated in Drupal 8.1.9 and to be removed in Drupal 9. + * Use $this->id instead. */ protected $initialAccountId; @@ -39,6 +49,7 @@ public function setAccount(AccountInterface $account) { $account = $account->getAccount(); } $this->account = $account; + $this->id = $account->id(); date_default_timezone_set(drupal_get_user_timezone()); } @@ -47,11 +58,11 @@ public function setAccount(AccountInterface $account) { */ public function getAccount() { if (!isset($this->account)) { - if ($this->initialAccountId) { + if ($this->id) { // After the container is rebuilt, DrupalKernel sets the initial // account to the id of the logged in user. This is necessary in order // to refresh the user account reference here. - $this->setAccount($this->loadUserEntity($this->initialAccountId)); + $this->setAccount($this->loadUserEntity($this->id)); } else { $this->account = new AnonymousUserSession(); @@ -65,7 +76,7 @@ public function getAccount() { * {@inheritdoc} */ public function id() { - return $this->getAccount()->id(); + return $this->id; } /** @@ -160,7 +171,7 @@ public function setInitialAccountId($account_id) { throw new \LogicException('AccountProxyInterface::setInitialAccountId() cannot be called after an account was set on the AccountProxy'); } - $this->initialAccountId = $account_id; + $this->id = $this->initialAccountId = $account_id; } /** diff --git a/core/tests/Drupal/Tests/Core/Session/AccountProxyTest.php b/core/tests/Drupal/Tests/Core/Session/AccountProxyTest.php new file mode 100644 index 0000000..b087810 --- /dev/null +++ b/core/tests/Drupal/Tests/Core/Session/AccountProxyTest.php @@ -0,0 +1,53 @@ +assertSame(0, $account_proxy->id()); + $account_proxy->setInitialAccountId(1); + $this->assertFalse(\Drupal::hasContainer()); + // If the following call loaded the user entity it would call + // AccountProxy::loadUserEntity() which would fail because the container + // does not exist. + $this->assertSame(1, $account_proxy->id()); + $current_user = $this->prophesize(AccountInterface::class); + $current_user->id()->willReturn(2); + $account_proxy->setAccount($current_user->reveal()); + $this->assertSame(2, $account_proxy->id()); + } + + /** + * @covers ::setInitialAccountId + */ + public function testSetInitialAccountIdException() { + $this->setExpectedException(\LogicException::class); + $account_proxy = new AccountProxy(); + $current_user = $this->prophesize(AccountInterface::class); + $account_proxy->setAccount($current_user->reveal()); + $account_proxy->setInitialAccountId(1); + } + +} + +namespace Drupal\Core\Session ; + +if (!function_exists('drupal_get_user_timezone')) { + function drupal_get_user_timezone() { + return date_default_timezone_get(); + } +}