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