You are here

public function DeleteTest::testCronDeleteNonExistingTemporary in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/file/tests/src/Kernel/DeleteTest.php \Drupal\Tests\file\Kernel\DeleteTest::testCronDeleteNonExistingTemporary()
  2. 10 core/modules/file/tests/src/Kernel/DeleteTest.php \Drupal\Tests\file\Kernel\DeleteTest::testCronDeleteNonExistingTemporary()

Tries to run cron deletion on file deleted from the file-system.

File

core/modules/file/tests/src/Kernel/DeleteTest.php, line 83

Class

DeleteTest
Tests the file delete function.

Namespace

Drupal\Tests\file\Kernel

Code

public function testCronDeleteNonExistingTemporary() {
  $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' => REQUEST_TIME - ($this
      ->config('system.file')
      ->get('temporary_maximum_age') + 1),
  ])
    ->condition('fid', $file
    ->id())
    ->execute();
  \Drupal::service('cron')
    ->run();
  $this
    ->assertNull(File::load($file
    ->id()), 'File was removed from the database.');
}