You are here

public function BlockCacheTest::testCachePerPage 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::testCachePerPage()
  2. 10 core/modules/block/tests/src/Functional/BlockCacheTest.php \Drupal\Tests\block\Functional\BlockCacheTest::testCachePerPage()

Tests "url" cache context.

File

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

Class

BlockCacheTest
Tests block caching.

Namespace

Drupal\Tests\block\Functional

Code

public function testCachePerPage() {
  \Drupal::state()
    ->set('block_test.cache_contexts', [
    'url',
  ]);
  $current_content = $this
    ->randomMachineName();
  \Drupal::state()
    ->set('block_test.content', $current_content);
  $this
    ->drupalGet('test-page');
  $this
    ->assertSession()
    ->pageTextContains($current_content);
  $old_content = $current_content;
  $current_content = $this
    ->randomMachineName();
  \Drupal::state()
    ->set('block_test.content', $current_content);
  $this
    ->drupalGet('user');
  $this
    ->assertSession()
    ->statusCodeEquals(200);

  // Verify that block content cached for the test page does not show up
  // for the user page.
  $this
    ->assertSession()
    ->pageTextNotContains($old_content);
  $this
    ->drupalGet('test-page');
  $this
    ->assertSession()
    ->statusCodeEquals(200);

  // Verify that the block content is cached for the test page.
  $this
    ->assertSession()
    ->pageTextContains($old_content);
}