1. 8.3.x core/modules/file/tests/src/Kernel/DeleteTest.php
  2. 8.0.x core/modules/rest/src/Tests/DeleteTest.php
  3. 8.0.x core/modules/file/src/Tests/DeleteTest.php
  4. 8.1.x core/modules/file/tests/src/Kernel/DeleteTest.php
  5. 8.1.x core/modules/rest/src/Tests/DeleteTest.php
  6. 8.2.x core/modules/file/tests/src/Kernel/DeleteTest.php
  7. 8.4.x core/modules/file/tests/src/Kernel/DeleteTest.php

Namespace

Drupal\Tests\file\Kernel

File

core/modules/file/tests/src/Kernel/DeleteTest.php
View source
  1. <?php
  2. namespace Drupal\Tests\file\Kernel;
  3. use Drupal\file\Entity\File;
  4. /**
  5. * Tests the file delete function.
  6. *
  7. * @group file
  8. */
  9. class DeleteTest extends FileManagedUnitTestBase {
  10. /**
  11. * Tries deleting a normal file (as opposed to a directory, symlink, etc).
  12. */
  13. function testUnused() {
  14. $file = $this->createFile();
  15. // Check that deletion removes the file and database record.
  16. $this->assertTrue(is_file($file->getFileUri()), 'File exists.');
  17. $file->delete();
  18. $this->assertFileHooksCalled(array('delete'));
  19. $this->assertFalse(file_exists($file->getFileUri()), 'Test file has actually been deleted.');
  20. $this->assertFalse(File::load($file->id()), 'File was removed from the database.');
  21. }
  22. /**
  23. * Tries deleting a file that is in use.
  24. */
  25. function testInUse() {
  26. $file = $this->createFile();
  27. $file_usage = $this->container->get('file.usage');
  28. $file_usage->add($file, 'testing', 'test', 1);
  29. $file_usage->add($file, 'testing', 'test', 1);
  30. $file_usage->delete($file, 'testing', 'test', 1);
  31. $usage = $file_usage->listUsage($file);
  32. $this->assertEqual($usage['testing']['test'], array(1 => 1), 'Test file is still in use.');
  33. $this->assertTrue(file_exists($file->getFileUri()), 'File still exists on the disk.');
  34. $this->assertTrue(File::load($file->id()), 'File still exists in the database.');
  35. // Clear out the call to hook_file_load().
  36. file_test_reset();
  37. $file_usage->delete($file, 'testing', 'test', 1);
  38. $usage = $file_usage->listUsage($file);
  39. $this->assertFileHooksCalled(array('load', 'update'));
  40. $this->assertTrue(empty($usage), 'File usage data was removed.');
  41. $this->assertTrue(file_exists($file->getFileUri()), 'File still exists on the disk.');
  42. $file = File::load($file->id());
  43. $this->assertTrue($file, 'File still exists in the database.');
  44. $this->assertTrue($file->isTemporary(), 'File is temporary.');
  45. file_test_reset();
  46. // Call file_cron() to clean up the file. Make sure the changed timestamp
  47. // of the file is older than the system.file.temporary_maximum_age
  48. // configuration value.
  49. db_update('file_managed')
  50. ->fields(array(
  51. 'changed' => REQUEST_TIME - ($this->config('system.file')->get('temporary_maximum_age') + 1),
  52. ))
  53. ->condition('fid', $file->id())
  54. ->execute();
  55. \Drupal::service('cron')->run();
  56. // file_cron() loads
  57. $this->assertFileHooksCalled(array('delete'));
  58. $this->assertFalse(file_exists($file->getFileUri()), 'File has been deleted after its last usage was removed.');
  59. $this->assertFalse(File::load($file->id()), 'File was removed from the database.');
  60. }
  61. }

Classes

Namesort descending Description
DeleteTest Tests the file delete function.