You are here

public function ResourceObjectNormalizerCacherTest::testLinkNormalizationCacheability in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/jsonapi/tests/src/Kernel/EventSubscriber/ResourceObjectNormalizerCacherTest.php \Drupal\Tests\jsonapi\Kernel\EventSubscriber\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 73

Class

ResourceObjectNormalizerCacherTest
@coversDefaultClass \Drupal\jsonapi\EventSubscriber\ResourceObjectNormalizationCacher @group jsonapi

Namespace

Drupal\Tests\jsonapi\Kernel\EventSubscriber

Code

public function testLinkNormalizationCacheability() {
  $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);
  $event = $this
    ->prophesize(TerminateEvent::class);
  $this->cacher
    ->onTerminate($event
    ->reveal());
  $this
    ->assertNotFalse((bool) $this->cacher
    ->get($resource_object));
  Cache::invalidateTags([
    $cache_tag_to_invalidate,
  ]);
  $this
    ->assertFalse((bool) $this->cacher
    ->get($resource_object));
}