function FileMoveTest::testOverwriteSelf

Same name and namespace in other branches
  1. 8.9.x 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()
  3. 11.x 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->assertFileDoesNotExist($uri);
    $this->assertFileExists($new_filepath);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.