From b8e771ecd55eae4cf6fa35d73053d267d7ab21bb Mon Sep 17 00:00:00 2001 From: catch Date: Wed, 22 Dec 2021 15:28:18 +0000 Subject: [PATCH] Issue #3248454 by daffie, Spokje, longwave, larowlan, alexpott: [Symfony6] The Drupal\Tests\media_library\Kernel\MediaLibraryStateTest fails for Symfony 5.4 --- .../media_library/src/MediaLibraryState.php | 33 ++++++++++++++++--- .../src/Kernel/MediaLibraryStateTest.php | 9 ++++- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/core/modules/media_library/src/MediaLibraryState.php b/core/modules/media_library/src/MediaLibraryState.php index e1f0b4632dc..6e1773073ca 100644 --- a/core/modules/media_library/src/MediaLibraryState.php +++ b/core/modules/media_library/src/MediaLibraryState.php @@ -101,10 +101,10 @@ class MediaLibraryState extends ParameterBag implements CacheableDependencyInter // all validation runs. $state = static::create( $query->get('media_library_opener_id'), - $query->get('media_library_allowed_types', []), + $query->all('media_library_allowed_types'), $query->get('media_library_selected_type'), $query->get('media_library_remaining'), - $query->get('media_library_opener_parameters', []) + $query->all('media_library_opener_parameters') ); // The request parameters need to contain a valid hash to prevent a @@ -224,7 +224,7 @@ class MediaLibraryState extends ParameterBag implements CacheableDependencyInter * The media type IDs. */ public function getAllowedTypeIds() { - return $this->get('media_library_allowed_types'); + return $this->all('media_library_allowed_types'); } /** @@ -268,7 +268,32 @@ class MediaLibraryState extends ParameterBag implements CacheableDependencyInter * An associative array of all opener-specific parameter values. */ public function getOpenerParameters() { - return $this->get('media_library_opener_parameters', []); + return $this->all('media_library_opener_parameters'); + } + + /** + * Returns the parameters. + * + * @param string|null $key + * The name of the parameter to return or null to get them all. + * + * @return array + * An array of parameters. + * + * @todo Remove this when Symfony 4 is no longer supported. + * See https://www.drupal.org/node/3162981 + */ + public function all(string $key = NULL): array { + if ($key === NULL) { + return $this->parameters; + } + + $value = $this->parameters[$key] ?? []; + if (!is_array($value)) { + throw new \UnexpectedValueException(sprintf('Unexpected value for parameter "%s": expecting "array", got "%s".', $key, get_debug_type($value))); + } + + return $value; } /** diff --git a/core/modules/media_library/tests/src/Kernel/MediaLibraryStateTest.php b/core/modules/media_library/tests/src/Kernel/MediaLibraryStateTest.php index ffc5dd27ee4..bc4419a3b5a 100644 --- a/core/modules/media_library/tests/src/Kernel/MediaLibraryStateTest.php +++ b/core/modules/media_library/tests/src/Kernel/MediaLibraryStateTest.php @@ -4,6 +4,7 @@ namespace Drupal\Tests\media_library\Kernel; use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\CacheableDependencyInterface; +use Drupal\Core\Http\InputBag; use Drupal\KernelTests\KernelTestBase; use Drupal\media_library\MediaLibraryState; use Drupal\Tests\media\Traits\MediaTypeCreationTrait; @@ -286,7 +287,13 @@ class MediaLibraryStateTest extends KernelTestBase { $this->expectException(BadRequestHttpException::class); $this->expectExceptionMessage("Invalid media library parameters specified."); } - $state = MediaLibraryState::fromRequest(new Request($query)); + + // @todo Remove this when Symfony 4 is no longer supported. + // See https://www.drupal.org/node/3162981 + $request = new Request(); + $request->query = new InputBag($query); + + $state = MediaLibraryState::fromRequest($request); $this->assertInstanceOf(MediaLibraryState::class, $state); }