You are here

public function UsageTest::createTempFiles in Drupal 9

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

  // Temporary file that is old.
  $temp_old = file_save_data('');
  $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 = file_save_data('');
  $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 = file_save_data('');
  $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 = file_save_data('');
  $this
    ->assertFileExists($perm_new
    ->getFileUri());
  return [
    $temp_old,
    $temp_new,
    $perm_old,
    $perm_new,
  ];
}