function FileDirectoryTest::testFileCreateNewFilepath

This will take a directory and path, and find a valid filepath that is not taken by another file.

File

modules/simpletest/tests/file.test, line 1149

Class

FileDirectoryTest
Directory related tests.

Code

function testFileCreateNewFilepath() {
    // First we test against an imaginary file that does not exist in a
    // directory.
    $basename = 'xyz.txt';
    $directory = 'misc';
    $original = $directory . '/' . $basename;
    $path = file_create_filename($basename, $directory);
    $this->assertEqual($path, $original, format_string('New filepath %new equals %original.', array(
        '%new' => $path,
        '%original' => $original,
    )), 'File');
    // 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_create_filename($basename, $directory);
    $this->assertEqual($path, $expected, format_string('Creating a new filepath from %original equals %new.', array(
        '%new' => $path,
        '%original' => $original,
    )), 'File');
    try {
        $filename = "a\xfftest\x80€.txt";
        file_create_filename($filename, $directory);
        $this->fail('Expected exception not thrown');
    } catch (RuntimeException $e) {
        $this->assertEqual("Invalid filename '{$filename}'", $e->getMessage(), 'The invalid filename has been detected and RuntimeException has been thrown.');
    }
    // @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.