3 namespace Drupal\Tests\Core\File;
5 use Drupal\Core\File\FileSystem;
6 use Drupal\Core\Site\Settings;
7 use Drupal\Tests\UnitTestCase;
8 use org\bovigo\vfs\vfsStream;
11 * @coversDefaultClass \Drupal\Core\File\FileSystem
15 class FileSystemTest extends UnitTestCase {
18 * @var \Drupal\Core\File\FileSystem
20 protected $fileSystem;
23 * The file logger channel.
25 * @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
32 protected function setUp() {
35 $settings = new Settings([]);
36 $stream_wrapper_manager = $this->getMock('Drupal\Core\StreamWrapper\StreamWrapperManagerInterface');
37 $this->logger = $this->getMock('Psr\Log\LoggerInterface');
38 $this->fileSystem = new FileSystem($stream_wrapper_manager, $settings, $this->logger);
44 public function testChmodFile() {
45 vfsStream::setup('dir');
46 vfsStream::create(['test.txt' => 'asdf']);
47 $uri = 'vfs://dir/test.txt';
49 $this->assertTrue($this->fileSystem->chmod($uri));
50 $this->assertFilePermissions(FileSystem::CHMOD_FILE, $uri);
51 $this->assertTrue($this->fileSystem->chmod($uri, 0444));
52 $this->assertFilePermissions(0444, $uri);
58 public function testChmodDir() {
59 vfsStream::setup('dir');
60 vfsStream::create(['nested_dir' => []]);
61 $uri = 'vfs://dir/nested_dir';
63 $this->assertTrue($this->fileSystem->chmod($uri));
64 $this->assertFilePermissions(FileSystem::CHMOD_DIRECTORY, $uri);
65 $this->assertTrue($this->fileSystem->chmod($uri, 0444));
66 $this->assertFilePermissions(0444, $uri);
72 public function testChmodUnsuccessful() {
73 vfsStream::setup('dir');
74 $this->logger->expects($this->once())
76 $this->assertFalse($this->fileSystem->chmod('vfs://dir/test.txt'));
82 public function testUnlink() {
83 vfsStream::setup('dir');
84 vfsStream::create(['test.txt' => 'asdf']);
85 $uri = 'vfs://dir/test.txt';
87 $this->fileSystem = $this->getMockBuilder('Drupal\Core\File\FileSystem')
88 ->disableOriginalConstructor()
89 ->setMethods(['validScheme'])
91 $this->fileSystem->expects($this->once())
92 ->method('validScheme')
95 $this->assertFileExists($uri);
96 $this->fileSystem->unlink($uri);
97 $this->assertFileNotExists($uri);
103 * @dataProvider providerTestBasename
105 public function testBasename($uri, $expected, $suffix = NULL) {
106 $this->assertSame($expected, $this->fileSystem->basename($uri, $suffix));
109 public function providerTestBasename() {
112 'public://nested/dir',
116 'public://dir/test.txt',
120 'public://dir/test.txt',
128 * @covers ::uriScheme
130 * @dataProvider providerTestUriScheme
132 public function testUriScheme($uri, $expected) {
133 $this->assertSame($expected, $this->fileSystem->uriScheme($uri));
136 public function providerTestUriScheme() {
154 * Asserts that the file permissions of a given URI matches.
156 * @param int $expected_mode
158 * @param string $message
160 protected function assertFilePermissions($expected_mode, $uri, $message = '') {
161 // Mask out all but the last three octets.
162 $actual_mode = fileperms($uri) & 0777;
164 // PHP on Windows has limited support for file permissions. Usually each of
165 // "user", "group" and "other" use one octal digit (3 bits) to represent the
166 // read/write/execute bits. On Windows, chmod() ignores the "group" and
167 // "other" bits, and fileperms() returns the "user" bits in all three
168 // positions. $expected_mode is updated to reflect this.
169 if (substr(PHP_OS, 0, 3) == 'WIN') {
170 // Reset the "group" and "other" bits.
171 $expected_mode = $expected_mode & 0700;
172 // Shift the "user" bits to the "group" and "other" positions also.
173 $expected_mode = $expected_mode | $expected_mode >> 3 | $expected_mode >> 6;
175 $this->assertSame($expected_mode, $actual_mode, $message);