function BlockCacheTest::testCachePerPage

Same name in other branches
  1. 9 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()
  3. 11.x core/modules/block/tests/src/Functional/BlockCacheTest.php \Drupal\Tests\block\Functional\BlockCacheTest::testCachePerPage()

Test "url" cache context.

File

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

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->assertText($current_content, 'Block content displays on the test page.');
    $old_content = $current_content;
    $current_content = $this->randomMachineName();
    \Drupal::state()->set('block_test.content', $current_content);
    $this->drupalGet('user');
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertNoText($old_content, 'Block content cached for the test page does not show up for the user page.');
    $this->drupalGet('test-page');
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertText($old_content, 'Block content cached for the test page.');
}

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