Problem/Motivation

The Drupal\Tests\media_library\Kernel\MediaLibraryStateTest fails for Symfony 5.4 with the following deprecation warning:

16x: Since symfony/http-foundation 5.1: Passing a non-scalar value as 2nd argument to "Symfony\Component\HttpFoundation\InputBag::get()" is deprecated, pass a scalar or null instead.
    14x in MediaLibraryStateTest::testFromRequest from Drupal\Tests\media_library\Kernel
    2x in MediaLibraryStateTest::testFromRequestQueryLess from Drupal\Tests\media_library\Kernel

Proposed resolution

Update the class Drupal\media_library\MediaLibraryState.

Remaining tasks

TBD

User interface changes

None

API changes

None

Data model changes

None

Release notes snippet

TBD

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

daffie created an issue. See original summary.

daffie’s picture

Status: Active » Needs review
FileSize
1.42 KB

The fix.

larowlan’s picture

Isn't the default NULL? If so can we just use ?: Instead of calling ::all?

daffie’s picture

The problem is that the method get() needs to return a string, int or float value (only scalar values) and the method all() only whats to return array values. Drupal likes to mix array values with string values and therefor we cannot use the method get() any more. The key is to use the method all() to get all values and then to select the value you need: all()['media_library_opener_parameters']. It then works for string values as for array values. The method all() does not have a default return value. See: https://www.drupal.org/node/3213138

larowlan’s picture

+++ b/core/modules/media_library/src/MediaLibraryState.php
@@ -101,10 +101,10 @@ public static function fromRequest(Request $request) {
-      $query->get('media_library_opener_parameters', [])
+      $query->all()['media_library_opener_parameters'] ?? []

Sorry, I was asking is there a reason we can't write this as


$query->get('media_library_opener_parameters') ?: [];

And avoid calling ::all

But I get it now, its not about the 'null' case its about the 'value exists' case

+++ b/core/modules/media_library/src/MediaLibraryState.php
@@ -101,10 +101,10 @@ public static function fromRequest(Request $request) {
+      $query->all()['media_library_allowed_types'] ?? [],
...
+      $query->all()['media_library_opener_parameters'] ?? []

should we store the return from the first call to :all and avoid calling it twice?

In fact, could we re-write this using one call to :all and array_intersect_key and remove the ::get calls too?

daffie’s picture

FileSize
1.22 KB
1.78 KB

In fact, could we re-write this using one call to :all and array_intersect_key and remove the ::get calls too?

Good idea and fixed.

Status: Needs review » Needs work

The last submitted patch, 6: 3244854-6.patch, failed testing. View results

daffie’s picture

Status: Needs work » Needs review
FileSize
919 bytes
1.78 KB

The default value for the method Symfony\Component\HttpFoundation\ParameterBag::get() is NULL and not an empty string as used in the previous patch. See: https://github.com/symfony/symfony/blob/4.4/src/Symfony/Component/HttpFo...

larowlan’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me

longwave’s picture

+++ b/core/modules/media_library/src/MediaLibraryState.php
@@ -96,15 +96,16 @@ public static function create($opener_id, array $allowed_media_type_ids, $select
-      $query->get('media_library_opener_id'),
...
-      $query->get('media_library_remaining'),
-      $query->get('media_library_opener_parameters', [])
+      $params['media_library_opener_id'] ?? NULL,
+      $params['media_library_allowed_types'] ?? [],
+      $params['media_library_selected_type'] ?? NULL,
+      $params['media_library_remaining'] ?? NULL,
+      $params['media_library_opener_parameters'] ?? []

The reason we use all() instead of all('key') or get('key') is when what we are retrieving really could either be a scalar or an array, and subsequent code is set up to handle both cases. Is it really the case that we don't know this for each of these values? Surely e.g. the media_library_opener_id can never be an array?

longwave’s picture

Status: Reviewed & tested by the community » Needs work

This was solved a cleaner way back in https://git.drupalcode.org/project/drupal/-/merge_requests/462/diffs#dif... - this was then reverted for other reasons but I think we can reintroduce the same fix for MediaLibraryState at least.

longwave’s picture

Status: Needs work » Needs review
FileSize
3.15 KB

Alternative approach that uses all() and get() as Symfony intends, this provides extra safety against a malicious user sending an array instead of a string or vice versa.

The InputBag replacement in the test is done at runtime in bootstrap.php, this will be removed when we no longer need to support both versions of Symfony.

daffie’s picture

Status: Needs review » Reviewed & tested by the community

I am giving this current patch a RTBC, because it is a correct solution.
The solution from comment #12 looks like it is copying code from Symfony and it is needing a temporary fix in the test MediaLibraryStateTest.
Personally I like the solution from comment #8 more. To me, it is more in line with how Symfony thinks it should be used.
I leave it up to the committer to decide which solution should land.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
  1. +++ b/core/modules/media_library/src/MediaLibraryState.php
    @@ -268,7 +268,29 @@ public function getAvailableSlots() {
    +  /**
    +   * 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.
    +   */
    +  public function all(string $key = NULL): array {
    

    We should add an @todo to remove this. I agree with @longwave - I think using all(PARAM_NAME) when we expect an array and get(PARAM_NAME) when we expect a scalar is how this is intended to be used.

    I would be great to have an InputBag clean up issue that we can link to.

  2. +++ b/core/modules/media_library/tests/src/Kernel/MediaLibraryStateTest.php
    @@ -286,7 +287,12 @@ public function testFromRequest(array $query_overrides, $exception_expected) {
    +    // @todo Remove this when Symfony 4 is no longer supported.
    

    This could link to the inputbag clean up issue.

catch’s picture

Spokje’s picture

Status: Needs work » Needs review
FileSize
1.02 KB
3.31 KB

Tried to address #14.1 and .2.

daffie’s picture

Status: Needs review » Reviewed & tested by the community

The points of @alexpott from comment #14 are addressed.
Back to RTBC.

@Spokje: Thank you for working on this!

  • catch committed b8e771e on 10.0.x
    Issue #3248454 by daffie, Spokje, longwave, larowlan, alexpott: [...

  • catch committed 2030a5b on 9.4.x
    Issue #3248454 by daffie, Spokje, longwave, larowlan, alexpott: [...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 10.0.x, also cherry-picked to 9.4.x to keep the tests in sync.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.