Same name and namespace in other branches
  1. 8.9.x core/modules/file/tests/src/Kernel/UsageTest.php \Drupal\Tests\file\Kernel\UsageTest::createTempFiles()
  2. 9 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 157

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' => \Drupal::time()
      ->getRequestTime() - $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' => \Drupal::time()
      ->getRequestTime() - $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,
  ];
}