function MoveTest::testExistingRename
Same name in other branches
- 8.9.x core/modules/file/tests/src/Kernel/MoveTest.php \Drupal\Tests\file\Kernel\MoveTest::testExistingRename()
- 10 core/modules/file/tests/src/Kernel/MoveTest.php \Drupal\Tests\file\Kernel\MoveTest::testExistingRename()
- 11.x core/modules/file/tests/src/Kernel/MoveTest.php \Drupal\Tests\file\Kernel\MoveTest::testExistingRename()
Tests renaming when moving onto a file that already exists.
@covers ::move
File
-
core/
modules/ file/ tests/ src/ Kernel/ MoveTest.php, line 74
Class
- MoveTest
- Tests the file move function.
Namespace
Drupal\Tests\file\KernelCode
public function testExistingRename() {
// 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());
// Check the return status and that the contents changed.
$this->assertNotFalse($result, 'File moved successfully.');
$this->assertFileDoesNotExist($source->getFileUri());
$this->assertEquals($contents, file_get_contents($result->getFileUri()), 'Contents of file correctly written.');
// Check that the correct hooks were called.
$this->assertFileHooksCalled([
'move',
'load',
'update',
]);
// Compare the returned value to what made it into the database.
$this->assertFileUnchanged($result, File::load($result->id()));
// The target file should not have been altered.
$this->assertFileUnchanged($target, File::load($target->id()));
// Make sure we end up with two distinct files afterwards.
$this->assertDifferentFile($target, $result);
// Compare the source and results.
$loaded_source = File::load($source->id());
$this->assertEquals($result->id(), $loaded_source->id(), "Returned file's id matches the source.");
$this->assertNotEquals($source->getFileUri(), $loaded_source->getFileUri(), 'Returned file path has changed from the original.');
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.