MasqueradeCacheTest.php in Masquerade 8.2
File
tests/src/Functional/MasqueradeCacheTest.php
View source
<?php
namespace Drupal\Tests\masquerade\Functional;
use Drupal\block\Entity\Block;
class MasqueradeCacheTest extends MasqueradeWebTestBase {
protected static $modules = [
'masquerade',
'user',
'block',
'node',
'dynamic_page_cache',
'toolbar',
];
public function testMasqueradeSwitchBlockCaching() {
$umberto = $this
->drupalCreateUser([
'masquerade as any user',
'access content',
], 'umberto');
$nelle = $this
->drupalCreateUser([
'masquerade as any user',
'access content',
], 'nelle');
$masquerade_block = $this
->drupalPlaceBlock('masquerade');
$this
->drupalLogin($umberto);
$this
->drupalGet('<front>');
$this
->assertBlockAppears($masquerade_block);
$edit = [
'masquerade_as' => $nelle
->getAccountName(),
];
$this
->submitForm($edit, 'Switch');
$this
->drupalGet('<front>');
$this
->assertNoBlockAppears($masquerade_block);
$this
->drupalLogout();
$this
->drupalLogin($nelle);
$this
->drupalGet('<front>');
$this
->assertBlockAppears($masquerade_block);
$this
->validateMasqueradeBlock($masquerade_block
->id());
}
public function testMasqueradeToolbarLinkCaching() {
$test_user = $this
->drupalCreateUser([
'access content',
'access toolbar',
]);
$this
->drupalLogin($this->admin_user);
$this
->masqueradeAs($test_user);
$this
->assertSession()
->linkExists('Unmasquerade');
$this
->assertCacheContext('session');
$this
->drupalLogin($test_user);
$this
->assertSession()
->linkNotExists('Unmasquerade');
$this
->assertCacheContext('session.is_masquerading');
}
protected function validateMasqueradeBlock($bid) {
$block = Block::load($bid);
$this
->assertContains('session.is_masquerading', $block
->getPlugin()
->getCacheContexts());
$result = $block
->access('view', NULL, TRUE);
$this
->assertContains('session.is_masquerading', $result
->getCacheContexts());
}
}