From 6012189bde9fae89e1adc2c79dac260f5eca5bce Mon Sep 17 00:00:00 2001 From: Kevin Wenger Date: Fri, 31 May 2024 11:59:34 +0200 Subject: [PATCH] fix deprecation using HttpKernelInterface::MAIN_REQUEST instead of HttpKernelInterface::MASTER_REQUEST until drop support of Drupal 9.x --- CHANGELOG.md | 2 +- phpstan.neon | 5 +--- ...HomepageBrowserLanguageRedirectionTest.php | 27 +++++++++++++------ .../HomepageCookieLanguageRedirectionTest.php | 21 ++++++++++----- 4 files changed, 36 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 519a3a9..74aba57 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,8 +24,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - fix automated Drupal 10 compatibility fixes - Issue #3329302 by Project Update Bot - fix library testing path that may be inconsistent between Github Actions & GitlabCI - fix call to deprecated method withConsecutive() on PHPUnit -- ignore deprecation using HttpKernelInterface::MAIN_REQUEST instead of HttpKernelInterface::MASTER_REQUEST until drop support of Drupal 9.x - fix deprecation by passing @dataprovider as static function +- fix deprecation using HttpKernelInterface::MAIN_REQUEST instead of HttpKernelInterface::MASTER_REQUEST until drop support of Drupal 9.x ## [1.0.0] - 2022-12-16 ### Fixed diff --git a/phpstan.neon b/phpstan.neon index 3fab0f8..9b4eb86 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -4,7 +4,4 @@ includes: parameters: level: 1 reportUnmatchedIgnoredErrors: false - ignoreErrors: - # The module support Drupal 9, Drupal 10 and Drupal 11, so we cannot fix that deprecation without dropping support of Drupal 9. - - "#^Fetching deprecated class constant MASTER_REQUEST of class Symfony\\\\Component\\\\HttpKernel\\\\HttpKernelInterface#" - - "#^Fetching deprecated class constant MASTER_REQUEST of interface Symfony\\\\Component\\\\HttpKernel\\\\HttpKernelInterface#" + # ignoreErrors: diff --git a/tests/src/Unit/HomepageBrowserLanguageRedirectionTest.php b/tests/src/Unit/HomepageBrowserLanguageRedirectionTest.php index 7d9d63e..958a022 100644 --- a/tests/src/Unit/HomepageBrowserLanguageRedirectionTest.php +++ b/tests/src/Unit/HomepageBrowserLanguageRedirectionTest.php @@ -27,6 +27,17 @@ class HomepageBrowserLanguageRedirectionTest extends UnitTestCase { use ProphecyTrait; + + /** + * Mimic Symfony MASTER_REQUEST/MAIN_REQUEST from HttpKernelInterface. + * + * In order to keep Drupal 9 compatibility, we need to define this constant by + * ourselves as Drupal 11 (Symfony 7) drop usage of MASTER_REQUEST. + * + * @see https://www.drupal.org/node/3236639 + */ + private const MAIN_REQUEST = 1; + /** * The event subscriber to be tested. * @@ -111,7 +122,7 @@ public function testStopOnFrontpage() { $kernel = $this->prophesize(HttpKernelInterface::class); $request = Request::create('/', 'GET'); - $event = new RequestEvent($kernel->reveal(), $request, HttpKernelInterface::MASTER_REQUEST); + $event = new RequestEvent($kernel->reveal(), $request, self::MAIN_REQUEST); $this->pathMatcher ->expects($this->once()) @@ -132,7 +143,7 @@ public function testStopWhenBrowserFallbackDisabled() { $kernel = $this->prophesize(HttpKernelInterface::class); $request = Request::create('/', 'GET'); - $event = new RequestEvent($kernel->reveal(), $request, HttpKernelInterface::MASTER_REQUEST); + $event = new RequestEvent($kernel->reveal(), $request, self::MAIN_REQUEST); $this->pathMatcher ->expects($this->once()) @@ -165,7 +176,7 @@ public function testStopWhenNoHeaderAcceptLanguageFromBrowser() { $kernel = $this->prophesize(HttpKernelInterface::class); $request = Request::create('/', 'GET'); - $event = new RequestEvent($kernel->reveal(), $request, HttpKernelInterface::MASTER_REQUEST); + $event = new RequestEvent($kernel->reveal(), $request, self::MAIN_REQUEST); $this->pathMatcher ->expects($this->once()) @@ -201,7 +212,7 @@ public function testStopWhenReferrerBypassEnabledAndExternal() { $this->request->server->set('HTTP_ACCEPT_LANGUAGE', 'fr'); $this->request->server->set('HTTP_REFERER', 'https://www.google.ch'); - $event = new RequestEvent($kernel->reveal(), $request, HttpKernelInterface::MASTER_REQUEST); + $event = new RequestEvent($kernel->reveal(), $request, self::MAIN_REQUEST); $currentLanguage = $this->createMock(LanguageInterface::class); $currentLanguage->expects($this->never()) @@ -246,7 +257,7 @@ public function testStopWhenCookieExists() { $this->request->cookies->set('home_redirect_lang_preferred_langcode', 'en'); $this->request->server->set('HTTP_ACCEPT_LANGUAGE', 'fr'); - $event = new RequestEvent($kernel->reveal(), $request, HttpKernelInterface::MASTER_REQUEST); + $event = new RequestEvent($kernel->reveal(), $request, self::MAIN_REQUEST); $currentLanguage = $this->createMock(LanguageInterface::class); $currentLanguage->expects($this->never()) @@ -294,7 +305,7 @@ public function testStopWhenBrowserLanguageSameAsRequestLanguage() { $this->request->cookies->set('home_redirect_lang_preferred_langcode', ''); $this->request->server->set('HTTP_ACCEPT_LANGUAGE', 'fr'); - $event = new RequestEvent($kernel->reveal(), $request, HttpKernelInterface::MASTER_REQUEST); + $event = new RequestEvent($kernel->reveal(), $request, self::MAIN_REQUEST); $currentLanguage = $this->createMock(LanguageInterface::class); $currentLanguage->expects($this->once()) @@ -349,7 +360,7 @@ public function testStopWhenBrowserLanguageNotSupported() { $this->request->cookies->set('home_redirect_lang_preferred_langcode', ''); $this->request->server->set('HTTP_ACCEPT_LANGUAGE', 'en'); - $event = new RequestEvent($kernel->reveal(), $request, HttpKernelInterface::MASTER_REQUEST); + $event = new RequestEvent($kernel->reveal(), $request, self::MAIN_REQUEST); $currentLanguage = $this->createMock(LanguageInterface::class); $currentLanguage->expects($this->once()) @@ -406,7 +417,7 @@ public function testRedirectPreferredLanguage() { $this->request->cookies->set('home_redirect_lang_preferred_langcode', ''); $this->request->server->set('HTTP_ACCEPT_LANGUAGE', 'en'); - $event = new RequestEvent($kernel->reveal(), $request, HttpKernelInterface::MASTER_REQUEST); + $event = new RequestEvent($kernel->reveal(), $request, self::MAIN_REQUEST); $currentLanguage = $this->createMock(LanguageInterface::class); $currentLanguage->expects($this->once()) diff --git a/tests/src/Unit/HomepageCookieLanguageRedirectionTest.php b/tests/src/Unit/HomepageCookieLanguageRedirectionTest.php index a516fae..68ff498 100644 --- a/tests/src/Unit/HomepageCookieLanguageRedirectionTest.php +++ b/tests/src/Unit/HomepageCookieLanguageRedirectionTest.php @@ -34,6 +34,15 @@ class HomepageCookieLanguageRedirectionTest extends UnitTestCase { */ protected $cookieLanguageRedirectionEventSubscriber; + /** + * Mimic Symfony MASTER_REQUEST/MAIN_REQUEST from HttpKernelInterface. + * + * In order to keep Drupal 9 compatibility, we need to define this constant by + * ourselves as Drupal 11 (Symfony 7) drop usage of MASTER_REQUEST. + * + * @see https://www.drupal.org/node/3236639 + */ + private const MAIN_REQUEST = 1; /** * The event subscriber to be tested. @@ -119,7 +128,7 @@ public function testStopOnFrontpage() { $kernel = $this->prophesize(HttpKernelInterface::class); $request = Request::create('/', 'GET'); - $event = new RequestEvent($kernel->reveal(), $request, HttpKernelInterface::MASTER_REQUEST); + $event = new RequestEvent($kernel->reveal(), $request, self::MAIN_REQUEST); $this->pathMatcher ->expects($this->once()) @@ -143,7 +152,7 @@ public function testStopWhenReferrerBypassEnabledAndExternal() { $this->request->server->set('HTTP_ACCEPT_LANGUAGE', 'fr'); $this->request->server->set('HTTP_REFERER', 'https://www.google.ch'); - $event = new RequestEvent($kernel->reveal(), $request, HttpKernelInterface::MASTER_REQUEST); + $event = new RequestEvent($kernel->reveal(), $request, self::MAIN_REQUEST); $currentLanguage = $this->createMock(LanguageInterface::class); $currentLanguage->expects($this->never()) @@ -183,7 +192,7 @@ public function testStopWhenCookieNotExists() { $this->request->server->set('HTTP_ACCEPT_LANGUAGE', 'fr'); - $event = new RequestEvent($kernel->reveal(), $request, HttpKernelInterface::MASTER_REQUEST); + $event = new RequestEvent($kernel->reveal(), $request, self::MAIN_REQUEST); $currentLanguage = $this->createMock(LanguageInterface::class); $currentLanguage->expects($this->never()) @@ -224,7 +233,7 @@ public function testStopWhenCookieLanguageSameAsRequestLanguage() { $this->request->server->set('HTTP_ACCEPT_LANGUAGE', 'fr'); $this->request->cookies->set('home_redirect_lang_preferred_langcode', 'fr'); - $event = new RequestEvent($kernel->reveal(), $request, HttpKernelInterface::MASTER_REQUEST); + $event = new RequestEvent($kernel->reveal(), $request, self::MAIN_REQUEST); $currentLanguage = $this->createMock(LanguageInterface::class); $currentLanguage->expects($this->once()) @@ -266,7 +275,7 @@ public function testStopWhenCookieLanguageNotSupported() { $this->request->server->set('HTTP_ACCEPT_LANGUAGE', 'fr'); $this->request->cookies->set('home_redirect_lang_preferred_langcode', 'ru'); - $event = new RequestEvent($kernel->reveal(), $request, HttpKernelInterface::MASTER_REQUEST); + $event = new RequestEvent($kernel->reveal(), $request, self::MAIN_REQUEST); $currentLanguage = $this->createMock(LanguageInterface::class); $currentLanguage->expects($this->once()) @@ -313,7 +322,7 @@ public function testRedirectPreferredLanguage() { $this->request->server->set('HTTP_ACCEPT_LANGUAGE', 'fr'); $this->request->cookies->set('home_redirect_lang_preferred_langcode', 'en'); - $event = new RequestEvent($kernel->reveal(), $request, HttpKernelInterface::MASTER_REQUEST); + $event = new RequestEvent($kernel->reveal(), $request, self::MAIN_REQUEST); $currentLanguage = $this->createMock(LanguageInterface::class); $currentLanguage->expects($this->once())