function FileUnmanagedCopyTest::testOverwriteSelf in SimpleTest 7
Copy a file onto itself.
File
- tests/file.test, line 1190 
- This provides SimpleTests for the core file handling functionality. These include FileValidateTest and FileSaveTest.
Class
- FileUnmanagedCopyTest
- Unmanaged copy related tests.
Code
function testOverwriteSelf() {
  // Create a file for testing
  $file = $this
    ->createFile();
  // Copy the file onto itself with renaming works.
  $new_filepath = file_unmanaged_copy($file->uri, $file->uri, FILE_EXISTS_RENAME);
  $this
    ->assertTrue($new_filepath, t('Copying onto itself with renaming works.'));
  $this
    ->assertNotEqual($new_filepath, $file->uri, t('Copied file has a new name.'));
  $this
    ->assertTrue(file_exists($file->uri), t('Original file exists after copying onto itself.'));
  $this
    ->assertTrue(file_exists($new_filepath), t('Copied file exists after copying onto itself.'));
  $this
    ->assertFilePermissions($new_filepath, variable_get('file_chmod_file', 0664));
  // Copy the file onto itself without renaming fails.
  $new_filepath = file_unmanaged_copy($file->uri, $file->uri, FILE_EXISTS_ERROR);
  $this
    ->assertFalse($new_filepath, t('Copying onto itself without renaming fails.'));
  $this
    ->assertTrue(file_exists($file->uri), t('File exists after copying onto itself.'));
  // Copy the file into same directory without renaming fails.
  $new_filepath = file_unmanaged_copy($file->uri, drupal_dirname($file->uri), FILE_EXISTS_ERROR);
  $this
    ->assertFalse($new_filepath, t('Copying onto itself fails.'));
  $this
    ->assertTrue(file_exists($file->uri), t('File exists after copying onto itself.'));
  // Copy the file into same directory with renaming works.
  $new_filepath = file_unmanaged_copy($file->uri, drupal_dirname($file->uri), FILE_EXISTS_RENAME);
  $this
    ->assertTrue($new_filepath, t('Copying into same directory works.'));
  $this
    ->assertNotEqual($new_filepath, $file->uri, t('Copied file has a new name.'));
  $this
    ->assertTrue(file_exists($file->uri), t('Original file exists after copying onto itself.'));
  $this
    ->assertTrue(file_exists($new_filepath), t('Copied file exists after copying onto itself.'));
  $this
    ->assertFilePermissions($new_filepath, variable_get('file_chmod_file', 0664));
}