function CopyTest::testNormal

Test file copying in the normal, base case.

File

drupal/core/modules/file/lib/Drupal/file/Tests/CopyTest.php, line 25
Definition of Drupal\file\Tests\CopyTest.

Class

CopyTest
Copy related tests.

Namespace

Drupal\file\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, t('File copied successfully.'));
  $this
    ->assertEqual($contents, file_get_contents($result->uri), t('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, t('The copied file entity has the desired filepath.'));
  $this
    ->assertTrue(file_exists($source->uri), t('The original file still exists.'));
  $this
    ->assertTrue(file_exists($result->uri), t('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));
}