1 file.test FileDeleteTest::testInUse()

Tries deleting a file that is in use.

File

core/modules/simpletest/tests/file.test, line 1728
This provides SimpleTests for the core file handling functionality. These include FileValidateTest and FileSaveTest.

Class

FileDeleteTest
Deletion related tests.

Code

function testInUse() {
  $file = $this->createFile();
  file_usage_add($file, 'testing', 'test', 1);
  file_usage_add($file, 'testing', 'test', 1);

  file_usage_delete($file, 'testing', 'test', 1);
  $usage = file_usage_list($file);
  $this->assertEqual($usage['testing']['test'], array(1 => 1), 'Test file is still in use.');
  $this->assertTrue(file_exists($file->uri), 'File still exists on the disk.');
  $this->assertTrue(file_load($file->fid), 'File still exists in the database.');

  // Clear out the call to hook_file_load().
  file_test_reset();

  file_usage_delete($file, 'testing', 'test', 1);
  $usage = file_usage_list($file);
  $this->assertFileHooksCalled(array('load', 'update'));
  $this->assertTrue(empty($usage), 'File usage data was removed.');
  $this->assertTrue(file_exists($file->uri), 'File still exists on the disk.');
  $file = file_load($file->fid);
  $this->assertTrue($file, 'File still exists in the database.');
  $this->assertEqual($file->status, 0, 'File is temporary.');
  file_test_reset();

  // Call system_cron() to clean up the file. Make sure the timestamp
  // of the file is older than BACKDROP_MAXIMUM_TEMP_FILE_AGE.
  db_update('file_managed')
    ->fields(array(
      'timestamp' => REQUEST_TIME - (BACKDROP_MAXIMUM_TEMP_FILE_AGE + 1),
    ))
    ->condition('fid', $file->fid)
    ->execute();
  // Remove the entity cache entry for this file so that hook_file_load will
  // be called again.
  cache('entity_file')->delete($file->fid);

  backdrop_cron_run();

  // system_cron() loads
  $this->assertFileHooksCalled(array('load', 'delete'));
  $this->assertFalse(file_exists($file->uri), 'File has been deleted after its last usage was removed.');
  $this->assertFalse(file_load($file->fid), 'File was removed from the database.');
}