3 namespace Drupal\Tests\settings_tray\Unit\Access;
5 use Drupal\block\BlockInterface;
6 use Drupal\Core\Access\AccessResultAllowed;
7 use Drupal\Core\Access\AccessResultInterface;
8 use Drupal\Core\Access\AccessResultNeutral;
9 use Drupal\Core\Block\BlockPluginInterface;
10 use Drupal\Core\Plugin\PluginWithFormsInterface;
11 use Drupal\settings_tray\Access\BlockPluginHasSettingsTrayFormAccessCheck;
12 use Drupal\Tests\UnitTestCase;
13 use Prophecy\Argument;
16 * @coversDefaultClass \Drupal\settings_tray\Access\BlockPluginHasSettingsTrayFormAccessCheck
17 * @group settings_tray
19 class BlockPluginHasSettingsTrayFormAccessCheckTest extends UnitTestCase {
23 * @covers ::accessBlockPlugin
24 * @dataProvider providerTestAccess
26 public function testAccess($with_forms, array $plugin_definition, AccessResultInterface $expected_access_result) {
27 $block_plugin = $this->prophesize()->willImplement(BlockPluginInterface::class);
30 $block_plugin->willImplement(PluginWithFormsInterface::class);
31 $block_plugin->hasFormClass(Argument::type('string'))->will(function ($arguments) use ($plugin_definition) {
32 return !empty($plugin_definition['forms'][$arguments[0]]);
36 $block = $this->prophesize(BlockInterface::class);
37 $block->getPlugin()->willReturn($block_plugin->reveal());
39 $access_check = new BlockPluginHasSettingsTrayFormAccessCheck();
40 $this->assertEquals($expected_access_result, $access_check->access($block->reveal()));
41 $this->assertEquals($expected_access_result, $access_check->accessBlockPlugin($block_plugin->reveal()));
45 * Provides test data for ::testAccess().
47 public function providerTestAccess() {
48 $annotation_forms_settings_tray_class = [
50 'settings_tray' => $this->randomMachineName(),
53 $annotation_forms_settings_tray_not_set = [];
54 $annotation_forms_settings_tray_false = [
56 'settings_tray' => FALSE,
60 'block plugin with forms, forms[settings_tray] set to class' => [
62 $annotation_forms_settings_tray_class,
63 new AccessResultAllowed(),
65 'block plugin with forms, forms[settings_tray] not set' => [
67 $annotation_forms_settings_tray_not_set,
68 new AccessResultNeutral(),
70 'block plugin with forms, forms[settings_tray] set to FALSE' => [
72 $annotation_forms_settings_tray_false,
73 new AccessResultNeutral(),
75 // In practice, all block plugins extend BlockBase, which means they all
76 // implement PluginWithFormsInterface, but this may change in the future.
77 // This ensures Settings Tray will continue to work correctly.
78 'block plugin without forms, forms[settings_tray] set to class' => [
80 $annotation_forms_settings_tray_class,
81 new AccessResultNeutral(),
83 'block plugin without forms, forms[settings_tray] not set' => [
85 $annotation_forms_settings_tray_not_set,
86 new AccessResultNeutral(),
88 'block plugin without forms, forms[settings_tray] set to FALSE' => [
90 $annotation_forms_settings_tray_false,
91 new AccessResultNeutral(),