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 $file = $this->createFile();
32 $file_usage = $this->container->get('file.usage');
33 $file_usage->add($file, 'testing', 'test', 1);
34 $file_usage->add($file, 'testing', 'test', 1);
36 $file_usage->delete($file, 'testing', 'test', 1);
37 $usage = $file_usage->listUsage($file);
38 $this->assertEqual($usage['testing']['test'], [1 => 1], 'Test file is still in use.');
39 $this->assertTrue(file_exists($file->getFileUri()), 'File still exists on the disk.');
40 $this->assertTrue(File::load($file->id()), 'File still exists in the database.');
42 // Clear out the call to hook_file_load().
45 $file_usage->delete($file, 'testing', 'test', 1);
46 $usage = $file_usage->listUsage($file);
47 $this->assertFileHooksCalled(['load', 'update']);
48 $this->assertTrue(empty($usage), 'File usage data was removed.');
49 $this->assertTrue(file_exists($file->getFileUri()), 'File still exists on the disk.');
50 $file = File::load($file->id());
51 $this->assertTrue($file, 'File still exists in the database.');
52 $this->assertTrue($file->isTemporary(), 'File is temporary.');
55 // Call file_cron() to clean up the file. Make sure the changed timestamp
56 // of the file is older than the system.file.temporary_maximum_age
57 // configuration value.
58 db_update('file_managed')
60 'changed' => REQUEST_TIME - ($this->config('system.file')->get('temporary_maximum_age') + 1),
62 ->condition('fid', $file->id())
64 \Drupal::service('cron')->run();
67 $this->assertFileHooksCalled(['delete']);
68 $this->assertFalse(file_exists($file->getFileUri()), 'File has been deleted after its last usage was removed.');
69 $this->assertFalse(File::load($file->id()), 'File was removed from the database.');