You are here

public function FileMoveTest::testOverwriteSelf in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/File/FileMoveTest.php \Drupal\KernelTests\Core\File\FileMoveTest::testOverwriteSelf()
  2. 10 core/tests/Drupal/KernelTests/Core/File/FileMoveTest.php \Drupal\KernelTests\Core\File\FileMoveTest::testOverwriteSelf()

Try to move a file onto itself.

File

core/tests/Drupal/KernelTests/Core/File/FileMoveTest.php, line 63

Class

FileMoveTest
Tests the unmanaged file move function.

Namespace

Drupal\KernelTests\Core\File

Code

public function testOverwriteSelf() {

  // Create a file for testing.
  $uri = $this
    ->createUri();

  // Move the file onto itself without renaming shouldn't make changes.

  /** @var \Drupal\Core\File\FileSystemInterface $file_system */
  $file_system = \Drupal::service('file_system');
  $this
    ->expectException(FileException::class);
  $new_filepath = $file_system
    ->move($uri, $uri, FileSystemInterface::EXISTS_REPLACE);
  $this
    ->assertFalse($new_filepath, 'Moving onto itself without renaming fails.');
  $this
    ->assertFileExists($uri);

  // Move the file onto itself with renaming will result in a new filename.
  $new_filepath = $file_system
    ->move($uri, $uri, FileSystemInterface::EXISTS_RENAME);
  $this
    ->assertNotFalse($new_filepath, 'Moving onto itself with renaming works.');
  $this
    ->assertFileNotExists($uri);
  $this
    ->assertFileExists($new_filepath);
}