diff --git a/core/modules/file/file.module b/core/modules/file/file.module index e90a5933ff94530ea399e8cb339341d835658de9..654530165cf340fc246736360864737153198b62 100644 --- a/core/modules/file/file.module +++ b/core/modules/file/file.module @@ -856,10 +856,10 @@ function file_save_upload($form_field_name, $validators = array(), $destination // Move uploaded files from PHP's upload_tmp_dir to Drupal's temporary // directory. This overcomes open_basedir restrictions for future file // operations. - $file->uri = $file->destination; + $file->setFileUri($file->destination); if (!drupal_move_uploaded_file($file_info->getRealPath(), $file->getFileUri())) { drupal_set_message(t('File upload error. Could not move uploaded file.'), 'error'); - \Drupal::logger('file')->notice('Upload error. Could not move uploaded file %file to destination %destination.', array('%file' => $file->filename, '%destination' => $file->uri)); + \Drupal::logger('file')->notice('Upload error. Could not move uploaded file %file to destination %destination.', array('%file' => $file->getFilename(), '%destination' => $file->getFileUri())); $files[$i] = FALSE; continue; } diff --git a/core/modules/file/src/Tests/SaveUploadTest.php b/core/modules/file/src/Tests/SaveUploadTest.php index 3b34f91dd6deff42e688be6fe5a94626bcf746ec..032a3d4d8538f75d57043c79c64f005a01a065e6 100644 --- a/core/modules/file/src/Tests/SaveUploadTest.php +++ b/core/modules/file/src/Tests/SaveUploadTest.php @@ -13,6 +13,13 @@ * @group file */ class SaveUploadTest extends FileManagedTestBase { + /** + * Modules to enable. + * + * @var array + */ + public static $modules = array('dblog'); + /** * An image file path for uploading. */ @@ -30,7 +37,7 @@ class SaveUploadTest extends FileManagedTestBase { protected function setUp() { parent::setUp(); - $account = $this->drupalCreateUser(); + $account = $this->drupalCreateUser(array('access site reports')); $this->drupalLogin($account); $image_files = $this->drupalGetTestFiles('image'); @@ -316,4 +323,33 @@ function testNoUpload() { $this->drupalPostForm('file-test/upload', array(), t('Submit')); $this->assertNoRaw(t('Epic upload FAIL!'), 'Failure message not found.'); } + + /** + * Tests for log entry on failing destination. + */ + function testDrupalMovingUploadedFileError() { + // Create a directory and make it not writable. + $test_directory = 'test_drupal_move_uploaded_file_fail'; + drupal_mkdir('temporary://' . $test_directory, 0000); + $this->assertTrue(is_dir('temporary://' . $test_directory)); + + $edit = array( + 'file_subdir' => $test_directory, + 'files[file_test_upload]' => drupal_realpath($this->image->getFileUri()) + ); + + \Drupal::state()->set('file_test.disable_error_collection', TRUE); + $this->drupalPostForm('file-test/upload', $edit, t('Submit')); + $this->assertResponse(200, 'Received a 200 response for posted test file.'); + $this->assertRaw(t('File upload error. Could not move uploaded file.'), 'Found the failure message.'); + $this->assertRaw(t('Epic upload FAIL!'), 'Found the failure message.'); + + // Uploading failed. Now check the log. + $this->drupalGet('admin/reports/dblog'); + $this->assertResponse(200); + $this->assertRaw(t('Upload error. Could not move uploaded file @file to destination @destination.', array( + '@file' => $this->image->getFilename(), + '@destination' => 'temporary://' . $test_directory . '/' . $this->image->getFilename() + )), 'Found upload error log entry.'); + } } diff --git a/core/modules/file/tests/file_test/src/Form/FileTestForm.php b/core/modules/file/tests/file_test/src/Form/FileTestForm.php index bdefad46673322fc00ead8ebdb3439a389603854..f903bf0e45bc1853d767919bcaa894205c469191 100644 --- a/core/modules/file/tests/file_test/src/Form/FileTestForm.php +++ b/core/modules/file/tests/file_test/src/Form/FileTestForm.php @@ -102,6 +102,13 @@ public function submitForm(array &$form, FormStateInterface $form_state) { $validators['file_validate_extensions'] = array($form_state->getValue('extensions')); } + // The test for drupal_move_uploaded_file() triggering a warning is + // unavoidable. We're interested in what happens afterwards in + // file_save_upload(). + if (\Drupal::state()->get('file_test.disable_error_collection')) { + define('SIMPLETEST_COLLECT_ERRORS', FALSE); + } + $file = file_save_upload('file_test_upload', $validators, $destination, 0, $form_state->getValue('file_test_replace')); if ($file) { $form_state->setValue('file_test_upload', $file);