Issue #2942600 by tim.plunkett: SectionComponent::toRenderArray() runs access checks even when not needed

merge-requests/1654/head
xjm 2018-02-06 21:47:47 -05:00
parent c924020c45
commit 97a4389997
3 changed files with 59 additions and 5 deletions

View File

@ -3,6 +3,7 @@
namespace Drupal\layout_builder;
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Plugin\ContextAwarePluginInterface;
@ -103,11 +104,18 @@ class SectionComponent {
// @todo Figure out the best way to unify fields and blocks and components
// in https://www.drupal.org/node/1875974.
if ($plugin instanceof BlockPluginInterface) {
$access = $plugin->access($this->currentUser(), TRUE);
$cacheability = CacheableMetadata::createFromObject($access);
$cacheability = CacheableMetadata::createFromObject($plugin);
if ($in_preview || $access->isAllowed()) {
$cacheability->addCacheableDependency($plugin);
// Only check access if the component is not being previewed.
if ($in_preview) {
$access = AccessResult::allowed()->setCacheMaxAge(0);
}
else {
$access = $plugin->access($this->currentUser(), TRUE);
}
$cacheability->addCacheableDependency($access);
if ($access->isAllowed()) {
// @todo Move this to BlockBase in https://www.drupal.org/node/2931040.
$output = [
'#theme' => 'block',

View File

@ -76,7 +76,6 @@ class LayoutSectionTest extends BrowserTestBase {
],
[
'foobar',
'User context found',
],
'user',
'user:2',

View File

@ -136,6 +136,9 @@ class SectionRenderTest extends UnitTestCase {
$access_result = AccessResult::forbidden();
$block->access($this->account->reveal(), TRUE)->willReturn($access_result);
$block->build()->shouldNotBeCalled();
$block->getCacheContexts()->willReturn([]);
$block->getCacheTags()->willReturn([]);
$block->getCacheMaxAge()->willReturn(Cache::PERMANENT);
$section = [
new SectionComponent('some_uuid', 'content', ['id' => 'block_plugin_id']),
@ -155,6 +158,50 @@ class SectionRenderTest extends UnitTestCase {
$this->assertEquals($expected, $result);
}
/**
* @covers ::toRenderArray
*/
public function testToRenderArrayPreview() {
$block_content = ['#markup' => 'The block content.'];
$render_array = [
'#theme' => 'block',
'#weight' => 0,
'#configuration' => [],
'#plugin_id' => 'block_plugin_id',
'#base_plugin_id' => 'block_plugin_id',
'#derivative_plugin_id' => NULL,
'content' => $block_content,
'#cache' => [
'contexts' => [],
'tags' => [],
'max-age' => 0,
],
];
$block = $this->prophesize(BlockPluginInterface::class);
$this->blockManager->createInstance('block_plugin_id', ['id' => 'block_plugin_id'])->willReturn($block->reveal());
$block->access($this->account->reveal(), TRUE)->shouldNotBeCalled();
$block->build()->willReturn($block_content);
$block->getCacheContexts()->willReturn([]);
$block->getCacheTags()->willReturn([]);
$block->getCacheMaxAge()->willReturn(Cache::PERMANENT);
$block->getConfiguration()->willReturn([]);
$block->getPluginId()->willReturn('block_plugin_id');
$block->getBaseId()->willReturn('block_plugin_id');
$block->getDerivativeId()->willReturn(NULL);
$section = [
new SectionComponent('some_uuid', 'content', ['id' => 'block_plugin_id']),
];
$expected = [
'content' => [
'some_uuid' => $render_array,
],
];
$result = (new Section('layout_onecol', [], $section))->toRenderArray([], TRUE);
$this->assertEquals($expected, $result);
}
/**
* @covers ::toRenderArray
*/