function VariationCacheTest::testNestedVariationsSelfHealing

Same name in other branches
  1. 11.x core/tests/Drupal/Tests/Core/Cache/VariationCacheTest.php \Drupal\Tests\Core\Cache\VariationCacheTest::testNestedVariationsSelfHealing()

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

@covers ::get @covers ::set

@depends testNestedVariations

File

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

Class

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

Namespace

Drupal\Tests\Core\Cache

Code

public function testNestedVariationsSelfHealing() : void {
    // 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_parts = [
        '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) {
        [
            $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($this->getSortedCacheId($cache_id_parts), 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.
    [
        $this->housingType,
        $this->gardenType,
        $this->houseOrientation,
    ] = [
        'house',
        'no-garden',
        NULL,
    ];
    $this->setVariationCacheItem('You have a nice house', $this->gardenTypeCacheability, $this->housingTypeCacheability);
    $this->assertCacheBackendItem($this->getSortedCacheId($cache_id_parts), new CacheRedirect($this->gardenTypeCacheability));
    // Verify that the previously set outcomes are all inaccessible now.
    foreach ($possible_outcomes as $cache_context_values => $data) {
        [
            $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) {
        [
            $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.
    $cache_id_parts[] = 'gt.garden';
    $this->assertCacheBackendItem($this->getSortedCacheId($cache_id_parts), new CacheRedirect($this->houseOrientationCacheability));
}

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