You are here

function MoveTest::testExistingReplace 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::testExistingReplace()

Test replacement when moving onto a file that already exists.

File

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

Class

MoveTest
Tests the file move function.

Namespace

Drupal\file\Tests

Code

function testExistingReplace() {

  // Setup a file to overwrite.
  $contents = $this
    ->randomMachineName(10);
  $source = $this
    ->createFile(NULL, $contents);
  $target = $this
    ->createFile();
  $this
    ->assertDifferentFile($source, $target);

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

  // Look at the results.
  $this
    ->assertEqual($contents, file_get_contents($result
    ->getFileUri()), 'Contents of file were overwritten.');
  $this
    ->assertFalse(file_exists($source
    ->getFileUri()));
  $this
    ->assertTrue($result, 'File moved successfully.');

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

  // Reload the file from the database and check that the changes were
  // actually saved.
  $loaded_result = File::load($result
    ->id());
  $this
    ->assertFileUnchanged($result, $loaded_result);

  // Check that target was re-used.
  $this
    ->assertSameFile($target, $loaded_result);

  // Source and result should be totally different.
  $this
    ->assertDifferentFile($source, $loaded_result);
}