function ResourceObjectNormalizerCacherTest::testLinkNormalizationCacheability
Tests that link normalization cache information is not lost.
See also
https://www.drupal.org/project/drupal/issues/3077287
File
- 
              core/
modules/ jsonapi/ tests/ src/ Kernel/ EventSubscriber/ ResourceObjectNormalizerCacherTest.php, line 82  
Class
- ResourceObjectNormalizerCacherTest
 - @coversDefaultClass \Drupal\jsonapi\EventSubscriber\ResourceObjectNormalizationCacher[[api-linebreak]] @group jsonapi
 
Namespace
Drupal\Tests\jsonapi\Kernel\EventSubscriberCode
public function testLinkNormalizationCacheability() : void {
  $user = User::create([
    'name' => $this->randomMachineName(),
    'pass' => $this->randomString(),
  ]);
  $user->save();
  $resource_type = $this->resourceTypeRepository
    ->get($user->getEntityTypeId(), $user->bundle());
  $resource_object = ResourceObject::createFromEntity($resource_type, $user);
  $cache_tag_to_invalidate = 'link_normalization';
  $normalized_links = $this->serializer
    ->normalize($resource_object->getLinks(), 'api_json')
    ->withCacheableDependency((new CacheableMetadata())->addCacheTags([
    $cache_tag_to_invalidate,
  ]));
  assert($normalized_links instanceof CacheableNormalization);
  $normalization_parts = [
    ResourceObjectNormalizationCacher::RESOURCE_CACHE_SUBSET_BASE => [
      'type' => CacheableNormalization::permanent($resource_object->getTypeName()),
      'id' => CacheableNormalization::permanent($resource_object->getId()),
      'links' => $normalized_links,
    ],
    ResourceObjectNormalizationCacher::RESOURCE_CACHE_SUBSET_FIELDS => [],
  ];
  $this->cacher
    ->saveOnTerminate($resource_object, $normalization_parts);
  $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
    ->onTerminate($event);
  $this->assertNotFalse((bool) $this->cacher
    ->get($resource_object));
  Cache::invalidateTags([
    $cache_tag_to_invalidate,
  ]);
  $this->assertFalse((bool) $this->cacher
    ->get($resource_object));
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.