1 file.test FileMoveTest::testExistingReplace()

Test replacement when moving onto a file that already exists.

File

core/modules/simpletest/tests/file.test, line 1846
This provides SimpleTests for the core file handling functionality. These include FileValidateTest and FileSaveTest.

Class

FileMoveTest
Move related tests

Code

function testExistingReplace() {
  // 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_REPLACE);

  // Look at the results.
  $this->assertEqual($contents, file_get_contents($result->uri), 'Contents of file were overwritten.');
  $this->assertFalse(file_exists($source->uri));
  $this->assertTrue($result, 'File moved successfully.');

  // Check that the correct hooks were called.
  $this->assertFileHooksCalled(array('move', 'update', 'delete', 'load'));

  // Reload the file from the database and check that the changes were
  // actually saved.
  $loaded_result = file_load($result->fid);
  $this->assertFileUnchanged($result, $loaded_result);
  // Check that target was re-used.
  $this->assertSameFile($target, $loaded_result);
  // Source and result should be totally different.
  $this->assertDifferentFile($source, $loaded_result);
}