You are here

public function CopyTest::testNormal in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/file/tests/src/Kernel/CopyTest.php \Drupal\Tests\file\Kernel\CopyTest::testNormal()

Test file copying in the normal, base case.

File

core/modules/file/tests/src/Kernel/CopyTest.php, line 18

Class

CopyTest
Tests the file copy function.

Namespace

Drupal\Tests\file\Kernel

Code

public function testNormal() {
  $contents = $this
    ->randomMachineName(10);
  $source = $this
    ->createFile(NULL, $contents);
  $desired_uri = 'public://' . $this
    ->randomMachineName();

  // Clone the object so we don't have to worry about the function changing
  // our reference copy.
  $result = file_copy(clone $source, $desired_uri, FileSystemInterface::EXISTS_ERROR);

  // Check the return status and that the contents changed.
  $this
    ->assertNotFalse($result, 'File copied successfully.');
  $this
    ->assertEqual($contents, file_get_contents($result
    ->getFileUri()), 'Contents of file were copied correctly.');

  // Check that the correct hooks were called.
  $this
    ->assertFileHooksCalled([
    'copy',
    'insert',
  ]);
  $this
    ->assertDifferentFile($source, $result);
  $this
    ->assertEqual($result
    ->getFileUri(), $desired_uri, 'The copied file entity has the desired filepath.');
  $this
    ->assertFileExists($source
    ->getFileUri());
  $this
    ->assertFileExists($result
    ->getFileUri());

  // Reload the file from the database and check that the changes were
  // actually saved.
  $this
    ->assertFileUnchanged($result, File::load($result
    ->id()));
}