function VariationCacheTest::testNestedVariations

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

Tests a cache item that has nested variations.

@covers ::get @covers ::set

File

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

Class

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

Namespace

Drupal\Tests\Core\Cache

Code

public function testNestedVariations() : void {
    // We are running this scenario in the best possible outcome: The redirects
    // are stored in expanding order, meaning the simplest one is stored first
    // and the nested ones are stored in subsequent ::set() calls. This means no
    // self-healing takes place where overly specific redirects are overwritten
    // with simpler ones.
    $possible_outcomes = [
        'apartment' => 'You have a nice apartment!',
        'house|no-garden' => 'You have a nice house!',
        '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 . '||');
        $cacheability = $this->housingTypeCacheability;
        if (!empty($this->houseOrientation)) {
            $cacheability = $this->houseOrientationCacheability;
        }
        elseif (!empty($this->gardenType)) {
            $cacheability = $this->gardenTypeCacheability;
        }
        $this->assertVariationCacheMiss($this->housingTypeCacheability);
        $this->setVariationCacheItem($data, $cacheability, $this->housingTypeCacheability);
        $this->assertVariationCacheItem($data, $cacheability, $this->housingTypeCacheability);
        $cache_id_parts = [
            "ht.{$this->housingType}",
        ];
        if (!empty($this->gardenType)) {
            $this->assertCacheBackendItem($this->getSortedCacheId($cache_id_parts), new CacheRedirect($this->gardenTypeCacheability));
            $cache_id_parts[] = "gt.{$this->gardenType}";
        }
        if (!empty($this->houseOrientation)) {
            $this->assertCacheBackendItem($this->getSortedCacheId($cache_id_parts), new CacheRedirect($this->houseOrientationCacheability));
            $cache_id_parts[] = "ho.{$this->houseOrientation}";
        }
        $this->assertCacheBackendItem($this->getSortedCacheId($cache_id_parts), $data, $cacheability);
    }
}

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