1. drupal
    1. 8
    2. 7
7 FileCopyTest::testNormal()

Test file copying in the normal, base case.


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

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 object 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));