View source
<?php
namespace Drupal\KernelTests\Core\File;
use Drupal\Core\File\Exception\FileExistsException;
use Drupal\Core\File\Exception\FileNotExistsException;
use Drupal\Core\File\FileSystem;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Site\Settings;
class FileCopyTest extends FileTestBase {
public function testNormal() {
$uri = $this
->createUri();
$desired_filepath = 'public://' . $this
->randomMachineName();
$new_filepath = \Drupal::service('file_system')
->copy($uri, $desired_filepath, FileSystemInterface::EXISTS_ERROR);
$this
->assertNotFalse($new_filepath, 'Copy was successful.');
$this
->assertEquals($desired_filepath, $new_filepath, 'Returned expected filepath.');
$this
->assertFileExists($uri);
$this
->assertFileExists($new_filepath);
$this
->assertFilePermissions($new_filepath, Settings::get('file_chmod_file', FileSystem::CHMOD_FILE));
$desired_filepath = 'public://' . $this
->randomMachineName();
$this
->assertNotFalse(file_put_contents($desired_filepath, ' '), 'Created a file so a rename will have to happen.');
$newer_filepath = \Drupal::service('file_system')
->copy($uri, $desired_filepath, FileSystemInterface::EXISTS_RENAME);
$this
->assertNotFalse($newer_filepath, 'Copy was successful.');
$this
->assertNotEquals($desired_filepath, $newer_filepath, 'Returned expected filepath.');
$this
->assertFileExists($uri);
$this
->assertFileExists($newer_filepath);
$this
->assertFilePermissions($newer_filepath, Settings::get('file_chmod_file', FileSystem::CHMOD_FILE));
}
public function testNonExistent() {
$desired_filepath = $this
->randomMachineName();
$this
->assertFileDoesNotExist($desired_filepath);
$this
->expectException(FileNotExistsException::class);
$new_filepath = \Drupal::service('file_system')
->copy($desired_filepath, $this
->randomMachineName());
$this
->assertFalse($new_filepath, 'Copying a missing file fails.');
}
public function testOverwriteSelf() {
$uri = $this
->createUri();
$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));
$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);
$new_filepath = $file_system
->copy($uri, $file_system
->dirname($uri), FileSystemInterface::EXISTS_ERROR);
$this
->assertFalse($new_filepath, 'Copying onto itself fails.');
$this
->assertFileExists($uri);
$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));
}
}