Skip to content
Snippets Groups Projects
Commit bb03ed5c authored by catch's avatar catch
Browse files

Issue #3513875 by mstrelan: Move test middleware out of CoreServiceProvider

parent 5da58a21
No related branches found
No related tags found
4 merge requests!5423Draft: Resolve #3329907 "Test2",!3478Issue #3337882: Deleted menus are not removed from content type config,!2964Issue #2865710 : Dependencies from only one instance of a widget are used in display modes,!579Issue #2230909: Simple decimals fail to pass validation
Pipeline #457007 canceled
...@@ -3487,12 +3487,6 @@ ...@@ -3487,12 +3487,6 @@
'count' => 1, 'count' => 1,
'path' => __DIR__ . '/lib/Drupal/Core/CoreServiceProvider.php', 'path' => __DIR__ . '/lib/Drupal/Core/CoreServiceProvider.php',
]; ];
$ignoreErrors[] = [
'message' => '#^Method Drupal\\\\Core\\\\CoreServiceProvider\\:\\:registerTest\\(\\) has no return type specified\\.$#',
'identifier' => 'missingType.return',
'count' => 1,
'path' => __DIR__ . '/lib/Drupal/Core/CoreServiceProvider.php',
];
$ignoreErrors[] = [ $ignoreErrors[] = [
'message' => '#^Method Drupal\\\\Core\\\\Cron\\:\\:invokeCronHandlers\\(\\) has no return type specified\\.$#', 'message' => '#^Method Drupal\\\\Core\\\\Cron\\:\\:invokeCronHandlers\\(\\) has no return type specified\\.$#',
'identifier' => 'missingType.return', 'identifier' => 'missingType.return',
...@@ -34,7 +34,6 @@ ...@@ -34,7 +34,6 @@
use Drupal\Core\Site\Settings; use Drupal\Core\Site\Settings;
use Psr\Log\LoggerAwareInterface; use Psr\Log\LoggerAwareInterface;
use Symfony\Component\DependencyInjection\Compiler\PassConfig; use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass; use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass;
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
...@@ -56,8 +55,6 @@ class CoreServiceProvider implements ServiceProviderInterface, ServiceModifierIn ...@@ -56,8 +55,6 @@ class CoreServiceProvider implements ServiceProviderInterface, ServiceModifierIn
* {@inheritdoc} * {@inheritdoc}
*/ */
public function register(ContainerBuilder $container) { public function register(ContainerBuilder $container) {
$this->registerTest($container);
// Only register the private file stream wrapper if a file path has been // Only register the private file stream wrapper if a file path has been
// set. // set.
if (Settings::get('file_private_path')) { if (Settings::get('file_private_path')) {
...@@ -162,32 +159,4 @@ public function alter(ContainerBuilder $container) { ...@@ -162,32 +159,4 @@ public function alter(ContainerBuilder $container) {
} }
} }
/**
* Registers services and event subscribers for a site under test.
*
* @param \Drupal\Core\DependencyInjection\ContainerBuilder $container
* The container builder.
*/
protected function registerTest(ContainerBuilder $container) {
// Do nothing if we are not in a test environment.
if (!drupal_valid_test_ua()) {
return;
}
// The test middleware is not required for kernel tests as there is no child
// site. DRUPAL_TEST_IN_CHILD_SITE is not defined in this case.
if (!defined('DRUPAL_TEST_IN_CHILD_SITE')) {
return;
}
// Add the HTTP request middleware to Guzzle.
$container
->register('test.http_client.middleware', 'Drupal\Core\Test\HttpClientMiddleware\TestHttpClientMiddleware')
->addTag('http_client_middleware');
// Add the wait terminate middleware which acquires a lock to signal request
// termination to the test runner.
$container
->register('test.http_middleware.wait_terminate_middleware', 'Drupal\Core\Test\StackMiddleware\TestWaitTerminateMiddleware')
->setArguments([new Reference('state'), new Reference('lock')])
->addTag('http_middleware', ['priority' => -1024]);
}
} }
...@@ -167,6 +167,17 @@ protected function prepareSettings() { ...@@ -167,6 +167,17 @@ protected function prepareSettings() {
'tags' => [['name' => 'event_subscriber']], 'tags' => [['name' => 'event_subscriber']],
]; ];
} }
// Register test middleware.
$services['services']['testing.http_client.middleware'] = [
'class' => 'Drupal\Core\Test\HttpClientMiddleware\TestHttpClientMiddleware',
'tags' => [['name' => 'http_client_middleware']],
];
$services['services']['testing.http_middleware.wait_terminate_middleware'] = [
'class' => 'Drupal\Core\Test\StackMiddleware\TestWaitTerminateMiddleware',
'arguments' => ['@lock', '%drupal.test_wait_terminate%'],
'tags' => [['name' => 'http_middleware', 'priority' => -1024]],
];
$services['parameters']['drupal.test_wait_terminate'] = FALSE;
file_put_contents($directory . '/services.yml', $yaml->dump($services)); file_put_contents($directory . '/services.yml', $yaml->dump($services));
// Since Drupal is bootstrapped already, install_begin_request() will not // Since Drupal is bootstrapped already, install_begin_request() will not
// bootstrap again. Hence, we have to reload the newly written custom // bootstrap again. Hence, we have to reload the newly written custom
......
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
namespace Drupal\Core\Test\StackMiddleware; namespace Drupal\Core\Test\StackMiddleware;
use Drupal\Core\Lock\LockBackendInterface; use Drupal\Core\Lock\LockBackendInterface;
use Drupal\Core\State\StateInterface;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface; use Symfony\Component\HttpKernel\HttpKernelInterface;
...@@ -18,15 +17,15 @@ class TestWaitTerminateMiddleware implements HttpKernelInterface { ...@@ -18,15 +17,15 @@ class TestWaitTerminateMiddleware implements HttpKernelInterface {
* *
* @param \Symfony\Component\HttpKernel\HttpKernelInterface $httpKernel * @param \Symfony\Component\HttpKernel\HttpKernelInterface $httpKernel
* The decorated kernel. * The decorated kernel.
* @param \Drupal\Core\State\StateInterface $state
* The state server.
* @param \Drupal\Core\Lock\LockBackendInterface $lock * @param \Drupal\Core\Lock\LockBackendInterface $lock
* The lock backend. * The lock backend.
* @param bool $waitForTerminate
* Container parameter to toggle this behavior.
*/ */
public function __construct( public function __construct(
protected HttpKernelInterface $httpKernel, protected HttpKernelInterface $httpKernel,
protected StateInterface $state,
protected LockBackendInterface $lock, protected LockBackendInterface $lock,
protected bool $waitForTerminate = FALSE,
) { ) {
} }
...@@ -36,14 +35,16 @@ public function __construct( ...@@ -36,14 +35,16 @@ public function __construct(
public function handle(Request $request, $type = self::MAIN_REQUEST, $catch = TRUE): Response { public function handle(Request $request, $type = self::MAIN_REQUEST, $catch = TRUE): Response {
$result = $this->httpKernel->handle($request, $type, $catch); $result = $this->httpKernel->handle($request, $type, $catch);
if ($this->state->get('drupal.test_wait_terminate')) { if (!$this->waitForTerminate) {
// Set a header on the response to instruct the test runner that it must return $result;
// await the lock. Note that the lock acquired here is automatically
// released from within a shutdown function.
$this->lock->acquire('test_wait_terminate');
$result->headers->set('X-Drupal-Wait-Terminate', '1');
} }
// Set a header on the response to instruct the test runner that it must
// await the lock. Note that the lock acquired here is automatically
// released from within a shutdown function.
$this->lock->acquire('test_wait_terminate');
$result->headers->set('X-Drupal-Wait-Terminate', '1');
return $result; return $result;
} }
......
...@@ -17,7 +17,8 @@ trait WaitTerminateTestTrait { ...@@ -17,7 +17,8 @@ trait WaitTerminateTestTrait {
* event need to enable this. * event need to enable this.
*/ */
protected function setWaitForTerminate(): void { protected function setWaitForTerminate(): void {
$this->container->get('state')->set('drupal.test_wait_terminate', TRUE); $this->setContainerParameter('drupal.test_wait_terminate', TRUE);
$this->rebuildContainer();
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment