3 namespace Drupal\Tests\file\Kernel;
5 use Drupal\file\Entity\File;
8 * Tests the file delete function.
12 class DeleteTest extends FileManagedUnitTestBase {
14 * Tries deleting a normal file (as opposed to a directory, symlink, etc).
16 public function testUnused() {
17 $file = $this->createFile();
19 // Check that deletion removes the file and database record.
20 $this->assertTrue(is_file($file->getFileUri()), 'File exists.');
22 $this->assertFileHooksCalled(['delete']);
23 $this->assertFalse(file_exists($file->getFileUri()), 'Test file has actually been deleted.');
24 $this->assertFalse(File::load($file->id()), 'File was removed from the database.');
28 * Tries deleting a file that is in use.
30 public function testInUse() {
31 // This test expects unused managed files to be marked as a temporary file
32 // and then deleted up by file_cron().
33 $this->config('file.settings')
34 ->set('make_unused_managed_files_temporary', TRUE)
36 $file = $this->createFile();
37 $file_usage = $this->container->get('file.usage');
38 $file_usage->add($file, 'testing', 'test', 1);
39 $file_usage->add($file, 'testing', 'test', 1);
41 $file_usage->delete($file, 'testing', 'test', 1);
42 $usage = $file_usage->listUsage($file);
43 $this->assertEqual($usage['testing']['test'], [1 => 1], 'Test file is still in use.');
44 $this->assertTrue(file_exists($file->getFileUri()), 'File still exists on the disk.');
45 $this->assertTrue(File::load($file->id()), 'File still exists in the database.');
47 // Clear out the call to hook_file_load().
50 $file_usage->delete($file, 'testing', 'test', 1);
51 $usage = $file_usage->listUsage($file);
52 $this->assertFileHooksCalled(['load', 'update']);
53 $this->assertTrue(empty($usage), 'File usage data was removed.');
54 $this->assertTrue(file_exists($file->getFileUri()), 'File still exists on the disk.');
55 $file = File::load($file->id());
56 $this->assertTrue($file, 'File still exists in the database.');
57 $this->assertTrue($file->isTemporary(), 'File is temporary.');
60 // Call file_cron() to clean up the file. Make sure the changed timestamp
61 // of the file is older than the system.file.temporary_maximum_age
62 // configuration value.
63 db_update('file_managed')
65 'changed' => REQUEST_TIME - ($this->config('system.file')->get('temporary_maximum_age') + 1),
67 ->condition('fid', $file->id())
69 \Drupal::service('cron')->run();
72 $this->assertFileHooksCalled(['delete']);
73 $this->assertFalse(file_exists($file->getFileUri()), 'File has been deleted after its last usage was removed.');
74 $this->assertFalse(File::load($file->id()), 'File was removed from the database.');