3 namespace Drupal\Tests\shortcut\Functional;
5 use Drupal\Core\Cache\CacheBackendInterface;
6 use Drupal\shortcut\Entity\Shortcut;
7 use Drupal\Tests\system\Functional\Entity\EntityCacheTagsTestBase;
8 use Drupal\user\Entity\Role;
9 use Drupal\user\RoleInterface;
12 * Tests the Shortcut entity's cache tags.
16 class ShortcutCacheTagsTest extends EntityCacheTagsTestBase {
21 public static $modules = ['shortcut'];
26 protected function setUp() {
29 // Give anonymous users permission to customize shortcut links, so that we
30 // can verify the cache tags of cached versions of shortcuts.
31 $user_role = Role::load(RoleInterface::ANONYMOUS_ID);
32 $user_role->grantPermission('customize shortcut links');
33 $user_role->grantPermission('access shortcuts');
40 protected function createEntity() {
41 // Create a "Llama" shortcut.
42 $shortcut = Shortcut::create([
43 'shortcut_set' => 'default',
44 'title' => t('Llama'),
46 'link' => [['uri' => 'internal:/admin']],
54 * Tests that when creating a shortcut, the shortcut set tag is invalidated.
56 public function testEntityCreation() {
57 // Create a cache entry that is tagged with a shortcut set cache tag.
58 $cache_tags = ['config:shortcut.set.default'];
59 \Drupal::cache('render')->set('foo', 'bar', CacheBackendInterface::CACHE_PERMANENT, $cache_tags);
61 // Verify a cache hit.
62 $this->verifyRenderCache('foo', $cache_tags);
64 // Now create a shortcut entity in that shortcut set.
65 $this->createEntity();
67 // Verify a cache miss.
68 $this->assertFalse(\Drupal::cache('render')->get('foo'), 'Creating a new shortcut invalidates the cache tag of the shortcut set.');