You are here

function MoveTest::testNormal in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/file/src/Tests/MoveTest.php \Drupal\file\Tests\MoveTest::testNormal()

Move a normal file.

File

core/modules/file/src/Tests/MoveTest.php, line 21
Contains \Drupal\file\Tests\MoveTest.

Class

MoveTest
Tests the file move function.

Namespace

Drupal\file\Tests

Code

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

  // Clone the object so we don't have to worry about the function changing
  // our reference copy.
  $result = file_move(clone $source, $desired_filepath, FILE_EXISTS_ERROR);

  // Check the return status and that the contents changed.
  $this
    ->assertTrue($result, 'File moved successfully.');
  $this
    ->assertFalse(file_exists($source
    ->getFileUri()));
  $this
    ->assertEqual($contents, file_get_contents($result
    ->getFileUri()), 'Contents of file correctly written.');

  // Check that the correct hooks were called.
  $this
    ->assertFileHooksCalled(array(
    'move',
    'load',
    'update',
  ));

  // Make sure we got the same file back.
  $this
    ->assertEqual($source
    ->id(), $result
    ->id(), format_string("Source file id's' %fid is unchanged after move.", array(
    '%fid' => $source
      ->id(),
  )));

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