View source
<?php
namespace Drupal\Tests\file\Kernel;
use Drupal\Core\File\FileSystemInterface;
use Drupal\file\Entity\File;
class CopyTest extends FileManagedUnitTestBase {
public function testNormal() {
$contents = $this
->randomMachineName(10);
$source = $this
->createFile(NULL, $contents);
$desired_uri = 'public://' . $this
->randomMachineName();
$result = file_copy(clone $source, $desired_uri, FileSystemInterface::EXISTS_ERROR);
$this
->assertNotFalse($result, 'File copied successfully.');
$this
->assertEquals($contents, file_get_contents($result
->getFileUri()), 'Contents of file were copied correctly.');
$this
->assertFileHooksCalled([
'copy',
'insert',
]);
$this
->assertDifferentFile($source, $result);
$this
->assertEquals($result
->getFileUri(), $desired_uri, 'The copied file entity has the desired filepath.');
$this
->assertFileExists($source
->getFileUri());
$this
->assertFileExists($result
->getFileUri());
$this
->assertFileUnchanged($result, File::load($result
->id()));
}
public function testExistingRename() {
$contents = $this
->randomMachineName(10);
$source = $this
->createFile(NULL, $contents);
$target = $this
->createFile();
$this
->assertDifferentFile($source, $target);
$result = file_copy(clone $source, $target
->getFileUri(), FileSystemInterface::EXISTS_RENAME);
$this
->assertNotFalse($result, 'File copied successfully.');
$this
->assertEquals($contents, file_get_contents($result
->getFileUri()), 'Contents of file were copied correctly.');
$this
->assertNotEquals($source
->getFileUri(), $result
->getFileUri(), 'Returned file path has changed from the original.');
$this
->assertFileHooksCalled([
'copy',
'insert',
]);
$loaded_source = File::load($source
->id());
$loaded_target = File::load($target
->id());
$loaded_result = File::load($result
->id());
$this
->assertFileUnchanged($source, $loaded_source);
$this
->assertFileUnchanged($result, $loaded_result);
$this
->assertDifferentFile($loaded_source, $loaded_target);
$this
->assertDifferentFile($loaded_target, $loaded_result);
$this
->assertDifferentFile($loaded_source, $loaded_result);
}
public function testExistingReplace() {
$contents = $this
->randomMachineName(10);
$source = $this
->createFile(NULL, $contents);
$target = $this
->createFile();
$this
->assertDifferentFile($source, $target);
$result = file_copy(clone $source, $target
->getFileUri(), FileSystemInterface::EXISTS_REPLACE);
$this
->assertNotFalse($result, 'File copied successfully.');
$this
->assertEquals($contents, file_get_contents($result
->getFileUri()), 'Contents of file were overwritten.');
$this
->assertDifferentFile($source, $result);
$this
->assertFileHooksCalled([
'load',
'copy',
'update',
]);
$loaded_source = File::load($source
->id());
$loaded_target = File::load($target
->id());
$loaded_result = File::load($result
->id());
$this
->assertFileUnchanged($source, $loaded_source);
$this
->assertFileUnchanged($result, $loaded_result);
$this
->assertFileUnchanged($loaded_target, $loaded_result);
}
public function testExistingError() {
$contents = $this
->randomMachineName(10);
$source = $this
->createFile();
$target = $this
->createFile(NULL, $contents);
$this
->assertDifferentFile($source, $target);
$result = file_copy(clone $source, $target
->getFileUri(), FileSystemInterface::EXISTS_ERROR);
$this
->assertFalse($result, 'File copy failed.');
$this
->assertEquals($contents, file_get_contents($target
->getFileUri()), 'Contents of file were not altered.');
$this
->assertFileHooksCalled([]);
$this
->assertFileUnchanged($source, File::load($source
->id()));
$this
->assertFileUnchanged($target, File::load($target
->id()));
}
}