public function MoveTest::testExistingReplace in Drupal 10
Same name and namespace in other branches
- 8 core/modules/file/tests/src/Kernel/MoveTest.php \Drupal\Tests\file\Kernel\MoveTest::testExistingReplace()
- 9 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\KernelCode
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);
}