Skip to content
Snippets Groups Projects
Verified Commit 1a8a6184 authored by Dave Long's avatar Dave Long
Browse files

Issue #3493858 by vidorado, xavier.masson, smustgrave: Extend ViewsBlockBase...

Issue #3493858 by vidorado, xavier.masson, smustgrave: Extend ViewsBlockBase to merge cache metadata from display handler

(cherry picked from commit 1ad501aa)
parent d3b4dce5
Branches
Tags
9 merge requests!12227Issue #3181946 by jonmcl, mglaman,!12079Issue #3523476 by matthiasm11: Add empty check on operator,!12024Fix: DocBlock comment for return value of Drupal\Core\Database\Connection::transactionManager(),!11974Draft: Issue #3495165 by catch, joeyroth, berdir, texas-bronius: Better warning...,!11934Issue #3520997: DefaultLazyPluginCollection unnecessarily instantiates plugins when sorting collection,!11887Issue #3520065: The migrate Row class API is incomplete,!11636Draft: Issue #3515643 by macsim: fieldNameExists method is inconsistent,!11515Issue #3480419 by mondrake, smustgrave, catch: Method...,!11380Issue #3490698 by catch, spokje: Bump MINIMUM_STABILITY back to 'stable' when...
Pipeline #435164 passed with warnings
Pipeline: drupal

#435172

    ......@@ -2,6 +2,7 @@
    namespace Drupal\views\Plugin\Block;
    use Drupal\Core\Cache\Cache;
    use Drupal\Core\Url;
    use Drupal\Core\Access\AccessResult;
    use Drupal\Core\Block\BlockBase;
    ......@@ -86,6 +87,30 @@ public static function create(ContainerInterface $container, array $configuratio
    );
    }
    /**
    * {@inheritdoc}
    */
    public function getCacheContexts() {
    $contexts = $this->view->display_handler->getCacheMetadata()->getCacheContexts();
    return Cache::mergeContexts(parent::getCacheContexts(), $contexts);
    }
    /**
    * {@inheritdoc}
    */
    public function getCacheTags() {
    $tags = $this->view->display_handler->getCacheMetadata()->getCacheTags();
    return Cache::mergeTags(parent::getCacheTags(), $tags);
    }
    /**
    * {@inheritdoc}
    */
    public function getCacheMaxAge() {
    $max_age = $this->view->display_handler->getCacheMetadata()->getCacheMaxAge();
    return Cache::mergeMaxAges(parent::getCacheMaxAge(), $max_age);
    }
    /**
    * {@inheritdoc}
    */
    ......
    ......@@ -4,7 +4,11 @@
    namespace Drupal\Tests\views\Unit\Plugin\Block;
    use Drupal\Core\Cache\Cache;
    use Drupal\Core\Cache\CacheableMetadata;
    use Drupal\Core\Cache\Context\CacheContextsManager;
    use Drupal\Core\DependencyInjection\ContainerBuilder;
    use Drupal\Core\Plugin\Context\ContextInterface;
    use Drupal\Tests\UnitTestCase;
    use Drupal\views\Plugin\Block\ViewsBlock;
    ......@@ -60,14 +64,25 @@ class ViewsBlockTest extends UnitTestCase {
    * {@inheritdoc}
    */
    protected function setUp(): void {
    // @todo Change the autogenerated stub.
    parent::setUp();
    $container = new ContainerBuilder();
    $cache_context_manager = $this->createMock(CacheContextsManager::class);
    $cache_context_manager->expects($this->any())
    ->method('getAll')
    ->willReturn([]);
    $cache_context_manager->expects($this->any())
    ->method('assertValidTokens')
    ->willReturn(TRUE);
    $container->set('cache_contexts_manager', $cache_context_manager);
    $condition_plugin_manager = $this->createMock('Drupal\Core\Executable\ExecutableManagerInterface');
    $condition_plugin_manager->expects($this->any())
    ->method('getDefinitions')
    ->willReturn([]);
    $container = new ContainerBuilder();
    $container->set('plugin.manager.condition', $condition_plugin_manager);
    \Drupal::setContainer($container);
    $this->executable = $this->getMockBuilder('Drupal\views\ViewExecutable')
    ......@@ -84,7 +99,7 @@ protected function setUp(): void {
    $this->executable->display_handler = $this->getMockBuilder('Drupal\views\Plugin\views\display\Block')
    ->disableOriginalConstructor()
    ->onlyMethods([])
    ->onlyMethods(['getCacheMetadata'])
    ->getMock();
    $this->view = $this->getMockBuilder('Drupal\views\Entity\View')
    ......@@ -155,6 +170,76 @@ public function testBuild(): void {
    $this->assertEquals($build, $plugin->build());
    }
    /**
    * Tests that cacheable metadata is retrieved from the view and merged with block cacheable metadata.
    *
    * @dataProvider providerTestCacheableMetadata
    *
    * @see \Drupal\views\Plugin\block\ViewsBlock::build()
    */
    public function testCacheableMetadata(int $blockCacheMaxAge, int $viewCacheMaxAge, int $expectedCacheMaxAge): void {
    $blockCacheTags = ['block-cachetag-1', 'block-cachetag-2'];
    $blockCacheContexts = ['block-cache-context-1', 'block-cache-context-2'];
    $viewCacheTags = ['view-cachetag-1', 'view-cachetag-2'];
    $viewCacheContexts = ['view-cache-context-1', 'view-cache-context-2'];
    // Mock view cache metadata.
    $viewCacheMetadata = $this->createMock(CacheableMetadata::class);
    $viewCacheMetadata
    ->method('getCacheTags')
    ->willReturn($viewCacheTags);
    $viewCacheMetadata
    ->method('getCacheContexts')
    ->willReturn($viewCacheContexts);
    $viewCacheMetadata
    ->method('getCacheMaxAge')
    ->willReturn($viewCacheMaxAge);
    $this->executable->display_handler
    ->method('getCacheMetadata')
    ->willReturn($viewCacheMetadata);
    // Mock block context.
    $blockContext = $this->createMock(ContextInterface::class);
    $blockContext
    ->method('getCacheTags')
    ->willReturn($blockCacheTags);
    $blockContext
    ->method('getCacheContexts')
    ->willReturn($blockCacheContexts);
    $blockContext
    ->method('getCacheMaxAge')
    ->willReturn($blockCacheMaxAge);
    // Create the views block.
    $block_id = 'views_block:test_view-block_1';
    $config = [];
    $definition = [
    'provider' => 'views',
    ];
    $plugin = new ViewsBlock($config, $block_id, $definition, $this->executableFactory, $this->storage, $this->account);
    $plugin->setContext('context_name', $blockContext);
    // Assertions.
    $this->assertEmpty(array_diff(Cache::mergeTags($viewCacheTags, $blockCacheTags), $plugin->getCacheTags()));
    $this->assertEmpty(array_diff(Cache::mergeContexts($viewCacheContexts, $blockCacheContexts), $plugin->getCacheContexts()));
    $this->assertEquals($expectedCacheMaxAge, $plugin->getCacheMaxAge());
    }
    /**
    * Data provider for ::testCacheableMetadata()
    */
    public static function providerTestCacheableMetadata(): array {
    return [
    'View expires before' => [500, 1000, 500],
    'Block expires before' => [1000, 500, 500],
    'Only block is permanent' => [Cache::PERMANENT, 500, 500],
    'Only view is permanent' => [500, Cache::PERMANENT, 500],
    'Both view and block are permanent' => [Cache::PERMANENT, Cache::PERMANENT, Cache::PERMANENT],
    ];
    }
    /**
    * Tests the build method.
    *
    ......
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Please register or to comment