View source
<?php
namespace Drupal\Tests\shortcut\Functional;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Url;
use Drupal\shortcut\Entity\Shortcut;
use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait;
use Drupal\Tests\system\Functional\Entity\EntityCacheTagsTestBase;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
class ShortcutCacheTagsTest extends EntityCacheTagsTestBase {
use AssertPageCacheContextsAndTagsTrait;
protected static $modules = [
'toolbar',
'shortcut',
'test_page_test',
'block',
];
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
$user_role = Role::load(RoleInterface::ANONYMOUS_ID);
$user_role
->grantPermission('customize shortcut links');
$user_role
->grantPermission('access shortcuts');
$user_role
->save();
}
protected function createEntity() {
$shortcut = Shortcut::create([
'shortcut_set' => 'default',
'title' => t('Llama'),
'weight' => 0,
'link' => [
[
'uri' => 'internal:/admin',
],
],
]);
$shortcut
->save();
return $shortcut;
}
public function testEntityCreation() {
$cache_tags = [
'config:shortcut.set.default',
];
\Drupal::cache('render')
->set('foo', 'bar', CacheBackendInterface::CACHE_PERMANENT, $cache_tags);
$this
->verifyRenderCache('foo', $cache_tags);
$this
->createEntity();
$this
->assertFalse(\Drupal::cache('render')
->get('foo'), 'Creating a new shortcut invalidates the cache tag of the shortcut set.');
}
public function testToolbar() {
$this
->drupalPlaceBlock('page_title_block', [
'id' => 'title',
]);
$test_page_url = Url::fromRoute('test_page_test.test_page');
$this
->verifyPageCache($test_page_url, 'MISS');
$this
->verifyPageCache($test_page_url, 'HIT');
$this
->drupalLogin($this->rootUser);
$this
->drupalGet('admin/config/system/cron');
$expected_cache_tags = [
'block_view',
'config:block.block.title',
'config:block_list',
'config:shortcut.set.default',
'config:system.menu.admin',
'config:system.theme',
'config:user.role.authenticated',
'rendered',
'user:' . $this->rootUser
->id(),
];
$this
->assertCacheTags($expected_cache_tags);
\Drupal::configFactory()
->getEditable('stark.settings')
->set('third_party_settings.shortcut.module_link', TRUE)
->save(TRUE);
$this
->drupalGet('admin/config/system/cron');
$this
->clickLink('Add to Default shortcuts');
$expected_cache_tags[] = 'config:shortcut_set_list';
$this
->assertCacheTags($expected_cache_tags);
$this
->drupalLogin($this
->drupalCreateUser([
'access toolbar',
]));
$this
->assertSession()
->linkNotExists('Shortcuts');
$this
->verifyDynamicPageCache($test_page_url, 'MISS');
$this
->verifyDynamicPageCache($test_page_url, 'HIT');
$this
->drupalLogin($this
->drupalCreateUser([
'access toolbar',
'access shortcuts',
]));
$this
->verifyDynamicPageCache($test_page_url, 'MISS');
$this
->verifyDynamicPageCache($test_page_url, 'HIT');
$this
->assertSession()
->linkExists('Shortcuts');
$this
->assertSession()
->linkNotExists('Cron');
$site_configuration_role = $this
->drupalCreateRole([
'access toolbar',
'access shortcuts',
'administer site configuration',
'access administration pages',
]);
$site_configuration_user1 = $this
->drupalCreateUser();
$site_configuration_user1
->addRole($site_configuration_role);
$site_configuration_user1
->save();
$site_configuration_user2 = $this
->drupalCreateUser();
$site_configuration_user2
->addRole($site_configuration_role);
$site_configuration_user2
->save();
$this
->drupalLogin($site_configuration_user1);
$this
->verifyDynamicPageCache($test_page_url, 'MISS');
$this
->verifyDynamicPageCache($test_page_url, 'HIT');
$this
->assertCacheContexts([
'user',
'url.query_args:_wrapper_format',
]);
$this
->assertSession()
->linkExists('Shortcuts');
$this
->assertSession()
->linkExists('Cron');
$this
->drupalLogin($site_configuration_user2);
$this
->verifyDynamicPageCache($test_page_url, 'HIT');
$this
->assertCacheContexts([
'user',
'url.query_args:_wrapper_format',
]);
$this
->assertSession()
->linkExists('Shortcuts');
$this
->assertSession()
->linkExists('Cron');
$shortcut = Shortcut::create([
'shortcut_set' => 'default',
'title' => 'Llama',
'weight' => 0,
'link' => [
[
'uri' => 'internal:/admin/config',
],
],
]);
$shortcut
->save();
$this
->verifyDynamicPageCache($test_page_url, 'HIT');
$this
->assertSession()
->linkExists('Cron');
$this
->assertSession()
->linkExists('Llama');
$shortcut
->set('title', 'Alpaca');
$shortcut
->save();
$this
->verifyDynamicPageCache($test_page_url, 'HIT');
$this
->assertSession()
->linkExists('Cron');
$this
->assertSession()
->linkExists('Alpaca');
$shortcut
->delete();
$this
->verifyDynamicPageCache($test_page_url, 'HIT');
$this
->assertSession()
->linkExists('Cron');
$this
->assertSession()
->linkNotExists('Alpaca');
}
}