function FileCopyTest::testNormal

Same name and namespace in other branches
  1. 7.x modules/simpletest/tests/file.test \FileCopyTest::testNormal()
  2. 9 core/tests/Drupal/KernelTests/Core/File/FileCopyTest.php \Drupal\KernelTests\Core\File\FileCopyTest::testNormal()
  3. 8.9.x core/tests/Drupal/KernelTests/Core/File/FileCopyTest.php \Drupal\KernelTests\Core\File\FileCopyTest::testNormal()
  4. 11.x core/tests/Drupal/KernelTests/Core/File/FileCopyTest.php \Drupal\KernelTests\Core\File\FileCopyTest::testNormal()

Copy a normal file.

File

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

Class

FileCopyTest
Tests the unmanaged file copy function.

Namespace

Drupal\KernelTests\Core\File

Code

public function testNormal() : void {
  // 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, FileExists::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, FileExists::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)
}

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