function FileSaveHtaccessLoggingTest::testHtaccessSave

Same name and namespace in other branches
  1. 9 core/modules/system/tests/src/Functional/File/FileSaveHtaccessLoggingTest.php \Drupal\Tests\system\Functional\File\FileSaveHtaccessLoggingTest::testHtaccessSave()
  2. 10 core/modules/system/tests/src/Functional/File/FileSaveHtaccessLoggingTest.php \Drupal\Tests\system\Functional\File\FileSaveHtaccessLoggingTest::testHtaccessSave()
  3. 11.x core/modules/system/tests/src/Functional/File/FileSaveHtaccessLoggingTest.php \Drupal\Tests\system\Functional\File\FileSaveHtaccessLoggingTest::testHtaccessSave()

Tests file_save_htaccess().

File

core/modules/system/tests/src/Functional/File/FileSaveHtaccessLoggingTest.php, line 25

Class

FileSaveHtaccessLoggingTest
Tests the log message added by <a href="/api/drupal/core%21includes%21file.inc/function/file_save_htaccess/8.9.x" title="Creates a .htaccess file in the given directory." class="local">file_save_htaccess</a>().

Namespace

Drupal\Tests\system\Functional\File

Code

public function testHtaccessSave() {
    // Prepare test directories.
    $private = $this->publicFilesDirectory . '/test/private';
    // Verify that file_save_htaccess() returns FALSE if .htaccess cannot be
    // written and writes a correctly formatted message to the error log. Set
    // $private to TRUE so all possible .htaccess lines are written.
    
    /** @var \Drupal\Core\File\HtaccessWriterInterface $htaccess */
    $htaccess = \Drupal::service('file.htaccess_writer');
    $this->assertFalse($htaccess->write($private, TRUE));
    $this->drupalLogin($this->rootUser);
    $this->drupalGet('admin/reports/dblog');
    $this->clickLink("Security warning: Couldn't write .htaccess file. Please…");
    $lines = FileSecurity::htaccessLines(TRUE);
    foreach (array_filter(explode("\n", $lines)) as $line) {
        $this->assertSession()
            ->assertEscaped($line);
    }
}

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