View source
<?php
namespace Drupal\Tests\Core\File;
use Drupal\Core\File\Exception\FileException;
use Drupal\Core\File\FileSystem;
use Drupal\Core\Site\Settings;
use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
use Drupal\Tests\UnitTestCase;
use org\bovigo\vfs\vfsStream;
class FileSystemTest extends UnitTestCase {
protected $fileSystem;
protected $logger;
protected $streamWrapperManager;
protected function setUp() : void {
parent::setUp();
$settings = new Settings([]);
$this->streamWrapperManager = $this
->createMock(StreamWrapperManagerInterface::class);
$this->logger = $this
->createMock('Psr\\Log\\LoggerInterface');
$this->fileSystem = new FileSystem($this->streamWrapperManager, $settings, $this->logger);
}
public function testChmodFile() {
vfsStream::setup('dir');
vfsStream::create([
'test.txt' => 'asdf',
]);
$uri = 'vfs://dir/test.txt';
$this
->assertTrue($this->fileSystem
->chmod($uri));
$this
->assertFilePermissions(FileSystem::CHMOD_FILE, $uri);
$this
->assertTrue($this->fileSystem
->chmod($uri, 0444));
$this
->assertFilePermissions(0444, $uri);
}
public function testChmodDir() {
vfsStream::setup('dir');
vfsStream::create([
'nested_dir' => [],
]);
$uri = 'vfs://dir/nested_dir';
$this
->assertTrue($this->fileSystem
->chmod($uri));
$this
->assertFilePermissions(FileSystem::CHMOD_DIRECTORY, $uri);
$this
->assertTrue($this->fileSystem
->chmod($uri, 0444));
$this
->assertFilePermissions(0444, $uri);
}
public function testChmodUnsuccessful() {
vfsStream::setup('dir');
$this->logger
->expects($this
->once())
->method('error');
$this
->assertFalse($this->fileSystem
->chmod('vfs://dir/test.txt'));
}
public function testUnlink() {
vfsStream::setup('dir');
vfsStream::create([
'test.txt' => 'asdf',
]);
$uri = 'vfs://dir/test.txt';
$this->streamWrapperManager
->expects($this
->once())
->method('isValidUri')
->willReturn(TRUE);
$this
->assertFileExists($uri);
$this->fileSystem
->unlink($uri);
$this
->assertFileDoesNotExist($uri);
}
public function testBasename($uri, $expected, $suffix = NULL) {
$this
->assertSame($expected, $this->fileSystem
->basename($uri, $suffix));
}
public function providerTestBasename() {
$data = [];
$data[] = [
'public://nested/dir',
'dir',
];
$data[] = [
'public://dir/test.txt',
'test.txt',
];
$data[] = [
'public://dir/test.txt',
'test',
'.txt',
];
return $data;
}
protected function assertFilePermissions($expected_mode, $uri, $message = '') {
$actual_mode = fileperms($uri) & 0777;
if (substr(PHP_OS, 0, 3) == 'WIN') {
$expected_mode = $expected_mode & 0700;
$expected_mode = $expected_mode | $expected_mode >> 3 | $expected_mode >> 6;
}
$this
->assertSame($expected_mode, $actual_mode, $message);
}
public function testInvalidUTF8() {
vfsStream::setup('dir');
$filename = "" . '.txt';
$this
->expectException(FileException::class);
$this
->expectExceptionMessage("Invalid filename '{$filename}'");
$this->fileSystem
->createFilename($filename, 'vfs://dir');
}
}