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. 8.9.x 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()

Tests "url" cache context.

File

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

Class

BlockCacheTest
Tests block caching.

Namespace

Drupal\Tests\block\Functional

Code

public function testCachePerPage() : void {
    \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);
}

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