Version 1
[yaffs-website] / web / core / modules / file / tests / src / Kernel / DeleteTest.php
1 <?php
2
3 namespace Drupal\Tests\file\Kernel;
4
5 use Drupal\file\Entity\File;
6
7 /**
8  * Tests the file delete function.
9  *
10  * @group file
11  */
12 class DeleteTest extends FileManagedUnitTestBase {
13   /**
14    * Tries deleting a normal file (as opposed to a directory, symlink, etc).
15    */
16   public function testUnused() {
17     $file = $this->createFile();
18
19     // Check that deletion removes the file and database record.
20     $this->assertTrue(is_file($file->getFileUri()), 'File exists.');
21     $file->delete();
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.');
25   }
26
27   /**
28    * Tries deleting a file that is in use.
29    */
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);
35
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.');
41
42     // Clear out the call to hook_file_load().
43     file_test_reset();
44
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.');
53     file_test_reset();
54
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')
59       ->fields([
60         'changed' => REQUEST_TIME - ($this->config('system.file')->get('temporary_maximum_age') + 1),
61       ])
62       ->condition('fid', $file->id())
63       ->execute();
64     \Drupal::service('cron')->run();
65
66     // file_cron() loads
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.');
70   }
71
72 }