function UsageTest::testTempFileCleanup

Ensure that temporary files are removed.

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

drupal/core/modules/file/lib/Drupal/file/Tests/UsageTest.php, line 129
Definition of Drupal\file\Tests\UsageTest.

Class

UsageTest
Tests file usage functions.

Namespace

Drupal\file\Tests

Code

function testTempFileCleanup() {

  // Temporary file that is older than DRUPAL_MAXIMUM_TEMP_FILE_AGE.
  $temp_old = file_save_data('');
  db_update('file_managed')
    ->fields(array(
    'status' => 0,
    'timestamp' => 1,
  ))
    ->condition('fid', $temp_old->fid)
    ->execute();
  $this
    ->assertTrue(file_exists($temp_old->uri), t('Old temp file was created correctly.'));

  // Temporary file that is less than DRUPAL_MAXIMUM_TEMP_FILE_AGE.
  $temp_new = file_save_data('');
  db_update('file_managed')
    ->fields(array(
    'status' => 0,
  ))
    ->condition('fid', $temp_new->fid)
    ->execute();
  $this
    ->assertTrue(file_exists($temp_new->uri), t('New temp file was created correctly.'));

  // Permanent file that is older than DRUPAL_MAXIMUM_TEMP_FILE_AGE.
  $perm_old = file_save_data('');
  db_update('file_managed')
    ->fields(array(
    'timestamp' => 1,
  ))
    ->condition('fid', $temp_old->fid)
    ->execute();
  $this
    ->assertTrue(file_exists($perm_old->uri), t('Old permanent file was created correctly.'));

  // Permanent file that is newer than DRUPAL_MAXIMUM_TEMP_FILE_AGE.
  $perm_new = file_save_data('');
  $this
    ->assertTrue(file_exists($perm_new->uri), t('New permanent file was created correctly.'));

  // Run cron and then ensure that only the old, temp file was deleted.
  $this
    ->cronRun();
  $this
    ->assertFalse(file_exists($temp_old->uri), t('Old temp file was correctly removed.'));
  $this
    ->assertTrue(file_exists($temp_new->uri), t('New temp file was correctly ignored.'));
  $this
    ->assertTrue(file_exists($perm_old->uri), t('Old permanent file was correctly ignored.'));
  $this
    ->assertTrue(file_exists($perm_new->uri), t('New permanent file was correctly ignored.'));
}