3 namespace Drupal\Tests\file\Kernel;
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\field\Entity\FieldStorageConfig;
7 use Drupal\language\Entity\ConfigurableLanguage;
8 use Drupal\language\Entity\ContentLanguageSettings;
9 use Drupal\node\Entity\Node;
10 use Drupal\node\Entity\NodeType;
13 * Tests file usage functions.
17 class UsageTest extends FileManagedUnitTestBase {
19 * Tests \Drupal\file\FileUsage\DatabaseFileUsageBackend::listUsage().
21 public function testGetUsage() {
22 $file = $this->createFile();
23 db_insert('file_usage')
26 'module' => 'testing',
32 db_insert('file_usage')
35 'module' => 'testing',
42 $usage = $this->container->get('file.usage')->listUsage($file);
44 $this->assertEqual(count($usage['testing']), 2, 'Returned the correct number of items.');
45 $this->assertTrue(isset($usage['testing']['foo'][1]), 'Returned the correct id.');
46 $this->assertTrue(isset($usage['testing']['bar'][2]), 'Returned the correct id.');
47 $this->assertEqual($usage['testing']['foo'][1], 1, 'Returned the correct count.');
48 $this->assertEqual($usage['testing']['bar'][2], 2, 'Returned the correct count.');
52 * Tests \Drupal\file\FileUsage\DatabaseFileUsageBackend::add().
54 public function testAddUsage() {
55 $file = $this->createFile();
56 $file_usage = $this->container->get('file.usage');
57 $file_usage->add($file, 'testing', 'foo', 1);
58 // Add the file twice to ensure that the count is incremented rather than
59 // creating additional records.
60 $file_usage->add($file, 'testing', 'bar', 2);
61 $file_usage->add($file, 'testing', 'bar', 2);
63 $usage = db_select('file_usage', 'f')
65 ->condition('f.fid', $file->id())
67 ->fetchAllAssoc('id');
68 $this->assertEqual(count($usage), 2, 'Created two records');
69 $this->assertEqual($usage[1]->module, 'testing', 'Correct module');
70 $this->assertEqual($usage[2]->module, 'testing', 'Correct module');
71 $this->assertEqual($usage[1]->type, 'foo', 'Correct type');
72 $this->assertEqual($usage[2]->type, 'bar', 'Correct type');
73 $this->assertEqual($usage[1]->count, 1, 'Correct count');
74 $this->assertEqual($usage[2]->count, 2, 'Correct count');
78 * Tests file usage deletion when files are made temporary.
80 public function testRemoveUsageTemporary() {
81 $this->config('file.settings')
82 ->set('make_unused_managed_files_temporary', TRUE)
84 $file = $this->doTestRemoveUsage();
85 $this->assertTrue($file->isTemporary());
89 * Tests file usage deletion when files are made temporary.
91 public function testRemoveUsageNonTemporary() {
92 $this->config('file.settings')
93 ->set('make_unused_managed_files_temporary', FALSE)
95 $file = $this->doTestRemoveUsage();
96 $this->assertFalse($file->isTemporary());
100 * Tests \Drupal\file\FileUsage\DatabaseFileUsageBackend::delete().
102 public function doTestRemoveUsage() {
103 $file = $this->createFile();
104 $file->setPermanent();
105 $file_usage = $this->container->get('file.usage');
106 db_insert('file_usage')
108 'fid' => $file->id(),
109 'module' => 'testing',
117 $file_usage->delete($file, 'testing', 'bar', 2);
118 $count = db_select('file_usage', 'f')
119 ->fields('f', ['count'])
120 ->condition('f.fid', $file->id())
123 $this->assertEqual(2, $count, 'The count was decremented correctly.');
125 // Multiple decrement and removal.
126 $file_usage->delete($file, 'testing', 'bar', 2, 2);
127 $count = db_select('file_usage', 'f')
128 ->fields('f', ['count'])
129 ->condition('f.fid', $file->id())
132 $this->assertIdentical(FALSE, $count, 'The count was removed entirely when empty.');
134 // Non-existent decrement.
135 $file_usage->delete($file, 'testing', 'bar', 2);
136 $count = db_select('file_usage', 'f')
137 ->fields('f', ['count'])
138 ->condition('f.fid', $file->id())
141 $this->assertIdentical(FALSE, $count, 'Decrementing non-exist record complete.');
146 * Create files for all the possible combinations of age and status.
148 * We are using UPDATE statements because using the API would set the
151 public function createTempFiles() {
152 // Temporary file that is old.
153 $temp_old = file_save_data('');
154 db_update('file_managed')
157 'changed' => REQUEST_TIME - $this->config('system.file')->get('temporary_maximum_age') - 1,
159 ->condition('fid', $temp_old->id())
161 $this->assertTrue(file_exists($temp_old->getFileUri()), 'Old temp file was created correctly.');
163 // Temporary file that is new.
164 $temp_new = file_save_data('');
165 db_update('file_managed')
166 ->fields(['status' => 0])
167 ->condition('fid', $temp_new->id())
169 $this->assertTrue(file_exists($temp_new->getFileUri()), 'New temp file was created correctly.');
171 // Permanent file that is old.
172 $perm_old = file_save_data('');
173 db_update('file_managed')
174 ->fields(['changed' => REQUEST_TIME - $this->config('system.file')->get('temporary_maximum_age') - 1])
175 ->condition('fid', $temp_old->id())
177 $this->assertTrue(file_exists($perm_old->getFileUri()), 'Old permanent file was created correctly.');
179 // Permanent file that is new.
180 $perm_new = file_save_data('');
181 $this->assertTrue(file_exists($perm_new->getFileUri()), 'New permanent file was created correctly.');
182 return [$temp_old, $temp_new, $perm_old, $perm_new];
186 * Ensure that temporary files are removed by default.
188 public function testTempFileCleanupDefault() {
189 list($temp_old, $temp_new, $perm_old, $perm_new) = $this->createTempFiles();
191 // Run cron and then ensure that only the old, temp file was deleted.
192 $this->container->get('cron')->run();
193 $this->assertFalse(file_exists($temp_old->getFileUri()), 'Old temp file was correctly removed.');
194 $this->assertTrue(file_exists($temp_new->getFileUri()), 'New temp file was correctly ignored.');
195 $this->assertTrue(file_exists($perm_old->getFileUri()), 'Old permanent file was correctly ignored.');
196 $this->assertTrue(file_exists($perm_new->getFileUri()), 'New permanent file was correctly ignored.');
200 * Ensure that temporary files are kept as configured.
202 public function testTempFileNoCleanup() {
203 list($temp_old, $temp_new, $perm_old, $perm_new) = $this->createTempFiles();
205 // Set the max age to 0, meaning no temporary files will be deleted.
206 $this->config('system.file')
207 ->set('temporary_maximum_age', 0)
210 // Run cron and then ensure that no file was deleted.
211 $this->container->get('cron')->run();
212 $this->assertTrue(file_exists($temp_old->getFileUri()), 'Old temp file was correctly ignored.');
213 $this->assertTrue(file_exists($temp_new->getFileUri()), 'New temp file was correctly ignored.');
214 $this->assertTrue(file_exists($perm_old->getFileUri()), 'Old permanent file was correctly ignored.');
215 $this->assertTrue(file_exists($perm_new->getFileUri()), 'New permanent file was correctly ignored.');
219 * Ensure that temporary files are kept as configured.
221 public function testTempFileCustomCleanup() {
222 list($temp_old, $temp_new, $perm_old, $perm_new) = $this->createTempFiles();
224 // Set the max age to older than default.
225 $this->config('system.file')
226 ->set('temporary_maximum_age', 21600 + 2)
229 // Run cron and then ensure that more files were deleted.
230 $this->container->get('cron')->run();
231 $this->assertTrue(file_exists($temp_old->getFileUri()), 'Old temp file was correctly ignored.');
232 $this->assertTrue(file_exists($temp_new->getFileUri()), 'New temp file was correctly ignored.');
233 $this->assertTrue(file_exists($perm_old->getFileUri()), 'Old permanent file was correctly ignored.');
234 $this->assertTrue(file_exists($perm_new->getFileUri()), 'New permanent file was correctly ignored.');
238 * Tests file usage with translated entities.
240 public function testFileUsageWithEntityTranslation() {
241 /** @var \Drupal\file\FileUsage\FileUsageInterface $file_usage */
242 $file_usage = $this->container->get('file.usage');
244 $this->enableModules(['node', 'language']);
245 $this->installEntitySchema('node');
246 $this->installSchema('node', ['node_access']);
248 // Activate English and Romanian languages.
249 ConfigurableLanguage::create(['id' => 'en'])->save();
250 ConfigurableLanguage::create(['id' => 'ro'])->save();
252 NodeType::create(['type' => 'page'])->save();
253 ContentLanguageSettings::loadByEntityTypeBundle('node', 'page')
254 ->setLanguageAlterable(FALSE)
255 ->setDefaultLangcode('en')
257 // Create a file field attached to 'page' node-type.
258 FieldStorageConfig::create([
260 'entity_type' => 'node',
261 'field_name' => 'file',
263 FieldConfig::create([
264 'entity_type' => 'node',
266 'field_name' => 'file',
270 // Create a node, attach a file and add a Romanian translation.
271 $node = Node::create(['type' => 'page', 'title' => 'Page']);
273 ->set('file', $file = $this->createFile())
274 ->addTranslation('ro', $node->getTranslation('en')->toArray())
277 // Check that the file is used twice.
278 $usage = $file_usage->listUsage($file);
279 $this->assertEquals(2, $usage['file']['node'][$node->id()]);
281 // Remove the Romanian translation.
282 $node->removeTranslation('ro');
285 // Check that one usage has been removed and is used only once now.
286 $usage = $file_usage->listUsage($file);
287 $this->assertEquals(1, $usage['file']['node'][$node->id()]);