Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / shortcut / tests / src / Functional / ShortcutCacheTagsTest.php
1 <?php
2
3 namespace Drupal\Tests\shortcut\Functional;
4
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;
10
11 /**
12  * Tests the Shortcut entity's cache tags.
13  *
14  * @group shortcut
15  */
16 class ShortcutCacheTagsTest extends EntityCacheTagsTestBase {
17
18   /**
19    * {@inheritdoc}
20    */
21   public static $modules = ['shortcut'];
22
23   /**
24    * {@inheritdoc}
25    */
26   protected function setUp() {
27     parent::setUp();
28
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');
34     $user_role->save();
35   }
36
37   /**
38    * {@inheritdoc}
39    */
40   protected function createEntity() {
41     // Create a "Llama" shortcut.
42     $shortcut = Shortcut::create([
43       'shortcut_set' => 'default',
44       'title' => t('Llama'),
45       'weight' => 0,
46       'link' => [['uri' => 'internal:/admin']],
47     ]);
48     $shortcut->save();
49
50     return $shortcut;
51   }
52
53   /**
54    * Tests that when creating a shortcut, the shortcut set tag is invalidated.
55    */
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);
60
61     // Verify a cache hit.
62     $this->verifyRenderCache('foo', $cache_tags);
63
64     // Now create a shortcut entity in that shortcut set.
65     $this->createEntity();
66
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.');
69   }
70
71 }