function MoveTest::testExistingReplaceSelf

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

Tests replacement when moving onto itself.

@covers ::move

File

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

Class

MoveTest
Tests the file move function.

Namespace

Drupal\Tests\file\Kernel

Code

public function testExistingReplaceSelf() : void {
    // Setup a file to overwrite.
    $contents = $this->randomMachineName(10);
    $source = $this->createFile(NULL, $contents);
    // Copy the file over itself. Clone the object so we don't have to worry
    // about the function changing our reference copy.
    try {
        $this->fileRepository
            ->move(clone $source, $source->getFileUri(), FileExists::Error);
        $this->fail('expected FileExistsException');
    } catch (FileExistsException $e) {
        $this->assertStringContainsString("could not be copied because a file by that name already exists in the destination directory", $e->getMessage());
    }
    $this->assertEquals($contents, file_get_contents($source->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()));
}

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