Issue #2155245 by ChristianAdamski, scor, areke: Use proper methods instead of directly calling $file->filename and $file->uri

8.0.x
Alex Pott 2015-01-22 15:11:19 +00:00
parent cc9d0f9646
commit 0c1276b68f
3 changed files with 46 additions and 3 deletions

View File

@ -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 // Move uploaded files from PHP's upload_tmp_dir to Drupal's temporary
// directory. This overcomes open_basedir restrictions for future file // directory. This overcomes open_basedir restrictions for future file
// operations. // operations.
$file->uri = $file->destination; $file->setFileUri($file->destination);
if (!drupal_move_uploaded_file($file_info->getRealPath(), $file->getFileUri())) { if (!drupal_move_uploaded_file($file_info->getRealPath(), $file->getFileUri())) {
drupal_set_message(t('File upload error. Could not move uploaded file.'), 'error'); 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; $files[$i] = FALSE;
continue; continue;
} }

View File

@ -13,6 +13,13 @@ namespace Drupal\file\Tests;
* @group file * @group file
*/ */
class SaveUploadTest extends FileManagedTestBase { class SaveUploadTest extends FileManagedTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('dblog');
/** /**
* An image file path for uploading. * An image file path for uploading.
*/ */
@ -30,7 +37,7 @@ class SaveUploadTest extends FileManagedTestBase {
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
$account = $this->drupalCreateUser(); $account = $this->drupalCreateUser(array('access site reports'));
$this->drupalLogin($account); $this->drupalLogin($account);
$image_files = $this->drupalGetTestFiles('image'); $image_files = $this->drupalGetTestFiles('image');
@ -316,4 +323,33 @@ class SaveUploadTest extends FileManagedTestBase {
$this->drupalPostForm('file-test/upload', array(), t('Submit')); $this->drupalPostForm('file-test/upload', array(), t('Submit'));
$this->assertNoRaw(t('Epic upload FAIL!'), 'Failure message not found.'); $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.');
}
} }

View File

@ -102,6 +102,13 @@ class FileTestForm implements FormInterface {
$validators['file_validate_extensions'] = array($form_state->getValue('extensions')); $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')); $file = file_save_upload('file_test_upload', $validators, $destination, 0, $form_state->getValue('file_test_replace'));
if ($file) { if ($file) {
$form_state->setValue('file_test_upload', $file); $form_state->setValue('file_test_upload', $file);