function UsageTest::createTempFiles

Same name in other branches
  1. 8.9.x core/modules/file/tests/src/Kernel/UsageTest.php \Drupal\Tests\file\Kernel\UsageTest::createTempFiles()
  2. 10 core/modules/file/tests/src/Kernel/UsageTest.php \Drupal\Tests\file\Kernel\UsageTest::createTempFiles()
  3. 11.x core/modules/file/tests/src/Kernel/UsageTest.php \Drupal\Tests\file\Kernel\UsageTest::createTempFiles()

Create files for all the possible combinations of age and status.

We are using UPDATE statements because using the API would set the timestamp.

3 calls to UsageTest::createTempFiles()
UsageTest::testTempFileCleanupDefault in core/modules/file/tests/src/Kernel/UsageTest.php
Ensure that temporary files are removed by default.
UsageTest::testTempFileCustomCleanup in core/modules/file/tests/src/Kernel/UsageTest.php
Ensure that temporary files are kept as configured.
UsageTest::testTempFileNoCleanup in core/modules/file/tests/src/Kernel/UsageTest.php
Ensure that temporary files are kept as configured.

File

core/modules/file/tests/src/Kernel/UsageTest.php, line 155

Class

UsageTest
Tests file usage functions.

Namespace

Drupal\Tests\file\Kernel

Code

public function createTempFiles() {
    
    /** @var \Drupal\file\FileRepositoryInterface $fileRepository */
    $fileRepository = \Drupal::service('file.repository');
    // Temporary file that is old.
    $destination = "public://";
    $temp_old = $fileRepository->writeData('', $destination);
    $connection = Database::getConnection();
    $connection->update('file_managed')
        ->fields([
        'status' => 0,
        'changed' => REQUEST_TIME - $this->config('system.file')
            ->get('temporary_maximum_age') - 1,
    ])
        ->condition('fid', $temp_old->id())
        ->execute();
    $this->assertFileExists($temp_old->getFileUri());
    // Temporary file that is new.
    $temp_new = $fileRepository->writeData('', $destination);
    $connection->update('file_managed')
        ->fields([
        'status' => 0,
    ])
        ->condition('fid', $temp_new->id())
        ->execute();
    $this->assertFileExists($temp_new->getFileUri());
    // Permanent file that is old.
    $perm_old = $fileRepository->writeData('', $destination);
    $connection->update('file_managed')
        ->fields([
        'changed' => REQUEST_TIME - $this->config('system.file')
            ->get('temporary_maximum_age') - 1,
    ])
        ->condition('fid', $temp_old->id())
        ->execute();
    $this->assertFileExists($perm_old->getFileUri());
    // Permanent file that is new.
    $perm_new = $fileRepository->writeData('', $destination);
    $this->assertFileExists($perm_new->getFileUri());
    return [
        $temp_old,
        $temp_new,
        $perm_old,
        $perm_new,
    ];
}

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