function VariationCacheTest::testIncompleteRedirectException

Same name and namespace in other branches
  1. main core/tests/Drupal/Tests/Core/Cache/VariationCacheTest.php \Drupal\Tests\Core\Cache\VariationCacheTest::testIncompleteRedirectException()

Tests exception for a cache item that has an incomplete redirect.

@legacy-covers ::get @legacy-covers ::set

File

core/tests/Drupal/Tests/Core/Cache/VariationCacheTest.php, line 434

Class

VariationCacheTest
Tests Drupal\Core\Cache\VariationCache.

Namespace

Drupal\Tests\Core\Cache

Code

public function testIncompleteRedirectException() : void {
  // @todo Remove in Drupal 12.0.0. For more information, see:
  //   https://www.drupal.org/project/drupal/issues/3468921
  set_error_handler(static function (int $errno, string $errstr) : never {
    throw new \LogicException($errstr, $errno);
  }, E_USER_WARNING);
  // This should never happen. When we have a cache redirect at address A,
  // pointing to 'A,B:foo' and then someone tries to store a cache redirect at
  // A pointing to 'A,B', something is wrong. The cache contexts leading up to
  // a cache redirect should always be present on the redirect itself. In this
  // example, the final cache redirect should be for 'A,B:foo,B'.
  $this->expectException(\LogicException::class);
  $this->expectExceptionMessageIs('Trying to overwrite a cache redirect for "your:housing:situation:ht.house" with one that has nothing in common, old one at address "house.type" was pointing to "garden.type:zen", new one points to "garden.type".');
  $this->housingType = 'house';
  $house_cacheability = (new CacheableMetadata())->setCacheContexts([
    'house.type',
  ]);
  $this->gardenType = '1';
  $calculated_garden_cacheability = (new CacheableMetadata())->setCacheContexts([
    'house.type',
    'garden.type:zen',
  ]);
  $this->setVariationCacheItem('You have a house with zen garden!', $calculated_garden_cacheability, $house_cacheability);
  $this->gardenType = 'baroque garden';
  $garden_cacheability = (new CacheableMetadata())->setCacheContexts([
    'house.type',
    'garden.type',
  ]);
  try {
    $this->setVariationCacheItem('You have a house with a baroque garden!', $garden_cacheability, $house_cacheability);
  } finally {
    restore_error_handler();
  }
}

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