FileSystemTest.php in Drupal 10
File
core/tests/Drupal/KernelTests/Core/File/FileSystemTest.php
View source
<?php
namespace Drupal\KernelTests\Core\File;
use Drupal\Core\File\Exception\DirectoryNotReadyException;
use Drupal\Core\File\Exception\FileException;
use Drupal\Core\File\Exception\FileExistsException;
use Drupal\Core\File\Exception\FileNotExistsException;
use Drupal\Core\File\FileSystemInterface;
use Drupal\KernelTests\KernelTestBase;
class FileSystemTest extends KernelTestBase {
protected $fileSystem;
protected static $modules = [
'system',
];
protected function setUp() : void {
parent::setUp();
$this->fileSystem = $this->container
->get('file_system');
}
public function testEnsureFileExistsBeforeCopy() {
$this
->expectException(FileNotExistsException::class);
$this
->expectExceptionMessage("File 'public://test.txt' ('{$this->siteDirectory}/files/test.txt') could not be copied because it does not exist");
$this->fileSystem
->copy('public://test.txt', 'public://test-copy.txt');
}
public function testDestinationDirectoryFailureOnCopy() {
$this
->expectException(DirectoryNotReadyException::class);
$this
->expectExceptionMessage("The specified file 'public://test.txt' could not be copied because the destination directory 'public://subdirectory' is not properly configured. This may be caused by a problem with file or directory permissions.");
touch('public://test.txt');
$this->fileSystem
->copy('public://test.txt', 'public://subdirectory/test.txt');
}
public function testCopyFailureIfFileAlreadyExists() {
$this
->expectException(FileExistsException::class);
$this
->expectExceptionMessage("File 'public://test.txt' could not be copied because a file by that name already exists in the destination directory ('')");
$uri = 'public://test.txt';
touch($uri);
$this->fileSystem
->copy($uri, $uri, FileSystemInterface::EXISTS_ERROR);
}
public function testCopyFailureIfSelfOverwrite() {
$this
->expectException(FileException::class);
$this
->expectExceptionMessage("'public://test.txt' could not be copied because it would overwrite itself");
$uri = 'public://test.txt';
touch($uri);
$this->fileSystem
->copy($uri, $uri, FileSystemInterface::EXISTS_REPLACE);
}
public function testCopySelfRename() {
$uri = 'public://test.txt';
touch($uri);
$this->fileSystem
->copy($uri, $uri);
$this
->assertFileExists('public://test_0.txt');
}
public function testSuccessfulCopy() {
touch('public://test.txt');
$this->fileSystem
->copy('public://test.txt', 'public://test-copy.txt');
$this
->assertFileExists('public://test-copy.txt');
}
}