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\Core\StreamWrapper\StreamWrapperManagerInterface;
use Drupal\file\Entity\File;
use Drupal\file\FileRepository;
class FileRepositoryTest extends FileManagedUnitTestBase {
protected $fileRepository;
protected $fileSystem;
protected function setUp() : void {
parent::setUp();
$this->fileRepository = $this->container
->get('file.repository');
$this->fileSystem = $this->container
->get('file_system');
}
public function testWithFilename() {
$contents = $this
->randomMachineName();
$filename = 'Текстовый файл.txt';
$result = $this->fileRepository
->writeData($contents, 'public://' . $filename);
$this
->assertNotFalse($result, 'Unnamed file saved correctly.');
$stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');
assert($stream_wrapper_manager instanceof StreamWrapperManagerInterface);
$this
->assertEquals('public', $stream_wrapper_manager::getScheme($result
->getFileUri()), "File was placed in Drupal's files directory.");
$this
->assertEquals($filename, \Drupal::service('file_system')
->basename($result
->getFileUri()), 'File was named correctly.');
$this
->assertEquals($contents, file_get_contents($result
->getFileUri()), 'Contents of the file are correct.');
$this
->assertEquals('text/plain', $result
->getMimeType(), 'A MIME type was set.');
$this
->assertTrue($result
->isPermanent(), "The file's status was set to permanent.");
$this
->assertFileHooksCalled([
'insert',
]);
$this
->assertFileUnchanged($result, File::load($result
->id()));
}
public function testExistingRename() {
$existing = $this
->createFile();
$contents = $this
->randomMachineName();
$result = $this->fileRepository
->writeData($contents, $existing
->getFileUri());
$this
->assertNotFalse($result, 'File saved successfully.');
$stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');
assert($stream_wrapper_manager instanceof StreamWrapperManagerInterface);
$this
->assertEquals('public', $stream_wrapper_manager::getScheme($result
->getFileUri()), "File was placed in Drupal's files directory.");
$this
->assertEquals($existing
->getFilename(), $result
->getFilename(), 'Filename was set to the basename of the source, rather than that of the renamed file.');
$this
->assertEquals($contents, file_get_contents($result
->getFileUri()), 'Contents of the file are correct.');
$this
->assertEquals('application/octet-stream', $result
->getMimeType(), 'A MIME type was set.');
$this
->assertTrue($result
->isPermanent(), "The file's status was set to permanent.");
$this
->assertFileHooksCalled([
'insert',
]);
$this
->assertDifferentFile($existing, $result);
$this
->assertFileUnchanged($existing, File::load($existing
->id()));
$this
->assertFileUnchanged($result, File::load($result
->id()));
}
public function testExistingReplace() {
$existing = $this
->createFile();
$contents = $this
->randomMachineName();
$result = $this->fileRepository
->writeData($contents, $existing
->getFileUri(), FileSystemInterface::EXISTS_REPLACE);
$this
->assertNotFalse($result, 'File saved successfully.');
$stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');
assert($stream_wrapper_manager instanceof StreamWrapperManagerInterface);
$this
->assertEquals('public', $stream_wrapper_manager::getScheme($result
->getFileUri()), "File was placed in Drupal's files directory.");
$this
->assertEquals($existing
->getFilename(), $result
->getFilename(), 'Filename was set to the basename of the existing file, rather than preserving the original name.');
$this
->assertEquals($contents, file_get_contents($result
->getFileUri()), 'Contents of the file are correct.');
$this
->assertEquals('application/octet-stream', $result
->getMimeType(), 'A MIME type was set.');
$this
->assertTrue($result
->isPermanent(), "The file's status was set to permanent.");
$this
->assertFileHooksCalled([
'load',
'update',
]);
$this
->assertSameFile($existing, $result);
$this
->assertFileUnchanged($result, File::load($result
->id()));
}
public function testExistingError() {
$contents = $this
->randomMachineName();
$existing = $this
->createFile(NULL, $contents);
try {
$this->fileRepository
->writeData('asdf', $existing
->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($existing
->getFileUri()), 'Contents of existing file were unchanged.');
$this
->assertFileHooksCalled([]);
$this
->assertFileUnchanged($existing, File::load($existing
->id()));
}
public function testInvalidStreamWrapper() {
$this
->expectException(InvalidStreamWrapperException::class);
$this->fileRepository
->writeData('asdf', '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);
$target = $this
->createFile();
$fileRepository
->writeData('asdf', $target
->getFileUri(), FileSystemInterface::EXISTS_REPLACE);
}
public function testLoadByUri() {
$source = $this
->createFile();
$result = $this->fileRepository
->loadByUri($source
->getFileUri());
$this
->assertSameFile($source, $result);
}
public function testLoadByUriCaseSensitive() {
$source = $this
->createFile('FooBar.txt');
$result = $this->fileRepository
->loadByUri('public://FooBar.txt');
$this
->assertSameFile($source, $result);
$result = $this->fileRepository
->loadByUri('public://foobar.txt');
$this
->assertNull($result);
}
}