function FileCopyTest::testOverwriteSelf

Same name in other branches
  1. 8.9.x core/tests/Drupal/KernelTests/Core/File/FileCopyTest.php \Drupal\KernelTests\Core\File\FileCopyTest::testOverwriteSelf()
  2. 10 core/tests/Drupal/KernelTests/Core/File/FileCopyTest.php \Drupal\KernelTests\Core\File\FileCopyTest::testOverwriteSelf()
  3. 11.x core/tests/Drupal/KernelTests/Core/File/FileCopyTest.php \Drupal\KernelTests\Core\File\FileCopyTest::testOverwriteSelf()

Copy a file onto itself.

File

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

Class

FileCopyTest
Tests the unmanaged file copy function.

Namespace

Drupal\KernelTests\Core\File

Code

public function testOverwriteSelf() {
    // Create a file for testing
    $uri = $this->createUri();
    // Copy the file onto itself with renaming works.
    
    /** @var \Drupal\Core\File\FileSystemInterface $file_system */
    $file_system = \Drupal::service('file_system');
    $new_filepath = $file_system->copy($uri, $uri, FileSystemInterface::EXISTS_RENAME);
    $this->assertNotFalse($new_filepath, 'Copying onto itself with renaming works.');
    $this->assertNotEquals($uri, $new_filepath, 'Copied file has a new name.');
    $this->assertFileExists($uri);
    $this->assertFileExists($new_filepath);
    $this->assertFilePermissions($new_filepath, Settings::get('file_chmod_file', FileSystem::CHMOD_FILE));
    // Copy the file onto itself without renaming fails.
    $this->expectException(FileExistsException::class);
    $new_filepath = $file_system->copy($uri, $uri, FileSystemInterface::EXISTS_ERROR);
    $this->assertFalse($new_filepath, 'Copying onto itself without renaming fails.');
    $this->assertFileExists($uri);
    // Copy the file into same directory without renaming fails.
    $new_filepath = $file_system->copy($uri, $file_system->dirname($uri), FileSystemInterface::EXISTS_ERROR);
    $this->assertFalse($new_filepath, 'Copying onto itself fails.');
    $this->assertFileExists($uri);
    // Copy the file into same directory with renaming works.
    $new_filepath = $file_system->copy($uri, $file_system->dirname($uri), FileSystemInterface::EXISTS_RENAME);
    $this->assertNotFalse($new_filepath, 'Copying into same directory works.');
    $this->assertNotEquals($uri, $new_filepath, 'Copied file has a new name.');
    $this->assertFileExists($uri);
    $this->assertFileExists($new_filepath);
    $this->assertFilePermissions($new_filepath, Settings::get('file_chmod_file', FileSystem::CHMOD_FILE));
}

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