Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / settings_tray / tests / src / Unit / Access / BlockPluginHasSettingsTrayFormAccessCheckTest.php
1 <?php
2
3 namespace Drupal\Tests\settings_tray\Unit\Access;
4
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;
14
15 /**
16  * @coversDefaultClass \Drupal\settings_tray\Access\BlockPluginHasSettingsTrayFormAccessCheck
17  * @group settings_tray
18  */
19 class BlockPluginHasSettingsTrayFormAccessCheckTest extends UnitTestCase {
20
21   /**
22    * @covers ::access
23    * @covers ::accessBlockPlugin
24    * @dataProvider providerTestAccess
25    */
26   public function testAccess($with_forms, array $plugin_definition, AccessResultInterface $expected_access_result) {
27     $block_plugin = $this->prophesize()->willImplement(BlockPluginInterface::class);
28
29     if ($with_forms) {
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]]);
33       });
34     }
35
36     $block = $this->prophesize(BlockInterface::class);
37     $block->getPlugin()->willReturn($block_plugin->reveal());
38
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()));
42   }
43
44   /**
45    * Provides test data for ::testAccess().
46    */
47   public function providerTestAccess() {
48     $annotation_forms_settings_tray_class = [
49       'forms' => [
50         'settings_tray' => $this->randomMachineName(),
51       ],
52     ];
53     $annotation_forms_settings_tray_not_set = [];
54     $annotation_forms_settings_tray_false = [
55       'forms' => [
56         'settings_tray' => FALSE,
57       ],
58     ];
59     return [
60       'block plugin with forms, forms[settings_tray] set to class' => [
61         TRUE,
62         $annotation_forms_settings_tray_class,
63         new AccessResultAllowed(),
64       ],
65       'block plugin with forms, forms[settings_tray] not set' => [
66         TRUE,
67         $annotation_forms_settings_tray_not_set,
68         new AccessResultNeutral(),
69       ],
70       'block plugin with forms, forms[settings_tray] set to FALSE' => [
71         TRUE,
72         $annotation_forms_settings_tray_false,
73         new AccessResultNeutral(),
74       ],
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' => [
79         FALSE,
80         $annotation_forms_settings_tray_class,
81         new AccessResultNeutral(),
82       ],
83       'block plugin without forms, forms[settings_tray] not set' => [
84         FALSE,
85         $annotation_forms_settings_tray_not_set,
86         new AccessResultNeutral(),
87       ],
88       'block plugin without forms, forms[settings_tray] set to FALSE' => [
89         FALSE,
90         $annotation_forms_settings_tray_false,
91         new AccessResultNeutral(),
92       ],
93     ];
94   }
95
96 }