function CopyTest::testNormal

Same name in other branches
  1. 9 core/modules/file/tests/src/Kernel/CopyTest.php \Drupal\Tests\file\Kernel\CopyTest::testNormal()
  2. 8.9.x core/modules/file/tests/src/Kernel/CopyTest.php \Drupal\Tests\file\Kernel\CopyTest::testNormal()
  3. 11.x core/modules/file/tests/src/Kernel/CopyTest.php \Drupal\Tests\file\Kernel\CopyTest::testNormal()

Tests file copying in the normal, base case.

@covers ::copy

File

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

Class

CopyTest
Tests the file copy function.

Namespace

Drupal\Tests\file\Kernel

Code

public function testNormal() : void {
    $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 = $this->fileRepository
        ->copy(clone $source, $desired_uri, FileExists::Error);
    // Check the return status and that the contents changed.
    $this->assertNotFalse($result, 'File copied successfully.');
    $this->assertEquals($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->assertEquals($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()));
}

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