function Fast404Test::testFast404PrivateFiles

Same name in other branches
  1. 10 core/tests/Drupal/FunctionalTests/EventSubscriber/Fast404Test.php \Drupal\FunctionalTests\EventSubscriber\Fast404Test::testFast404PrivateFiles()
  2. 11.x core/tests/Drupal/FunctionalTests/EventSubscriber/Fast404Test.php \Drupal\FunctionalTests\EventSubscriber\Fast404Test::testFast404PrivateFiles()

Tests the fast 404 functionality.

File

core/tests/Drupal/FunctionalTests/EventSubscriber/Fast404Test.php, line 81

Class

Fast404Test
Tests the fast 404 functionality.

Namespace

Drupal\FunctionalTests\EventSubscriber

Code

public function testFast404PrivateFiles() : void {
    $admin = $this->createUser([], NULL, TRUE);
    $this->drupalLogin($admin);
    $file_url = 'system/files/test/private-file-test.txt';
    $this->drupalGet($file_url);
    $this->assertSession()
        ->statusCodeEquals(404);
    $this->drupalGet($file_url);
    $this->assertSession()
        ->statusCodeEquals(404);
    // Create a private file for testing accessible by the admin user.
    \Drupal::service('file_system')->mkdir($this->privateFilesDirectory . '/test');
    $filepath = 'private://test/private-file-test.txt';
    $contents = "file_put_contents() doesn't seem to appreciate empty strings so let's put in some data.";
    file_put_contents($filepath, $contents);
    $file = File::create([
        'uri' => $filepath,
        'uid' => $admin->id(),
    ]);
    $file->save();
    $this->drupalGet($file_url);
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertSession()
        ->pageTextContains($contents);
}

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