Issue #2660528 by swentel, david_garcia: File entity completely broken if the underlying file is gone

8.2.x
Alex Pott 2016-05-08 14:21:46 -05:00
parent e65accdf9d
commit 8ae898a87f
2 changed files with 28 additions and 1 deletions

View File

@ -186,7 +186,11 @@ class File extends ContentEntityBase implements FileInterface {
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
$this->setSize(filesize($this->getFileUri()));
// The file itself might not exist or be available right now.
$uri = $this->getFileUri();
if ($size = @filesize($uri)) {
$this->setSize($size);
}
}
/**

View File

@ -171,4 +171,27 @@ class FileManagedFileElementTest extends FileFieldTestBase {
$this->assertRaw('The file referenced by the Managed <em>file &amp; butter</em> field does not exist.');
}
/**
* Ensure a file entity can be saved when the file does not exist on disk.
*/
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_realpath($test_file->getFileUri())];
$this->drupalPostForm(NULL, $edit, t('Upload'));
$this->drupalPostForm(NULL, array(), t('Save'));
$fid = $this->getLastFileId();
/** @var $file \Drupal\file\FileInterface */
$file = $this->container->get('entity_type.manager')->getStorage('file')->load($fid);
$file->setPermanent();
$file->save();
$this->assertTrue(file_unmanaged_delete($file->getFileUri()));
$file->save();
$this->assertTrue($file->isPermanent());
$file->delete();
}
}