class SessionConfigurationTest

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Session/SessionConfigurationTest.php \Drupal\Tests\Core\Session\SessionConfigurationTest
  2. 8.9.x core/tests/Drupal/Tests/Core/Session/SessionConfigurationTest.php \Drupal\Tests\Core\Session\SessionConfigurationTest
  3. 10 core/tests/Drupal/Tests/Core/Session/SessionConfigurationTest.php \Drupal\Tests\Core\Session\SessionConfigurationTest

@coversDefaultClass \Drupal\Core\Session\SessionConfiguration @group Session

Hierarchy

Expanded class hierarchy of SessionConfigurationTest

File

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

Namespace

Drupal\Tests\Core\Session
View source
class SessionConfigurationTest extends UnitTestCase {
    
    /**
     * Constructs a partially mocked SUT.
     *
     * @return \Drupal\Core\Session\SessionConfiguration|\PHPUnit\Framework\MockObject\MockObject
     */
    protected function createSessionConfiguration($options = []) {
        return $this->getMockBuilder('Drupal\\Core\\Session\\SessionConfiguration')
            ->onlyMethods([
            'drupalValidTestUa',
        ])
            ->setConstructorArgs([
            $options,
        ])
            ->getMock();
    }
    
    /**
     * Tests whether the session.cookie_domain ini settings is computed correctly.
     *
     * @covers ::getOptions
     *
     * @dataProvider providerTestGeneratedCookieDomain
     */
    public function testGeneratedCookieDomain($uri, $expected_domain) : void {
        $config = $this->createSessionConfiguration();
        $request = Request::create($uri);
        $options = $config->getOptions($request);
        $this->assertEquals($expected_domain, $options['cookie_domain']);
    }
    
    /**
     * Data provider for the cookie domain test.
     *
     * @return array
     *   Test data
     */
    public static function providerTestGeneratedCookieDomain() {
        return [
            [
                'http://example.com/path/index.php',
                '.example.com',
            ],
            [
                'http://www.example.com/path/index.php',
                '.www.example.com',
            ],
            [
                'http://subdomain.example.com/path/index.php',
                '.subdomain.example.com',
            ],
            [
                'http://example.com:8080/path/index.php',
                '.example.com',
            ],
            [
                'https://example.com/path/index.php',
                '.example.com',
            ],
            [
                'http://localhost/path/index.php',
                '',
            ],
            [
                'http://127.0.0.1/path/index.php',
                '',
            ],
            [
                'http://127.0.0.1:8888/path/index.php',
                '',
            ],
            [
                'http://1.1.1.1/path/index.php',
                '',
            ],
            [
                'http://[::1]/path/index.php',
                '',
            ],
            [
                'http://[::1]:8888/path/index.php',
                '',
            ],
        ];
    }
    
    /**
     * Tests the constructor injected session.cookie_domain ini setting.
     *
     * @covers ::__construct
     * @covers ::getOptions
     *
     * @dataProvider providerTestEnforcedCookieDomain
     */
    public function testEnforcedCookieDomain($uri, $expected_domain) : void {
        $config = $this->createSessionConfiguration([
            'cookie_domain' => '.example.com',
        ]);
        $request = Request::create($uri);
        $options = $config->getOptions($request);
        $this->assertEquals($expected_domain, $options['cookie_domain']);
    }
    
    /**
     * Data provider for the cookie domain test.
     *
     * @return array
     *   Test data
     */
    public static function providerTestEnforcedCookieDomain() {
        return [
            [
                'http://example.com/path/index.php',
                '.example.com',
            ],
            [
                'http://www.example.com/path/index.php',
                '.example.com',
            ],
            [
                'http://subdomain.example.com/path/index.php',
                '.example.com',
            ],
            [
                'http://example.com:8080/path/index.php',
                '.example.com',
            ],
            [
                'https://example.com/path/index.php',
                '.example.com',
            ],
            [
                'http://localhost/path/index.php',
                '.example.com',
            ],
            [
                'http://127.0.0.1/path/index.php',
                '.example.com',
            ],
            [
                'http://127.0.0.1:8888/path/index.php',
                '.example.com',
            ],
            [
                'http://1.1.1.1/path/index.php',
                '.example.com',
            ],
            [
                'http://[::1]/path/index.php',
                '.example.com',
            ],
            [
                'http://[::1]:8888/path/index.php',
                '.example.com',
            ],
        ];
    }
    
    /**
     * Tests whether the session.cookie_secure ini settings is computed correctly.
     *
     * @covers ::getOptions
     *
     * @dataProvider providerTestCookieSecure
     */
    public function testCookieSecure($uri, $expected_secure) : void {
        $config = $this->createSessionConfiguration();
        $request = Request::create($uri);
        $options = $config->getOptions($request);
        $this->assertEquals($expected_secure, $options['cookie_secure']);
    }
    
    /**
     * Test that session.cookie_samesite is configured correctly.
     */
    public function testSameSiteCookie() : void {
        $request = Request::create('https://example.com');
        $config = $this->createSessionConfiguration([
            'cookie_samesite' => 'Strict',
        ]);
        $options = $config->getOptions($request);
        $this->assertEquals('Strict', $options['cookie_samesite']);
    }
    
    /**
     * Tests that session.cookie_secure ini settings cannot be overridden.
     *
     * @covers ::__construct
     * @covers ::getOptions
     *
     * @dataProvider providerTestCookieSecure
     */
    public function testCookieSecureNotOverridable($uri, $expected_secure) : void {
        $config = $this->createSessionConfiguration([
            'cookie_secure' => FALSE,
        ]);
        $request = Request::create($uri);
        $options = $config->getOptions($request);
        $this->assertEquals($expected_secure, $options['cookie_secure']);
    }
    
    /**
     * Data provider for the cookie secure test.
     *
     * @return array
     *   Test data
     */
    public static function providerTestCookieSecure() {
        return [
            [
                'http://example.com/path/index.php',
                FALSE,
            ],
            [
                'https://www.example.com/path/index.php',
                TRUE,
            ],
            [
                'http://127.0.0.1/path/index.php',
                FALSE,
            ],
            [
                'https://127.0.0.1:8888/path/index.php',
                TRUE,
            ],
            [
                'http://[::1]/path/index.php',
                FALSE,
            ],
            [
                'https://[::1]:8888/path/index.php',
                TRUE,
            ],
        ];
    }
    
    /**
     * Tests whether the session.name ini settings is computed correctly.
     *
     * @covers ::getOptions
     *
     * @dataProvider providerTestGeneratedSessionName
     */
    public function testGeneratedSessionName($uri, $expected_name) : void {
        $config = $this->createSessionConfiguration();
        $request = Request::create($uri);
        $options = $config->getOptions($request);
        $this->assertEquals($expected_name, $options['name']);
    }
    
    /**
     * Data provider for the cookie name test.
     *
     * @return array
     *   Test data
     */
    public static function providerTestGeneratedSessionName() {
        $data = [
            [
                'http://example.com/path/index.php',
                'SESS',
                'example.com',
            ],
            [
                'http://www.example.com/path/index.php',
                'SESS',
                'www.example.com',
            ],
            [
                'http://subdomain.example.com/path/index.php',
                'SESS',
                'subdomain.example.com',
            ],
            [
                'http://example.com:8080/path/index.php',
                'SESS',
                'example.com',
            ],
            [
                'https://example.com/path/index.php',
                'SSESS',
                'example.com',
            ],
            [
                'http://example.com/path/core/install.php',
                'SESS',
                'example.com',
            ],
            [
                'http://localhost/path/index.php',
                'SESS',
                'localhost',
            ],
            [
                'http://127.0.0.1/path/index.php',
                'SESS',
                '127.0.0.1',
            ],
            [
                'http://127.0.0.1:8888/path/index.php',
                'SESS',
                '127.0.0.1',
            ],
            [
                'https://127.0.0.1/path/index.php',
                'SSESS',
                '127.0.0.1',
            ],
            [
                'https://127.0.0.1:8443/path/index.php',
                'SSESS',
                '127.0.0.1',
            ],
            [
                'http://1.1.1.1/path/index.php',
                'SESS',
                '1.1.1.1',
            ],
            [
                'https://1.1.1.1/path/index.php',
                'SSESS',
                '1.1.1.1',
            ],
            [
                'http://[::1]/path/index.php',
                'SESS',
                '[::1]',
            ],
            [
                'http://[::1]:8888/path/index.php',
                'SESS',
                '[::1]',
            ],
            [
                'https://[::1]/path/index.php',
                'SSESS',
                '[::1]',
            ],
            [
                'https://[::1]:8443/path/index.php',
                'SSESS',
                '[::1]',
            ],
        ];
        return array_map(function ($record) {
            return [
                $record[0],
                $record[1] . substr(hash('sha256', $record[2]), 0, 32),
            ];
        }, $data);
    }
    
    /**
     * Tests whether the session.name ini settings is computed correctly.
     *
     * @covers ::getOptions
     *
     * @dataProvider providerTestEnforcedSessionName
     */
    public function testEnforcedSessionNameViaCookieDomain($uri, $expected_name) : void {
        $config = $this->createSessionConfiguration([
            'cookie_domain' => '.example.com',
        ]);
        $request = Request::create($uri);
        $options = $config->getOptions($request);
        $this->assertEquals($expected_name, $options['name']);
    }
    
    /**
     * Data provider for the cookie name test.
     *
     * @return array
     *   Test data
     */
    public static function providerTestEnforcedSessionName() {
        $data = [
            [
                'http://example.com/path/index.php',
                'SESS',
                '.example.com',
            ],
            [
                'http://www.example.com/path/index.php',
                'SESS',
                '.example.com',
            ],
            [
                'http://subdomain.example.com/path/index.php',
                'SESS',
                '.example.com',
            ],
            [
                'http://example.com:8080/path/index.php',
                'SESS',
                '.example.com',
            ],
            [
                'https://example.com/path/index.php',
                'SSESS',
                '.example.com',
            ],
            [
                'http://example.com/path/core/install.php',
                'SESS',
                '.example.com',
            ],
            [
                'http://localhost/path/index.php',
                'SESS',
                '.example.com',
            ],
            [
                'http://127.0.0.1/path/index.php',
                'SESS',
                '.example.com',
            ],
            [
                'http://127.0.0.1:8888/path/index.php',
                'SESS',
                '.example.com',
            ],
            [
                'https://127.0.0.1/path/index.php',
                'SSESS',
                '.example.com',
            ],
            [
                'https://127.0.0.1:8443/path/index.php',
                'SSESS',
                '.example.com',
            ],
            [
                'http://1.1.1.1/path/index.php',
                'SESS',
                '.example.com',
            ],
            [
                'https://1.1.1.1/path/index.php',
                'SSESS',
                '.example.com',
            ],
            [
                'http://[::1]/path/index.php',
                'SESS',
                '.example.com',
            ],
            [
                'http://[::1]:8888/path/index.php',
                'SESS',
                '.example.com',
            ],
            [
                'https://[::1]/path/index.php',
                'SSESS',
                '.example.com',
            ],
            [
                'https://[::1]:8443/path/index.php',
                'SSESS',
                '.example.com',
            ],
        ];
        return array_map(function ($record) {
            return [
                $record[0],
                $record[1] . substr(hash('sha256', $record[2]), 0, 32),
            ];
        }, $data);
    }
    
    /**
     * Tests constructor's default settings.
     *
     * @covers ::__construct
     */
    public function testConstructorDefaultSettings() : void {
        $config = $this->createSessionConfiguration([]);
        $options = $config->getOptions(Request::createFromGlobals());
        $this->assertSame('', $options['name_suffix']);
        $config = $this->createSessionConfiguration([
            'name_suffix' => 'some-suffix',
        ]);
        $options = $config->getOptions(Request::createFromGlobals());
        $this->assertSame('some-suffix', $options['name_suffix']);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::getCallableName private static function Returns a callable as a string suitable for inclusion in a message.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
SessionConfigurationTest::createSessionConfiguration protected function Constructs a partially mocked SUT.
SessionConfigurationTest::providerTestCookieSecure public static function Data provider for the cookie secure test.
SessionConfigurationTest::providerTestEnforcedCookieDomain public static function Data provider for the cookie domain test.
SessionConfigurationTest::providerTestEnforcedSessionName public static function Data provider for the cookie name test.
SessionConfigurationTest::providerTestGeneratedCookieDomain public static function Data provider for the cookie domain test.
SessionConfigurationTest::providerTestGeneratedSessionName public static function Data provider for the cookie name test.
SessionConfigurationTest::testConstructorDefaultSettings public function Tests constructor's default settings.
SessionConfigurationTest::testCookieSecure public function Tests whether the session.cookie_secure ini settings is computed correctly.
SessionConfigurationTest::testCookieSecureNotOverridable public function Tests that session.cookie_secure ini settings cannot be overridden.
SessionConfigurationTest::testEnforcedCookieDomain public function Tests the constructor injected session.cookie_domain ini setting.
SessionConfigurationTest::testEnforcedSessionNameViaCookieDomain public function Tests whether the session.name ini settings is computed correctly.
SessionConfigurationTest::testGeneratedCookieDomain public function Tests whether the session.cookie_domain ini settings is computed correctly.
SessionConfigurationTest::testGeneratedSessionName public function Tests whether the session.name ini settings is computed correctly.
SessionConfigurationTest::testSameSiteCookie public function Test that session.cookie_samesite is configured correctly.
UnitTestCase::$root protected property The app root.
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::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUp protected function 367
UnitTestCase::setUpBeforeClass public static function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.