FileSystemTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace Drupal\KernelTests\Core\File;
  3. use Drupal\Core\File\Exception\DirectoryNotReadyException;
  4. use Drupal\Core\File\Exception\FileException;
  5. use Drupal\Core\File\Exception\FileExistsException;
  6. use Drupal\Core\File\Exception\FileNotExistsException;
  7. use Drupal\Core\File\FileSystemInterface;
  8. use Drupal\KernelTests\KernelTestBase;
  9. /**
  10. * @coversDefaultClass \Drupal\Core\File\FileSystem
  11. * @group File
  12. */
  13. class FileSystemTest extends KernelTestBase {
  14. /**
  15. * The file handler under test.
  16. *
  17. * @var \Drupal\Core\File\FileSystemInterface
  18. */
  19. protected $fileSystem;
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public static $modules = ['system'];
  24. /**
  25. * {@inheritdoc}
  26. */
  27. protected function setUp() {
  28. parent::setUp();
  29. $this->fileSystem = $this->container->get('file_system');
  30. }
  31. /**
  32. * @covers ::copy
  33. */
  34. public function testEnsureFileExistsBeforeCopy() {
  35. // We need to compute the exception message here because it will include
  36. // the 'real' path to the file, which varies with $this->siteDirectory.
  37. $this->expectException(FileNotExistsException::class);
  38. $this->expectExceptionMessage("File 'public://test.txt' ('{$this->siteDirectory}/files/test.txt') could not be copied because it does not exist");
  39. $this->fileSystem->copy('public://test.txt', 'public://test-copy.txt');
  40. }
  41. /**
  42. * @covers ::copy
  43. */
  44. public function testDestinationDirectoryFailureOnCopy() {
  45. $this->expectException(DirectoryNotReadyException::class);
  46. $this->expectExceptionMessage("The specified file 'public://test.txt' could not be copied because the destination directory is not properly configured. This may be caused by a problem with file or directory permissions");
  47. touch('public://test.txt');
  48. // public://subdirectory has not been created, so \Drupal::service('file_system')->prepareDirectory()
  49. // will fail, causing copy() to throw DirectoryNotReadyException.
  50. $this->fileSystem->copy('public://test.txt', 'public://subdirectory/test.txt');
  51. }
  52. /**
  53. * @covers ::copy
  54. */
  55. public function testCopyFailureIfFileAlreadyExists() {
  56. $this->expectException(FileExistsException::class);
  57. $this->expectExceptionMessage("File 'public://test.txt' could not be copied because a file by that name already exists in the destination directory ('')");
  58. $uri = 'public://test.txt';
  59. touch($uri);
  60. $this->fileSystem->copy($uri, $uri, FileSystemInterface::EXISTS_ERROR);
  61. }
  62. /**
  63. * @covers ::copy
  64. */
  65. public function testCopyFailureIfSelfOverwrite() {
  66. $this->expectException(FileException::class);
  67. $this->expectExceptionMessage("'public://test.txt' could not be copied because it would overwrite itself");
  68. $uri = 'public://test.txt';
  69. touch($uri);
  70. $this->fileSystem->copy($uri, $uri, FileSystemInterface::EXISTS_REPLACE);
  71. }
  72. /**
  73. * @covers ::copy
  74. */
  75. public function testCopySelfRename() {
  76. $uri = 'public://test.txt';
  77. touch($uri);
  78. $this->fileSystem->copy($uri, $uri);
  79. $this->assertFileExists('public://test_0.txt');
  80. }
  81. /**
  82. * @covers ::copy
  83. */
  84. public function testSuccessfulCopy() {
  85. touch('public://test.txt');
  86. $this->fileSystem->copy('public://test.txt', 'public://test-copy.txt');
  87. $this->assertFileExists('public://test-copy.txt');
  88. }
  89. }