View source
<?php
namespace Drupal\Tests\file\Kernel;
use Drupal\Core\Database\Database;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\language\Entity\ContentLanguageSettings;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
class UsageTest extends FileManagedUnitTestBase {
public function testGetUsage() {
$file = $this
->createFile();
$connection = Database::getConnection();
$connection
->insert('file_usage')
->fields([
'fid' => $file
->id(),
'module' => 'testing',
'type' => 'foo',
'id' => 1,
'count' => 1,
])
->execute();
$connection
->insert('file_usage')
->fields([
'fid' => $file
->id(),
'module' => 'testing',
'type' => 'bar',
'id' => 2,
'count' => 2,
])
->execute();
$usage = $this->container
->get('file.usage')
->listUsage($file);
$this
->assertCount(2, $usage['testing'], 'Returned the correct number of items.');
$this
->assertTrue(isset($usage['testing']['foo'][1]), 'Returned the correct id.');
$this
->assertTrue(isset($usage['testing']['bar'][2]), 'Returned the correct id.');
$this
->assertEquals(1, $usage['testing']['foo'][1], 'Returned the correct count.');
$this
->assertEquals(2, $usage['testing']['bar'][2], 'Returned the correct count.');
}
public function testAddUsage() {
$file = $this
->createFile();
$file_usage = $this->container
->get('file.usage');
$file_usage
->add($file, 'testing', 'foo', 1);
$file_usage
->add($file, 'testing', 'bar', 2);
$file_usage
->add($file, 'testing', 'bar', 2);
$usage = Database::getConnection()
->select('file_usage', 'f')
->fields('f')
->condition('f.fid', $file
->id())
->execute()
->fetchAllAssoc('id');
$this
->assertCount(2, $usage, 'Created two records');
$this
->assertEquals('testing', $usage[1]->module, 'Correct module');
$this
->assertEquals('testing', $usage[2]->module, 'Correct module');
$this
->assertEquals('foo', $usage[1]->type, 'Correct type');
$this
->assertEquals('bar', $usage[2]->type, 'Correct type');
$this
->assertEquals(1, $usage[1]->count, 'Correct count');
$this
->assertEquals(2, $usage[2]->count, 'Correct count');
}
public function testRemoveUsageTemporary() {
$this
->config('file.settings')
->set('make_unused_managed_files_temporary', TRUE)
->save();
$file = $this
->doTestRemoveUsage();
$this
->assertTrue($file
->isTemporary());
}
public function testRemoveUsageNonTemporary() {
$this
->config('file.settings')
->set('make_unused_managed_files_temporary', FALSE)
->save();
$file = $this
->doTestRemoveUsage();
$this
->assertFalse($file
->isTemporary());
}
public function doTestRemoveUsage() {
$file = $this
->createFile();
$file
->setPermanent();
$file_usage = $this->container
->get('file.usage');
$connection = Database::getConnection();
$connection
->insert('file_usage')
->fields([
'fid' => $file
->id(),
'module' => 'testing',
'type' => 'bar',
'id' => 2,
'count' => 3,
])
->execute();
$file_usage
->delete($file, 'testing', 'bar', 2);
$count = $connection
->select('file_usage', 'f')
->fields('f', [
'count',
])
->condition('f.fid', $file
->id())
->execute()
->fetchField();
$this
->assertEquals(2, $count, 'The count was decremented correctly.');
$file_usage
->delete($file, 'testing', 'bar', 2, 2);
$count = $connection
->select('file_usage', 'f')
->fields('f', [
'count',
])
->condition('f.fid', $file
->id())
->execute()
->fetchField();
$this
->assertFalse($count, 'The count was removed entirely when empty.');
$file_usage
->delete($file, 'testing', 'bar', 2);
$count = $connection
->select('file_usage', 'f')
->fields('f', [
'count',
])
->condition('f.fid', $file
->id())
->execute()
->fetchField();
$this
->assertFalse($count, 'Decrementing non-exist record complete.');
return $file;
}
public function createTempFiles() {
$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());
$temp_new = file_save_data('');
$connection
->update('file_managed')
->fields([
'status' => 0,
])
->condition('fid', $temp_new
->id())
->execute();
$this
->assertFileExists($temp_new
->getFileUri());
$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());
$perm_new = file_save_data('');
$this
->assertFileExists($perm_new
->getFileUri());
return [
$temp_old,
$temp_new,
$perm_old,
$perm_new,
];
}
public function testTempFileCleanupDefault() {
list($temp_old, $temp_new, $perm_old, $perm_new) = $this
->createTempFiles();
$this->container
->get('cron')
->run();
$this
->assertFileDoesNotExist($temp_old
->getFileUri());
$this
->assertFileExists($temp_new
->getFileUri());
$this
->assertFileExists($perm_old
->getFileUri());
$this
->assertFileExists($perm_new
->getFileUri());
}
public function testTempFileNoCleanup() {
list($temp_old, $temp_new, $perm_old, $perm_new) = $this
->createTempFiles();
$this
->config('system.file')
->set('temporary_maximum_age', 0)
->save();
$this->container
->get('cron')
->run();
$this
->assertFileExists($temp_old
->getFileUri());
$this
->assertFileExists($temp_new
->getFileUri());
$this
->assertFileExists($perm_old
->getFileUri());
$this
->assertFileExists($perm_new
->getFileUri());
}
public function testTempFileCustomCleanup() {
list($temp_old, $temp_new, $perm_old, $perm_new) = $this
->createTempFiles();
$this
->config('system.file')
->set('temporary_maximum_age', 21600 + 2)
->save();
$this->container
->get('cron')
->run();
$this
->assertFileExists($temp_old
->getFileUri());
$this
->assertFileExists($temp_new
->getFileUri());
$this
->assertFileExists($perm_old
->getFileUri());
$this
->assertFileExists($perm_new
->getFileUri());
}
public function testFileUsageWithEntityTranslation() {
$file_usage = $this->container
->get('file.usage');
$this
->enableModules([
'node',
'language',
]);
$this
->installEntitySchema('node');
$this
->installSchema('node', [
'node_access',
]);
ConfigurableLanguage::create([
'id' => 'en',
])
->save();
ConfigurableLanguage::create([
'id' => 'ro',
])
->save();
NodeType::create([
'type' => 'page',
])
->save();
ContentLanguageSettings::loadByEntityTypeBundle('node', 'page')
->setLanguageAlterable(FALSE)
->setDefaultLangcode('en')
->save();
FieldStorageConfig::create([
'type' => 'file',
'entity_type' => 'node',
'field_name' => 'file',
])
->save();
FieldConfig::create([
'entity_type' => 'node',
'bundle' => 'page',
'field_name' => 'file',
'label' => 'File',
])
->save();
$node = Node::create([
'type' => 'page',
'title' => 'Page',
]);
$node
->set('file', $file = $this
->createFile())
->addTranslation('ro', $node
->getTranslation('en')
->toArray())
->save();
$usage = $file_usage
->listUsage($file);
$this
->assertEquals(2, $usage['file']['node'][$node
->id()]);
$node
->removeTranslation('ro');
$node
->save();
$usage = $file_usage
->listUsage($file);
$this
->assertEquals(1, $usage['file']['node'][$node
->id()]);
}
}