function ResourceObjectNormalizerCacherTest::testResourceObjectMaxAge0IsHandledByCacher

Same name and namespace in other branches
  1. 11.x core/modules/jsonapi/tests/src/Kernel/EventSubscriber/ResourceObjectNormalizerCacherTest.php \Drupal\Tests\jsonapi\Kernel\EventSubscriber\ResourceObjectNormalizerCacherTest::testResourceObjectMaxAge0IsHandledByCacher()

Tests that when max-age is set to 0 the cacher does not cache the normalization.

File

core/modules/jsonapi/tests/src/Kernel/EventSubscriber/ResourceObjectNormalizerCacherTest.php, line 184

Class

ResourceObjectNormalizerCacherTest
Tests Drupal\jsonapi\EventSubscriber\ResourceObjectNormalizationCacher.

Namespace

Drupal\Tests\jsonapi\Kernel\EventSubscriber

Code

public function testResourceObjectMaxAge0IsHandledByCacher() : void {
  $this->installEntitySchema('entity_test_computed_field');
  // Use EntityTestComputedField since ComputedTestCacheableStringItemList has
  // a max age of 800.
  $entity = EntityTestComputedField::create([]);
  $entity->save();
  $resource_type = $this->resourceTypeRepository
    ->get($entity->getEntityTypeId(), $entity->bundle());
  $resource_object = ResourceObject::createFromEntity($resource_type, $entity);
  // Not yet cached, so this should return false.
  $this->assertFalse($this->cacher
    ->get($resource_object));
  // Save the normalization to cache, this is done at TerminateEvent.
  $http_kernel = $this->prophesize(HttpKernelInterface::class);
  $request = $this->prophesize(Request::class);
  $response = $this->prophesize(Response::class);
  $event = new TerminateEvent($http_kernel->reveal(), $request->reveal(), $response->reveal());
  $this->cacher
    ->saveOnTerminate($resource_object, [
    'base' => [],
    'fields' => [],
  ]);
  $this->cacher
    ->onTerminate($event);
  $this->assertNotFalse($this->cacher
    ->get($resource_object));
  // Set max-age to 0 and see if we not skip caching.
  $entity->mergeCacheMaxAge(0);
  $resource_object = ResourceObject::createFromEntity($resource_type, $entity);
  $this->cacher
    ->saveOnTerminate($resource_object, [
    'base' => [],
    'fields' => [],
  ]);
  $this->cacher
    ->onTerminate($event);
  // The cacher should not cache the normalization since max-age is 0.
  $this->assertFalse($this->cacher
    ->get($resource_object));
}

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