Skip to content

Commit 5d400cc

Browse files
committed
Issue #3032376 by alexpott, dww, Berdir: Files renamed by \_file_save_upload_single() do not have the correct filename on the File entity
(cherry picked from commit 9431a1c)
1 parent 59c282e commit 5d400cc

File tree

4 files changed

+11
-4
lines changed

4 files changed

+11
-4
lines changed

modules/file/file.module

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,10 @@ function _file_save_upload_single(\SplFileInfo $file_info, $form_field_name, $va
10761076
}
10771077
}
10781078

1079+
// Update the filename with any changes as a result of security or renaming
1080+
// due to an existing file.
1081+
$file->setFilename(\Drupal::service('file_system')->basename($file->destination));
1082+
10791083
// If we made it this far it's safe to record this file in the database.
10801084
$file->save();
10811085

modules/file/src/Tests/FileFieldWidgetTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ public function testMultiValuedWidget() {
259259
'%field' => $field_name,
260260
'@max' => $cardinality,
261261
'@count' => count($upload_files_node_creation) + count($upload_files_node_revision),
262-
'%list' => implode(', ', array_fill(0, 3, $test_file->getFilename())),
262+
'%list' => implode(', ', ['text-0_2.txt', 'text-0_3.txt', 'text-0_4.txt']),
263263
];
264264
$this->assertRaw(t('Field %field can only hold @max values but there were @count uploaded. The following files have been omitted as a result: %list.', $args));
265265
$node_storage->resetCache([$nid]);
@@ -291,7 +291,7 @@ public function testMultiValuedWidget() {
291291
'%field' => $field_name,
292292
'@max' => $cardinality,
293293
'@count' => count($upload_files),
294-
'%list' => $test_file->getFileName(),
294+
'%list' => 'text-0_12.txt',
295295
];
296296
$this->assertRaw(t('Field %field can only hold @max values but there were @count uploaded. The following files have been omitted as a result: %list.', $args));
297297
}

modules/file/tests/src/Functional/SaveUploadTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ public function testHandleDangerousFile() {
206206
$this->assertResponse(200, 'Received a 200 response for posted test file.');
207207
$message = t('For security reasons, your upload has been renamed to') . ' <em class="placeholder">' . $this->phpfile->filename . '.txt' . '</em>';
208208
$this->assertRaw($message, 'Dangerous file was renamed.');
209+
$this->assertSession()->pageTextContains('File name is php-2.php.txt.');
209210
$this->assertRaw(t('File MIME type is text/plain.'), "Dangerous file's MIME type was changed.");
210211
$this->assertRaw(t('You WIN!'), 'Found the success message.');
211212

@@ -221,7 +222,7 @@ public function testHandleDangerousFile() {
221222
$this->drupalPostForm('file-test/upload', $edit, t('Submit'));
222223
$this->assertResponse(200, 'Received a 200 response for posted test file.');
223224
$this->assertNoRaw(t('For security reasons, your upload has been renamed'), 'Found no security message.');
224-
$this->assertRaw(t('File name is @filename', ['@filename' => $this->phpfile->filename]), 'Dangerous file was not renamed when insecure uploads is TRUE.');
225+
$this->assertSession()->pageTextContains('File name is php-2.php.');
225226
$this->assertRaw(t('You WIN!'), 'Found the success message.');
226227

227228
// Check that the correct hooks were called.
@@ -291,6 +292,7 @@ public function testExistingRename() {
291292
$this->drupalPostForm('file-test/upload', $edit, t('Submit'));
292293
$this->assertResponse(200, 'Received a 200 response for posted test file.');
293294
$this->assertRaw(t('You WIN!'), 'Found the success message.');
295+
$this->assertSession()->pageTextContains('File name is image-test_0.png.');
294296

295297
// Check that the correct hooks were called.
296298
$this->assertFileHooksCalled(['validate', 'insert']);
@@ -307,6 +309,7 @@ public function testExistingReplace() {
307309
$this->drupalPostForm('file-test/upload', $edit, t('Submit'));
308310
$this->assertResponse(200, 'Received a 200 response for posted test file.');
309311
$this->assertRaw(t('You WIN!'), 'Found the success message.');
312+
$this->assertSession()->pageTextContains('File name is image-test.png.');
310313

311314
// Check that the correct hooks were called.
312315
$this->assertFileHooksCalled(['validate', 'load', 'update']);

modules/layout_builder/tests/src/FunctionalJavascript/InlineBlockPrivateFilesTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public function testPrivateFiles() {
139139
$assert_session->pageTextContains('You are not authorized to access this page');
140140

141141
$this->drupalGet('node/2/layout');
142-
$file4 = $this->createPrivateFile('drupal.txt');
142+
$file4 = $this->createPrivateFile('drupal_4.txt');
143143
$this->addInlineFileBlockToLayout('The file', $file4);
144144
$this->assertSaveLayout();
145145

0 commit comments

Comments
 (0)