function VariationCacheTest::testIncompatibleRedirectsException

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

Tests exception for a cache item that has incompatible cache redirects.

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

File

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

Class

VariationCacheTest
Tests Drupal\Core\Cache\VariationCache.

Namespace

Drupal\Tests\Core\Cache

Code

public function testIncompatibleRedirectsException() : 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 someone first triggers the storing of a
  // redirect using context A and then tries to store another redirect in the
  // same spot using context B, something is wrong. The cache contexts of all
  // previous redirects should always be present on the next redirect or item
  // you're trying to store.
  $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", new one points to "house.orientation".');
  $this->housingType = 'house';
  $house_cacheability = (new CacheableMetadata())->setCacheContexts([
    'house.type',
  ]);
  $this->gardenType = 'garden';
  $garden_cacheability = (new CacheableMetadata())->setCacheContexts([
    'house.type',
    'garden.type',
  ]);
  $this->houseOrientation = 'north';
  $orientation_cacheability = (new CacheableMetadata())->setCacheContexts([
    'house.type',
    'house.orientation',
  ]);
  $this->setVariationCacheItem('You have a nice house with a garden!', $garden_cacheability, $house_cacheability);
  try {
    $this->setVariationCacheItem('You have a nice north-facing house!', $orientation_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.