function MoveTest::testExistingError

Same name in other branches
  1. 9 core/modules/file/tests/src/Kernel/MoveTest.php \Drupal\Tests\file\Kernel\MoveTest::testExistingError()
  2. 10 core/modules/file/tests/src/Kernel/MoveTest.php \Drupal\Tests\file\Kernel\MoveTest::testExistingError()
  3. 11.x core/modules/file/tests/src/Kernel/MoveTest.php \Drupal\Tests\file\Kernel\MoveTest::testExistingError()

Test that moving onto an existing file fails when instructed to do so.

File

core/modules/file/tests/src/Kernel/MoveTest.php, line 138

Class

MoveTest
Tests the file move function.

Namespace

Drupal\Tests\file\Kernel

Code

public function testExistingError() {
    $contents = $this->randomMachineName(10);
    $source = $this->createFile();
    $target = $this->createFile(NULL, $contents);
    $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(), FileSystemInterface::EXISTS_ERROR);
    // Check the return status and that the contents did not change.
    $this->assertFalse($result, 'File move failed.');
    $this->assertFileExists($source->getFileUri());
    $this->assertEqual($contents, file_get_contents($target->getFileUri()), 'Contents of file were not altered.');
    // Check that no hooks were called while failing.
    $this->assertFileHooksCalled([]);
    // Load the file from the database and make sure it is identical to what
    // was returned.
    $this->assertFileUnchanged($source, File::load($source->id()));
    $this->assertFileUnchanged($target, File::load($target->id()));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.