Verified Commit cd3e02cb authored by Dave Long's avatar Dave Long
Browse files

refactor: #3577376 Remove recently introduced deprecations from SessionManager

By: znerol
parent 1cb14a53
Loading
Loading
Loading
Loading
Loading
+0 −24
Original line number Diff line number Diff line
@@ -9613,42 +9613,18 @@
	'count' => 1,
	'path' => __DIR__ . '/lib/Drupal/Core/Session/MetadataBag.php',
];
$ignoreErrors[] = [
	'message' => '#^Method Drupal\\\\Core\\\\Session\\\\SessionManager\\:\\:delete\\(\\) has no return type specified\\.$#',
	'identifier' => 'missingType.return',
	'count' => 1,
	'path' => __DIR__ . '/lib/Drupal/Core/Session/SessionManager.php',
];
$ignoreErrors[] = [
	'message' => '#^Method Drupal\\\\Core\\\\Session\\\\SessionManager\\:\\:destroy\\(\\) has no return type specified\\.$#',
	'identifier' => 'missingType.return',
	'count' => 1,
	'path' => __DIR__ . '/lib/Drupal/Core/Session/SessionManager.php',
];
$ignoreErrors[] = [
	'message' => '#^Method Drupal\\\\Core\\\\Session\\\\SessionManager\\:\\:setWriteSafeHandler\\(\\) has no return type specified\\.$#',
	'identifier' => 'missingType.return',
	'count' => 1,
	'path' => __DIR__ . '/lib/Drupal/Core/Session/SessionManager.php',
];
$ignoreErrors[] = [
	'message' => '#^Method Drupal\\\\Core\\\\Session\\\\SessionManagerInterface\\:\\:delete\\(\\) has no return type specified\\.$#',
	'identifier' => 'missingType.return',
	'count' => 1,
	'path' => __DIR__ . '/lib/Drupal/Core/Session/SessionManagerInterface.php',
];
$ignoreErrors[] = [
	'message' => '#^Method Drupal\\\\Core\\\\Session\\\\SessionManagerInterface\\:\\:destroy\\(\\) has no return type specified\\.$#',
	'identifier' => 'missingType.return',
	'count' => 1,
	'path' => __DIR__ . '/lib/Drupal/Core/Session/SessionManagerInterface.php',
];
$ignoreErrors[] = [
	'message' => '#^Method Drupal\\\\Core\\\\Session\\\\SessionManagerInterface\\:\\:setWriteSafeHandler\\(\\) has no return type specified\\.$#',
	'identifier' => 'missingType.return',
	'count' => 1,
	'path' => __DIR__ . '/lib/Drupal/Core/Session/SessionManagerInterface.php',
];
$ignoreErrors[] = [
	'message' => '#^Method Drupal\\\\Core\\\\Session\\\\WriteSafeSessionHandler\\:\\:setSessionWritable\\(\\) has no return type specified\\.$#',
	'identifier' => 'missingType.return',
+1 −36
Original line number Diff line number Diff line
@@ -3,7 +3,6 @@
namespace Drupal\Core\Session;

use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
@@ -36,11 +35,6 @@ class SessionManager extends NativeSessionStorage implements SessionManagerInter
   */
  protected $startedLazy;

  /**
   * The user session repository.
   */
  protected UserSessionRepositoryInterface $sessionRepository;

  /**
   * Constructs a new session manager instance.
   *
@@ -54,33 +48,17 @@ class SessionManager extends NativeSessionStorage implements SessionManagerInter
   *   The session configuration interface.
   * @param \Drupal\Component\Datetime\TimeInterface $time
   *   The time service.
   * @param \Drupal\Core\Session\UserSessionRepositoryInterface $session_repository
   *   The user session repository.
   *
   * @see \Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage::setSaveHandler()
   */
  public function __construct(
    protected RequestStack $requestStack,
    Connection|AbstractProxy|\SessionHandlerInterface|null $handler,
    AbstractProxy|\SessionHandlerInterface|null $handler,
    MetadataBag $metadata_bag,
    protected SessionConfigurationInterface $sessionConfiguration,
    protected TimeInterface $time,
    $session_repository = NULL,
  ) {
    // The second parameter ($handler) used to be the database connection. And
    // the last parameter ($session_repository) used to be $handler. Rearrange
    // the parameters if constructor was called like this.
    if ($handler instanceof Connection && !$session_repository instanceof UserSessionRepositoryInterface) {
      @trigger_error('Calling ' . __METHOD__ . '() with a database $connection as the second argument is deprecated in drupal:11.4.0 and it will throw an error in drupal:12.0.0. See https://www.drupal.org/node/3570851', E_USER_DEPRECATED);
      $handler = $session_repository;
    }
    parent::__construct([], $handler, $metadata_bag);
    if ($session_repository instanceof UserSessionRepositoryInterface) {
      $this->sessionRepository = $session_repository;
    }
    else {
      $this->sessionRepository = \Drupal::service(UserSessionRepositoryInterface::class);
    }
  }

  /**
@@ -200,13 +178,6 @@ public function regenerate($destroy = FALSE, $lifetime = NULL): bool {
    return parent::regenerate($destroy, $lifetime);
  }

  /**
   * {@inheritdoc}
   */
  public function delete($uid) {
    $this->sessionRepository->deleteAll($uid);
  }

  /**
   * {@inheritdoc}
   */
@@ -234,12 +205,6 @@ public function destroy() {
    }
  }

  /**
   * {@inheritdoc}
   */
  public function setWriteSafeHandler(WriteSafeSessionHandlerInterface $handler) {
  }

  /**
   * Returns whether the current PHP process runs on CLI.
   *
+0 −26
Original line number Diff line number Diff line
@@ -9,35 +9,9 @@
 */
interface SessionManagerInterface extends SessionStorageInterface {

  /**
   * Ends a specific user's session(s).
   *
   * @param int $uid
   *   User ID.
   *
   * @deprecated in drupal:11.4.0 and is removed from drupal:12.0.0. Use
   * Drupal\user\UserSessionRepositoryInterface::deleteAll() instead.
   * @see https://www.drupal.org/node/3570851
   */
  public function delete($uid);

  /**
   * Destroys the current session and removes session cookies.
   */
  public function destroy();

  /**
   * Sets the write safe session handler.
   *
   * @todo This should be removed once all database queries are removed from
   *   the session manager class.
   *
   * @var \Drupal\Core\Session\WriteSafeSessionHandlerInterface
   *
   * @deprecated in drupal:11.4.0 and is removed from drupal:12.0.0. There is no
   * replacement.
   * @see https://www.drupal.org/node/3570851
   */
  public function setWriteSafeHandler(WriteSafeSessionHandlerInterface $handler);

}
+0 −57
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\Tests\Core\Session;

use Drupal\Component\Datetime\Time;
use Drupal\Core\Database\Connection;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Session\MetadataBag;
use Drupal\Core\Session\SessionConfiguration;
use Drupal\Core\Session\SessionManager;
use Drupal\Core\Session\UserSessionRepositoryInterface;
use Drupal\Core\Site\Settings;
use Drupal\Tests\UnitTestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\NullSessionHandler;
use Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy;

/**
 * Tests Drupal\Core\Session\SessionManager.
 */
#[CoversClass(SessionManager::class)]
#[Group('Session')]
class SessionManagerTest extends UnitTestCase {

  /**
   * Tests that session manager is constructed with deprecated argument list.
   */
  #[IgnoreDeprecations]
  public function testDeprecatedMiddlewaresArgument(): void {
    $connection = $this->createStub(Connection::class);
    $handler = new NullSessionHandler();
    $sessionRepository = $this->createStub(UserSessionRepositoryInterface::class);

    $container = new ContainerBuilder();
    $container->set(UserSessionRepositoryInterface::class, $sessionRepository);
    \Drupal::setContainer($container);

    $this->expectUserDeprecationMessage('Calling Drupal\Core\Session\SessionManager::__construct() with a database $connection as the second argument is deprecated in drupal:11.4.0 and it will throw an error in drupal:12.0.0. See https://www.drupal.org/node/3570851');
    $sessionManager = new SessionManager(
      new RequestStack(),
      $connection,
      new MetadataBag(new Settings([])),
      new SessionConfiguration(),
      new Time(),
      $handler,
    );
    $abstractProxy = $sessionManager->getSaveHandler();
    assert($abstractProxy instanceof SessionHandlerProxy);
    $this->assertSame($handler, $abstractProxy->getHandler());
  }

}