function FinishResponseSubscriberTest::testDebugCacheTagAndCacheContextsHeadersLength

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/Tests/Core/EventSubscriber/FinishResponseSubscriberTest.php \Drupal\Tests\Core\EventSubscriber\FinishResponseSubscriberTest::testDebugCacheTagAndCacheContextsHeadersLength()

Tests that long tags and contexts headers are split into multiple lines.

File

core/tests/Drupal/Tests/Core/EventSubscriber/FinishResponseSubscriberTest.php, line 182

Class

FinishResponseSubscriberTest
Tests Drupal\Core\EventSubscriber\FinishResponseSubscriber.

Namespace

Drupal\Tests\Core\EventSubscriber

Code

public function testDebugCacheTagAndCacheContextsHeadersLength() : void {
  $finishSubscriber = new FinishResponseSubscriber($this->languageManager, $this->getConfigFactoryStub(), $this->requestPolicy, $this->responsePolicy, $this->cacheContextsManager, $this->time, TRUE);
  $this->languageManager
    ->method('getCurrentLanguage')
    ->willReturn(new Language([
    'id' => 'en',
  ]));
  $request = $this->createStub(Request::class);
  $response = new CacheableResponse();
  // Create multiple cache tags that add up to more than 8k bytes. Each tag is
  // 15 bytes. The tags imploded together will have a space between
  // each value, so the total length is 8015.
  for ($i = 0; $i < 501; $i++) {
    $tags[] = 'cache-tag:' . str_pad("{$i}", 5, '0', STR_PAD_LEFT);
  }
  // For contexts, create multiple values that add up to more than 16k. Each
  // context is 19 bytes. The contexts imploded together will have a space
  // between each value, so the total length is 16019.
  for ($i = 0; $i < 801; $i++) {
    $contexts[] = 'cache-context:' . str_pad("{$i}", 5, '0', STR_PAD_LEFT);
  }
  $this->cacheContextsManager
    ->method('optimizeTokens')
    ->willReturn($contexts);
  $cacheData = (new CacheableMetadata())->setCacheTags($tags);
  $response->addCacheableDependency($cacheData);
  $event = new ResponseEvent($this->kernel, $request, HttpKernelInterface::MAIN_REQUEST, $response);
  $finishSubscriber->onRespond($event);
  // Check that X-Drupal-Cache-Tags has been split into two lines.
  $headers = (string) $response->headers;
  $this->assertEquals(2, substr_count($headers, 'X-Drupal-Cache-Tags: '));
  // Check that X-Drupal-Cache-Contexts has been split into three lines.
  $this->assertEquals(3, substr_count($headers, 'X-Drupal-Cache-Contexts: '));
}

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