function DirectoryTest::testFileCreateNewFilepath

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

Tests the file paths of newly created files.

File

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

Class

DirectoryTest
Tests operations dealing with directories.

Namespace

Drupal\KernelTests\Core\File

Code

public function testFileCreateNewFilepath() : void {
    // First we test against an imaginary file that does not exist in a
    // directory.
    $basename = 'xyz.txt';
    $directory = 'core/misc';
    $original = $directory . '/' . $basename;
    
    /** @var \Drupal\Core\File\FileSystemInterface $file_system */
    $file_system = \Drupal::service('file_system');
    $path = $file_system->createFilename($basename, $directory);
    $this->assertEquals($original, $path, "New filepath {$path} equals {$original}.");
    // Then we test against a file that already exists within that directory.
    $basename = 'druplicon.png';
    $original = $directory . '/' . $basename;
    $expected = $directory . '/druplicon_0.png';
    $path = $file_system->createFilename($basename, $directory);
    $this->assertEquals($expected, $path, "Creating a new filepath from {$path} equals {$original} (expected {$expected}).");
    // @todo Finally we copy a file into a directory several times, to ensure a properly iterating filename suffix.
}

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