<?php
namespace Drupal\file\Tests;
class DeleteTest extends FileManagedTestBase {
public static function getInfo() {
return array(
'name' => 'File delete',
'description' => 'Tests the file delete function.',
'group' => 'File API',
);
}
function testUnused() {
$file = $this
->createFile();
$this
->assertTrue(is_file($file->uri), t('File exists.'));
$file
->delete();
$this
->assertFileHooksCalled(array(
'delete',
));
$this
->assertFalse(file_exists($file->uri), t('Test file has actually been deleted.'));
$this
->assertFalse(file_load($file->fid), t('File was removed from the database.'));
}
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()
->listUsage($file);
$this
->assertEqual($usage['testing']['test'], array(
1 => 1,
), t('Test file is still in use.'));
$this
->assertTrue(file_exists($file->uri), t('File still exists on the disk.'));
$this
->assertTrue(file_load($file->fid), t('File still exists in the database.'));
file_test_reset();
file_usage()
->delete($file, 'testing', 'test', 1);
$usage = file_usage()
->listUsage($file);
$this
->assertFileHooksCalled(array(
'load',
'update',
));
$this
->assertTrue(empty($usage), t('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();
db_update('file_managed')
->fields(array(
'timestamp' => REQUEST_TIME - (DRUPAL_MAXIMUM_TEMP_FILE_AGE + 1),
))
->condition('fid', $file->fid)
->execute();
drupal_cron_run();
$this
->assertFileHooksCalled(array(
'delete',
));
$this
->assertFalse(file_exists($file->uri), t('File has been deleted after its last usage was removed.'));
$this
->assertFalse(file_load($file->fid), t('File was removed from the database.'));
}
}