function SaveUploadFormTest::testDrupalMovingUploadedFileError

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

Tests for log entry on failing destination.

File

core/modules/file/tests/src/Functional/SaveUploadFormTest.php, line 464

Class

SaveUploadFormTest
Tests the <a href="/api/drupal/core%21modules%21file%21file.module/function/_file_save_upload_from_form/11.x" title="Saves form file uploads." class="local">_file_save_upload_from_form</a>() function.

Namespace

Drupal\Tests\file\Functional

Code

public function testDrupalMovingUploadedFileError() : void {
    // Create a directory and make it not writable.
    $test_directory = 'test_drupal_move_uploaded_file_fail';
    \Drupal::service('file_system')->mkdir('temporary://' . $test_directory, 00);
    $this->assertDirectoryExists('temporary://' . $test_directory);
    
    /** @var \Drupal\Core\File\FileSystemInterface $file_system */
    $file_system = \Drupal::service('file_system');
    $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->drupalGet('file-test/save_upload_from_form_test');
    $this->submitForm($edit, 'Submit');
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertSession()
        ->pageTextContains('File upload error. Could not move uploaded file.');
    $this->assertSession()
        ->pageTextContains("Epic upload FAIL!");
    // Uploading failed. Now check the log.
    $this->drupalGet('admin/reports/dblog');
    $this->assertSession()
        ->statusCodeEquals(200);
    // The full log message is in the title attribute of the link, so we cannot
    // use ::pageTextContains() here.
    $destination = 'temporary://' . $test_directory . '/' . $this->image
        ->getFilename();
    $this->assertSession()
        ->responseContains("Upload error. Could not move uploaded file {$this->image->getFilename()} to destination {$destination}.");
}

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