public function MoveTest::testExistingRename in Drupal 8
Same name and namespace in other branches
- 9 core/modules/file/tests/src/Kernel/MoveTest.php \Drupal\Tests\file\Kernel\MoveTest::testExistingRename()
Test renaming when moving onto a file that already exists.
File
- core/
modules/ file/ tests/ src/ Kernel/ MoveTest.php, line 49
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 = file_move(clone $source, $target
->getFileUri(), FileSystemInterface::EXISTS_RENAME);
// Check the return status and that the contents changed.
$this
->assertNotFalse($result, 'File moved successfully.');
$this
->assertFileNotExists($source
->getFileUri());
$this
->assertEqual($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
->assertEqual($loaded_source
->id(), $result
->id(), "Returned file's id matches the source.");
$this
->assertNotEqual($loaded_source
->getFileUri(), $source
->getFileUri(), 'Returned file path has changed from the original.');
}