More updates to stop using dev or alpha or beta versions.
[yaffs-website] / web / core / tests / Drupal / Tests / Core / File / FileSystemTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\File;
4
5 use Drupal\Core\File\FileSystem;
6 use Drupal\Core\Site\Settings;
7 use Drupal\Tests\UnitTestCase;
8 use org\bovigo\vfs\vfsStream;
9
10 /**
11  * @coversDefaultClass \Drupal\Core\File\FileSystem
12  *
13  * @group File
14  */
15 class FileSystemTest extends UnitTestCase {
16
17   /**
18    * @var \Drupal\Core\File\FileSystem
19    */
20   protected $fileSystem;
21
22   /**
23    * The file logger channel.
24    *
25    * @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
26    */
27   protected $logger;
28
29   /**
30    * {@inheritdoc}
31    */
32   protected function setUp() {
33     parent::setUp();
34
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);
39   }
40
41   /**
42    * @covers ::chmod
43    */
44   public function testChmodFile() {
45     vfsStream::setup('dir');
46     vfsStream::create(['test.txt' => 'asdf']);
47     $uri = 'vfs://dir/test.txt';
48
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);
53   }
54
55   /**
56    * @covers ::chmod
57    */
58   public function testChmodDir() {
59     vfsStream::setup('dir');
60     vfsStream::create(['nested_dir' => []]);
61     $uri = 'vfs://dir/nested_dir';
62
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);
67   }
68
69   /**
70    * @covers ::chmod
71    */
72   public function testChmodUnsuccessful() {
73     vfsStream::setup('dir');
74     $this->logger->expects($this->once())
75       ->method('error');
76     $this->assertFalse($this->fileSystem->chmod('vfs://dir/test.txt'));
77   }
78
79   /**
80    * @covers ::unlink
81    */
82   public function testUnlink() {
83     vfsStream::setup('dir');
84     vfsStream::create(['test.txt' => 'asdf']);
85     $uri = 'vfs://dir/test.txt';
86
87     $this->fileSystem = $this->getMockBuilder('Drupal\Core\File\FileSystem')
88       ->disableOriginalConstructor()
89       ->setMethods(['validScheme'])
90       ->getMock();
91     $this->fileSystem->expects($this->once())
92       ->method('validScheme')
93       ->willReturn(TRUE);
94
95     $this->assertFileExists($uri);
96     $this->fileSystem->unlink($uri);
97     $this->assertFileNotExists($uri);
98   }
99
100   /**
101    * @covers ::basename
102    *
103    * @dataProvider providerTestBasename
104    */
105   public function testBasename($uri, $expected, $suffix = NULL) {
106     $this->assertSame($expected, $this->fileSystem->basename($uri, $suffix));
107   }
108
109   public function providerTestBasename() {
110     $data = [];
111     $data[] = [
112       'public://nested/dir',
113       'dir',
114     ];
115     $data[] = [
116       'public://dir/test.txt',
117       'test.txt',
118     ];
119     $data[] = [
120       'public://dir/test.txt',
121       'test',
122       '.txt'
123     ];
124     return $data;
125   }
126
127   /**
128    * @covers ::uriScheme
129    *
130    * @dataProvider providerTestUriScheme
131    */
132   public function testUriScheme($uri, $expected) {
133     $this->assertSame($expected, $this->fileSystem->uriScheme($uri));
134   }
135
136   public function providerTestUriScheme() {
137     $data = [];
138     $data[] = [
139       'public://filename',
140       'public',
141     ];
142     $data[] = [
143       'public://extra://',
144       'public',
145     ];
146     $data[] = [
147       'invalid',
148       FALSE,
149     ];
150     return $data;
151   }
152
153   /**
154    * Asserts that the file permissions of a given URI matches.
155    *
156    * @param int $expected_mode
157    * @param string $uri
158    * @param string $message
159    */
160   protected function assertFilePermissions($expected_mode, $uri, $message = '') {
161     // Mask out all but the last three octets.
162     $actual_mode = fileperms($uri) & 0777;
163
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;
174     }
175     $this->assertSame($expected_mode, $actual_mode, $message);
176   }
177
178 }