| 1 file.test | FileCopyTest::testExistingRename() | 
Test renaming when copying over a file that already exists.
File
- core/modules/ simpletest/ tests/ file.test, line 1978 
- This provides SimpleTests for the core file handling functionality. These include FileValidateTest and FileSaveTest.
Class
- FileCopyTest
- Copy 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_copy(clone $source, $target->uri, FILE_EXISTS_RENAME);
  // Check the return status and that the contents changed.
  $this->assertTrue($result, 'File copied successfully.');
  $this->assertEqual($contents, file_get_contents($result->uri), 'Contents of file were copied correctly.');
  $this->assertNotEqual($result->uri, $source->uri, 'Returned file path has changed from the original.');
  // Check that the correct hooks were called.
  $this->assertFileHooksCalled(array('copy', 'insert'));
  // Load all the affected files to check the changes that actually made it
  // to the database.
  $loaded_source = file_load($source->fid);
  $loaded_target = file_load($target->fid);
  $loaded_result = file_load($result->fid);
  // Verify that the source file wasn't changed.
  $this->assertFileUnchanged($source, $loaded_source);
  // Verify that what was returned is what's in the database.
  $this->assertFileUnchanged($result, $loaded_result);
  // Make sure we end up with three distinct files afterwards.
  $this->assertDifferentFile($loaded_source, $loaded_target);
  $this->assertDifferentFile($loaded_target, $loaded_result);
  $this->assertDifferentFile($loaded_source, $loaded_result);
}
