class DrupalKernelTest

Same name in this branch
  1. 10 core/tests/Drupal/KernelTests/Core/DrupalKernel/DrupalKernelTest.php \Drupal\KernelTests\Core\DrupalKernel\DrupalKernelTest
Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/DrupalKernel/DrupalKernelTest.php \Drupal\KernelTests\Core\DrupalKernel\DrupalKernelTest
  2. 9 core/tests/Drupal/Tests/Core/DrupalKernel/DrupalKernelTest.php \Drupal\Tests\Core\DrupalKernel\DrupalKernelTest
  3. 8.9.x core/tests/Drupal/KernelTests/Core/DrupalKernel/DrupalKernelTest.php \Drupal\KernelTests\Core\DrupalKernel\DrupalKernelTest
  4. 8.9.x core/tests/Drupal/Tests/Core/DrupalKernel/DrupalKernelTest.php \Drupal\Tests\Core\DrupalKernel\DrupalKernelTest
  5. 11.x core/tests/Drupal/KernelTests/Core/DrupalKernel/DrupalKernelTest.php \Drupal\KernelTests\Core\DrupalKernel\DrupalKernelTest
  6. 11.x core/tests/Drupal/Tests/Core/DrupalKernel/DrupalKernelTest.php \Drupal\Tests\Core\DrupalKernel\DrupalKernelTest

@coversDefaultClass \Drupal\Core\DrupalKernel @group DrupalKernel

Hierarchy

Expanded class hierarchy of DrupalKernelTest

File

core/tests/Drupal/Tests/Core/DrupalKernel/DrupalKernelTest.php, line 20

Namespace

Drupal\Tests\Core\DrupalKernel
View source
class DrupalKernelTest extends UnitTestCase {
    
    /**
     * Tests hostname validation with settings.
     *
     * @covers ::setupTrustedHosts
     * @dataProvider providerTestTrustedHosts
     */
    public function testTrustedHosts($host, $server_name, $message, $expected = FALSE) : void {
        $request = new Request();
        $trusted_host_patterns = [
            '^example\\.com$',
            '^.+\\.example\\.com$',
            '^example\\.org',
            '^.+\\.example\\.org',
        ];
        if (!empty($host)) {
            $request->headers
                ->set('HOST', $host);
        }
        $request->server
            ->set('SERVER_NAME', $server_name);
        $method = new \ReflectionMethod('Drupal\\Core\\DrupalKernel', 'setupTrustedHosts');
        $valid_host = $method->invoke(NULL, $request, $trusted_host_patterns);
        $this->assertSame($expected, $valid_host, $message);
        // Reset the trusted hosts because it is statically stored on the request.
        $method->invoke(NULL, $request, []);
        // Reset the request factory because it is statically stored on the request.
        Request::setFactory(NULL);
    }
    
    /**
     * Provides test data for testTrustedHosts().
     */
    public static function providerTestTrustedHosts() {
        $data = [];
        // Tests canonical URL.
        $data[] = [
            'www.example.com',
            'www.example.com',
            'canonical URL is trusted',
            TRUE,
        ];
        // Tests missing hostname for HTTP/1.0 compatibility where the Host
        // header is optional.
        $data[] = [
            NULL,
            'www.example.com',
            'empty Host is valid',
            TRUE,
        ];
        // Tests the additional patterns from the settings.
        $data[] = [
            'example.com',
            'www.example.com',
            'host from settings is trusted',
            TRUE,
        ];
        $data[] = [
            'subdomain.example.com',
            'www.example.com',
            'host from settings is trusted',
            TRUE,
        ];
        $data[] = [
            'www.example.org',
            'www.example.com',
            'host from settings is trusted',
            TRUE,
        ];
        $data[] = [
            'example.org',
            'www.example.com',
            'host from settings is trusted',
            TRUE,
        ];
        // Tests mismatch.
        $data[] = [
            'www.black_hat.com',
            'www.example.com',
            'unspecified host is untrusted',
            FALSE,
        ];
        return $data;
    }
    
    /**
     * Tests site path finding.
     *
     * This test is run in a separate process since it defines DRUPAL_ROOT. This
     * stops any possible pollution of other tests.
     *
     * @covers ::findSitePath
     * @runInSeparateProcess
     */
    public function testFindSitePath() : void {
        $vfs_root = vfsStream::setup('drupal_root');
        $sites_php = <<<'EOD'
<?php
$sites['8888.www.example.org'] = 'example';
EOD;
        // Create the expected directory structure.
        vfsStream::create([
            'sites' => [
                'sites.php' => $sites_php,
                'example' => [
                    'settings.php' => 'test',
                ],
            ],
        ]);
        $request = new Request();
        $request->server
            ->set('SERVER_NAME', 'www.example.org');
        $request->server
            ->set('SERVER_PORT', '8888');
        $request->server
            ->set('SCRIPT_NAME', '/index.php');
        $this->assertEquals('sites/example', DrupalKernel::findSitePath($request, TRUE, $vfs_root->url()));
        $this->assertEquals('sites/example', DrupalKernel::findSitePath($request, FALSE, $vfs_root->url()));
    }
    
    /**
     * @covers ::getServiceIdMapping
     * @group legacy
     */
    public function testGetServiceIdMapping() : void {
        $this->expectDeprecation("Drupal\\Core\\DrupalKernel::getServiceIdMapping() is deprecated in drupal:9.5.1 and is removed from drupal:11.0.0. Use the 'Drupal\\Component\\DependencyInjection\\ReverseContainer' service instead. See https://www.drupal.org/node/3327942");
        $this->expectDeprecation("Drupal\\Core\\DrupalKernel::collectServiceIdMapping() is deprecated in drupal:9.5.1 and is removed from drupal:11.0.0. Use the 'Drupal\\Component\\DependencyInjection\\ReverseContainer' service instead. See https://www.drupal.org/node/3327942");
        $service = new BarClass();
        $container = TestKernel::setContainerWithKernel();
        $container->set('bar', $service);
        $this->assertEquals($container->get('kernel')
            ->getServiceIdMapping()[$container->generateServiceIdHash($service)], 'bar');
    }
    
    /**
     * @covers ::terminate
     * @runInSeparateProcess
     */
    public function testUnBootedTerminate() : void {
        $kernel = new DrupalKernel('test', new ClassLoader());
        $kernel->terminate(new Request(), new Response());
        $this->assertTrue(TRUE, "\\Drupal\\Core\\DrupalKernel::terminate() called without error on kernel which has not booted");
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overrides
DrupalKernelTest::providerTestTrustedHosts public static function Provides test data for testTrustedHosts().
DrupalKernelTest::testFindSitePath public function Tests site path finding.
DrupalKernelTest::testGetServiceIdMapping public function @covers ::getServiceIdMapping
@group legacy
DrupalKernelTest::testTrustedHosts public function Tests hostname validation with settings.
DrupalKernelTest::testUnBootedTerminate public function @covers ::terminate
@runInSeparateProcess
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
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.
RandomGeneratorTrait::randomStringValidate Deprecated public function Callback for random string validation.
UnitTestCase::$root protected property The app root. 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::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUp protected function 358
UnitTestCase::setUpBeforeClass public static function
UnitTestCase::__get public function

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