FileCopyTest.php 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace Drupal\KernelTests\Core\File;
  3. use Drupal\Core\File\Exception\FileExistsException;
  4. use Drupal\Core\File\Exception\FileNotExistsException;
  5. use Drupal\Core\File\FileSystem;
  6. use Drupal\Core\File\FileSystemInterface;
  7. use Drupal\Core\Site\Settings;
  8. /**
  9. * Tests the unmanaged file copy function.
  10. *
  11. * @group File
  12. */
  13. class FileCopyTest extends FileTestBase {
  14. /**
  15. * Copy a normal file.
  16. */
  17. public function testNormal() {
  18. // Create a file for testing
  19. $uri = $this->createUri();
  20. // Copying to a new name.
  21. $desired_filepath = 'public://' . $this->randomMachineName();
  22. $new_filepath = \Drupal::service('file_system')->copy($uri, $desired_filepath, FileSystemInterface::EXISTS_ERROR);
  23. $this->assertNotFalse($new_filepath, 'Copy was successful.');
  24. $this->assertEqual($new_filepath, $desired_filepath, 'Returned expected filepath.');
  25. $this->assertFileExists($uri);
  26. $this->assertFileExists($new_filepath);
  27. $this->assertFilePermissions($new_filepath, Settings::get('file_chmod_file', FileSystem::CHMOD_FILE));
  28. // Copying with rename.
  29. $desired_filepath = 'public://' . $this->randomMachineName();
  30. $this->assertNotFalse(file_put_contents($desired_filepath, ' '), 'Created a file so a rename will have to happen.');
  31. $newer_filepath = \Drupal::service('file_system')->copy($uri, $desired_filepath, FileSystemInterface::EXISTS_RENAME);
  32. $this->assertNotFalse($newer_filepath, 'Copy was successful.');
  33. $this->assertNotEqual($newer_filepath, $desired_filepath, 'Returned expected filepath.');
  34. $this->assertFileExists($uri);
  35. $this->assertFileExists($newer_filepath);
  36. $this->assertFilePermissions($newer_filepath, Settings::get('file_chmod_file', FileSystem::CHMOD_FILE));
  37. // TODO: test copying to a directory (rather than full directory/file path)
  38. // TODO: test copying normal files using normal paths (rather than only streams)
  39. }
  40. /**
  41. * Copy a non-existent file.
  42. */
  43. public function testNonExistent() {
  44. // Copy non-existent file
  45. $desired_filepath = $this->randomMachineName();
  46. $this->assertFileNotExists($desired_filepath);
  47. $this->expectException(FileNotExistsException::class);
  48. $new_filepath = \Drupal::service('file_system')->copy($desired_filepath, $this->randomMachineName());
  49. $this->assertFalse($new_filepath, 'Copying a missing file fails.');
  50. }
  51. /**
  52. * Copy a file onto itself.
  53. */
  54. public function testOverwriteSelf() {
  55. // Create a file for testing
  56. $uri = $this->createUri();
  57. // Copy the file onto itself with renaming works.
  58. /** @var \Drupal\Core\File\FileSystemInterface $file_system */
  59. $file_system = \Drupal::service('file_system');
  60. $new_filepath = $file_system->copy($uri, $uri, FileSystemInterface::EXISTS_RENAME);
  61. $this->assertNotFalse($new_filepath, 'Copying onto itself with renaming works.');
  62. $this->assertNotEqual($new_filepath, $uri, 'Copied file has a new name.');
  63. $this->assertFileExists($uri);
  64. $this->assertFileExists($new_filepath);
  65. $this->assertFilePermissions($new_filepath, Settings::get('file_chmod_file', FileSystem::CHMOD_FILE));
  66. // Copy the file onto itself without renaming fails.
  67. $this->expectException(FileExistsException::class);
  68. $new_filepath = $file_system->copy($uri, $uri, FileSystemInterface::EXISTS_ERROR);
  69. $this->assertFalse($new_filepath, 'Copying onto itself without renaming fails.');
  70. $this->assertFileExists($uri);
  71. // Copy the file into same directory without renaming fails.
  72. $new_filepath = $file_system->copy($uri, $file_system->dirname($uri), FileSystemInterface::EXISTS_ERROR);
  73. $this->assertFalse($new_filepath, 'Copying onto itself fails.');
  74. $this->assertFileExists($uri);
  75. // Copy the file into same directory with renaming works.
  76. $new_filepath = $file_system->copy($uri, $file_system->dirname($uri), FileSystemInterface::EXISTS_RENAME);
  77. $this->assertNotFalse($new_filepath, 'Copying into same directory works.');
  78. $this->assertNotEqual($new_filepath, $uri, 'Copied file has a new name.');
  79. $this->assertFileExists($uri);
  80. $this->assertFileExists($new_filepath);
  81. $this->assertFilePermissions($new_filepath, Settings::get('file_chmod_file', FileSystem::CHMOD_FILE));
  82. }
  83. }