class FileSystemTest

Same name in this branch
  1. 11.x core/tests/Drupal/KernelTests/Core/File/FileSystemTest.php \Drupal\KernelTests\Core\File\FileSystemTest
Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/File/FileSystemTest.php \Drupal\KernelTests\Core\File\FileSystemTest
  2. 9 core/tests/Drupal/Tests/Core/File/FileSystemTest.php \Drupal\Tests\Core\File\FileSystemTest
  3. 8.9.x core/tests/Drupal/KernelTests/Core/File/FileSystemTest.php \Drupal\KernelTests\Core\File\FileSystemTest
  4. 8.9.x core/tests/Drupal/Tests/Core/File/FileSystemTest.php \Drupal\Tests\Core\File\FileSystemTest
  5. 10 core/tests/Drupal/KernelTests/Core/File/FileSystemTest.php \Drupal\KernelTests\Core\File\FileSystemTest
  6. 10 core/tests/Drupal/Tests/Core/File/FileSystemTest.php \Drupal\Tests\Core\File\FileSystemTest

@coversDefaultClass \Drupal\Core\File\FileSystem

@group File

Hierarchy

Expanded class hierarchy of FileSystemTest

File

core/tests/Drupal/Tests/Core/File/FileSystemTest.php, line 19

Namespace

Drupal\Tests\Core\File
View source
class FileSystemTest extends UnitTestCase {
    
    /**
     * @var \Drupal\Core\File\FileSystemInterface
     */
    protected $fileSystem;
    
    /**
     * The file logger channel.
     *
     * @var \Psr\Log\LoggerInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $logger;
    
    /**
     * The stream wrapper manager.
     *
     * @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $streamWrapperManager;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        $settings = new Settings([]);
        $this->streamWrapperManager = $this->createMock(StreamWrapperManagerInterface::class);
        $this->fileSystem = new FileSystem($this->streamWrapperManager, $settings);
    }
    
    /**
     * @covers ::chmod
     */
    public function testChmodFile() : void {
        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);
    }
    
    /**
     * @covers ::chmod
     */
    public function testChmodDir() : void {
        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);
    }
    
    /**
     * @covers ::chmod
     */
    public function testChmodUnsuccessful() : void {
        vfsStream::setup('dir');
        $this->assertFalse($this->fileSystem
            ->chmod('vfs://dir/test.txt'));
    }
    
    /**
     * @covers ::unlink
     */
    public function testUnlink() : void {
        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);
    }
    
    /**
     * @covers ::basename
     *
     * @dataProvider providerTestBasename
     */
    public function testBasename($uri, $expected, $suffix = NULL) : void {
        $this->assertSame($expected, $this->fileSystem
            ->basename($uri, $suffix));
    }
    public static function providerTestBasename() {
        $data = [];
        $data[] = [
            'public://nested/dir',
            'dir',
        ];
        $data[] = [
            'public://dir/test.txt',
            'test.txt',
        ];
        $data[] = [
            'public://dir/test.txt',
            'test',
            '.txt',
        ];
        return $data;
    }
    
    /**
     * Asserts that the file permissions of a given URI matches.
     *
     * @param int $expected_mode
     *   The expected file mode.
     * @param string $uri
     *   The URI to test.
     * @param string $message
     *   An optional error message.
     *
     * @internal
     */
    protected function assertFilePermissions(int $expected_mode, string $uri, string $message = '') : void {
        // Mask out all but the last three octets.
        $actual_mode = fileperms($uri) & 0777;
        $this->assertSame($expected_mode, $actual_mode, $message);
    }
    
    /**
     * Tests that invalid UTF-8 results in an exception.
     *
     * @covers ::createFilename
     */
    public function testInvalidUTF8() : void {
        vfsStream::setup('dir');
        // cspell:disable-next-line
        $filename = "a\xffsdf\x80€" . '.txt';
        $this->expectException(FileException::class);
        $this->expectExceptionMessage("Invalid filename '{$filename}'");
        $this->fileSystem
            ->createFilename($filename, 'vfs://dir');
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::getCallableName private static function Returns a callable as a string suitable for inclusion in a message.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
FileSystemTest::$fileSystem protected property
FileSystemTest::$logger protected property The file logger channel.
FileSystemTest::$streamWrapperManager protected property The stream wrapper manager.
FileSystemTest::assertFilePermissions protected function Asserts that the file permissions of a given URI matches.
FileSystemTest::providerTestBasename public static function
FileSystemTest::setUp protected function Overrides UnitTestCase::setUp
FileSystemTest::testBasename public function @covers ::basename
FileSystemTest::testChmodDir public function @covers ::chmod
FileSystemTest::testChmodFile public function @covers ::chmod
FileSystemTest::testChmodUnsuccessful public function @covers ::chmod
FileSystemTest::testInvalidUTF8 public function Tests that invalid UTF-8 results in an exception.
FileSystemTest::testUnlink public function @covers ::unlink
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
UnitTestCase::$root protected property The app root.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUpBeforeClass public static function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.