function BlockCacheTest::testCachePerUser

Same name and namespace in other branches
  1. 8.9.x 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()
  3. 11.x 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 184

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);
}

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