summaryrefslogtreecommitdiffstatshomepage
path: root/core/modules/file/tests/src/Kernel/DeleteTest.php
blob: 95139ef26f908724f5b8e28df8923943cd126359 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php

declare(strict_types=1);

namespace Drupal\Tests\file\Kernel;

use Drupal\Core\Database\Database;
use Drupal\file\Entity\File;
use Drupal\file_test\FileTestHelper;

/**
 * Tests the file delete function.
 *
 * @group file
 */
class DeleteTest extends FileManagedUnitTestBase {

  /**
   * Tries deleting a normal file (as opposed to a directory, symlink, etc).
   */
  public function testUnused(): void {
    $file = $this->createFile();

    // Check that deletion removes the file and database record.
    $this->assertFileExists($file->getFileUri());
    $file->delete();
    $this->assertFileHooksCalled(['delete']);
    $this->assertFileDoesNotExist($file->getFileUri());
    $this->assertNull(File::load($file->id()), 'File was removed from the database.');
  }

  /**
   * Tries deleting a file that is in use.
   */
  public function testInUse(): void {
    // This test expects unused managed files to be marked as a temporary file
    // and then deleted up by file_cron().
    $this->config('file.settings')
      ->set('make_unused_managed_files_temporary', TRUE)
      ->save();
    $file = $this->createFile();
    $file_usage = $this->container->get('file.usage');
    $file_usage->add($file, 'testing', 'test', 1);
    $file_usage->add($file, 'testing', 'test', 1);

    $file_usage->delete($file, 'testing', 'test', 1);
    $usage = $file_usage->listUsage($file);
    $this->assertEquals([1 => 1], $usage['testing']['test'], 'Test file is still in use.');
    $this->assertFileExists($file->getFileUri());
    $this->assertNotEmpty(File::load($file->id()), 'File still exists in the database.');

    // Clear out the call to hook_file_load().
    FileTestHelper::reset();

    $file_usage->delete($file, 'testing', 'test', 1);
    $usage = $file_usage->listUsage($file);
    $this->assertFileHooksCalled(['load', 'update']);
    $this->assertEmpty($usage, 'File usage data was removed.');
    $this->assertFileExists($file->getFileUri());
    $file = File::load($file->id());
    $this->assertNotEmpty($file, 'File still exists in the database.');
    $this->assertTrue($file->isTemporary(), 'File is temporary.');
    FileTestHelper::reset();

    // Call file_cron() to clean up the file. Make sure the changed timestamp
    // of the file is older than the system.file.temporary_maximum_age
    // configuration value. We use an UPDATE statement because using the API
    // would set the timestamp.
    Database::getConnection()->update('file_managed')
      ->fields([
        'changed' => \Drupal::time()->getRequestTime() - ($this->config('system.file')->get('temporary_maximum_age') + 3),
      ])
      ->condition('fid', $file->id())
      ->execute();
    \Drupal::service('cron')->run();

    // file_cron() loads
    $this->assertFileHooksCalled(['delete']);
    $this->assertFileDoesNotExist($file->getFileUri());
    $this->assertNull(File::load($file->id()), 'File was removed from the database.');
  }

  /**
   * Tries to run cron deletion on file deleted from the file-system.
   */
  public function testCronDeleteNonExistingTemporary(): void {
    $file = $this->createFile();
    // Delete the file, but leave it in the file_managed table.
    \Drupal::service('file_system')->delete($file->getFileUri());
    $this->assertFileDoesNotExist($file->getFileUri());
    $this->assertInstanceOf(File::class, File::load($file->id()));

    // Call file_cron() to clean up the file. Make sure the changed timestamp
    // of the file is older than the system.file.temporary_maximum_age
    // configuration value.
    \Drupal::database()->update('file_managed')
      ->fields([
        'changed' => \Drupal::time()->getRequestTime() - ($this->config('system.file')->get('temporary_maximum_age') + 3),
      ])
      ->condition('fid', $file->id())
      ->execute();
    \Drupal::service('cron')->run();

    $this->assertNull(File::load($file->id()), 'File was removed from the database.');
  }

}