You are here

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

Test renaming when moving onto a file that already exists.

File

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

Class

MoveTest
Tests the file move function.

Namespace

Drupal\file\Tests

Code

function testExistingRename() {

  // 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_RENAME);

  // 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',
  ));

  // Compare the returned value to what made it into the database.
  $this
    ->assertFileUnchanged($result, File::load($result
    ->id()));

  // The target file should not have been altered.
  $this
    ->assertFileUnchanged($target, File::load($target
    ->id()));

  // Make sure we end up with two distinct files afterwards.
  $this
    ->assertDifferentFile($target, $result);

  // Compare the source and results.
  $loaded_source = File::load($source
    ->id());
  $this
    ->assertEqual($loaded_source
    ->id(), $result
    ->id(), "Returned file's id matches the source.");
  $this
    ->assertNotEqual($loaded_source
    ->getFileUri(), $source
    ->getFileUri(), 'Returned file path has changed from the original.');
}