View source
<?php
namespace Drupal\Tests\file\Kernel;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\File\Exception\FileExistsException;
use Drupal\Core\File\Exception\InvalidStreamWrapperException;
use Drupal\Core\File\FileSystemInterface;
use Drupal\file\Entity\File;
use Drupal\file\FileRepository;
class CopyTest extends FileManagedUnitTestBase {
protected $fileRepository;
protected function setUp() : void {
parent::setUp();
$this->fileRepository = $this->container
->get('file.repository');
}
public function testNormal() {
$contents = $this
->randomMachineName(10);
$source = $this
->createFile(NULL, $contents);
$desired_uri = 'public://' . $this
->randomMachineName();
$result = $this->fileRepository
->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 = $this->fileRepository
->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 = $this->fileRepository
->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);
try {
$result = $this->fileRepository
->copy(clone $source, $target
->getFileUri(), FileSystemInterface::EXISTS_ERROR);
$this
->fail('expected FileExistsException');
} catch (FileExistsException $e) {
$this
->assertStringContainsString("could not be copied because a file by that name already exists in the destination directory", $e
->getMessage());
}
$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()));
}
public function testInvalidStreamWrapper() {
$this
->expectException(InvalidStreamWrapperException::class);
$source = $this
->createFile();
$this->fileRepository
->copy($source, 'foo://');
}
public function testEntityStorageException() {
$entityTypeManager = $this
->prophesize(EntityTypeManager::class);
$entityTypeManager
->getStorage('file')
->willThrow(EntityStorageException::class);
$fileRepository = new FileRepository($this->container
->get('file_system'), $this->container
->get('stream_wrapper_manager'), $entityTypeManager
->reveal(), $this->container
->get('module_handler'), $this->container
->get('file.usage'), $this->container
->get('current_user'));
$this
->expectException(EntityStorageException::class);
$source = $this
->createFile();
$target = $this
->createFile();
$fileRepository
->copy($source, $target
->getFileUri(), FileSystemInterface::EXISTS_REPLACE);
}
}