function SaveUploadTest::testDrupalMovingUploadedFileError

Same name in other branches
  1. 9 core/modules/file/tests/src/Functional/SaveUploadTest.php \Drupal\Tests\file\Functional\SaveUploadTest::testDrupalMovingUploadedFileError()
  2. 10 core/modules/file/tests/src/Functional/SaveUploadTest.php \Drupal\Tests\file\Functional\SaveUploadTest::testDrupalMovingUploadedFileError()
  3. 11.x core/modules/file/tests/src/Functional/SaveUploadTest.php \Drupal\Tests\file\Functional\SaveUploadTest::testDrupalMovingUploadedFileError()

Tests for log entry on failing destination.

File

core/modules/file/tests/src/Functional/SaveUploadTest.php, line 537

Class

SaveUploadTest
Tests the file_save_upload() function.

Namespace

Drupal\Tests\file\Functional

Code

public function testDrupalMovingUploadedFileError() {
    // Create a directory and make it not writable.
    $test_directory = 'test_drupal_move_uploaded_file_fail';
    
    /** @var \Drupal\Core\File\FileSystemInterface $file_system */
    $file_system = \Drupal::service('file_system');
    $file_system->mkdir('temporary://' . $test_directory, 00);
    $this->assertDirectoryExists('temporary://' . $test_directory);
    $edit = [
        'file_subdir' => $test_directory,
        'files[file_test_upload]' => $file_system->realpath($this->image
            ->getFileUri()),
    ];
    \Drupal::state()->set('file_test.disable_error_collection', TRUE);
    $this->drupalPostForm('file-test/upload', $edit, t('Submit'));
    $this->assertSession()
        ->statusCodeEquals(200);
    $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->assertSession()
        ->statusCodeEquals(200);
    $this->assertRaw(t('Upload error. Could not move uploaded file @file to destination @destination.', [
        '@file' => $this->image
            ->getFilename(),
        '@destination' => 'temporary://' . $test_directory . '/' . $this->image
            ->getFilename(),
    ]), 'Found upload error log entry.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.