View source
<?php
namespace Drupal\system\Tests\File;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\simpletest\KernelTestBase;
class HtaccessUnitTest extends KernelTestBase {
function testHtaccessSave() {
$public = $this->publicFilesDirectory . '/test/public';
$private = $this->publicFilesDirectory . '/test/private';
$stream = 'public://test/stream';
$this
->assertFalse(file_save_htaccess($public, FALSE));
mkdir($public, 0777, TRUE);
$this
->assertTrue(file_save_htaccess($public, FALSE));
$content = file_get_contents($public . '/.htaccess');
$this
->assertTrue(strpos($content, "SetHandler Drupal_Security_Do_Not_Remove_See_SA_2006_006") !== FALSE);
$this
->assertFalse(strpos($content, "Require all denied") !== FALSE);
$this
->assertFalse(strpos($content, "Deny from all") !== FALSE);
$this
->assertTrue(strpos($content, "Options -Indexes -ExecCGI -Includes -MultiViews") !== FALSE);
$this
->assertTrue(strpos($content, "SetHandler Drupal_Security_Do_Not_Remove_See_SA_2013_003") !== FALSE);
$this
->assertFilePermissions($public . '/.htaccess', 0444);
$this
->assertTrue(file_save_htaccess($public, FALSE));
mkdir($private, 0777, TRUE);
$this
->assertTrue(file_save_htaccess($private));
$content = file_get_contents($private . '/.htaccess');
$this
->assertTrue(strpos($content, "SetHandler Drupal_Security_Do_Not_Remove_See_SA_2006_006") !== FALSE);
$this
->assertTrue(strpos($content, "Require all denied") !== FALSE);
$this
->assertTrue(strpos($content, "Deny from all") !== FALSE);
$this
->assertTrue(strpos($content, "Options -Indexes -ExecCGI -Includes -MultiViews") !== FALSE);
$this
->assertTrue(strpos($content, "SetHandler Drupal_Security_Do_Not_Remove_See_SA_2013_003") !== FALSE);
$this
->assertFilePermissions($private . '/.htaccess', 0444);
$this
->assertTrue(file_save_htaccess($private));
mkdir($stream, 0777, TRUE);
$this
->assertTrue(file_save_htaccess($stream));
$content = file_get_contents($stream . '/.htaccess');
$this
->assertTrue(strpos($content, "SetHandler Drupal_Security_Do_Not_Remove_See_SA_2006_006") !== FALSE);
$this
->assertTrue(strpos($content, "Require all denied") !== FALSE);
$this
->assertTrue(strpos($content, "Deny from all") !== FALSE);
$this
->assertTrue(strpos($content, "Options -Indexes -ExecCGI -Includes -MultiViews") !== FALSE);
$this
->assertTrue(strpos($content, "SetHandler Drupal_Security_Do_Not_Remove_See_SA_2013_003") !== FALSE);
$this
->assertFilePermissions($stream . '/.htaccess', 0444);
$this
->assertTrue(file_save_htaccess($stream));
}
protected function assertFilePermissions($uri, $expected) {
$actual = fileperms($uri) & 0777;
return $this
->assertIdentical($actual, $expected, SafeMarkup::format('@uri file permissions @actual are identical to @expected.', array(
'@uri' => $uri,
'@actual' => 0 . decoct($actual),
'@expected' => 0 . decoct($expected),
)));
}
}