public function HttpCacheTest::testFetchesFullResponseWhenCacheStaleAndNoValidatorsPresent

File

drupal/core/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php, line 612

Class

HttpCacheTest

Namespace

Symfony\Component\HttpKernel\Tests\HttpCache

Code

public function testFetchesFullResponseWhenCacheStaleAndNoValidatorsPresent() {
  $time = \DateTime::createFromFormat('U', time() + 5);
  $this
    ->setNextResponse(200, array(
    'Cache-Control' => 'public',
    'Expires' => $time
      ->format(DATE_RFC2822),
  ));

  // build initial request
  $this
    ->request('GET', '/');
  $this
    ->assertHttpKernelIsCalled();
  $this
    ->assertEquals(200, $this->response
    ->getStatusCode());
  $this
    ->assertNotNull($this->response->headers
    ->get('Date'));
  $this
    ->assertNotNull($this->response->headers
    ->get('X-Content-Digest'));
  $this
    ->assertNotNull($this->response->headers
    ->get('Age'));
  $this
    ->assertTraceContains('miss');
  $this
    ->assertTraceContains('store');
  $this
    ->assertEquals('Hello World', $this->response
    ->getContent());

  # go in and play around with the cached metadata directly ...
  $values = $this
    ->getMetaStorageValues();
  $this
    ->assertCount(1, $values);
  $tmp = unserialize($values[0]);
  $time = \DateTime::createFromFormat('U', time());
  $tmp[0][1]['expires'] = $time
    ->format(DATE_RFC2822);
  $r = new \ReflectionObject($this->store);
  $m = $r
    ->getMethod('save');
  $m
    ->setAccessible(true);
  $m
    ->invoke($this->store, 'md' . sha1('http://localhost/'), serialize($tmp));

  // build subsequent request; should be found but miss due to freshness
  $this
    ->request('GET', '/');
  $this
    ->assertHttpKernelIsCalled();
  $this
    ->assertEquals(200, $this->response
    ->getStatusCode());
  $this
    ->assertTrue($this->response->headers
    ->get('Age') <= 1);
  $this
    ->assertNotNull($this->response->headers
    ->get('X-Content-Digest'));
  $this
    ->assertTraceContains('stale');
  $this
    ->assertTraceNotContains('fresh');
  $this
    ->assertTraceNotContains('miss');
  $this
    ->assertTraceContains('store');
  $this
    ->assertEquals('Hello World', $this->response
    ->getContent());
}