Commit 0c1276b6 authored by alexpott's avatar alexpott

Issue #2155245 by ChristianAdamski, scor, areke: Use proper methods instead of...

Issue #2155245 by ChristianAdamski, scor, areke: Use proper methods instead of directly calling $file->filename and $file->uri
parent cc9d0f96
......@@ -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;
}
......
......@@ -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.');
}
}
......@@ -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);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment