class FileCopyTest

Same name in this branch
  1. 9 core/modules/migrate/tests/src/Unit/process/FileCopyTest.php \Drupal\Tests\migrate\Unit\process\FileCopyTest
  2. 9 core/modules/migrate/tests/src/Kernel/process/FileCopyTest.php \Drupal\Tests\migrate\Kernel\process\FileCopyTest
Same name and namespace in other branches
  1. 7.x modules/simpletest/tests/file.test \FileCopyTest
  2. 11.x core/modules/migrate/tests/src/Unit/process/FileCopyTest.php \Drupal\Tests\migrate\Unit\process\FileCopyTest
  3. 11.x core/modules/migrate/tests/src/Kernel/process/FileCopyTest.php \Drupal\Tests\migrate\Kernel\process\FileCopyTest
  4. 11.x core/tests/Drupal/KernelTests/Core/File/FileCopyTest.php \Drupal\KernelTests\Core\File\FileCopyTest
  5. 10 core/modules/migrate/tests/src/Unit/process/FileCopyTest.php \Drupal\Tests\migrate\Unit\process\FileCopyTest
  6. 10 core/modules/migrate/tests/src/Kernel/process/FileCopyTest.php \Drupal\Tests\migrate\Kernel\process\FileCopyTest
  7. 10 core/tests/Drupal/KernelTests/Core/File/FileCopyTest.php \Drupal\KernelTests\Core\File\FileCopyTest
  8. 8.9.x core/modules/migrate/tests/src/Unit/process/FileCopyTest.php \Drupal\Tests\migrate\Unit\process\FileCopyTest
  9. 8.9.x core/modules/migrate/tests/src/Kernel/process/FileCopyTest.php \Drupal\Tests\migrate\Kernel\process\FileCopyTest
  10. 8.9.x core/tests/Drupal/KernelTests/Core/File/FileCopyTest.php \Drupal\KernelTests\Core\File\FileCopyTest

Tests the unmanaged file copy function.

@group File

Hierarchy

Expanded class hierarchy of FileCopyTest

File

core/tests/Drupal/KernelTests/Core/File/FileCopyTest.php, line 16

Namespace

Drupal\KernelTests\Core\File
View source
class FileCopyTest extends FileTestBase {
  
  /**
   * Copy a normal file.
   */
  public function testNormal() {
    // Create a file for testing
    $uri = $this->createUri();
    // Copying to a new name.
    $desired_filepath = 'public://' . $this->randomMachineName();
    $new_filepath = \Drupal::service('file_system')->copy($uri, $desired_filepath, FileSystemInterface::EXISTS_ERROR);
    $this->assertNotFalse($new_filepath, 'Copy was successful.');
    $this->assertEquals($desired_filepath, $new_filepath, 'Returned expected filepath.');
    $this->assertFileExists($uri);
    $this->assertFileExists($new_filepath);
    $this->assertFilePermissions($new_filepath, Settings::get('file_chmod_file', FileSystem::CHMOD_FILE));
    // Copying with rename.
    $desired_filepath = 'public://' . $this->randomMachineName();
    $this->assertNotFalse(file_put_contents($desired_filepath, ' '), 'Created a file so a rename will have to happen.');
    $newer_filepath = \Drupal::service('file_system')->copy($uri, $desired_filepath, FileSystemInterface::EXISTS_RENAME);
    $this->assertNotFalse($newer_filepath, 'Copy was successful.');
    $this->assertNotEquals($desired_filepath, $newer_filepath, 'Returned expected filepath.');
    $this->assertFileExists($uri);
    $this->assertFileExists($newer_filepath);
    $this->assertFilePermissions($newer_filepath, Settings::get('file_chmod_file', FileSystem::CHMOD_FILE));
    // TODO: test copying to a directory (rather than full directory/file path)
    // TODO: test copying normal files using normal paths (rather than only streams)
  }
  
  /**
   * Copy a non-existent file.
   */
  public function testNonExistent() {
    // Copy non-existent file
    $desired_filepath = $this->randomMachineName();
    $this->assertFileDoesNotExist($desired_filepath);
    $this->expectException(FileNotExistsException::class);
    $new_filepath = \Drupal::service('file_system')->copy($desired_filepath, $this->randomMachineName());
    $this->assertFalse($new_filepath, 'Copying a missing file fails.');
  }
  
  /**
   * Copy a file onto itself.
   */
  public function testOverwriteSelf() {
    // Create a file for testing
    $uri = $this->createUri();
    // Copy the file onto itself with renaming works.
    /** @var \Drupal\Core\File\FileSystemInterface $file_system */
    $file_system = \Drupal::service('file_system');
    $new_filepath = $file_system->copy($uri, $uri, FileSystemInterface::EXISTS_RENAME);
    $this->assertNotFalse($new_filepath, 'Copying onto itself with renaming works.');
    $this->assertNotEquals($uri, $new_filepath, 'Copied file has a new name.');
    $this->assertFileExists($uri);
    $this->assertFileExists($new_filepath);
    $this->assertFilePermissions($new_filepath, Settings::get('file_chmod_file', FileSystem::CHMOD_FILE));
    // Copy the file onto itself without renaming fails.
    $this->expectException(FileExistsException::class);
    $new_filepath = $file_system->copy($uri, $uri, FileSystemInterface::EXISTS_ERROR);
    $this->assertFalse($new_filepath, 'Copying onto itself without renaming fails.');
    $this->assertFileExists($uri);
    // Copy the file into same directory without renaming fails.
    $new_filepath = $file_system->copy($uri, $file_system->dirname($uri), FileSystemInterface::EXISTS_ERROR);
    $this->assertFalse($new_filepath, 'Copying onto itself fails.');
    $this->assertFileExists($uri);
    // Copy the file into same directory with renaming works.
    $new_filepath = $file_system->copy($uri, $file_system->dirname($uri), FileSystemInterface::EXISTS_RENAME);
    $this->assertNotFalse($new_filepath, 'Copying into same directory works.');
    $this->assertNotEquals($uri, $new_filepath, 'Copied file has a new name.');
    $this->assertFileExists($uri);
    $this->assertFileExists($new_filepath);
    $this->assertFilePermissions($new_filepath, Settings::get('file_chmod_file', FileSystem::CHMOD_FILE));
  }

}

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