diff --git a/core/modules/shortcut/tests/src/Functional/ShortcutCacheTagsTest.php b/core/modules/shortcut/tests/src/Functional/ShortcutCacheTagsTest.php index f8101fa1d2..fd16a3b5ec 100644 --- a/core/modules/shortcut/tests/src/Functional/ShortcutCacheTagsTest.php +++ b/core/modules/shortcut/tests/src/Functional/ShortcutCacheTagsTest.php @@ -5,7 +5,7 @@ use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Url; use Drupal\shortcut\Entity\Shortcut; -use Drupal\Tests\system\Functional\Entity\EntityCacheTagsTestBase; +use Drupal\Tests\system\Functional\Cache\PageCacheTagsTestBase; use Drupal\user\Entity\Role; use Drupal\user\RoleInterface; @@ -14,12 +14,12 @@ * * @group shortcut */ -class ShortcutCacheTagsTest extends EntityCacheTagsTestBase { +class ShortcutCacheTagsTest extends PageCacheTagsTestBase { /** * {@inheritdoc} */ - public static $modules = ['toolbar', 'shortcut', 'test_page_test']; + public static $modules = ['toolbar', 'shortcut', 'test_page_test', 'block']; /** * {@inheritdoc} @@ -62,24 +62,30 @@ protected function createEntity() { public function testEntityCreation() { // Create a cache entry that is tagged with a shortcut set cache tag. $cache_tags = ['config:shortcut.set.default']; - \Drupal::cache('render')->set('foo', 'bar', CacheBackendInterface::CACHE_PERMANENT, $cache_tags); + $render_cache = \Drupal::cache('render'); + $render_cache->set('foo', 'bar', CacheBackendInterface::CACHE_PERMANENT, $cache_tags); // Verify a cache hit. - $this->verifyRenderCache('foo', $cache_tags); + $item = $render_cache->get('foo'); + $this->assertEquals($cache_tags, $item->tags); // Now create a shortcut entity in that shortcut set. $this->createEntity(); // Verify a cache miss. - $this->assertFalse(\Drupal::cache('render')->get('foo'), 'Creating a new shortcut invalidates the cache tag of the shortcut set.'); + $this->assertFalse($render_cache->get('foo'), 'Creating a new shortcut invalidates the cache tag of the shortcut set.'); } + /** + * Tests visibility and cacheability of shortcuts in the toolbar. + */ public function testToolbar() { - // Change to a theme that displays shortcuts. - \Drupal::service('theme_installer')->install(['seven']); - $this->config('system.theme') - ->set('default', 'seven') - ->save(); + \Drupal::configFactory() + ->getEditable('stark.settings') + ->set('third_party_settings.shortcut.module_link', TRUE) + ->save(TRUE); + + $this->drupalPlaceBlock('page_title_block'); $this->verifyPageCache(Url::fromRoute('test_page_test.test_page'), 'MISS'); $this->verifyPageCache(Url::fromRoute('test_page_test.test_page'), 'HIT'); @@ -92,7 +98,7 @@ public function testToolbar() { // Verify that users without the 'access shortcuts' permission can't see the // shortcuts. $this->drupalLogin($this->drupalCreateUser(['access toolbar'])); - $this->assertNoLink('Shortcuts', 'Shortcut link not found on page.'); + $this->assertNoLink('Shortcuts'); $this->verifyDynamicPageCache(Url::fromRoute('test_page_test.test_page'), 'MISS'); $this->verifyDynamicPageCache(Url::fromRoute('test_page_test.test_page'), 'HIT'); @@ -105,18 +111,51 @@ public function testToolbar() { $this->verifyDynamicPageCache(Url::fromRoute('test_page_test.test_page'), 'MISS'); $this->verifyDynamicPageCache(Url::fromRoute('test_page_test.test_page'), 'HIT'); $this->assertLink('Shortcuts'); + $this->assertNoLink('Cron'); // Verify that users with the 'access shortcuts' permission can see the // shortcuts. - $this->drupalLogin($this->drupalCreateUser([ + $site_configuration_user = $this->drupalCreateUser([ 'access toolbar', 'access shortcuts', 'administer site configuration', - ])); + 'access administration pages', + ]); + $this->drupalLogin($site_configuration_user); $this->verifyDynamicPageCache(Url::fromRoute('test_page_test.test_page'), 'MISS'); $this->verifyDynamicPageCache(Url::fromRoute('test_page_test.test_page'), 'HIT'); - $this->clickLink('Shortcuts', 0, 'Shortcut link found on page.'); - $this->assertLink('Cron', 0, 'Cron shortcut link found on page.'); + $this->assertLink('Shortcuts'); + $this->assertLink('Cron'); + + // Add another shortcut. + $shortcut = Shortcut::create([ + 'shortcut_set' => 'default', + 'title' => 'Llama', + 'weight' => 0, + 'link' => [['uri' => 'internal:/admin/config']], + ]); + $shortcut->save(); + + $this->drupalGet('admin/config'); + + // The shortcuts are displayed in a lazy builder, so the page is still a + // cache HIT but shows the new shortcut immediately. + $this->verifyDynamicPageCache(Url::fromRoute('test_page_test.test_page'), 'HIT'); + $this->assertLink('Cron'); + $this->assertLink('Llama'); + + // Update the shortcut title and assert that it is updated. + $shortcut->set('title', 'Alpaca'); + $shortcut->save(); + $this->verifyDynamicPageCache(Url::fromRoute('test_page_test.test_page'), 'HIT'); + $this->assertLink('Cron'); + $this->assertLink('Alpaca'); + + // Delete the shortcut and assert that the link is gone. + $shortcut->delete(); + $this->verifyDynamicPageCache(Url::fromRoute('test_page_test.test_page'), 'HIT'); + $this->assertLink('Cron'); + $this->assertNoLink('Alpaca'); } }