function DirectoryTest::testFileDestination
Same name in other branches
- 9 core/tests/Drupal/KernelTests/Core/File/DirectoryTest.php \Drupal\KernelTests\Core\File\DirectoryTest::testFileDestination()
- 10 core/tests/Drupal/KernelTests/Core/File/DirectoryTest.php \Drupal\KernelTests\Core\File\DirectoryTest::testFileDestination()
- 11.x core/tests/Drupal/KernelTests/Core/File/DirectoryTest.php \Drupal\KernelTests\Core\File\DirectoryTest::testFileDestination()
This will test the filepath for a destination based on passed flags and whether or not the file exists.
If a file exists, ::getDestinationFilename($destination, $replace) will either return:
- the existing filepath, if $replace is FileSystemInterface::EXISTS_REPLACE
- a new filepath if FileSystemInterface::EXISTS_RENAME
- an error (returning FALSE) if FileSystemInterface::EXISTS_ERROR.
If the file doesn't currently exist, then it will simply return the filepath.
File
-
core/
tests/ Drupal/ KernelTests/ Core/ File/ DirectoryTest.php, line 165
Class
- DirectoryTest
- Tests operations dealing with directories.
Namespace
Drupal\KernelTests\Core\FileCode
public function testFileDestination() {
// First test for non-existent file.
$destination = 'core/misc/xyz.txt';
/** @var \Drupal\Core\File\FileSystemInterface $file_system */
$file_system = \Drupal::service('file_system');
$path = $file_system->getDestinationFilename($destination, FileSystemInterface::EXISTS_REPLACE);
$this->assertEqual($path, $destination, 'Non-existing filepath destination is correct with FileSystemInterface::EXISTS_REPLACE.', 'File');
$path = $file_system->getDestinationFilename($destination, FileSystemInterface::EXISTS_RENAME);
$this->assertEqual($path, $destination, 'Non-existing filepath destination is correct with FileSystemInterface::EXISTS_RENAME.', 'File');
$path = $file_system->getDestinationFilename($destination, FileSystemInterface::EXISTS_ERROR);
$this->assertEqual($path, $destination, 'Non-existing filepath destination is correct with FileSystemInterface::EXISTS_ERROR.', 'File');
$destination = 'core/misc/druplicon.png';
$path = $file_system->getDestinationFilename($destination, FileSystemInterface::EXISTS_REPLACE);
$this->assertEqual($path, $destination, 'Existing filepath destination remains the same with FileSystemInterface::EXISTS_REPLACE.', 'File');
$path = $file_system->getDestinationFilename($destination, FileSystemInterface::EXISTS_RENAME);
$this->assertNotEqual($path, $destination, 'A new filepath destination is created when filepath destination already exists with FileSystemInterface::EXISTS_RENAME.', 'File');
$path = $file_system->getDestinationFilename($destination, FileSystemInterface::EXISTS_ERROR);
$this->assertEqual($path, FALSE, 'An error is returned when filepath destination already exists with FileSystemInterface::EXISTS_ERROR.', 'File');
// Invalid UTF-8 causes an exception.
$this->expectException(FileException::class);
$this->expectExceptionMessage("Invalid filename 'a\xfftest\x80€.txt'");
$file_system->getDestinationFilename("core/misc/a\xfftest\x80€.txt", FileSystemInterface::EXISTS_REPLACE);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.