Same filename in this branch
- 10 core/tests/Drupal/KernelTests/Core/File/HtaccessTest.php
- 10 core/modules/system/tests/src/Functional/System/HtaccessTest.php
Same filename and directory in other branches
- 8.9.x core/modules/system/tests/src/Functional/System/HtaccessTest.php
- 9 core/modules/system/tests/src/Functional/System/HtaccessTest.php
Namespace
Drupal\Tests\system\Functional\System
File
core/modules/system/tests/src/Functional/System/HtaccessTest.php
View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\system\Functional\System;
use Drupal\Tests\BrowserTestBase;
class HtaccessTest extends BrowserTestBase {
protected static $modules = [
'node',
'path',
];
protected $defaultTheme = 'stark';
protected function getProtectedFiles() {
$path = $this
->getModulePath('system') . '/tests/fixtures/HtaccessTest';
$file_exts_to_deny = [
'engine',
'inc',
'install',
'make',
'module',
'module~',
'module.bak',
'module.orig',
'module.save',
'module.swo',
'module.swp',
'php~',
'php.bak',
'php.orig',
'php.save',
'php.swo',
'php.swp',
'profile',
'po',
'sh',
'sql',
'theme',
'twig',
'tpl.php',
'xtmpl',
'yml',
];
foreach ($file_exts_to_deny as $file_ext) {
$file_paths["{$path}/access_test.{$file_ext}"] = 403;
}
$file_paths['vendor/composer/installed.json'] = 403;
$file_paths['core/lib/Drupal.php'] = 403;
$file_paths['vendor/autoload.php'] = 403;
$file_paths['autoload.php'] = 403;
$file_exts_to_allow = [
'php-info.txt',
];
foreach ($file_exts_to_allow as $file_ext) {
$file_paths["{$path}/access_test.{$file_ext}"] = 200;
}
$file_paths["{$path}/composer.json"] = 403;
$file_paths["{$path}/composer.lock"] = 403;
$file_paths["{$path}/package.json"] = 403;
$file_paths["{$path}/yarn.lock"] = 403;
$file_paths["{$path}/.htaccess"] = 403;
$file_paths["{$path}/web.config"] = 403;
return $file_paths;
}
public function testFileAccess() {
foreach ($this
->getProtectedFiles() as $file => $response_code) {
$this
->assertFileAccess($file, $response_code);
}
$this
->drupalGet('core/lib/Drupal.php/1');
$this
->assertSession()
->statusCodeEquals(403);
$type = $this
->drupalCreateContentType();
$node = $this
->drupalCreateNode([
'title' => 'This is a node',
'type' => $type
->id(),
'path' => '/test.php',
]);
$node
->save();
$this
->drupalGet('test.php');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains('This is a node');
$node->path = '/test.php/test';
$node
->save();
$this
->drupalGet('test.php/test');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains('This is a node');
}
protected function assertFileAccess(string $path, int $response_code) : void {
$this
->assertFileExists(\Drupal::root() . '/' . $path);
$this
->drupalGet($path);
$this
->assertEquals($response_code, $this
->getSession()
->getStatusCode(), "Response code to {$path} should be {$response_code}");
}
public function testSvgzContentEncoding() {
$this
->drupalGet('core/modules/system/tests/logo.svgz');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->responseHeaderEquals('x-encoded-content-encoding', 'gzip');
}
}
Classes