function MoveTest::testExistingReplace

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

Tests replacement when moving onto a file that already exists.

@covers ::move

File

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

Class

MoveTest
Tests the file move function.

Namespace

Drupal\Tests\file\Kernel

Code

public 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 = $this->fileRepository
        ->move(clone $source, $target->getFileUri(), FileSystemInterface::EXISTS_REPLACE);
    // Look at the results.
    $this->assertEquals($contents, file_get_contents($result->getFileUri()), 'Contents of file were overwritten.');
    $this->assertFileDoesNotExist($source->getFileUri());
    $this->assertNotEmpty($result, 'File moved successfully.');
    // Check that the correct hooks were called.
    $this->assertFileHooksCalled([
        '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);
}

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