function FileManagedFileElementTest::testFileRemovedFromDisk

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

Ensure a file entity can be saved when the file does not exist on disk.

File

core/modules/file/tests/src/Functional/FileManagedFileElementTest.php, line 184

Class

FileManagedFileElementTest
Tests the 'managed_file' element type.

Namespace

Drupal\Tests\file\Functional

Code

public function testFileRemovedFromDisk() {
    $this->drupalGet('file/test/1/0/1');
    $test_file = $this->getTestFile('text');
    $file_field_name = 'files[nested_file][]';
    $edit = [
        $file_field_name => \Drupal::service('file_system')->realpath($test_file->getFileUri()),
    ];
    $this->submitForm($edit, 'Upload');
    $this->submitForm([], 'Save');
    $fid = $this->getLastFileId();
    
    /** @var \Drupal\file\FileInterface $file */
    $file = $this->container
        ->get('entity_type.manager')
        ->getStorage('file')
        ->load($fid);
    $file->setPermanent();
    $file->save();
    $this->assertTrue(\Drupal::service('file_system')->delete($file->getFileUri()));
    $file->save();
    $this->assertTrue($file->isPermanent());
    $file->delete();
}

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