function FileMoveTest::testExistingRename

Test renaming when moving onto a file that already exists.

File

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

Class

FileMoveTest
Move related tests

Code

function testExistingRename() {
    // Setup a file to overwrite.
    $contents = $this->randomName(10);
    $source = $this->createFile(NULL, $contents);
    $target = $this->createFile();
    $this->assertDifferentFile($source, $target);
    // Clone the object so we don't have to worry about the function changing
    // our reference copy.
    $result = file_move(clone $source, $target->uri, FILE_EXISTS_RENAME);
    // Check the return status and that the contents changed.
    $this->assertTrue($result, 'File moved successfully.');
    $this->assertFalse(file_exists($source->uri));
    $this->assertEqual($contents, file_get_contents($result->uri), 'Contents of file correctly written.');
    // Check that the correct hooks were called.
    $this->assertFileHooksCalled(array(
        'move',
        'load',
        'update',
    ));
    // Compare the returned value to what made it into the database.
    $this->assertFileUnchanged($result, file_load($result->fid, TRUE));
    // The target file should not have been altered.
    $this->assertFileUnchanged($target, file_load($target->fid, TRUE));
    // Make sure we end up with two distinct files afterwards.
    $this->assertDifferentFile($target, $result);
    // Compare the source and results.
    $loaded_source = file_load($source->fid, TRUE);
    $this->assertEqual($loaded_source->fid, $result->fid, "Returned file's id matches the source.");
    $this->assertNotEqual($loaded_source->uri, $source->uri, 'Returned file path has changed from the original.');
}

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