Skip to content
Snippets Groups Projects
Commit 6d05fc8e authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2381763 by chx, webflo: Adjust the order of container yamls to override...

Issue #2381763 by chx, webflo: Adjust the order of container yamls to override settings per environment
parent 5be46c3a
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
......@@ -530,12 +530,8 @@ public function discoverServiceProviders() {
}
}
}
if ($container_yamls = Settings::get('container_yamls')) {
$this->serviceYamls['site'] = $container_yamls;
}
$site_services_yml = $this->getSitePath() . '/services.yml';
if (file_exists($site_services_yml) && is_readable($site_services_yml)) {
$this->serviceYamls['site'][] = $site_services_yml;
if (!$this->addServiceFiles(Settings::get('container_yamls'))) {
throw new \Exception('The container_yamls setting is missing from settings.php');
}
}
......@@ -1295,4 +1291,21 @@ protected static function setupTrustedHosts(Request $request, $hostPatterns) {
return TRUE;
}
/**
* Add service files.
*
* @param $service_yamls
* A list of service files.
*
* @return bool
* TRUE if the list was an array, FALSE otherwise.
*/
protected function addServiceFiles($service_yamls) {
if (is_array($service_yamls)) {
$this->serviceYamls['site'] = array_filter($service_yamls, 'file_exists');
return TRUE;
}
return FALSE;
}
}
......@@ -40,4 +40,11 @@ public function resetConfigStorage() {
$this->configStorage = NULL;
}
/**
* {@inheritdoc}
*/
protected function addServiceFiles($service_yamls) {
// In the beginning there is no settings.php and no service YAMLs.
return parent::addServiceFiles($service_yamls ?: []);
}
}
......@@ -55,6 +55,7 @@ public function boot() {
if (!Settings::getAll()) {
new Settings(array(
'hash_salt' => 'run-tests',
'container_yamls' => [],
// If there is no settings.php, then there is no parent site. In turn,
// there is no public files directory; use a custom public files path.
'file_public_path' => 'sites/default/files',
......
......@@ -135,11 +135,16 @@ protected function prepareConfigDirectories() {
protected function setUp() {
$this->keyValueFactory = new KeyValueMemoryFactory();
// Back up settings from TestBase::prepareEnvironment().
$settings = Settings::getAll();
// Allow for test-specific overrides.
$settings_services_file = DRUPAL_ROOT . '/' . $this->originalSite . '/testing.services.yml';
if (file_exists($settings_services_file)) {
// Copy the testing-specific service overrides in place.
copy($settings_services_file, DRUPAL_ROOT . '/' . $this->siteDirectory . '/services.yml');
$testing_services_file = DRUPAL_ROOT . '/' . $this->siteDirectory . '/services.yml';
copy($settings_services_file, $testing_services_file);
$this->settingsSet('container_yamls', [$testing_services_file]);
}
// Create and set new configuration directories.
......@@ -149,8 +154,6 @@ protected function setUp() {
// @todo Remove the indirection; implement ServiceProviderInterface instead.
$GLOBALS['conf']['container_service_providers']['TestServiceProvider'] = 'Drupal\simpletest\TestServiceProvider';
// Back up settings from TestBase::prepareEnvironment().
$settings = Settings::getAll();
// Bootstrap a new kernel. Don't use createFromRequest so we don't mess with settings.
$class_loader = require DRUPAL_ROOT . '/core/vendor/autoload.php';
$this->kernel = new DrupalKernel('testing', $class_loader, FALSE);
......
......@@ -1210,6 +1210,7 @@ private function prepareEnvironment() {
new Settings(array(
// For performance, simply use the database prefix as hash salt.
'hash_salt' => $this->databasePrefix,
'container_yamls' => [],
));
drupal_set_time_limit($this->timeLimit);
......
......@@ -7,6 +7,7 @@
namespace Drupal\system\Tests\DrupalKernel;
use Drupal\Core\Site\Settings;
use Drupal\simpletest\KernelTestBase;
/**
......@@ -20,6 +21,9 @@ class DrupalKernelSiteTest extends KernelTestBase {
* Tests services.yml in site directory.
*/
public function testServicesYml() {
$container_yamls = Settings::get('container_yamls');
$container_yamls[] = $this->siteDirectory . '/services.yml';
$this->settingsSet('container_yamls', $container_yamls);
$this->assertFalse($this->container->has('site.service.yml'));
// A service provider class always has precedence over services.yml files.
// KernelTestBase::buildContainer() swaps out many services with in-memory
......
......@@ -54,6 +54,7 @@ protected function getTestKernel(Request $request, array $modules_enabled = NULL
// Manually create kernel to avoid replacing settings.
$class_loader = require DRUPAL_ROOT . '/core/vendor/autoload.php';
$kernel = DrupalKernel::createFromRequest($request, $class_loader, 'testing');
$this->settingsSet('container_yamls', []);
$this->settingsSet('hash_salt', $this->databasePrefix);
if (isset($modules_enabled)) {
$kernel->updateModules($modules_enabled);
......@@ -169,6 +170,7 @@ public function testRepeatedBootWithDifferentEnvironment() {
foreach ($environments as $environment) {
$kernel = DrupalKernel::createFromRequest($request, $class_loader, $environment);
$this->settingsSet('container_yamls', []);
$this->settingsSet('hash_salt', $this->databasePrefix);
$kernel->boot();
}
......
<?php
/**
* @file
* Contains \Drupal\Tests\Core\DrupalKernel\DiscoverServiceProvidersTest.
*/
namespace Drupal\Tests\Core\DrupalKernel;
use Drupal\Core\DrupalKernel;
use Drupal\Core\Site\Settings;
use Drupal\Tests\UnitTestCase;
/**
* @coversDefaultClass \Drupal\Core\DrupalKernel
* @group DrupalKernel
*/
class DiscoverServiceProvidersTest extends UnitTestCase {
/**
* Tests discovery with user defined container yaml.
*
* @covers ::discoverServiceProviders()
*/
public function testDiscoverServiceCustom() {
new Settings(array(
'container_yamls' => array(
__DIR__ . '/fixtures/custom.yml'
),
));
$kernel = new DrupalKernel('prod', new \Composer\Autoload\ClassLoader());
$kernel->discoverServiceProviders();
$expect = array(
'app' => array(
'core' => 'core/core.services.yml',
),
'site' => array(
__DIR__ . '/fixtures/custom.yml',
),
);
$this->assertAttributeSame($expect, 'serviceYamls', $kernel);
}
/**
* Tests the exception when container_yamls is not set.
*
* @covers ::discoverServiceProviders()
* @expectedException \Exception
*/
public function testDiscoverServiceNoContainerYamls() {
new Settings([]);
$kernel = new DrupalKernel('prod', new \Composer\Autoload\ClassLoader());
$kernel->discoverServiceProviders();
}
}
parameters:
test.config:
test: true
......@@ -594,6 +594,11 @@
# $config['system.performance']['fast_404']['paths'] = '/\.(?:txt|png|gif|jpe?g|css|js|ico|swf|flv|cgi|bat|pl|dll|exe|asp)$/i';
# $config['system.performance']['fast_404']['html'] = '<!DOCTYPE html><html><head><title>404 Not Found</title></head><body><h1>Not Found</h1><p>The requested URL "@path" was not found on this server.</p></body></html>';
/**
* Load services definition file.
*/
$settings['container_yamls'][] = __DIR__ . '/services.yml';
/**
* Load local development override configuration, if available.
*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment