You are here

public function DirectoryTest::testFileDestination in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/File/DirectoryTest.php \Drupal\KernelTests\Core\File\DirectoryTest::testFileDestination()
  2. 9 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:

If the file doesn't currently exist, then it will simply return the filepath.

File

core/tests/Drupal/KernelTests/Core/File/DirectoryTest.php, line 152

Class

DirectoryTest
Tests operations dealing with directories.

Namespace

Drupal\KernelTests\Core\File

Code

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
    ->assertEquals($destination, $path, 'Non-existing filepath destination is correct with FileSystemInterface::EXISTS_REPLACE.');
  $path = $file_system
    ->getDestinationFilename($destination, FileSystemInterface::EXISTS_RENAME);
  $this
    ->assertEquals($destination, $path, 'Non-existing filepath destination is correct with FileSystemInterface::EXISTS_RENAME.');
  $path = $file_system
    ->getDestinationFilename($destination, FileSystemInterface::EXISTS_ERROR);
  $this
    ->assertEquals($destination, $path, 'Non-existing filepath destination is correct with FileSystemInterface::EXISTS_ERROR.');
  $destination = 'core/misc/druplicon.png';
  $path = $file_system
    ->getDestinationFilename($destination, FileSystemInterface::EXISTS_REPLACE);
  $this
    ->assertEquals($destination, $path, 'Existing filepath destination remains the same with FileSystemInterface::EXISTS_REPLACE.');
  $path = $file_system
    ->getDestinationFilename($destination, FileSystemInterface::EXISTS_RENAME);
  $this
    ->assertNotEquals($destination, $path, 'A new filepath destination is created when filepath destination already exists with FileSystemInterface::EXISTS_RENAME.');
  $path = $file_system
    ->getDestinationFilename($destination, FileSystemInterface::EXISTS_ERROR);
  $this
    ->assertFalse($path, 'An error is returned when filepath destination already exists with FileSystemInterface::EXISTS_ERROR.');

  // Invalid UTF-8 causes an exception.
  $this
    ->expectException(FileException::class);
  $this
    ->expectExceptionMessage("");
  $file_system
    ->getDestinationFilename("", FileSystemInterface::EXISTS_REPLACE);
}