FileMoveTest.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace Drupal\KernelTests\Core\File;
  3. use Drupal\Core\File\Exception\FileException;
  4. use Drupal\Core\File\Exception\FileNotExistsException;
  5. use Drupal\Core\File\FileSystemInterface;
  6. use Drupal\Core\Site\Settings;
  7. use Drupal\Core\File\FileSystem;
  8. /**
  9. * Tests the unmanaged file move function.
  10. *
  11. * @group File
  12. */
  13. class FileMoveTest extends FileTestBase {
  14. /**
  15. * Move a normal file.
  16. */
  17. public function testNormal() {
  18. // Create a file for testing
  19. $uri = $this->createUri();
  20. // Moving to a new name.
  21. $desired_filepath = 'public://' . $this->randomMachineName();
  22. /** @var \Drupal\Core\File\FileSystemInterface $file_system */
  23. $file_system = \Drupal::service('file_system');
  24. $new_filepath = $file_system->move($uri, $desired_filepath, FileSystemInterface::EXISTS_ERROR);
  25. $this->assertNotFalse($new_filepath, 'Move was successful.');
  26. $this->assertEqual($new_filepath, $desired_filepath, 'Returned expected filepath.');
  27. $this->assertFileExists($new_filepath);
  28. $this->assertFileNotExists($uri);
  29. $this->assertFilePermissions($new_filepath, Settings::get('file_chmod_file', FileSystem::CHMOD_FILE));
  30. // Moving with rename.
  31. $desired_filepath = 'public://' . $this->randomMachineName();
  32. $this->assertFileExists($new_filepath);
  33. $this->assertNotFalse(file_put_contents($desired_filepath, ' '), 'Created a file so a rename will have to happen.');
  34. $newer_filepath = $file_system->move($new_filepath, $desired_filepath, FileSystemInterface::EXISTS_RENAME);
  35. $this->assertNotFalse($newer_filepath, 'Move was successful.');
  36. $this->assertNotEqual($newer_filepath, $desired_filepath, 'Returned expected filepath.');
  37. $this->assertFileExists($newer_filepath);
  38. $this->assertFileNotExists($new_filepath);
  39. $this->assertFilePermissions($newer_filepath, Settings::get('file_chmod_file', FileSystem::CHMOD_FILE));
  40. // TODO: test moving to a directory (rather than full directory/file path)
  41. // TODO: test creating and moving normal files (rather than streams)
  42. }
  43. /**
  44. * Try to move a missing file.
  45. */
  46. public function testMissing() {
  47. // Move non-existent file.
  48. $this->expectException(FileNotExistsException::class);
  49. \Drupal::service('file_system')->move($this->randomMachineName(), $this->randomMachineName());
  50. }
  51. /**
  52. * Try to move a file onto itself.
  53. */
  54. public function testOverwriteSelf() {
  55. // Create a file for testing.
  56. $uri = $this->createUri();
  57. // Move the file onto itself without renaming shouldn't make changes.
  58. /** @var \Drupal\Core\File\FileSystemInterface $file_system */
  59. $file_system = \Drupal::service('file_system');
  60. $this->expectException(FileException::class);
  61. $new_filepath = $file_system->move($uri, $uri, FileSystemInterface::EXISTS_REPLACE);
  62. $this->assertFalse($new_filepath, 'Moving onto itself without renaming fails.');
  63. $this->assertFileExists($uri);
  64. // Move the file onto itself with renaming will result in a new filename.
  65. $new_filepath = $file_system->move($uri, $uri, FileSystemInterface::EXISTS_RENAME);
  66. $this->assertNotFalse($new_filepath, 'Moving onto itself with renaming works.');
  67. $this->assertFileNotExists($uri);
  68. $this->assertFileExists($new_filepath);
  69. }
  70. }