PageRenderTime 45ms CodeModel.GetById 3ms RepoModel.GetById 0ms app.codeStats 1ms

/core/modules/file/tests/src/Kernel/UsageTest.php

https://gitlab.com/guillaumev/alkarama
PHP | 266 lines | 181 code | 32 blank | 53 comment | 0 complexity | de89a33ffa0ffe94515f2b4a68c2bf60 MD5 | raw file
  1. <?php
  2. namespace Drupal\Tests\file\Kernel;
  3. use Drupal\field\Entity\FieldConfig;
  4. use Drupal\field\Entity\FieldStorageConfig;
  5. use Drupal\language\Entity\ConfigurableLanguage;
  6. use Drupal\language\Entity\ContentLanguageSettings;
  7. use Drupal\node\Entity\Node;
  8. use Drupal\node\Entity\NodeType;
  9. /**
  10. * Tests file usage functions.
  11. *
  12. * @group file
  13. */
  14. class UsageTest extends FileManagedUnitTestBase {
  15. /**
  16. * Tests \Drupal\file\FileUsage\DatabaseFileUsageBackend::listUsage().
  17. */
  18. function testGetUsage() {
  19. $file = $this->createFile();
  20. db_insert('file_usage')
  21. ->fields(array(
  22. 'fid' => $file->id(),
  23. 'module' => 'testing',
  24. 'type' => 'foo',
  25. 'id' => 1,
  26. 'count' => 1
  27. ))
  28. ->execute();
  29. db_insert('file_usage')
  30. ->fields(array(
  31. 'fid' => $file->id(),
  32. 'module' => 'testing',
  33. 'type' => 'bar',
  34. 'id' => 2,
  35. 'count' => 2
  36. ))
  37. ->execute();
  38. $usage = $this->container->get('file.usage')->listUsage($file);
  39. $this->assertEqual(count($usage['testing']), 2, 'Returned the correct number of items.');
  40. $this->assertTrue(isset($usage['testing']['foo'][1]), 'Returned the correct id.');
  41. $this->assertTrue(isset($usage['testing']['bar'][2]), 'Returned the correct id.');
  42. $this->assertEqual($usage['testing']['foo'][1], 1, 'Returned the correct count.');
  43. $this->assertEqual($usage['testing']['bar'][2], 2, 'Returned the correct count.');
  44. }
  45. /**
  46. * Tests \Drupal\file\FileUsage\DatabaseFileUsageBackend::add().
  47. */
  48. function testAddUsage() {
  49. $file = $this->createFile();
  50. $file_usage = $this->container->get('file.usage');
  51. $file_usage->add($file, 'testing', 'foo', 1);
  52. // Add the file twice to ensure that the count is incremented rather than
  53. // creating additional records.
  54. $file_usage->add($file, 'testing', 'bar', 2);
  55. $file_usage->add($file, 'testing', 'bar', 2);
  56. $usage = db_select('file_usage', 'f')
  57. ->fields('f')
  58. ->condition('f.fid', $file->id())
  59. ->execute()
  60. ->fetchAllAssoc('id');
  61. $this->assertEqual(count($usage), 2, 'Created two records');
  62. $this->assertEqual($usage[1]->module, 'testing', 'Correct module');
  63. $this->assertEqual($usage[2]->module, 'testing', 'Correct module');
  64. $this->assertEqual($usage[1]->type, 'foo', 'Correct type');
  65. $this->assertEqual($usage[2]->type, 'bar', 'Correct type');
  66. $this->assertEqual($usage[1]->count, 1, 'Correct count');
  67. $this->assertEqual($usage[2]->count, 2, 'Correct count');
  68. }
  69. /**
  70. * Tests \Drupal\file\FileUsage\DatabaseFileUsageBackend::delete().
  71. */
  72. function testRemoveUsage() {
  73. $file = $this->createFile();
  74. $file_usage = $this->container->get('file.usage');
  75. db_insert('file_usage')
  76. ->fields(array(
  77. 'fid' => $file->id(),
  78. 'module' => 'testing',
  79. 'type' => 'bar',
  80. 'id' => 2,
  81. 'count' => 3,
  82. ))
  83. ->execute();
  84. // Normal decrement.
  85. $file_usage->delete($file, 'testing', 'bar', 2);
  86. $count = db_select('file_usage', 'f')
  87. ->fields('f', array('count'))
  88. ->condition('f.fid', $file->id())
  89. ->execute()
  90. ->fetchField();
  91. $this->assertEqual(2, $count, 'The count was decremented correctly.');
  92. // Multiple decrement and removal.
  93. $file_usage->delete($file, 'testing', 'bar', 2, 2);
  94. $count = db_select('file_usage', 'f')
  95. ->fields('f', array('count'))
  96. ->condition('f.fid', $file->id())
  97. ->execute()
  98. ->fetchField();
  99. $this->assertIdentical(FALSE, $count, 'The count was removed entirely when empty.');
  100. // Non-existent decrement.
  101. $file_usage->delete($file, 'testing', 'bar', 2);
  102. $count = db_select('file_usage', 'f')
  103. ->fields('f', array('count'))
  104. ->condition('f.fid', $file->id())
  105. ->execute()
  106. ->fetchField();
  107. $this->assertIdentical(FALSE, $count, 'Decrementing non-exist record complete.');
  108. }
  109. /**
  110. * Create files for all the possible combinations of age and status.
  111. *
  112. * We are using UPDATE statements because using the API would set the
  113. * timestamp.
  114. */
  115. function createTempFiles() {
  116. // Temporary file that is old.
  117. $temp_old = file_save_data('');
  118. db_update('file_managed')
  119. ->fields(array(
  120. 'status' => 0,
  121. 'changed' => REQUEST_TIME - $this->config('system.file')->get('temporary_maximum_age') - 1,
  122. ))
  123. ->condition('fid', $temp_old->id())
  124. ->execute();
  125. $this->assertTrue(file_exists($temp_old->getFileUri()), 'Old temp file was created correctly.');
  126. // Temporary file that is new.
  127. $temp_new = file_save_data('');
  128. db_update('file_managed')
  129. ->fields(array('status' => 0))
  130. ->condition('fid', $temp_new->id())
  131. ->execute();
  132. $this->assertTrue(file_exists($temp_new->getFileUri()), 'New temp file was created correctly.');
  133. // Permanent file that is old.
  134. $perm_old = file_save_data('');
  135. db_update('file_managed')
  136. ->fields(array('changed' => REQUEST_TIME - $this->config('system.file')->get('temporary_maximum_age') - 1))
  137. ->condition('fid', $temp_old->id())
  138. ->execute();
  139. $this->assertTrue(file_exists($perm_old->getFileUri()), 'Old permanent file was created correctly.');
  140. // Permanent file that is new.
  141. $perm_new = file_save_data('');
  142. $this->assertTrue(file_exists($perm_new->getFileUri()), 'New permanent file was created correctly.');
  143. return array($temp_old, $temp_new, $perm_old, $perm_new);
  144. }
  145. /**
  146. * Ensure that temporary files are removed by default.
  147. */
  148. function testTempFileCleanupDefault() {
  149. list($temp_old, $temp_new, $perm_old, $perm_new) = $this->createTempFiles();
  150. // Run cron and then ensure that only the old, temp file was deleted.
  151. $this->container->get('cron')->run();
  152. $this->assertFalse(file_exists($temp_old->getFileUri()), 'Old temp file was correctly removed.');
  153. $this->assertTrue(file_exists($temp_new->getFileUri()), 'New temp file was correctly ignored.');
  154. $this->assertTrue(file_exists($perm_old->getFileUri()), 'Old permanent file was correctly ignored.');
  155. $this->assertTrue(file_exists($perm_new->getFileUri()), 'New permanent file was correctly ignored.');
  156. }
  157. /**
  158. * Ensure that temporary files are kept as configured.
  159. */
  160. function testTempFileNoCleanup() {
  161. list($temp_old, $temp_new, $perm_old, $perm_new) = $this->createTempFiles();
  162. // Set the max age to 0, meaning no temporary files will be deleted.
  163. $this->config('system.file')
  164. ->set('temporary_maximum_age', 0)
  165. ->save();
  166. // Run cron and then ensure that no file was deleted.
  167. $this->container->get('cron')->run();
  168. $this->assertTrue(file_exists($temp_old->getFileUri()), 'Old temp file was correctly ignored.');
  169. $this->assertTrue(file_exists($temp_new->getFileUri()), 'New temp file was correctly ignored.');
  170. $this->assertTrue(file_exists($perm_old->getFileUri()), 'Old permanent file was correctly ignored.');
  171. $this->assertTrue(file_exists($perm_new->getFileUri()), 'New permanent file was correctly ignored.');
  172. }
  173. /**
  174. * Ensure that temporary files are kept as configured.
  175. */
  176. function testTempFileCustomCleanup() {
  177. list($temp_old, $temp_new, $perm_old, $perm_new) = $this->createTempFiles();
  178. // Set the max age to older than default.
  179. $this->config('system.file')
  180. ->set('temporary_maximum_age', 21600 + 2)
  181. ->save();
  182. // Run cron and then ensure that more files were deleted.
  183. $this->container->get('cron')->run();
  184. $this->assertTrue(file_exists($temp_old->getFileUri()), 'Old temp file was correctly ignored.');
  185. $this->assertTrue(file_exists($temp_new->getFileUri()), 'New temp file was correctly ignored.');
  186. $this->assertTrue(file_exists($perm_old->getFileUri()), 'Old permanent file was correctly ignored.');
  187. $this->assertTrue(file_exists($perm_new->getFileUri()), 'New permanent file was correctly ignored.');
  188. }
  189. /**
  190. * Tests file usage with translated entities.
  191. */
  192. public function testFileUsageWithEntityTranslation() {
  193. /** @var \Drupal\file\FileUsage\FileUsageInterface $file_usage */
  194. $file_usage = $this->container->get('file.usage');
  195. $this->enableModules(['node', 'language']);
  196. $this->installEntitySchema('node');
  197. $this->installSchema('node', ['node_access']);
  198. // Activate English and Romanian languages.
  199. ConfigurableLanguage::create(['id' => 'en'])->save();
  200. ConfigurableLanguage::create(['id' => 'ro'])->save();
  201. NodeType::create(['type' => 'page'])->save();
  202. ContentLanguageSettings::loadByEntityTypeBundle('node', 'page')
  203. ->setLanguageAlterable(FALSE)
  204. ->setDefaultLangcode('en')
  205. ->save();
  206. // Create a file field attached to 'page' node-type.
  207. FieldStorageConfig::create([
  208. 'type' => 'file',
  209. 'entity_type' => 'node',
  210. 'field_name' => 'file',
  211. ])->save();
  212. FieldConfig::create([
  213. 'entity_type' => 'node',
  214. 'bundle' => 'page',
  215. 'field_name' => 'file',
  216. 'label' => 'File',
  217. ])->save();
  218. // Create a node, attach a file and add a Romanian translation.
  219. $node = Node::create(['type' => 'page', 'title' => 'Page']);
  220. $node
  221. ->set('file', $file = $this->createFile())
  222. ->addTranslation('ro', $node->getTranslation('en')->toArray())
  223. ->save();
  224. // Check that the file is used twice.
  225. $usage = $file_usage->listUsage($file);
  226. $this->assertEquals(2, $usage['file']['node'][$node->id()]);
  227. // Remove the Romanian translation.
  228. $node->removeTranslation('ro');
  229. $node->save();
  230. // Check that one usage has been removed and is used only once now.
  231. $usage = $file_usage->listUsage($file);
  232. $this->assertEquals(1, $usage['file']['node'][$node->id()]);
  233. }
  234. }