View source
<?php
namespace Drupal\Tests\file\Functional;
use Drupal\Core\Database\Database;
use Drupal\file\Entity\File;
class FileFieldRevisionTest extends FileFieldTestBase {
protected $defaultTheme = 'stark';
public function testRevisions() {
$this
->config('file.settings')
->set('make_unused_managed_files_temporary', TRUE)
->save();
$node_storage = $this->container
->get('entity_type.manager')
->getStorage('node');
$type_name = 'article';
$field_name = strtolower($this
->randomMachineName());
$this
->createFileField($field_name, 'node', $type_name);
$this
->createFileField($field_name, 'user', 'user');
$test_file = $this
->getTestFile('text');
$nid = $this
->uploadNodeFile($test_file, $field_name, $type_name);
$node_storage
->resetCache([
$nid,
]);
$node = $node_storage
->load($nid);
$node_file_r1 = File::load($node->{$field_name}->target_id);
$node_vid_r1 = $node
->getRevisionId();
$this
->assertFileExists($node_file_r1
->getFileUri());
$this
->assertFileEntryExists($node_file_r1, 'File entry exists in database on node creation.');
$this
->assertFileIsPermanent($node_file_r1, 'File is permanent.');
$this
->replaceNodeFile($test_file, $field_name, $nid);
$node_storage
->resetCache([
$nid,
]);
$node = $node_storage
->load($nid);
$node_file_r2 = File::load($node->{$field_name}->target_id);
$node_vid_r2 = $node
->getRevisionId();
$this
->assertFileExists($node_file_r2
->getFileUri());
$this
->assertFileEntryExists($node_file_r2, 'Replacement file entry exists in database after creating new revision.');
$this
->assertFileIsPermanent($node_file_r2, 'Replacement file is permanent.');
$node = node_revision_load($node_vid_r1);
$current_file = File::load($node->{$field_name}->target_id);
$this
->assertEqual($node_file_r1
->id(), $current_file
->id(), 'Original file still in place after replacing file in new revision.');
$this
->assertFileExists($node_file_r1
->getFileUri());
$this
->assertFileEntryExists($node_file_r1, 'Original file entry still in place after replacing file in new revision');
$this
->assertFileIsPermanent($node_file_r1, 'Original file is still permanent.');
$this
->drupalPostForm('node/' . $nid . '/edit', [
'revision' => '1',
], t('Save'));
$node_storage
->resetCache([
$nid,
]);
$node = $node_storage
->load($nid);
$node_file_r3 = File::load($node->{$field_name}->target_id);
$node_vid_r3 = $node
->getRevisionId();
$this
->assertEqual($node_file_r2
->id(), $node_file_r3
->id(), 'Previous revision file still in place after creating a new revision without a new file.');
$this
->assertFileIsPermanent($node_file_r3, 'New revision file is permanent.');
$this
->drupalPostForm('node/' . $nid . '/revisions/' . $node_vid_r1 . '/revert', [], t('Revert'));
$node_storage
->resetCache([
$nid,
]);
$node = $node_storage
->load($nid);
$node_file_r4 = File::load($node->{$field_name}->target_id);
$this
->assertEqual($node_file_r1
->id(), $node_file_r4
->id(), 'Original revision file still in place after reverting to the original revision.');
$this
->assertFileIsPermanent($node_file_r4, 'Original revision file still permanent after reverting to the original revision.');
$this
->drupalPostForm('node/' . $nid . '/revisions/' . $node_vid_r2 . '/delete', [], t('Delete'));
$this
->assertFileExists($node_file_r3
->getFileUri());
$this
->assertFileEntryExists($node_file_r3, 'Second file entry is still available after deleting second revision, since it is being used by the third revision.');
$this
->assertFileIsPermanent($node_file_r3, 'Second file entry is still permanent after deleting second revision, since it is being used by the third revision.');
$user = $this
->drupalCreateUser();
$user->{$field_name}->target_id = $node_file_r3
->id();
$user->{$field_name}->display = 1;
$user
->save();
$this
->drupalGet('user/' . $user
->id() . '/edit');
$this
->drupalPostForm('node/' . $nid . '/revisions/' . $node_vid_r3 . '/delete', [], t('Delete'));
$this
->assertFileExists($node_file_r3
->getFileUri());
$this
->assertFileEntryExists($node_file_r3, 'Second file entry is still available after deleting third revision, since it is being used by the user.');
$this
->assertFileIsPermanent($node_file_r3, 'Second file entry is still permanent after deleting third revision, since it is being used by the user.');
$user
->delete();
clearstatcache($node_file_r1
->getFileUri());
clearstatcache($node_file_r2
->getFileUri());
clearstatcache($node_file_r3
->getFileUri());
clearstatcache($node_file_r4
->getFileUri());
$connection = Database::getConnection();
$connection
->update('file_managed')
->fields([
'changed' => REQUEST_TIME - ($this
->config('system.file')
->get('temporary_maximum_age') + 1),
])
->condition('fid', $node_file_r3
->id())
->execute();
\Drupal::service('cron')
->run();
$this
->assertFileNotExists($node_file_r3
->getFileUri());
$this
->assertFileEntryNotExists($node_file_r3, 'Second file entry is now deleted after deleting third revision, since it is no longer being used by any other nodes.');
$this
->drupalPostForm('node/' . $nid . '/delete', [], t('Delete'));
$connection
->update('file_managed')
->fields([
'changed' => REQUEST_TIME - ($this
->config('system.file')
->get('temporary_maximum_age') + 1),
])
->condition('fid', $node_file_r1
->id())
->execute();
\Drupal::service('cron')
->run();
$this
->assertFileNotExists($node_file_r1
->getFileUri());
$this
->assertFileEntryNotExists($node_file_r1, 'Original file entry is deleted after deleting the entire node with two revisions remaining.');
}
}