You are here

public function BlockCacheTest::testCachePerUser in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/block/tests/src/Functional/BlockCacheTest.php \Drupal\Tests\block\Functional\BlockCacheTest::testCachePerUser()
  2. 10 core/modules/block/tests/src/Functional/BlockCacheTest.php \Drupal\Tests\block\Functional\BlockCacheTest::testCachePerUser()

Tests "user" cache context.

File

core/modules/block/tests/src/Functional/BlockCacheTest.php, line 181

Class

BlockCacheTest
Tests block caching.

Namespace

Drupal\Tests\block\Functional

Code

public function testCachePerUser() {
  \Drupal::state()
    ->set('block_test.cache_contexts', [
    'user',
  ]);
  $current_content = $this
    ->randomMachineName();
  \Drupal::state()
    ->set('block_test.content', $current_content);
  $this
    ->drupalLogin($this->normalUser);
  $this
    ->drupalGet('');
  $this
    ->assertSession()
    ->pageTextContains($current_content);
  $old_content = $current_content;
  $current_content = $this
    ->randomMachineName();
  \Drupal::state()
    ->set('block_test.content', $current_content);

  // Block is served from per-user cache.
  $this
    ->drupalGet('');
  $this
    ->assertSession()
    ->pageTextContains($old_content);

  // Per-user block cache is not served for other users.
  $this
    ->drupalLogin($this->normalUserAlt);
  $this
    ->drupalGet('');
  $this
    ->assertSession()
    ->pageTextContains($current_content);

  // Per-user block cache is persistent.
  $this
    ->drupalLogin($this->normalUser);
  $this
    ->drupalGet('');
  $this
    ->assertSession()
    ->pageTextContains($old_content);
}