You are here

public function VariationCacheTest::testNestedVariationsSelfHealing in VariationCache 8

Tests a cache item that has nested variations that trigger self-healing.

@covers ::get @covers ::set

@depends testNestedVariations

File

tests/src/Unit/VariationCacheTest.php, line 269

Class

VariationCacheTest
@coversDefaultClass \Drupal\variationcache\Cache\VariationCache @group Cache

Namespace

Drupal\Tests\variationcache\Unit

Code

public function testNestedVariationsSelfHealing() {

  // This is the worst possible scenario: A very specific item was stored
  // first, followed by a less specific one. This means an overly specific
  // cache redirect was stored that needs to be dumbed down. After this
  // process, the first ::get() for the more specific item will fail as we
  // have effectively destroyed the path to said item. Setting an item of the
  // same specificity will restore the path for all items of said specificity.
  $cache_id = "{$this->cacheIdBase}:ht.house";
  $possible_outcomes = [
    'house|garden|east' => 'You have a nice house with an east-facing garden!',
    'house|garden|south' => 'You have a nice house with a south-facing garden!',
    'house|garden|west' => 'You have a nice house with a west-facing garden!',
    'house|garden|north' => 'You have a nice house with a north-facing garden!',
  ];
  foreach ($possible_outcomes as $cache_context_values => $data) {
    list($this->housingType, $this->gardenType, $this->houseOrientation) = explode('|', $cache_context_values . '||');
    $this
      ->setVariationCacheItem($data, $this->houseOrientationCacheability, $this->housingTypeCacheability);
  }

  // Verify that the overly specific redirect is stored at the first possible
  // redirect location, i.e.: The base cache ID.
  $this
    ->assertCacheBackendItem($cache_id, new CacheRedirect($this->houseOrientationCacheability));

  // Store a simpler variation and verify that the first cache redirect is now
  // the one redirecting to the simplest known outcome.
  list($this->housingType, $this->gardenType, $this->houseOrientation) = [
    'house',
    'no-garden',
    NULL,
  ];
  $this
    ->setVariationCacheItem('You have a nice house', $this->gardenTypeCacheability, $this->housingTypeCacheability);
  $this
    ->assertCacheBackendItem($cache_id, new CacheRedirect($this->gardenTypeCacheability));

  // Verify that the previously set outcomes are all inaccessible now.
  foreach ($possible_outcomes as $cache_context_values => $data) {
    list($this->housingType, $this->gardenType, $this->houseOrientation) = explode('|', $cache_context_values . '||');
    $this
      ->assertVariationCacheMiss($this->housingTypeCacheability);
  }

  // Set at least one more specific item in the cache again.
  $this
    ->setVariationCacheItem($data, $this->houseOrientationCacheability, $this->housingTypeCacheability);

  // Verify that the previously set outcomes are all accessible again.
  foreach ($possible_outcomes as $cache_context_values => $data) {
    list($this->housingType, $this->gardenType, $this->houseOrientation) = explode('|', $cache_context_values . '||');
    $this
      ->assertVariationCacheItem($data, $this->houseOrientationCacheability, $this->housingTypeCacheability);
  }

  // Verify that the more specific cache redirect is now stored one step after
  // the less specific one.
  $this
    ->assertCacheBackendItem("{$cache_id}:gt.garden", new CacheRedirect($this->houseOrientationCacheability));
}