class FileSecurityTest

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Component/FileSecurity/FileSecurityTest.php \Drupal\Tests\Component\FileSecurity\FileSecurityTest
  2. 8.9.x core/tests/Drupal/Tests/Component/FileSecurity/FileSecurityTest.php \Drupal\Tests\Component\FileSecurity\FileSecurityTest
  3. 11.x core/tests/Drupal/Tests/Component/FileSecurity/FileSecurityTest.php \Drupal\Tests\Component\FileSecurity\FileSecurityTest

Tests the file security component.

@coversDefaultClass \Drupal\Component\FileSecurity\FileSecurity @group FileSecurity

Hierarchy

  • class \Drupal\Tests\Component\FileSecurity\FileSecurityTest extends \PHPUnit\Framework\TestCase

Expanded class hierarchy of FileSecurityTest

File

core/tests/Drupal/Tests/Component/FileSecurity/FileSecurityTest.php, line 17

Namespace

Drupal\Tests\Component\FileSecurity
View source
class FileSecurityTest extends TestCase {
    
    /**
     * @covers ::writeHtaccess
     */
    public function testWriteHtaccessPrivate() : void {
        vfsStream::setup('root');
        FileSecurity::writeHtaccess(vfsStream::url('root'));
        $htaccess_file = vfsStream::url('root') . '/.htaccess';
        $this->assertFileExists($htaccess_file);
        $this->assertEquals('0444', substr(sprintf('%o', fileperms($htaccess_file)), -4));
        $htaccess_contents = file_get_contents($htaccess_file);
        $this->assertStringContainsString("Require all denied", $htaccess_contents);
    }
    
    /**
     * @covers ::writeHtaccess
     */
    public function testWriteHtaccessPublic() : void {
        vfsStream::setup('root');
        $this->assertTrue(FileSecurity::writeHtaccess(vfsStream::url('root'), FALSE));
        $htaccess_file = vfsStream::url('root') . '/.htaccess';
        $this->assertFileExists($htaccess_file);
        $this->assertEquals('0444', substr(sprintf('%o', fileperms($htaccess_file)), -4));
        $htaccess_contents = file_get_contents($htaccess_file);
        $this->assertStringNotContainsString("Require all denied", $htaccess_contents);
    }
    
    /**
     * @covers ::writeHtaccess
     */
    public function testWriteHtaccessForceOverwrite() : void {
        vfsStream::setup('root');
        $htaccess_file = vfsStream::url('root') . '/.htaccess';
        file_put_contents($htaccess_file, "foo");
        $this->assertTrue(FileSecurity::writeHtaccess(vfsStream::url('root'), TRUE, TRUE));
        $htaccess_contents = file_get_contents($htaccess_file);
        $this->assertStringContainsString("Require all denied", $htaccess_contents);
        $this->assertStringNotContainsString("foo", $htaccess_contents);
    }
    
    /**
     * @covers ::writeHtaccess
     */
    public function testWriteHtaccessFailure() : void {
        vfsStream::setup('root');
        $this->assertFalse(FileSecurity::writeHtaccess(vfsStream::url('root') . '/foo'));
    }
    
    /**
     * @covers ::writeWebConfig
     */
    public function testWriteWebConfig() : void {
        vfsStream::setup('root');
        $this->assertTrue(FileSecurity::writeWebConfig(vfsStream::url('root')));
        $web_config_file = vfsStream::url('root') . '/web.config';
        $this->assertFileExists($web_config_file);
        $this->assertEquals('0444', substr(sprintf('%o', fileperms($web_config_file)), -4));
    }
    
    /**
     * @covers ::writeWebConfig
     */
    public function testWriteWebConfigForceOverwrite() : void {
        vfsStream::setup('root');
        $web_config_file = vfsStream::url('root') . '/web.config';
        file_put_contents($web_config_file, "foo");
        $this->assertTrue(FileSecurity::writeWebConfig(vfsStream::url('root'), TRUE));
        $this->assertFileExists($web_config_file);
        $this->assertEquals('0444', substr(sprintf('%o', fileperms($web_config_file)), -4));
        $this->assertStringNotContainsString("foo", $web_config_file);
    }
    
    /**
     * @covers ::writeWebConfig
     */
    public function testWriteWebConfigFailure() : void {
        vfsStream::setup('root');
        $this->assertFalse(FileSecurity::writeWebConfig(vfsStream::url('root') . '/foo'));
    }

}

Members

Title Sort descending Modifiers Object type Summary
FileSecurityTest::testWriteHtaccessFailure public function @covers ::writeHtaccess
FileSecurityTest::testWriteHtaccessForceOverwrite public function @covers ::writeHtaccess
FileSecurityTest::testWriteHtaccessPrivate public function @covers ::writeHtaccess
FileSecurityTest::testWriteHtaccessPublic public function @covers ::writeHtaccess
FileSecurityTest::testWriteWebConfig public function @covers ::writeWebConfig
FileSecurityTest::testWriteWebConfigFailure public function @covers ::writeWebConfig
FileSecurityTest::testWriteWebConfigForceOverwrite public function @covers ::writeWebConfig

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