You are here

class WriteSafeSessionHandlerTest in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/Session/WriteSafeSessionHandlerTest.php \Drupal\Tests\Core\Session\WriteSafeSessionHandlerTest

Tests \Drupal\Core\Session\WriteSafeSessionHandler.

@coversDefaultClass \Drupal\Core\Session\WriteSafeSessionHandler @group Session

Hierarchy

Expanded class hierarchy of WriteSafeSessionHandlerTest

File

core/tests/Drupal/Tests/Core/Session/WriteSafeSessionHandlerTest.php, line 14

Namespace

Drupal\Tests\Core\Session
View source
class WriteSafeSessionHandlerTest extends UnitTestCase {

  /**
   * The wrapped session handler.
   *
   * @var \SessionHandlerInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $wrappedSessionHandler;

  /**
   * The write safe session handler.
   *
   * @var \Drupal\Core\Session\WriteSafeSessionHandler
   */
  protected $sessionHandler;
  protected function setUp() {
    $this->wrappedSessionHandler = $this
      ->createMock('SessionHandlerInterface');
    $this->sessionHandler = new WriteSafeSessionHandler($this->wrappedSessionHandler);
  }

  /**
   * Tests creating a WriteSafeSessionHandler with default arguments.
   *
   * @covers ::__construct
   * @covers ::isSessionWritable
   * @covers ::write
   */
  public function testConstructWriteSafeSessionHandlerDefaultArgs() {
    $session_id = 'some-id';
    $session_data = 'serialized-session-data';
    $this
      ->assertSame(TRUE, $this->sessionHandler
      ->isSessionWritable());

    // Writing should be enabled, return value passed to the caller by default.
    $this->wrappedSessionHandler
      ->expects($this
      ->at(0))
      ->method('write')
      ->with($session_id, $session_data)
      ->will($this
      ->returnValue(TRUE));
    $this->wrappedSessionHandler
      ->expects($this
      ->at(1))
      ->method('write')
      ->with($session_id, $session_data)
      ->will($this
      ->returnValue(FALSE));
    $result = $this->sessionHandler
      ->write($session_id, $session_data);
    $this
      ->assertSame(TRUE, $result);
    $result = $this->sessionHandler
      ->write($session_id, $session_data);
    $this
      ->assertSame(FALSE, $result);
  }

  /**
   * Tests creating a WriteSafeSessionHandler with session writing disabled.
   *
   * @covers ::__construct
   * @covers ::isSessionWritable
   * @covers ::write
   */
  public function testConstructWriteSafeSessionHandlerDisableWriting() {
    $session_id = 'some-id';
    $session_data = 'serialized-session-data';

    // Disable writing upon construction.
    $this->sessionHandler = new WriteSafeSessionHandler($this->wrappedSessionHandler, FALSE);
    $this
      ->assertSame(FALSE, $this->sessionHandler
      ->isSessionWritable());
    $result = $this->sessionHandler
      ->write($session_id, $session_data);
    $this
      ->assertSame(TRUE, $result);
  }

  /**
   * Tests using setSessionWritable to enable/disable session writing.
   *
   * @covers ::setSessionWritable
   * @covers ::write
   */
  public function testSetSessionWritable() {
    $session_id = 'some-id';
    $session_data = 'serialized-session-data';
    $this
      ->assertSame(TRUE, $this->sessionHandler
      ->isSessionWritable());

    // Disable writing after construction.
    $this->sessionHandler
      ->setSessionWritable(FALSE);
    $this
      ->assertSame(FALSE, $this->sessionHandler
      ->isSessionWritable());
    $this->sessionHandler = new WriteSafeSessionHandler($this->wrappedSessionHandler, FALSE);
    $this
      ->assertSame(FALSE, $this->sessionHandler
      ->isSessionWritable());
    $result = $this->sessionHandler
      ->write($session_id, $session_data);
    $this
      ->assertSame(TRUE, $result);

    // Enable writing again.
    $this->sessionHandler
      ->setSessionWritable(TRUE);
    $this
      ->assertSame(TRUE, $this->sessionHandler
      ->isSessionWritable());

    // Writing should be enabled, return value passed to the caller by default.
    $this->wrappedSessionHandler
      ->expects($this
      ->at(0))
      ->method('write')
      ->with($session_id, $session_data)
      ->will($this
      ->returnValue(TRUE));
    $this->wrappedSessionHandler
      ->expects($this
      ->at(1))
      ->method('write')
      ->with($session_id, $session_data)
      ->will($this
      ->returnValue(FALSE));
    $result = $this->sessionHandler
      ->write($session_id, $session_data);
    $this
      ->assertSame(TRUE, $result);
    $result = $this->sessionHandler
      ->write($session_id, $session_data);
    $this
      ->assertSame(FALSE, $result);
  }

  /**
   * Tests that other invocations are passed unmodified to the wrapped handler.
   *
   * @covers ::setSessionWritable
   * @covers ::open
   * @covers ::read
   * @covers ::close
   * @covers ::destroy
   * @covers ::gc
   * @dataProvider providerTestOtherMethods
   */
  public function testOtherMethods($method, $expected_result, $args) {
    $invocation = $this->wrappedSessionHandler
      ->expects($this
      ->exactly(2))
      ->method($method)
      ->will($this
      ->returnValue($expected_result));

    // Set the parameter matcher.
    call_user_func_array([
      $invocation,
      'with',
    ], $args);

    // Test with writable session.
    $this
      ->assertSame(TRUE, $this->sessionHandler
      ->isSessionWritable());
    $actual_result = call_user_func_array([
      $this->sessionHandler,
      $method,
    ], $args);
    $this
      ->assertSame($expected_result, $actual_result);

    // Test with non-writable session.
    $this->sessionHandler
      ->setSessionWritable(FALSE);
    $this
      ->assertSame(FALSE, $this->sessionHandler
      ->isSessionWritable());
    $actual_result = call_user_func_array([
      $this->sessionHandler,
      $method,
    ], $args);
    $this
      ->assertSame($expected_result, $actual_result);
  }

  /**
   * Provides test data for the other methods test.
   *
   * @return array
   *   Test data.
   */
  public function providerTestOtherMethods() {
    return [
      [
        'open',
        TRUE,
        [
          '/some/path',
          'some-session-id',
        ],
      ],
      [
        'read',
        'some-session-data',
        [
          'a-session-id',
        ],
      ],
      [
        'close',
        TRUE,
        [],
      ],
      [
        'destroy',
        TRUE,
        [
          'old-session-id',
        ],
      ],
      [
        'gc',
        TRUE,
        [
          42,
        ],
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
WriteSafeSessionHandlerTest::$sessionHandler protected property The write safe session handler.
WriteSafeSessionHandlerTest::$wrappedSessionHandler protected property The wrapped session handler.
WriteSafeSessionHandlerTest::providerTestOtherMethods public function Provides test data for the other methods test.
WriteSafeSessionHandlerTest::setUp protected function Overrides UnitTestCase::setUp
WriteSafeSessionHandlerTest::testConstructWriteSafeSessionHandlerDefaultArgs public function Tests creating a WriteSafeSessionHandler with default arguments.
WriteSafeSessionHandlerTest::testConstructWriteSafeSessionHandlerDisableWriting public function Tests creating a WriteSafeSessionHandler with session writing disabled.
WriteSafeSessionHandlerTest::testOtherMethods public function Tests that other invocations are passed unmodified to the wrapped handler.
WriteSafeSessionHandlerTest::testSetSessionWritable public function Tests using setSessionWritable to enable/disable session writing.