Security update for Core, with self-updated composer
[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     // 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)
35       ->save();
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);
40
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.');
46
47     // Clear out the call to hook_file_load().
48     file_test_reset();
49
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.');
58     file_test_reset();
59
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')
64       ->fields([
65         'changed' => REQUEST_TIME - ($this->config('system.file')->get('temporary_maximum_age') + 1),
66       ])
67       ->condition('fid', $file->id())
68       ->execute();
69     \Drupal::service('cron')->run();
70
71     // file_cron() loads
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.');
75   }
76
77 }