You are here

function UnmanagedCopyTest::testOverwriteSelf in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/File/UnmanagedCopyTest.php \Drupal\system\Tests\File\UnmanagedCopyTest::testOverwriteSelf()

Copy a file onto itself.

File

core/modules/system/src/Tests/File/UnmanagedCopyTest.php, line 63
Contains \Drupal\system\Tests\File\UnmanagedCopyTest.

Class

UnmanagedCopyTest
Tests the unmanaged file copy function.

Namespace

Drupal\system\Tests\File

Code

function testOverwriteSelf() {

  // Create a file for testing
  $uri = $this
    ->createUri();

  // Copy the file onto itself with renaming works.
  $new_filepath = file_unmanaged_copy($uri, $uri, FILE_EXISTS_RENAME);
  $this
    ->assertTrue($new_filepath, 'Copying onto itself with renaming works.');
  $this
    ->assertNotEqual($new_filepath, $uri, 'Copied file has a new name.');
  $this
    ->assertTrue(file_exists($uri), 'Original file exists after copying onto itself.');
  $this
    ->assertTrue(file_exists($new_filepath), 'Copied file exists after copying onto itself.');
  $this
    ->assertFilePermissions($new_filepath, Settings::get('file_chmod_file', FileSystem::CHMOD_FILE));

  // Copy the file onto itself without renaming fails.
  $new_filepath = file_unmanaged_copy($uri, $uri, FILE_EXISTS_ERROR);
  $this
    ->assertFalse($new_filepath, 'Copying onto itself without renaming fails.');
  $this
    ->assertTrue(file_exists($uri), 'File exists after copying onto itself.');

  // Copy the file into same directory without renaming fails.
  $new_filepath = file_unmanaged_copy($uri, drupal_dirname($uri), FILE_EXISTS_ERROR);
  $this
    ->assertFalse($new_filepath, 'Copying onto itself fails.');
  $this
    ->assertTrue(file_exists($uri), 'File exists after copying onto itself.');

  // Copy the file into same directory with renaming works.
  $new_filepath = file_unmanaged_copy($uri, drupal_dirname($uri), FILE_EXISTS_RENAME);
  $this
    ->assertTrue($new_filepath, 'Copying into same directory works.');
  $this
    ->assertNotEqual($new_filepath, $uri, 'Copied file has a new name.');
  $this
    ->assertTrue(file_exists($uri), 'Original file exists after copying onto itself.');
  $this
    ->assertTrue(file_exists($new_filepath), 'Copied file exists after copying onto itself.');
  $this
    ->assertFilePermissions($new_filepath, Settings::get('file_chmod_file', FileSystem::CHMOD_FILE));
}