Skip to content
Snippets Groups Projects
Commit c29d83bc authored by catch's avatar catch
Browse files

Issue #3085475 by vidorado, Ankit.Gupta, kristiaanvandeneynde, smustgrave,...

Issue #3085475 by vidorado, Ankit.Gupta, kristiaanvandeneynde, smustgrave, catch: ShortcutCacheTagsTest needlessly test the render cache
parent f2c2c74c
No related branches found
No related tags found
1 merge request!2964Issue #2865710 : Dependencies from only one instance of a widget are used in display modes
Pipeline #439783 passed with warnings
Pipeline: drupal

#439796

    Pipeline: drupal

    #439791

      Pipeline: drupal

      #439784

        ......@@ -36105,12 +36105,6 @@
        'count' => 1,
        'path' => __DIR__ . '/modules/system/tests/src/Functional/Entity/EntityCacheTagsTestBase.php',
        ];
        $ignoreErrors[] = [
        'message' => '#^Method Drupal\\\\Tests\\\\system\\\\Functional\\\\Entity\\\\EntityCacheTagsTestBase\\:\\:verifyRenderCache\\(\\) has no return type specified\\.$#',
        'identifier' => 'missingType.return',
        'count' => 1,
        'path' => __DIR__ . '/modules/system/tests/src/Functional/Entity/EntityCacheTagsTestBase.php',
        ];
        $ignoreErrors[] = [
        'message' => '#^Method Drupal\\\\Tests\\\\system\\\\Functional\\\\Entity\\\\EntityFormTest\\:\\:loadEntityByName\\(\\) has no return type specified\\.$#',
        'identifier' => 'missingType.return',
        ......@@ -4,7 +4,6 @@
        namespace Drupal\Tests\shortcut\Functional;
        use Drupal\Core\Cache\CacheableMetadata;
        use Drupal\Core\Url;
        use Drupal\shortcut\Entity\Shortcut;
        use Drupal\shortcut\Entity\ShortcutSet;
        ......@@ -82,29 +81,6 @@ protected function createEntity() {
        return $shortcut;
        }
        /**
        * Tests that when creating a shortcut, the shortcut set tag is invalidated.
        */
        public function testEntityCreation(): void {
        $cache_bin = $this->getRenderCacheBackend();
        // Create a cache entry that is tagged with a shortcut set cache tag.
        $cache_tags = ['config:shortcut.set.default'];
        $cacheability = new CacheableMetadata();
        $cacheability->addCacheTags($cache_tags);
        $cache_bin->set(['foo'], 'bar', $cacheability, $cacheability);
        // Verify a cache hit.
        $this->verifyRenderCache(['foo'], $cache_tags, $cacheability);
        // Now create a shortcut entity in that shortcut set.
        $this->createEntity();
        // Verify a cache miss.
        $this->assertFalse($cache_bin->get(['foo'], $cacheability), 'Creating a new shortcut invalidates the cache tag of the shortcut set.');
        }
        /**
        * Tests visibility and cacheability of shortcuts in the toolbar.
        */
        ......
        <?php
        declare(strict_types=1);
        namespace Drupal\Tests\shortcut\Kernel;
        use Drupal\Core\Cache\CacheableMetadata;
        use Drupal\KernelTests\KernelTestBase;
        use Drupal\shortcut\Entity\Shortcut;
        use Drupal\shortcut\Entity\ShortcutSet;
        use Drupal\shortcut\ShortcutSetInterface;
        use Drupal\Tests\system\Traits\CacheTestTrait;
        use Drupal\Tests\user\Traits\UserCreationTrait;
        use Drupal\user\UserInterface;
        /**
        * Tests the Shortcut entity's cache tags.
        *
        * @group shortcut
        */
        class ShortcutCacheTagsTest extends KernelTestBase {
        use UserCreationTrait;
        use CacheTestTrait;
        /**
        * {@inheritdoc}
        */
        protected static $modules = [
        'system',
        'user',
        'toolbar',
        'shortcut',
        'link',
        ];
        /**
        * User with permission to administer shortcuts.
        */
        protected UserInterface $adminUser;
        /**
        * The default shortcut set.
        */
        protected ShortcutSetInterface $shortcutSet;
        /**
        * {@inheritdoc}
        */
        protected function setUp(): void {
        parent::setUp();
        $this->installEntitySchema('user');
        $this->installEntitySchema('shortcut');
        $this->installEntitySchema('shortcut_set');
        $this->adminUser = $this->createUser([
        'access toolbar',
        'access shortcuts',
        'administer site configuration',
        'administer shortcuts',
        'administer themes',
        ]);
        $this->shortcutSet = ShortcutSet::create([
        'id' => 'default',
        'label' => 'Default',
        ]);
        $this->shortcutSet->save();
        }
        /**
        * Creates a shortcut entity in the default shortcut set.
        */
        protected function createShortcutEntity(): Shortcut {
        // Create a "Llama" shortcut.
        $shortcut = Shortcut::create([
        'shortcut_set' => $this->shortcutSet->id(),
        'title' => 'Llama',
        'weight' => 0,
        'link' => [['uri' => 'internal:/admin']],
        ]);
        $shortcut->save();
        return $shortcut;
        }
        /**
        * Tests that when creating a shortcut, the shortcut set tag is invalidated.
        */
        public function testEntityCreation(): void {
        $cache_bin = $this->getDefaultVariationCache();
        // Create a cache entry that is tagged with a shortcut set cache tag.
        $cache_tags = ['config:shortcut.set.' . $this->shortcutSet->id()];
        $cacheability = new CacheableMetadata();
        $cacheability->addCacheTags($cache_tags);
        $cache_bin->set(['foo'], 'bar', $cacheability, $cacheability);
        // Verify a cache hit.
        $this->verifyDefaultCache(['foo'], $cache_tags, $cacheability);
        // Now create a shortcut entity in that shortcut set.
        $this->createShortcutEntity();
        // Verify a cache miss.
        $this->assertFalse($cache_bin->get(['foo'], $cacheability), 'Creating a new shortcut invalidates the cache tag of the shortcut set.');
        }
        }
        ......@@ -5,7 +5,6 @@
        namespace Drupal\Tests\system\Functional\Entity;
        use Drupal\Core\Cache\Cache;
        use Drupal\Core\Cache\CacheableDependencyInterface;
        use Drupal\Core\Cache\CacheableMetadata;
        use Drupal\Core\Entity\EntityInterface;
        use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
        ......@@ -16,6 +15,7 @@
        use Drupal\field\Entity\FieldStorageConfig;
        use Drupal\field\Entity\FieldConfig;
        use Drupal\Tests\system\Functional\Cache\PageCacheTagsTestBase;
        use Drupal\Tests\system\Traits\CacheTestTrait;
        use Drupal\user\Entity\Role;
        use Drupal\user\RoleInterface;
        ......@@ -23,6 +23,7 @@
        * Provides helper methods for Entity cache tags tests.
        */
        abstract class EntityCacheTagsTestBase extends PageCacheTagsTestBase {
        use CacheTestTrait;
        /**
        * {@inheritdoc}
        ......@@ -610,39 +611,22 @@ public function testReferencedEntity(): void {
        $this->verifyPageCache($nonempty_entity_listing_url, 'HIT', $nonempty_entity_listing_cache_tags);
        }
        /**
        * Verify that a given render cache entry exists, with the correct cache tags.
        *
        * @param string[] $keys
        * The render cache item keys.
        * @param array $tags
        * An array of expected cache tags.
        * @param \Drupal\Core\Cache\CacheableDependencyInterface $cacheability
        * The initial cacheability the item was rendered with.
        */
        protected function verifyRenderCache(array $keys, array $tags, CacheableDependencyInterface $cacheability) {
        $cache_bin = $this->getRenderCacheBackend();
        // Also verify the existence of an entity render cache entry.
        $cache_entry = $cache_bin->get($keys, $cacheability);
        $this->assertInstanceOf(\stdClass::class, $cache_entry);
        sort($cache_entry->tags);
        sort($tags);
        $this->assertSame($cache_entry->tags, $tags);
        }
        /**
        * Retrieves the render cache backend as a variation cache.
        *
        * This is how Drupal\Core\Render\RenderCache uses the render cache backend.
        *
        * @deprecated in drupal:11.2.0 and is removed from drupal:12.0.0.
        * Use ::getRenderVariationCache() instead, which is inherited
        * from CacheTestTrait.
        *
        * @see https://www.drupal.org/node/3508905
        *
        * @return \Drupal\Core\Cache\VariationCacheInterface
        * The render cache backend as a variation cache.
        */
        protected function getRenderCacheBackend() {
        /** @var \Drupal\Core\Cache\VariationCacheFactoryInterface $variation_cache_factory */
        $variation_cache_factory = \Drupal::service('variation_cache_factory');
        return $variation_cache_factory->get('render');
        return $this->getRenderVariationCache();
        }
        }
        <?php
        declare(strict_types=1);
        namespace Drupal\Tests\system\Traits;
        use Drupal\Core\Cache\CacheableDependencyInterface;
        use Drupal\Core\Cache\VariationCacheInterface;
        /**
        * Provides helper methods for interacting with cache backends.
        */
        trait CacheTestTrait {
        /**
        * Retrieves the render cache backend as a variation cache.
        *
        * This is how Drupal\Core\Render\RenderCache uses the render cache backend.
        *
        * @return \Drupal\Core\Cache\VariationCacheInterface
        * The render cache backend as a variation cache.
        */
        protected function getRenderVariationCache(): VariationCacheInterface {
        /** @var \Drupal\Core\Cache\VariationCacheFactoryInterface $variation_cache_factory */
        $variation_cache_factory = \Drupal::service('variation_cache_factory');
        return $variation_cache_factory->get('render');
        }
        /**
        * Retrieves the default cache backend as a variation cache.
        *
        * @return \Drupal\Core\Cache\VariationCacheInterface
        * The default cache backend as a variation cache.
        */
        protected function getDefaultVariationCache(): VariationCacheInterface {
        /** @var \Drupal\Core\Cache\VariationCacheFactoryInterface $variation_cache_factory */
        $variation_cache_factory = \Drupal::service('variation_cache_factory');
        return $variation_cache_factory->get('default');
        }
        /**
        * Verify that a given render cache entry exists with the correct cache tags.
        *
        * @param string[] $keys
        * The render cache item keys.
        * @param array $tags
        * An array of expected cache tags.
        * @param \Drupal\Core\Cache\CacheableDependencyInterface $cacheability
        * The initial cacheability the item was rendered with.
        */
        protected function verifyRenderCache(array $keys, array $tags, CacheableDependencyInterface $cacheability): void {
        $this->verifyCache($this->getRenderVariationCache(), $keys, $tags, $cacheability);
        }
        /**
        * Verify that a given default cache entry exists with the correct cache tags.
        *
        * @param string[] $keys
        * The cache item keys.
        * @param array $tags
        * An array of expected cache tags.
        * @param \Drupal\Core\Cache\CacheableDependencyInterface $cacheability
        * The initial cacheability for the item.
        */
        protected function verifyDefaultCache(array $keys, array $tags, CacheableDependencyInterface $cacheability): void {
        $this->verifyCache($this->getDefaultVariationCache(), $keys, $tags, $cacheability);
        }
        /**
        * Verify that a given cache entry exists, with the correct cache tags.
        *
        * @param \Drupal\Core\Cache\VariationCacheInterface $cache_bin
        * The cache bin to check.
        * @param string[] $keys
        * The cache item keys.
        * @param array $tags
        * An array of expected cache tags.
        * @param \Drupal\Core\Cache\CacheableDependencyInterface $cacheability
        * The initial cacheability for the item.
        */
        private function verifyCache(VariationCacheInterface $cache_bin, array $keys, array $tags, CacheableDependencyInterface $cacheability): void {
        $cache_entry = $cache_bin->get($keys, $cacheability);
        $this->assertInstanceOf(\stdClass::class, $cache_entry);
        sort($cache_entry->tags);
        sort($tags);
        $this->assertSame($cache_entry->tags, $tags);
        }
        }
        0% Loading or .
        You are about to add 0 people to the discussion. Proceed with caution.
        Finish editing this message first!
        Please register or to comment