function CacheCollectorTest::testUpdateCacheConflict

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/Tests/Core/Cache/CacheCollectorTest.php \Drupal\Tests\Core\Cache\CacheCollectorTest::testUpdateCacheConflict()

Tests updating the cache when there is a conflict after cache invalidation.

Attributes

#[TestWith([ TRUE, TRUE, TRUE, TRUE, ])] #[TestWith([ TRUE, TRUE, TRUE, FALSE, ])] #[TestWith([ TRUE, TRUE, FALSE, NULL, ])] #[TestWith([ TRUE, FALSE, FALSE, NULL, ])] #[TestWith([ TRUE, FALSE, TRUE, FALSE, ])] #[TestWith([ FALSE, FALSE, FALSE, NULL, ])] #[TestWith([ FALSE, TRUE, FALSE, NULL, ])] #[TestWith([ FALSE, FALSE, TRUE, FALSE, ])] #[TestWith([ FALSE, TRUE, TRUE, TRUE, ])] #[TestWith([ FALSE, TRUE, TRUE, FALSE, ])]

File

core/tests/Drupal/Tests/Core/Cache/CacheCollectorTest.php, line 313

Class

CacheCollectorTest
Tests Drupal\Core\Cache\CacheCollector.

Namespace

Drupal\Tests\Core\Cache

Code

public function testUpdateCacheConflict(bool $lock_acquired, bool $start_cache_item, bool $end_cache_item, ?bool $content_matches) : void {
  if ($end_cache_item === FALSE && isset($content_matches)) {
    throw new \BadMethodCallException('content_matches is ignored when end_cache_item is FALSE');
  }
  $this->setUpMockCacheBackend();
  $this->setUpMockLockBackend();
  $key = $this->randomMachineName();
  $value = $this->randomMachineName();
  $this->collector
    ->setCacheMissData($key, $value);
  $this->collector
    ->setCacheMissData('another key', 'another value');
  // Set up mock cache get with conflicting entries. The item present when the
  // cache is written contains the same data as the item loaded at the start
  // of the request only when $content_matches is TRUE; otherwise the data
  // differs, producing a different fingerprint.
  $this->cacheBackend
    ->expects($this->exactly(2))
    ->method('get')
    ->with($this->cid)
    ->willReturnOnConsecutiveCalls($start_cache_item ? (object) [
    'data' => [
      'conflicting' => 'original',
    ],
    'created' => (int) $_SERVER['REQUEST_TIME'],
  ] : FALSE, $end_cache_item ? (object) [
    'data' => [
      'conflicting' => $content_matches ? 'original' : 'changed',
    ],
    'created' => (int) $_SERVER['REQUEST_TIME'],
  ] : FALSE);
  $this->collector
    ->get($key);
  // When the cache is being warmed, if the lock can't be acquired, or if the
  // cache item has changed during the request, nothing should be set.
  $this->lock
    ->expects($this->once())
    ->method('acquire')
    ->with($this->cid . ':Drupal\\Core\\Cache\\CacheCollector')
    ->willReturn($lock_acquired);
  if ($end_cache_item && !$content_matches) {
    $this->cacheBackend
      ->expects($this->never())
      ->method('set')
      ->with($this->cid);
  }
  if ($lock_acquired) {
    $this->lock
      ->expects($this->once())
      ->method('release')
      ->with($this->cid . ':Drupal\\Core\\Cache\\CacheCollector');
  }
  $this->cacheBackend
    ->expects($this->never())
    ->method('delete');
  // Destruct the object to trigger the update data process.
  $this->collector
    ->destruct();
}

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