Verified Commit 40f67c92 authored by Alex Pott's avatar Alex Pott
Browse files

fix: #3466047 BlockContentBlock does not inherit block content entity cacheable dependency

By: smustgrave
By: larowlan
By: acbramley
By: xavier.masson
(cherry picked from commit a2c2d38d)
parent 49c6c843
Loading
Loading
Loading
Loading
Loading
+35
Changes for core/modules/block_content/src/Plugin/Block/BlockContentBlock.php: 35 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -2,12 +2,14 @@

namespace Drupal\block_content\Plugin\Block;

use Drupal\block_content\BlockContentInterface;
use Drupal\block_content\BlockContentUuidLookup;
use Drupal\block_content\Plugin\Derivative\BlockContent;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\Attribute\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Block\BlockManagerInterface;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
@@ -144,4 +146,37 @@ protected function getEntity() {
    return $this->entityRepository->getTranslationFromContext($this->blockContent);
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheContexts() {
    $contexts = parent::getCacheContexts();
    if (($entity = $this->getEntity()) && $entity instanceof BlockContentInterface) {
      $contexts = Cache::mergeContexts($contexts, $entity->getCacheContexts());
    }
    return $contexts;
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheTags() {
    $tags = parent::getCacheTags();
    if (($entity = $this->getEntity()) && $entity instanceof BlockContentInterface) {
      $tags = Cache::mergeTags($tags, $entity->getCacheTags());
    }
    return $tags;
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheMaxAge() {
    $max_age = parent::getCacheMaxAge();
    if (($entity = $this->getEntity()) && $entity instanceof BlockContentInterface) {
      $max_age = Cache::mergeMaxAges($max_age, $entity->getCacheMaxAge());
    }
    return $max_age;
  }

}
+58
Changes for core/modules/block_content/tests/src/Kernel/BlockContentCacheTest.php: 58 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\Tests\block_content\Kernel;

use Drupal\block\Entity\Block;
use Drupal\Component\Plugin\PluginBase;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\block_content\Traits\BlockContentCreationTrait;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;

/**
 * Tests cache metadata for BlockContentBlock plugin.
 */
#[Group('block_content')]
#[RunTestsInSeparateProcesses]
class BlockContentCacheTest extends KernelTestBase {

  use BlockContentCreationTrait;

  /**
   * {@inheritdoc}
   */
  protected static $modules = ['block', 'block_content', 'system', 'user'];

  /**
   * {@inheritdoc}
   */
  public function setUp(): void {
    parent::setUp();
    $this->installEntitySchema('user');
    $this->installEntitySchema('block_content');

    $this->createBlockContentType(['id' => 'basic', 'label' => 'Basic']);
  }

  /**
   * Tests the cache tags for BlockContentBlock.
   */
  public function testBlockContentBlockCacheData(): void {
    $block_content = $this->createBlockContent('Cache test');

    $block = Block::create([
      'plugin' => 'block_content' . PluginBase::DERIVATIVE_SEPARATOR . $block_content->uuid(),
      'region' => 'content',
      'id' => 'cache_test',
      'theme' => 'stark',
    ]);

    $plugin = $block->getPlugin();
    $this->assertCount(0, \array_diff($block_content->getCacheContexts(), $plugin->getCacheContexts()));
    $this->assertSame($block_content->getCacheMaxAge(), $plugin->getCacheMaxAge());
    $this->assertCount(0, \array_diff($block_content->getCacheTags(), $plugin->getCacheTags()));
  }

}
+1
Changes for core/profiles/demo_umami/tests/src/FunctionalJavascript/MultipleRequestsPerformanceTestAssertions/umamiLogin.yml: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -43,6 +43,7 @@ CacheTagGroupedLookups:
  -
    - 'config:block_list'
  -
    - 'block_content:1'
    - 'config:filter.format.basic_html'
    - 'config:system.menu.footer'
  -