.../BlockPluginHasOffCanvasFormAccessCheck.php | 3 +- ...OffCanvasFormAccessCheckTestFormsWithForms.php} | 80 +++++++++++++++------- 2 files changed, 56 insertions(+), 27 deletions(-) diff --git a/core/modules/outside_in/src/Access/BlockPluginHasOffCanvasFormAccessCheck.php b/core/modules/outside_in/src/Access/BlockPluginHasOffCanvasFormAccessCheck.php index 6966943..e8136d6 100644 --- a/core/modules/outside_in/src/Access/BlockPluginHasOffCanvasFormAccessCheck.php +++ b/core/modules/outside_in/src/Access/BlockPluginHasOffCanvasFormAccessCheck.php @@ -5,6 +5,7 @@ use Drupal\block\BlockInterface; use Drupal\Core\Access\AccessResult; use Drupal\Core\Block\BlockPluginInterface; +use Drupal\Core\Plugin\PluginWithFormsInterface; use Drupal\Core\Routing\Access\AccessInterface; /** @@ -42,7 +43,7 @@ public function access(BlockInterface $block) { * @todo Remove when outside_in_preprocess_block() is removed. */ public function accessBlockPlugin(BlockPluginInterface $block_plugin) { - return AccessResult::allowedIf($block_plugin->hasFormClass('off_canvas')); + return AccessResult::allowedIf($block_plugin instanceof PluginWithFormsInterface && $block_plugin->hasFormClass('off_canvas')); } } diff --git a/core/modules/outside_in/tests/src/Unit/Access/BlockPluginHasOffCanvasFormAccessCheckTest.php b/core/modules/outside_in/tests/src/Unit/Access/BlockPluginHasOffCanvasFormAccessCheckTestFormsWithForms.php similarity index 26% rename from core/modules/outside_in/tests/src/Unit/Access/BlockPluginHasOffCanvasFormAccessCheckTest.php rename to core/modules/outside_in/tests/src/Unit/Access/BlockPluginHasOffCanvasFormAccessCheckTestFormsWithForms.php index 6d5366b..8456f13 100644 --- a/core/modules/outside_in/tests/src/Unit/Access/BlockPluginHasOffCanvasFormAccessCheckTest.php +++ b/core/modules/outside_in/tests/src/Unit/Access/BlockPluginHasOffCanvasFormAccessCheckTestFormsWithForms.php @@ -7,6 +7,11 @@ use Drupal\Core\Access\AccessResultInterface; use Drupal\Core\Access\AccessResultNeutral; use Drupal\Core\Block\BlockBase; +use Drupal\Core\Block\BlockPluginInterface; +use Drupal\Core\Cache\UnchangingCacheableDependencyTrait; +use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Plugin\PluginWithFormsInterface; +use Drupal\Core\Session\AccountInterface; use Drupal\outside_in\Access\BlockPluginHasOffCanvasFormAccessCheck; use Drupal\Tests\UnitTestCase; @@ -21,8 +26,8 @@ class BlockPluginHasOffCanvasFormAccessCheckTest extends UnitTestCase { * @covers ::accessBlockPlugin * @dataProvider providerTestAccess */ - public function testAccess(array $plugin_definition, AccessResultInterface $expected_access_result) { - $block_plugin = new TestBlockClass([], $this->randomMachineName(), $plugin_definition); + public function testAccess($class, array $plugin_definition, AccessResultInterface $expected_access_result) { + $block_plugin = new $class([], $this->randomMachineName(), $plugin_definition); $block = $this->prophesize(BlockInterface::class); $block->getPlugin()->willReturn($block_plugin); @@ -32,40 +37,63 @@ public function testAccess(array $plugin_definition, AccessResultInterface $expe } public function providerTestAccess() { - return [ - 'set to class' => [ - [ - 'provider' => 'block_test', - 'forms' => [ - 'off_canvas' => $this->randomMachineName(), - ], - ], - new AccessResultAllowed(), - ], - 'not set' => [ - [ - 'provider' => 'block_test', - ], - new AccessResultNeutral(), + $annotation_forms_off_canvas_class = [ + 'provider' => 'block_test', + 'forms' => [ + 'off_canvas' => $this->randomMachineName(), ], - 'set to FALSE' => [ - [ - 'provider' => 'block_test', - 'forms' => [ - 'off_canvas' => FALSE, - ], - ], - new AccessResultNeutral(), + ]; + $annotation_forms_off_canvas_not_set = [ + 'provider' => 'block_test', + ]; + $annotation_forms_off_canvas_false = [ + 'provider' => 'block_test', + 'forms' => [ + 'off_canvas' => FALSE, ], ]; + return [ + 'block plugin with forms, forms[off_canvas] set to class' => [TestBlockClassWithForms::class, $annotation_forms_off_canvas_class, new AccessResultAllowed()], + 'block plugin with forms, forms[off_canvas] not set' => [TestBlockClassWithForms::class, $annotation_forms_off_canvas_not_set, new AccessResultNeutral()], + 'block plugin with forms, forms[off_canvas] set to FALSE' => [TestBlockClassWithForms::class, $annotation_forms_off_canvas_false, new AccessResultNeutral()], + // In practice, all block plugins extend BlockBase, which means they all + // implement PluginWithFormsInterface, but this may change in the future. + // This ensures Settings Tray will continue to work correctly. + 'block plugin without forms, forms[off_canvas] set to class' => [TestBlockClassWithoutForms::class, $annotation_forms_off_canvas_class, new AccessResultNeutral()], + 'block plugin without forms, forms[off_canvas] not set' => [TestBlockClassWithoutForms::class, $annotation_forms_off_canvas_not_set, new AccessResultNeutral()], + 'block plugin without forms, forms[off_canvas] set to FALSE' => [TestBlockClassWithoutForms::class, $annotation_forms_off_canvas_false, new AccessResultNeutral()], + ]; } } -class TestBlockClass extends BlockBase { +class TestBlockClassWithForms extends BlockBase { public function build() { return []; } } + +class TestBlockClassWithoutForms implements BlockPluginInterface { + use UnchangingCacheableDependencyTrait; + public function label() {} + public function access(AccountInterface $account, $return_as_object = FALSE) {} + public function build() {} + public function setConfigurationValue($key, $value) {} + public function blockForm($form, FormStateInterface $form_state) {} + public function blockValidate($form, FormStateInterface $form_state) {} + public function blockSubmit($form, FormStateInterface $form_state) {} + public function getMachineNameSuggestion() {} + public function getConfiguration() {} + public function setConfiguration(array $configuration) {} + public function defaultConfiguration() {} + public function buildConfigurationForm(array $form, FormStateInterface $form_state) {} + public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {} + public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {} + public function getPluginId() {} + public function getPluginDefinition() {} + public function getBaseId() {} + public function getDerivativeId() {} + public function calculateDependencies() {} +}