You are here

function PageCacheTest::testPageCacheAnonymous403404 in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/page_cache/src/Tests/PageCacheTest.php \Drupal\page_cache\Tests\PageCacheTest::testPageCacheAnonymous403404()

Tests the 4xx-response cache tag is added and invalidated.

File

core/modules/page_cache/src/Tests/PageCacheTest.php, line 342
Contains \Drupal\page_cache\Tests\PageCacheTest.

Class

PageCacheTest
Enables the page cache and tests it with various HTTP requests.

Namespace

Drupal\page_cache\Tests

Code

function testPageCacheAnonymous403404() {
  $admin_url = Url::fromRoute('system.admin');
  $invalid_url = 'foo/does_not_exist';
  $tests = [
    403 => $admin_url,
    404 => $invalid_url,
  ];
  foreach ($tests as $code => $content_url) {

    // Anonymous user, without permissions.
    $this
      ->drupalGet($content_url);
    $this
      ->assertResponse($code);
    $this
      ->assertEqual($this
      ->drupalGetHeader('X-Drupal-Cache'), 'MISS');
    $this
      ->assertCacheTag('4xx-response');
    $this
      ->drupalGet($content_url);
    $this
      ->assertResponse($code);
    $this
      ->assertEqual($this
      ->drupalGetHeader('X-Drupal-Cache'), 'HIT');
    $entity_values = array(
      'name' => $this
        ->randomMachineName(),
      'user_id' => 1,
      'field_test_text' => array(
        0 => array(
          'value' => $this
            ->randomString(),
          'format' => 'plain_text',
        ),
      ),
    );
    $entity = EntityTest::create($entity_values);
    $entity
      ->save();

    // Saving an entity clears 4xx cache tag.
    $this
      ->drupalGet($content_url);
    $this
      ->assertResponse($code);
    $this
      ->assertEqual($this
      ->drupalGetHeader('X-Drupal-Cache'), 'MISS');
    $this
      ->drupalGet($content_url);
    $this
      ->assertResponse($code);
    $this
      ->assertEqual($this
      ->drupalGetHeader('X-Drupal-Cache'), 'HIT');

    // Rebuilding the router should invalidate the 4xx cache tag.
    $this->container
      ->get('router.builder')
      ->rebuild();
    $this
      ->drupalGet($content_url);
    $this
      ->assertResponse($code);
    $this
      ->assertEqual($this
      ->drupalGetHeader('X-Drupal-Cache'), 'MISS');
  }
}