function FileCopyTest::testNormal

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/File/FileCopyTest.php \Drupal\KernelTests\Core\File\FileCopyTest::testNormal()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/File/FileCopyTest.php \Drupal\KernelTests\Core\File\FileCopyTest::testNormal()
  3. 10 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()

Test file copying in the normal, base case.

File

modules/simpletest/tests/file.test, line 1968

Class

FileCopyTest
Copy related tests.

Code

function testNormal() {
    $contents = $this->randomName(10);
    $source = $this->createFile(NULL, $contents);
    $desired_uri = 'public://' . $this->randomName();
    // Clone the object so we don't have to worry about the function changing
    // our reference copy.
    $result = file_copy(clone $source, $desired_uri, FILE_EXISTS_ERROR);
    // Check the return status and that the contents changed.
    $this->assertTrue($result, 'File copied successfully.');
    $this->assertEqual($contents, file_get_contents($result->uri), 'Contents of file were copied correctly.');
    // Check that the correct hooks were called.
    $this->assertFileHooksCalled(array(
        'copy',
        'insert',
    ));
    $this->assertDifferentFile($source, $result);
    $this->assertEqual($result->uri, $desired_uri, 'The copied file object has the desired filepath.');
    $this->assertTrue(file_exists($source->uri), 'The original file still exists.');
    $this->assertTrue(file_exists($result->uri), 'The copied file exists.');
    // Reload the file from the database and check that the changes were
    // actually saved.
    $this->assertFileUnchanged($result, file_load($result->fid, TRUE));
}

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