function DeleteTest::testCronDeleteNonExistingTemporary

Same name and namespace in other branches
  1. 9 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()
  3. 11.x 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->assertFileNotExists($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.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.