Verified Commit 00e559fe authored by Andrei Mateescu's avatar Andrei Mateescu
Browse files

fix: #3571849 Media Library allows user to select more media than expected

By: julio_retkwa
By: smustgrave
By: amateescu
(cherry picked from commit fc58a46e)
parent aa038bbc
Loading
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -319,8 +319,11 @@
        ) {
          $('#media-library-messages').empty();
        }
        // Use >= instead of === to also disable items when selection EXCEEDS the limit
        // (e.g., after uploading multiple images consecutively).
        if (
          currentSelection.length === settings.media_library.selection_remaining
          settings.media_library.selection_remaining > 0 &&
          currentSelection.length >= settings.media_library.selection_remaining
        ) {
          disableItems($mediaItems.not(':checked'));
          enableItems($mediaItems.filter(':checked'));
+21 −0
Original line number Diff line number Diff line
@@ -121,6 +121,19 @@ public function testWidgetOverflow(?string $selected_operation): void {
      $this->pressSaveButton();
    }
    $this->waitForElementTextContains('.messages--warning', 'There are currently 5 items selected. The maximum number of items for the field is 2. Remove 3 items from the selection.');

    // Assert that unchecked media items are disabled when the selection
    // exceeds the allowed maximum. This verifies the fix for the case where
    // uploading multiple files at once causes the selection to exceed the
    // limit; previously items were only disabled when the selection count
    // was exactly equal to the limit.
    $checkboxes = $this->getCheckboxes();
    foreach ($checkboxes as $checkbox) {
      if (!$checkbox->isChecked()) {
        $this->assertTrue($checkbox->hasAttribute('disabled'), 'Unchecked media items should be disabled when selection exceeds the limit.');
      }
    }

    // If the user tries to insert the selected items anyway, they should get
    // an error.
    $this->pressInsertSelected(NULL, FALSE);
@@ -132,6 +145,14 @@ public function testWidgetOverflow(?string $selected_operation): void {
    $this->deselectMediaItem(3);
    $this->deselectMediaItem(4);
    $assert_session->elementNotExists('css', '.messages--error');
    // After deselecting to exactly the limit (2 of 2), unchecked items
    // should still be disabled since the selection is at the maximum.
    $checkboxes = $this->getCheckboxes();
    foreach ($checkboxes as $checkbox) {
      if (!$checkbox->isChecked()) {
        $this->assertTrue($checkbox->hasAttribute('disabled'), 'Unchecked media items should be disabled when selection is at the limit.');
      }
    }
    $this->pressInsertSelected('Added 2 media items.');
  }