Issue #3151118 by alexpott, Beakerboy, kapilkumar0324, anmolgoyal74, jungle, heddn, Mile23, andypost, daffie: Include bootstrap.inc using composer

merge-requests/25/head
catch 2020-11-03 11:45:09 +00:00
parent a8ef4362e6
commit b72fe50a60
22 changed files with 166 additions and 234 deletions

5
composer.lock generated
View File

@ -527,7 +527,7 @@
"dist": {
"type": "path",
"url": "core",
"reference": "0809224944e41d0a5d03f65e1f7a179512647923"
"reference": "af5b9453adf15c6831b1f73e322f8c59da877f14"
},
"require": {
"asm89/stack-cors": "^1.1",
@ -752,6 +752,9 @@
"lib/Drupal/Core/DrupalKernelInterface.php",
"lib/Drupal/Core/Installer/InstallerRedirectTrait.php",
"lib/Drupal/Core/Site/Settings.php"
],
"files": [
"includes/bootstrap.inc"
]
},
"scripts": {

View File

@ -16,7 +16,6 @@ $autoloader = require_once 'autoload.php';
// Disable garbage collection during test runs. Under certain circumstances the
// update path will create so many objects that garbage collection causes
// segmentation faults.
require_once 'core/includes/bootstrap.inc';
if (drupal_valid_test_ua()) {
gc_collect_cycles();
gc_disable();

View File

@ -198,7 +198,8 @@
"lib/Drupal/Core/DrupalKernelInterface.php",
"lib/Drupal/Core/Installer/InstallerRedirectTrait.php",
"lib/Drupal/Core/Site/Settings.php"
]
],
"files": [ "includes/bootstrap.inc" ]
},
"config": {
"preferred-install": "dist"

View File

@ -311,9 +311,6 @@ function install_begin_request($class_loader, &$install_state) {
$install_state['parameters']['langcode'] = preg_replace('/[^a-zA-Z_0-9\-]/', '', $install_state['parameters']['langcode']);
}
// Allow command line scripts to override server variables used by Drupal.
require_once __DIR__ . '/bootstrap.inc';
// If the hash salt leaks, it becomes possible to forge a valid testing user
// agent, install a new copy of Drupal, and take over the original site.
// The user agent header is used to pass a database prefix in the request when

View File

@ -984,9 +984,6 @@ class DrupalKernel implements DrupalKernelInterface, TerminableInterface {
$app_root = static::guessApplicationRoot();
}
// Include our bootstrap file.
require_once $app_root . '/core/includes/bootstrap.inc';
// Enforce E_STRICT, but allow users to set levels not part of E_STRICT.
error_reporting(E_STRICT | E_ALL);

View File

@ -13,9 +13,6 @@ class TestKernel extends DrupalKernel {
* {@inheritdoc}
*/
public function __construct($environment, $class_loader, $allow_dumping = TRUE) {
// Include our bootstrap file.
require_once __DIR__ . '/../../../../includes/bootstrap.inc';
// Exit if we should be in a test environment but aren't.
if (!drupal_valid_test_ua()) {
header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden');

View File

@ -1195,24 +1195,3 @@ class TestEntityType extends ContentEntityType {
}
}
namespace Drupal\entity_test\Entity;
if (!function_exists('t')) {
function t($string, array $args = []) {
return strtr($string, $args);
}
}
namespace Drupal\Core\Entity;
if (!function_exists('t')) {
function t($string, array $args = []) {
return strtr($string, $args);
}
}

View File

@ -25,7 +25,6 @@ require_once __DIR__ . '/includes/utility.inc';
$request = Request::createFromGlobals();
// Manually resemble early bootstrap of DrupalKernel::boot().
require_once __DIR__ . '/includes/bootstrap.inc';
DrupalKernel::bootEnvironment();
try {

View File

@ -17,7 +17,6 @@ if (PHP_SAPI !== 'cli') {
// Bootstrap.
$autoloader = require __DIR__ . '/../../autoload.php';
require_once __DIR__ . '/../includes/bootstrap.inc';
$request = Request::createFromGlobals();
Settings::initialize(dirname(__DIR__, 2), DrupalKernel::findSitePath($request), $autoloader);
$kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod')->boot();

View File

@ -17,7 +17,6 @@ if (PHP_SAPI !== 'cli') {
// Bootstrap.
$autoloader = require __DIR__ . '/../../autoload.php';
require_once __DIR__ . '/../includes/bootstrap.inc';
$request = Request::createFromGlobals();
Settings::initialize(dirname(__DIR__, 2), DrupalKernel::findSitePath($request), $autoloader);
$kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod')->boot();

View File

@ -18,7 +18,6 @@ if (PHP_SAPI !== 'cli') {
// Bootstrap.
$autoloader = require __DIR__ . '/../../autoload.php';
require_once __DIR__ . '/../includes/bootstrap.inc';
$request = Request::createFromGlobals();
Settings::initialize(dirname(__DIR__, 2), DrupalKernel::findSitePath($request), $autoloader);
$kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod')->boot();

View File

@ -16,7 +16,6 @@ if (PHP_SAPI !== 'cli') {
}
$autoloader = require __DIR__ . '/../../autoload.php';
require_once __DIR__ . '/../includes/bootstrap.inc';
$request = Request::createFromGlobals();
Settings::initialize(DRUPAL_ROOT, DrupalKernel::findSitePath($request), $autoloader);

View File

@ -258,8 +258,6 @@ abstract class KernelTestBase extends TestCase implements ServiceProviderInterfa
$this->classLoader = require $this->root . '/autoload.php';
require_once $this->root . '/core/includes/bootstrap.inc';
// Set up virtual filesystem.
Database::addConnectionInfo('default', 'test-runner', $this->getDatabaseConnectionInfo()['default']);
$test_db = new TestDatabase();

View File

@ -60,7 +60,6 @@ class QuickStartTest extends TestCase {
}
// Get a lock and a valid site path.
$this->testDb = new TestDatabase();
include $this->root . '/core/includes/bootstrap.inc';
}
/**

View File

@ -59,13 +59,6 @@ class CronTest extends UnitTestCase {
protected function setUp(): void {
parent::setUp();
// @todo Remove in https://www.drupal.org/project/drupal/issues/2932518
//
// This line is currently needed so that watchdog_exception() is available
// when unit testing Drupal\Core\Cron and can safely be removed once that
// class no longer refers to it.
require_once $this->root . '/core/includes/bootstrap.inc';
// Construct a state object used for testing logger assertions.
$this->state = new State(new KeyValueMemoryFactory());

View File

@ -82,11 +82,6 @@ class DatabaseTest extends UnitTestCase {
*/
public function testFindDriverAutoloadDirectoryException($expected_message, $namespace, $include_tests) {
new Settings(['extension_discovery_scan_tests' => $include_tests]);
if ($include_tests === FALSE) {
// \Drupal\Core\Extension\ExtensionDiscovery::scan() needs
// drupal_valid_test_ua().
include $this->root . '/core/includes/bootstrap.inc';
}
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage($expected_message);
Database::findDriverAutoloadDirectory($namespace, $this->root);

View File

@ -1,195 +1,183 @@
<?php
namespace Drupal\Tests\Core\DrupalKernel {
namespace Drupal\Tests\Core\DrupalKernel;
use Drupal\Core\DrupalKernel;
use Drupal\Tests\UnitTestCase;
use org\bovigo\vfs\vfsStream;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\DrupalKernel;
use Drupal\Tests\UnitTestCase;
use org\bovigo\vfs\vfsStream;
use Symfony\Component\HttpFoundation\Request;
/**
* @coversDefaultClass \Drupal\Core\DrupalKernel
* @group DrupalKernel
*/
class DrupalKernelTest extends UnitTestCase {
/**
* @coversDefaultClass \Drupal\Core\DrupalKernel
* @group DrupalKernel
* Tests hostname validation with settings.
*
* @covers ::setupTrustedHosts
* @dataProvider providerTestTrustedHosts
*/
class DrupalKernelTest extends UnitTestCase {
public function testTrustedHosts($host, $server_name, $message, $expected = FALSE) {
$request = new Request();
/**
* Tests hostname validation with settings.
*
* @covers ::setupTrustedHosts
* @dataProvider providerTestTrustedHosts
*/
public function testTrustedHosts($host, $server_name, $message, $expected = FALSE) {
$request = new Request();
$trusted_host_patterns = [
'^example\.com$',
'^.+\.example\.com$',
'^example\.org',
'^.+\.example\.org',
];
$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');
$method->setAccessible(TRUE);
$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);
if (!empty($host)) {
$request->headers->set('HOST', $host);
}
/**
* Provides test data for testTrustedHosts().
*/
public function providerTestTrustedHosts() {
$data = [];
$request->server->set('SERVER_NAME', $server_name);
// Tests canonical URL.
$data[] = [
'www.example.com',
'www.example.com',
'canonical URL is trusted',
TRUE,
];
$method = new \ReflectionMethod('Drupal\Core\DrupalKernel', 'setupTrustedHosts');
$method->setAccessible(TRUE);
$valid_host = $method->invoke(NULL, $request, $trusted_host_patterns);
// Tests missing hostname for HTTP/1.0 compatibility where the Host
// header is optional.
$data[] = [NULL, 'www.example.com', 'empty Host is valid', TRUE];
$this->assertSame($expected, $valid_host, $message);
// 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,
];
// 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);
}
// Tests mismatch.
$data[] = [
'www.blackhat.com',
'www.example.com',
'unspecified host is untrusted',
FALSE,
];
/**
* Provides test data for testTrustedHosts().
*/
public function providerTestTrustedHosts() {
$data = [];
return $data;
}
// Tests canonical URL.
$data[] = [
'www.example.com',
'www.example.com',
'canonical URL is trusted',
TRUE,
];
/**
* 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() {
$vfs_root = vfsStream::setup('drupal_root');
$sites_php = <<<'EOD'
// 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.blackhat.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() {
$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',
],
// 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('drupal_root')));
$this->assertEquals('sites/example', DrupalKernel::findSitePath($request, FALSE, $vfs_root->url('drupal_root')));
}
$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('drupal_root')));
$this->assertEquals('sites/example', DrupalKernel::findSitePath($request, FALSE, $vfs_root->url('drupal_root')));
}
}
/**
* A fake autoloader for testing
*/
class FakeAutoloader {
/**
* Registers this instance as an autoloader.
*
* @param bool $prepend
* Whether to prepend the autoloader or not
*/
public function register($prepend = FALSE) {
spl_autoload_register([$this, 'loadClass'], TRUE, $prepend);
}
/**
* A fake autoloader for testing
* Unregisters this instance as an autoloader.
*/
class FakeAutoloader {
/**
* Registers this instance as an autoloader.
*
* @param bool $prepend
* Whether to prepend the autoloader or not
*/
public function register($prepend = FALSE) {
spl_autoload_register([$this, 'loadClass'], TRUE, $prepend);
}
/**
* Unregisters this instance as an autoloader.
*/
public function unregister() {
spl_autoload_unregister([$this, 'loadClass']);
}
/**
* Loads the given class or interface.
*
* @return null
* This class never loads.
*/
public function loadClass() {
return NULL;
}
/**
* Finds a file by class name while caching lookups to APC.
*
* @return null
* This class never finds.
*/
public function findFile() {
return NULL;
}
public function unregister() {
spl_autoload_unregister([$this, 'loadClass']);
}
}
namespace {
if (!function_exists('drupal_valid_test_ua')) {
function drupal_valid_test_ua($new_prefix = NULL) {
return FALSE;
}
/**
* Loads the given class or interface.
*
* @return null
* This class never loads.
*/
public function loadClass() {
return NULL;
}
/**
* Finds a file by class name while caching lookups to APC.
*
* @return null
* This class never finds.
*/
public function findFile() {
return NULL;
}
}

View File

@ -23,7 +23,6 @@ if (PHP_SAPI !== 'cli') {
}
$autoloader = require_once 'autoload.php';
require_once 'core/includes/bootstrap.inc';
require_once 'core/includes/errors.inc';
define('DRUPAL_TEST_IN_CHILD_SITE', FALSE);

View File

@ -44,8 +44,6 @@ class ExtensionSerializationTest extends UnitTestCase {
* @covers ::__wakeup
*/
public function testServiceAppRouteUsage() {
// The assumption of our test is that DRUPAL_ROOT is not defined.
$this->assertFalse(defined('DRUPAL_ROOT'), 'Constant DRUPAL_ROOT is defined.');
$container = new ContainerBuilder();
// Set a dummy container app.root to test against.
$container->setParameter('app.root', 'vfs://dummy_app_root');
@ -56,6 +54,14 @@ class ExtensionSerializationTest extends UnitTestCase {
$reflected_root = new \ReflectionProperty($extension, 'root');
$reflected_root->setAccessible(TRUE);
$this->assertEquals('vfs://dummy_app_root', $reflected_root->getValue($extension));
// Change the app root and test serializing and unserializing again.
$container->setParameter('app.root', 'vfs://dummy_app_root2');
\Drupal::setContainer($container);
$extension = unserialize(serialize($extension));
$reflected_root = new \ReflectionProperty($extension, 'root');
$reflected_root->setAccessible(TRUE);
$this->assertEquals('vfs://dummy_app_root2', $reflected_root->getValue($extension));
}
/**

View File

@ -111,13 +111,3 @@ class MachineNameTest extends UnitTestCase {
}
}
namespace Drupal\Core\Render\Element;
if (!function_exists('t')) {
function t($string, array $args = []) {
return strtr($string, $args);
}
}

View File

@ -77,9 +77,6 @@ trait TestRequirementsTrait {
* skipped. Callers should not catch this exception.
*/
private function checkModuleRequirements($root, array $annotations) {
// drupal_valid_ua() might not be loaded.
require_once $root . '/core/includes/bootstrap.inc';
// Make a list of required modules.
$required_modules = [];
foreach ($annotations as $requirement) {

View File

@ -16,7 +16,6 @@ $autoloader = require_once 'autoload.php';
// Disable garbage collection during test runs. Under certain circumstances the
// update path will create so many objects that garbage collection causes
// segmentation faults.
require_once 'core/includes/bootstrap.inc';
if (drupal_valid_test_ua()) {
gc_collect_cycles();
gc_disable();