diff --git a/core/modules/media_library/src/Form/FileUploadForm.php b/core/modules/media_library/src/Form/FileUploadForm.php
index 3062a184a1..ec7362fc63 100644
--- a/core/modules/media_library/src/Form/FileUploadForm.php
+++ b/core/modules/media_library/src/Form/FileUploadForm.php
@@ -85,7 +85,7 @@ public function __construct(EntityTypeManagerInterface $entity_type_manager, Med
$this->renderer = $renderer;
$this->fileSystem = $file_system;
if (!$file_usage) {
- @trigger_error('Calling FileUploadForm::__construct() without the file.usage service is deprecated in drupal:8.8.0 and the file usage service will be a required argument in drupal:9.0.0. See https://www.drupal.org/node/3075165', E_USER_DEPRECATED);
+ @trigger_error('Calling FileUploadForm::__construct() without the `file.usage` service is deprecated in drupal:8.8.0 and is removed in drupal:9.0.0. Pass the `file.usage` service to the constructor. See https://www.drupal.org/node/3075165', E_USER_DEPRECATED);
$file_usage = \Drupal::service('file.usage');
}
$this->fileUsage = $file_usage;
diff --git a/core/modules/media_library/tests/src/FunctionalJavascript/MediaLibraryTest.php b/core/modules/media_library/tests/src/FunctionalJavascript/MediaLibraryTest.php
index 83d867ad3f..88ec318286 100644
--- a/core/modules/media_library/tests/src/FunctionalJavascript/MediaLibraryTest.php
+++ b/core/modules/media_library/tests/src/FunctionalJavascript/MediaLibraryTest.php
@@ -237,8 +237,8 @@ public function testWidgetWithoutMediaTypes() {
$field_empty_types_message = 'There are no allowed media types configured for this field. Edit the field settings to select the allowed media types.';
$field_null_types_url = new Url('entity.field_config.node_field_edit_form', [
- 'field_config' => 'node.basic_page.field_null_types_media',
- ] + $route_bundle_params);
+ 'field_config' => 'node.basic_page.field_null_types_media',
+ ] + $route_bundle_params);
$field_null_types_message = 'There are no allowed media types configured for this field. Edit the field settings to select the allowed media types.';
// Visit a node create page.
@@ -1133,7 +1133,7 @@ public function testWidgetUpload() {
$assert_session->assertWaitOnAjaxRequest();
$png_uri_2 = $file_system->copy($png_image->uri, 'public://');
$page->attachFileToField('Add files', $this->container->get('file_system')->realpath($png_uri_2));
- $assert_session->assertWaitOnAjaxRequest();
+ $this->assertNotEmpty($assert_session->waitforButton('Remove'));
$page->fillField('Alternative text', $this->randomString());
$assert_session->elementExists('css', '.ui-dialog-buttonpane')->pressButton('Save and insert');
$this->assertNotEmpty($assert_session->waitForText('Added one media item.'));
@@ -1320,7 +1320,7 @@ public function testWidgetUpload() {
// Create a list of new files to upload.
$filenames = [];
$remote_paths = [];
- foreach (range(1, 3) as $i) {
+ foreach (range(1, 4) as $i) {
$path = $file_system->copy($png_image->uri, 'public://');
$filenames[] = $file_system->basename($path);
$remote_paths[] = $driver->uploadFileAndGetRemoteFilePath($file_system->realpath($path));
@@ -1335,12 +1335,14 @@ public function testWidgetUpload() {
$assert_session->fieldValueEquals('media[0][fields][name][0][value]', $filenames[0]);
$assert_session->fieldValueEquals('media[1][fields][name][0][value]', $filenames[1]);
$assert_session->fieldValueEquals('media[2][fields][name][0][value]', $filenames[2]);
+ $assert_session->fieldValueEquals('media[3][fields][name][0][value]', $filenames[3]);
// Assert the pre-selected items are shown.
- $selection_area = $assert_session->elementExists('css', '.media-library-add-form__selected-media');
+ $this->assertNotEmpty($selection_area = $assert_session->waitForElement('css', '.media-library-add-form__selected-media'));
$assert_session->elementExists('css', 'summary', $selection_area)->click();
$assert_session->checkboxChecked("Select $existing_media_name", $selection_area);
- // Set alt texts for items 1 and 2, leave the alt text empty for item 3 to
- // assert the field validation does not stop users from removing items.
+ // Set alt texts for items 1 and 2, leave the alt text empty for items 3
+ // and 4 to assert the field validation does not stop users from removing
+ // items.
$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.
@@ -1352,6 +1354,31 @@ 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]]));
+
+ // When a file is already in usage, it should not be deleted. To test,
+ // let's add a usage for the second file.
+ $files = $file_storage->loadByProperties(['filename' => $filenames[3]]);
+ $target_file = reset($files);
+ $this->assertCount(1, $files);
+ Media::create([
+ 'bundle' => 'type_three',
+ 'name' => 'Disturbing',
+ 'field_media_test_image' => [
+ [
+ 'target_id' => $target_file->id(),
+ 'alt' => 'I find your lack of faith disturbing.',
+ 'title' => 'I find your lack of faith disturbing.',
+ ],
+ ],
+ ])->save();
+ // 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-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.'));
+ // Assert the file was not deleted, due to being in use elsewhere.
+ $this->assertNotEmpty($file_storage->loadByProperties(['filename' => $filenames[3]]));
+
// The second media item should be removed (this has the delta 1 since we
// start counting from 0).
$assert_session->elementNotExists('css', '.media-library-add-form__media[data-media-library-added-delta=1]');