function VariationCacheTest::testIncompatibleChainedRedirectsComplexException

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

Tests the same as above, but even more complex.

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

Attributes

#[Depends('testIncompatibleChainedRedirectsException')]

File

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

Class

VariationCacheTest
Tests Drupal\Core\Cache\VariationCache.

Namespace

Drupal\Tests\Core\Cache

Code

public function testIncompatibleChainedRedirectsComplexException() : 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->expectException(\LogicException::class);
  $this->expectExceptionMessageIs('Trying to overwrite a cache redirect for "your:housing:situation:gt.garden:ht.house" with one that has nothing in common, old one at address "house.type, garden.type" was pointing to "house.orientation", new one points to "solar.type".');
  $this->housingType = 'house';
  $house_cacheability = (new CacheableMetadata())->setCacheContexts([
    'house.type',
  ]);
  $this->gardenType = 'garden';
  $this->houseOrientation = 'north';
  $orientation_cacheability = (new CacheableMetadata())->setCacheContexts([
    'house.type',
    'garden.type',
    'house.orientation',
  ]);
  $this->solarType = 'solar';
  $solar_cacheability = (new CacheableMetadata())->setCacheContexts([
    'house.type',
    'garden.type',
    'solar.type',
  ]);
  // This time, nothing primes the redirects so the first set will create a
  // redirect at ht.house, pointing to house.type, garden.type and solar.type.
  $this->setVariationCacheItem('You have a nice house with solar panels and a garden!', $solar_cacheability, $house_cacheability);
  // The second set will try to store a redirect at ht.house, pointing to
  // house.type, garden.type and house.orientation. This will trigger the
  // creation of a common redirect at ht.house, pointing to garden.type.
  $this->setVariationCacheItem('You have a nice north-facing house with a garden!', $orientation_cacheability, $house_cacheability);
  // Now we arrive at the same scenario as the test above. We have a redirect
  // chain at house.type of garden.type and finally house.orientation, but are
  // trying to set solar.type at that last address.
  try {
    $this->setVariationCacheItem('You have a nice house with solar panels and a garden!', $solar_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.