You are here

FileSecurityTest.php in Drupal 8

Same filename and directory in other branches
  1. 9 core/tests/Drupal/Tests/Component/FileSecurity/FileSecurityTest.php

File

core/tests/Drupal/Tests/Component/FileSecurity/FileSecurityTest.php
View source
<?php

namespace Drupal\Tests\Component\FileSecurity;

use Drupal\Component\FileSecurity\FileSecurity;
use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\TestCase;

/**
 * Tests the file security component.
 *
 * @coversDefaultClass \Drupal\Component\FileSecurity\FileSecurity
 * @group FileSecurity
 */
class FileSecurityTest extends TestCase {

  /**
   * @covers ::writeHtaccess
   */
  public function testWriteHtaccessPrivate() {
    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
      ->assertContains("Require all denied", $htaccess_contents);
  }

  /**
   * @covers ::writeHtaccess
   */
  public function testWriteHtaccessPublic() {
    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
      ->assertNotContains("Require all denied", $htaccess_contents);
  }

  /**
   * @covers ::writeHtaccess
   */
  public function testWriteHtaccessForceOverwrite() {
    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
      ->assertContains("Require all denied", $htaccess_contents);
    $this
      ->assertNotContains("foo", $htaccess_contents);
  }

  /**
   * @covers ::writeHtaccess
   */
  public function testWriteHtaccessFailure() {
    vfsStream::setup('root');
    $this
      ->assertFalse(FileSecurity::writeHtaccess(vfsStream::url('root') . '/foo'));
  }

  /**
   * @covers ::writeWebConfig
   */
  public function testWriteWebConfig() {
    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() {
    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
      ->assertNotContains("foo", $web_config_file);
  }

  /**
   * @covers ::writeWebConfig
   */
  public function testWriteWebConfigFailure() {
    vfsStream::setup('root');
    $this
      ->assertFalse(FileSecurity::writeWebConfig(vfsStream::url('root') . '/foo'));
  }

}

Classes

Namesort descending Description
FileSecurityTest Tests the file security component.