9ab4c51e0de07139609d67242f05642db820401b
[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   /**
15    * Tries deleting a normal file (as opposed to a directory, symlink, etc).
16    */
17   public function testUnused() {
18     $file = $this->createFile();
19
20     // Check that deletion removes the file and database record.
21     $this->assertTrue(is_file($file->getFileUri()), 'File exists.');
22     $file->delete();
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.');
26   }
27
28   /**
29    * Tries deleting a file that is in use.
30    */
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)
36       ->save();
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);
41
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.');
47
48     // Clear out the call to hook_file_load().
49     file_test_reset();
50
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.');
59     file_test_reset();
60
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')
65       ->fields([
66         'changed' => REQUEST_TIME - ($this->config('system.file')->get('temporary_maximum_age') + 1),
67       ])
68       ->condition('fid', $file->id())
69       ->execute();
70     \Drupal::service('cron')->run();
71
72     // file_cron() loads
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.');
76   }
77
78 }