BlockCacheTest.php in Drupal 9
File
core/modules/block/tests/src/Functional/BlockCacheTest.php
View source
<?php
namespace Drupal\Tests\block\Functional;
use Drupal\Core\Cache\Cache;
use Drupal\Tests\BrowserTestBase;
class BlockCacheTest extends BrowserTestBase {
protected static $modules = [
'block',
'block_test',
'test_page_test',
];
protected $defaultTheme = 'classy';
protected $adminUser;
protected $normalUser;
protected $normalUserAlt;
protected $block;
protected function setUp() : void {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser([
'administer blocks',
'access administration pages',
]);
$this
->drupalLogin($this->adminUser);
$this->normalUser = $this
->drupalCreateUser();
$this->normalUserAlt = $this
->drupalCreateUser();
$this->normalUserAlt->roles = $this->normalUser
->getRoles();
$this->normalUserAlt
->save();
$this->block = $this
->drupalPlaceBlock('test_cache');
}
public function testCachePerRole() {
\Drupal::state()
->set('block_test.cache_contexts', [
'user.roles',
]);
$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);
$this
->drupalGet('');
$this
->assertSession()
->pageTextContains($old_content);
Cache::invalidateTags([
'block_view',
]);
$this
->drupalGet('');
$this
->assertSession()
->pageTextNotContains($old_content);
$this
->assertSession()
->pageTextContains($current_content);
$old_content = $current_content;
$current_content = $this
->randomMachineName();
\Drupal::state()
->set('block_test.content', $current_content);
$this
->drupalLogout();
$this
->drupalGet('');
$this
->assertSession()
->pageTextNotContains($old_content);
$this
->drupalLogin($this->normalUserAlt);
$this
->drupalGet('');
$this
->assertSession()
->pageTextContains($old_content);
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('');
$this
->assertSession()
->pageTextNotContains($old_content);
$this
->drupalLogin($this->normalUser);
$this
->drupalGet('');
$this
->assertSession()
->pageTextContains($old_content);
}
public function testCachePermissions() {
\Drupal::state()
->set('block_test.cache_contexts', []);
$current_content = $this
->randomMachineName();
\Drupal::state()
->set('block_test.content', $current_content);
$this
->drupalGet('');
$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()
->pageTextContains($old_content);
$this
->drupalLogout();
$this
->drupalGet('user');
$this
->assertSession()
->pageTextContains($current_content);
}
public function testNoCache() {
\Drupal::state()
->set('block_test.cache_max_age', 0);
$current_content = $this
->randomMachineName();
\Drupal::state()
->set('block_test.content', $current_content);
$this
->drupalGet('');
$this
->assertSession()
->pageTextContains($current_content);
$current_content = $this
->randomMachineName();
\Drupal::state()
->set('block_test.content', $current_content);
$this
->drupalGet('');
$this
->assertSession()
->pageTextContains($current_content);
}
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);
$this
->drupalGet('');
$this
->assertSession()
->pageTextContains($old_content);
$this
->drupalLogin($this->normalUserAlt);
$this
->drupalGet('');
$this
->assertSession()
->pageTextContains($current_content);
$this
->drupalLogin($this->normalUser);
$this
->drupalGet('');
$this
->assertSession()
->pageTextContains($old_content);
}
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);
$this
->assertSession()
->pageTextNotContains($old_content);
$this
->drupalGet('test-page');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains($old_content);
}
}