public function MoveTest::testExistingError in Drupal 10
Same name and namespace in other branches
- 8 core/modules/file/tests/src/Kernel/MoveTest.php \Drupal\Tests\file\Kernel\MoveTest::testExistingError()
- 9 core/modules/file/tests/src/Kernel/MoveTest.php \Drupal\Tests\file\Kernel\MoveTest::testExistingError()
Tests that moving onto an existing file fails when instructed to do so.
@covers ::move
File
- core/
modules/ file/ tests/ src/ Kernel/ MoveTest.php, line 175
Class
- MoveTest
- Tests the file move function.
Namespace
Drupal\Tests\file\KernelCode
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.
try {
$result = $this->fileRepository
->move(clone $source, $target
->getFileUri(), FileSystemInterface::EXISTS_ERROR);
$this
->fail('expected FileExistsException');
} catch (FileExistsException $e) {
// expected exception.
$this
->assertStringContainsString("could not be copied because a file by that name already exists in the destination directory", $e
->getMessage());
}
// Check the return status and that the contents did not change.
$this
->assertFileExists($source
->getFileUri());
$this
->assertEquals($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()));
}