diff --git a/core/modules/media_library/src/Form/FileUploadForm.php b/core/modules/media_library/src/Form/FileUploadForm.php index ec7362fc63..81367821c2 100644 --- a/core/modules/media_library/src/Form/FileUploadForm.php +++ b/core/modules/media_library/src/Form/FileUploadForm.php @@ -320,9 +320,8 @@ public function removeButtonSubmit(array $form, FormStateInterface $form_state) $triggering_element = $form_state->getTriggeringElement(); $delta = array_slice($triggering_element['#array_parents'], -2, 1)[0]; - /** @var \Drupal\media\MediaInterface[] $added_media */ - $added_media = $form_state->get('media'); - $removed_media = $added_media[$delta]; + /** @var \Drupal\media\MediaInterface $removed_media */ + $removed_media = $form_state->get(['media', $delta]); $file = $removed_media->get($this->getSourceFieldName($removed_media->bundle->entity))->entity; if ($file instanceof FileInterface && empty($this->fileUsage->listUsage($file))) { diff --git a/core/modules/media_library/tests/src/FunctionalJavascript/MediaLibraryTest.php b/core/modules/media_library/tests/src/FunctionalJavascript/MediaLibraryTest.php index 261f29c1f3..a088e87678 100644 --- a/core/modules/media_library/tests/src/FunctionalJavascript/MediaLibraryTest.php +++ b/core/modules/media_library/tests/src/FunctionalJavascript/MediaLibraryTest.php @@ -1114,21 +1114,21 @@ public function testWidgetUpload() { $assert_session->pageTextContains('1 of 2 items selected'); $assert_session->hiddenFieldValueEquals('current_selection', $added_media->id()); // Ensure the created item is added in the widget. + $this->assertNotEmpty($assert_session->waitForText('Add or select media')); $assert_session->elementExists('css', '.ui-dialog-buttonpane')->pressButton('Insert selected'); $this->assertNotEmpty($assert_session->waitForText('Added one media item.')); - $assert_session->assertWaitOnAjaxRequest(); $assert_session->pageTextNotContains('Add or select media'); $assert_session->pageTextContains($png_image->filename); // Remove the item. $assert_session->elementExists('css', '.media-library-item__remove')->click(); - $assert_session->assertWaitOnAjaxRequest(); + $this->assertNotEmpty($assert_session->waitForElementRemoved('css', '.media-library-item')); $assert_session->pageTextNotContains($png_image->filename); // Assert we can also directly insert uploaded files in the widget. $assert_session->elementExists('css', '.media-library-open-button[name^="field_twin_media"]')->click(); $assert_session->assertWaitOnAjaxRequest(); - $assert_session->pageTextContains('Add or select media'); + $this->assertNotEmpty($assert_session->waitForText('Add or select media')); $page->clickLink('Type Three'); $assert_session->assertWaitOnAjaxRequest(); $png_uri_2 = $file_system->copy($png_image->uri, 'public://'); @@ -1163,16 +1163,15 @@ public function testWidgetUpload() { $page->fillField('Name', 'Unlimited Cardinality Image'); $page->fillField('Alternative text', $this->randomString()); $assert_session->elementExists('css', '.ui-dialog-buttonpane')->pressButton('Save and select'); - $assert_session->assertWaitOnAjaxRequest(); + // Ensure the media item was saved to the library and automatically + // selected. The added media items should be in the first position of the + // add form. // Load the created media item. + $this->assertNotEmpty($assert_session->waitForText('Add or select media')); + $this->assertNotEmpty($assert_session->waitForText('Unlimited Cardinality Image')); $media_items = Media::loadMultiple(); $added_media = array_pop($media_items); $added_media_name = $added_media->label(); - // Ensure the media item was saved to the library and automatically - // selected. The added media items should be in the first position of the - // add form. - $assert_session->pageTextContains('Add or select media'); - $assert_session->pageTextContains('Unlimited Cardinality Image'); $assert_session->fieldValueEquals('media_library_select_form[0]', $added_media->id()); $assert_session->checkboxChecked('media_library_select_form[0]'); // Assert the item that was selected before uploading the file is still @@ -1256,7 +1255,8 @@ public function testWidgetUpload() { // Assert the pre-selected items are shown. $selection_area = $assert_session->elementExists('css', '.media-library-add-form__selected-media'); $assert_session->elementExists('css', 'summary', $selection_area)->click(); - $assert_session->checkboxChecked("Select $existing_media_name", $selection_area); + $this->assertNotEmpty($checkbox = $assert_session->waitForField("Select $existing_media_name")); + $this->assertTrue($checkbox->isChecked()); $page->uncheckField("Select $existing_media_name"); $page->fillField('Alternative text', $this->randomString()); $assert_session->hiddenFieldValueEquals('current_selection', ''); @@ -1346,7 +1346,9 @@ public function testWidgetUpload() { $page->fillField('media[0][fields][field_media_test_image][0][alt]', $filenames[0]); $page->fillField('media[1][fields][field_media_test_image][0][alt]', $filenames[1]); // Assert the file is available in the file storage. - $this->assertCount(1, $file_storage->loadByProperties(['filename' => $filenames[1]])); + $files = $file_storage->loadByProperties(['filename' => $filenames[1]]); + $this->assertCount(1, $files); + $file_1_uri = reset($files)->getFileUri(); // Remove the second file and assert the focus is shifted to the container // of the next media item and field values are still correct. $page->pressButton('media-1-remove-button'); @@ -1354,9 +1356,10 @@ public function testWidgetUpload() { $assert_session->pageTextContains('The media item ' . $filenames[1] . ' has been removed.'); // Assert the file was deleted. $this->assertEmpty($file_storage->loadByProperties(['filename' => $filenames[1]])); + $this->assertFileNotExists($file_1_uri); // When a file is already in usage, it should not be deleted. To test, - // let's add a usage for the fourth file (now in the third position). + // let's add a usage for $filenames[3] (now in the third position). $files = $file_storage->loadByProperties(['filename' => $filenames[3]]); $target_file = reset($files); $this->assertCount(1, $files); @@ -1364,18 +1367,15 @@ public function testWidgetUpload() { 'bundle' => 'type_three', 'name' => 'Disturbing', 'field_media_test_image' => [ - [ - 'target_id' => $target_file->id(), - 'alt' => 'I find your lack of faith disturbing.', - ], + ['target_id' => $target_file->id()], ], ])->save(); - // Remove the fourth file (now in the third position) and assert the focus - // is shifted to the container of the previous media item and field values - // are still correct. + // Remove $filenames[3] (now in the third position) and assert the focus is + // shifted to the container of the previous media item and field values are + // still correct. $page->pressButton('media-3-remove-button'); - $this->assertJsCondition('jQuery(".media-library-add-form__media[data-media-library-added-delta=2]").is(":focus")'); $this->assertTrue($assert_session->waitForText('The media item ' . $filenames[3] . ' has been removed.')); + $this->assertJsCondition('jQuery(".media-library-add-form__media[data-media-library-added-delta=2]").is(":focus")'); // Assert the file was not deleted, due to being in use elsewhere. $this->assertNotEmpty($file_storage->loadByProperties(['filename' => $filenames[3]])); $this->assertFileExists($target_file->getFileUri());