function CopyTest::testExistingRename in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/file/src/Tests/CopyTest.php \Drupal\file\Tests\CopyTest::testExistingRename()
Test renaming when copying over a file that already exists.
File
- core/
modules/ file/ src/ Tests/ CopyTest.php, line 50 - Contains \Drupal\file\Tests\CopyTest.
Class
- CopyTest
- Tests the file copy function.
Namespace
Drupal\file\TestsCode
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_copy(clone $source, $target
->getFileUri(), FILE_EXISTS_RENAME);
// Check the return status and that the contents changed.
$this
->assertTrue($result, 'File copied successfully.');
$this
->assertEqual($contents, file_get_contents($result
->getFileUri()), 'Contents of file were copied correctly.');
$this
->assertNotEqual($result
->getFileUri(), $source
->getFileUri(), 'Returned file path has changed from the original.');
// Check that the correct hooks were called.
$this
->assertFileHooksCalled(array(
'copy',
'insert',
));
// Load all the affected files to check the changes that actually made it
// to the database.
$loaded_source = File::load($source
->id());
$loaded_target = File::load($target
->id());
$loaded_result = File::load($result
->id());
// Verify that the source file wasn't changed.
$this
->assertFileUnchanged($source, $loaded_source);
// Verify that what was returned is what's in the database.
$this
->assertFileUnchanged($result, $loaded_result);
// Make sure we end up with three distinct files afterwards.
$this
->assertDifferentFile($loaded_source, $loaded_target);
$this
->assertDifferentFile($loaded_target, $loaded_result);
$this
->assertDifferentFile($loaded_source, $loaded_result);
}