You are here

public function UsageTest::createTempFiles in Drupal 10

Same name and namespace in other branches
  1. 8 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.

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,
  ];
}