Commit 7e41973f authored by alexpott's avatar alexpott
Browse files

Issue #2426031 by almaudoh, cpj: Remove deprecated uses of...

Issue #2426031 by almaudoh, cpj: Remove deprecated uses of SessionManager::isEnabled(), SessionManager::enable() and SessionManager::disable()
parent 31151599
......@@ -1097,7 +1097,7 @@ services:
arguments: ['@authentication']
account_switcher:
class: Drupal\Core\Session\AccountSwitcher
arguments: ['@current_user', '@session_manager']
arguments: ['@current_user', '@session_handler.write_safe']
current_user:
class: Drupal\Core\Session\AccountProxy
arguments: ['@authentication', '@request_stack']
......
......@@ -31,11 +31,11 @@ class AccountSwitcher implements AccountSwitcherInterface {
protected $currentUser = array();
/**
* The session manager.
* The write-safe session handler.
*
* @var \Drupal\Core\Session\SessionManagerInterface
* @var \Drupal\Core\Session\WriteSafeSessionHandlerInterface
*/
protected $sessionManager;
protected $writeSafeHandler;
/**
* The original state of session saving prior to account switching.
......@@ -49,12 +49,12 @@ class AccountSwitcher implements AccountSwitcherInterface {
*
* @param \Drupal\Core\Session\AccountProxyInterface $current_user
* The current user service.
* @param \Drupal\Core\Session\SessionManagerInterface $session_manager
* The session manager.
* @param \Drupal\Core\Session\WriteSafeSessionHandlerInterface $write_safe_handler
* The write-safe session handler.
*/
public function __construct(AccountProxyInterface $current_user, SessionManagerInterface $session_manager) {
public function __construct(AccountProxyInterface $current_user, WriteSafeSessionHandlerInterface $write_safe_handler) {
$this->currentUser = $current_user;
$this->sessionManager = $session_manager;
$this->writeSafeHandler = $write_safe_handler;
}
/**
......@@ -64,9 +64,9 @@ public function switchTo(AccountInterface $account) {
// Prevent session information from being saved and push previous account.
if (!isset($this->originalSessionSaving)) {
// Ensure that only the first session saving status is saved.
$this->originalSessionSaving = $this->sessionManager->isEnabled();
$this->originalSessionSaving = $this->writeSafeHandler->isSessionWritable();
}
$this->sessionManager->disable();
$this->writeSafeHandler->setSessionWritable(FALSE);
array_push($this->accountStack, $this->currentUser->getAccount());
$this->currentUser->setAccount($account);
return $this;
......@@ -87,7 +87,7 @@ public function switchBack() {
// reverted.
if (empty($this->accountStack)) {
if ($this->originalSessionSaving) {
$this->sessionManager->enable();
$this->writeSafeHandler->setSessionWritable(TRUE);
}
}
return $this;
......
......@@ -94,7 +94,7 @@ public function testDateTimezone() {
$this->drupalPostForm('user/' . $test_user->id() . '/edit', $edit, t('Save'));
// Disable session saving as we are about to modify the global $user.
\Drupal::service('session_manager')->disable();
\Drupal::service('session_handler.write_safe')->setSessionWritable(FALSE);
// Save the original user and then replace it with the test user.
$real_user = $user;
$user = user_load($test_user->id(), TRUE);
......@@ -113,7 +113,7 @@ public function testDateTimezone() {
$user = $real_user;
// Restore default time zone.
date_default_timezone_set(drupal_get_user_timezone());
\Drupal::service('session_manager')->enable();
\Drupal::service('session_handler.write_safe')->setSessionWritable(TRUE);
}
......
......@@ -18,11 +18,11 @@
class AccountSwitcherTest extends KernelTestBase {
public function testAccountSwitching() {
$session_manager = $this->container->get('session_manager');
$session_handler = $this->container->get('session_handler.write_safe');
$user = $this->container->get('current_user');
$switcher = $this->container->get('account_switcher');
$original_user = $user->getAccount();
$original_session_saving = $session_manager->isEnabled();
$original_session_saving = $session_handler->isSessionWritable();
// Switch to user with uid 2.
$switcher->switchTo(new UserSession(array('uid' => 2)));
......@@ -30,7 +30,7 @@ public function testAccountSwitching() {
// Verify that the active user has changed, and that session saving is
// disabled.
$this->assertEqual($user->id(), 2, 'Switched to user 2.');
$this->assertFalse($session_manager->isEnabled(), 'Session saving is disabled.');
$this->assertFalse($session_handler->isSessionWritable(), 'Session saving is disabled.');
// Perform a second (nested) user account switch.
$switcher->switchTo(new UserSession(array('uid' => 3)));
......@@ -43,7 +43,7 @@ public function testAccountSwitching() {
// Since we are still in the account from the first switch, session handling
// still needs to be disabled.
$this->assertEqual($user->id(), 2, 'Reverted back to user 2.');
$this->assertFalse($session_manager->isEnabled(), 'Session saving still disabled.');
$this->assertFalse($session_handler->isSessionWritable(), 'Session saving still disabled.');
// Revert to the original account which was active before the first switch.
$switcher->switchBack();
......@@ -51,7 +51,7 @@ public function testAccountSwitching() {
// Assert that the original account is active again, and that session saving
// has been re-enabled.
$this->assertEqual($user->id(), $original_user->id(), 'Original user correctly restored.');
$this->assertEqual($session_manager->isEnabled(), $original_session_saving, 'Original session saving correctly restored.');
$this->assertEqual($session_handler->isSessionWritable(), $original_session_saving, 'Original session saving correctly restored.');
// Verify that AccountSwitcherInterface::switchBack() will throw
// an exception if there are no accounts left in the stack.
......
......@@ -26,13 +26,16 @@ class SessionTest extends WebTestBase {
protected $dumpHeaders = TRUE;
/**
* Tests for \Drupal\Core\Session\SessionManager::isEnabled() and ::regenerate().
* Tests for \Drupal\Core\Session\WriteSafeSessionHandler::setSessionWritable()
* ::isSessionWritable and \Drupal\Core\Session\SessionManager::regenerate().
*/
function testSessionSaveRegenerate() {
$session_manager = $this->container->get('session_manager');
$this->assertTrue($session_manager->isEnabled(), 'SessionManager->isEnabled() initially returns TRUE.');
$this->assertFalse($session_manager->disable()->isEnabled(), 'SessionManager->isEnabled() returns FALSE after disabling.');
$this->assertTrue($session_manager->enable()->isEnabled(), 'SessionManager->isEnabled() returns TRUE after enabling.');
$session_handler = $this->container->get('session_handler.write_safe');
$this->assertTrue($session_handler->isSessionWritable(), 'session_handler->isSessionWritable() initially returns TRUE.');
$session_handler->setSessionWritable(FALSE);
$this->assertFalse($session_handler->isSessionWritable(), '$session_handler->isSessionWritable() returns FALSE after disabling.');
$session_handler->setSessionWritable(TRUE);
$this->assertTrue($session_handler->isSessionWritable(), '$session_handler->isSessionWritable() returns TRUE after enabling.');
// Test session hardening code from SA-2008-044.
$user = $this->drupalCreateUser();
......
......@@ -85,7 +85,7 @@ public function set($test_value) {
* A notification message.
*/
public function noSet($test_value) {
\Drupal::service('session_manager')->disable();
\Drupal::service('session_handler.write_safe')->setSessionWritable(FALSE);
$this->set($test_value);
return ['#markup' => $this->t('session saving was disabled, and then %val was set', array('%val' => $test_value))];
}
......@@ -111,7 +111,7 @@ public function setMessage() {
* A notification message.
*/
public function setMessageButDontSave() {
\Drupal::service('session_manager')->disable();
\Drupal::service('session_handler.write_safe')->setSessionWritable(FALSE);
$this->setMessage();
return ['#markup' => ''];
}
......
......@@ -27,19 +27,15 @@ public function test_plugin_argument_default_current_user() {
// Create a user to test.
$account = $this->drupalCreateUser();
// Switch the user, we have to check the global user too, because drupalLogin is only for the simpletest browser.
$this->drupalLogin($account);
$admin = \Drupal::currentUser();
$session_manager = \Drupal::service('session_manager')->disable();
\Drupal::currentUser()->setAccount($account);
// Switch the user.
\Drupal::service('account_switcher')->switchTo($account);
$view = Views::getView('test_plugin_argument_default_current_user');
$view->initHandlers();
$this->assertEqual($view->argument['null']->getDefaultArgument(), $account->id(), 'Uid of the current user is used.');
// Switch back.
\Drupal::currentUser()->setAccount($admin);
$session_manager->enable();
\Drupal::service('account_switcher')->switchBack();
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment