summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2015-01-22 15:11:19 (GMT)
committerAlex Pott2015-01-22 15:11:19 (GMT)
commit0c1276b68fb09216f085ed5086f7e3609dc2bc85 (patch)
tree681c2e784486ca0cdc25e99d8ee2a4297cc92200
parentcc9d0f9646bd236e1ce642107d403018369db962 (diff)
Issue #2155245 by ChristianAdamski, scor, areke: Use proper methods instead of directly calling $file->filename and $file->uri
-rw-r--r--core/modules/file/file.module4
-rw-r--r--core/modules/file/src/Tests/SaveUploadTest.php38
-rw-r--r--core/modules/file/tests/file_test/src/Form/FileTestForm.php7
3 files changed, 46 insertions, 3 deletions
diff --git a/core/modules/file/file.module b/core/modules/file/file.module
index e90a593..6545301 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 3b34f91..032a3d4 100644
--- a/core/modules/file/src/Tests/SaveUploadTest.php
+++ b/core/modules/file/src/Tests/SaveUploadTest.php
@@ -14,6 +14,13 @@ namespace Drupal\file\Tests;
*/
class SaveUploadTest extends FileManagedTestBase {
/**
+ * Modules to enable.
+ *
+ * @var array
+ */
+ public static $modules = array('dblog');
+
+ /**
* An image file path for uploading.
*/
protected $image;
@@ -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 @@ class SaveUploadTest extends FileManagedTestBase {
$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 bdefad4..f903bf0 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 @@ class FileTestForm implements FormInterface {
$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);