function DirectoryTest::testFileDestination

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

Tests the destination file path.

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 FileExists::Replace
  • a new filepath if FileExists::Rename
  • an error (returning FALSE) if FileExists::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 155

Class

DirectoryTest
Tests operations dealing with directories.

Namespace

Drupal\KernelTests\Core\File

Code

public function testFileDestination() : void {
    // 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, FileExists::Replace);
    $this->assertEquals($destination, $path, 'Non-existing filepath destination is correct with FileExists::Replace.');
    $path = $file_system->getDestinationFilename($destination, FileExists::Rename);
    $this->assertEquals($destination, $path, 'Non-existing filepath destination is correct with FileExists::Rename.');
    $path = $file_system->getDestinationFilename($destination, FileExists::Error);
    $this->assertEquals($destination, $path, 'Non-existing filepath destination is correct with FileExists::Error.');
    $destination = 'core/misc/druplicon.png';
    $path = $file_system->getDestinationFilename($destination, FileExists::Replace);
    $this->assertEquals($destination, $path, 'Existing filepath destination remains the same with FileExists::Replace.');
    $path = $file_system->getDestinationFilename($destination, FileExists::Rename);
    $this->assertNotEquals($destination, $path, 'A new filepath destination is created when filepath destination already exists with FileExists::Rename.');
    $path = $file_system->getDestinationFilename($destination, FileExists::Error);
    $this->assertFalse($path, 'An error is returned when filepath destination already exists with FileExists::Error.');
    // 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", FileExists::Replace);
}

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