Loading core/modules/block/tests/modules/block_test/src/Plugin/Block/TestAccessBlock.php +6 −13 Original line number Diff line number Diff line Loading @@ -8,9 +8,9 @@ use Drupal\Core\Block\Attribute\Block; use Drupal\Core\Block\BlockBase; use Drupal\Core\Cache\Cache; use Drupal\Core\KeyValueStore\KeyValueStoreInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\State\StateInterface; use Drupal\Core\StringTranslation\TranslatableMarkup; use Symfony\Component\DependencyInjection\ContainerInterface; Loading @@ -23,11 +23,6 @@ )] class TestAccessBlock extends BlockBase implements ContainerFactoryPluginInterface { /** * The state service. */ protected StateInterface $state; /** * Tests the test access block. * Loading @@ -41,13 +36,11 @@ class TestAccessBlock extends BlockBase implements ContainerFactoryPluginInterfa * The plugin ID for the plugin instance. * @param mixed $plugin_definition * The plugin implementation definition. * @param \Drupal\Core\State\StateInterface $state * The state. * @param \Drupal\Core\KeyValueStore\KeyValueStoreInterface $keyValue * The key value store. */ public function __construct(array $configuration, $plugin_id, $plugin_definition, StateInterface $state) { public function __construct(array $configuration, $plugin_id, $plugin_definition, protected KeyValueStoreInterface $keyValue) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->state = $state; } /** Loading @@ -58,7 +51,7 @@ public static function create(ContainerInterface $container, array $configuratio $configuration, $plugin_id, $plugin_definition, $container->get('state') $container->get('keyvalue')->get('block_test') ); } Loading @@ -66,7 +59,7 @@ public static function create(ContainerInterface $container, array $configuratio * {@inheritdoc} */ protected function blockAccess(AccountInterface $account) { return $this->state->get('test_block_access', FALSE) ? AccessResult::allowed()->setCacheMaxAge(0) : AccessResult::forbidden()->setCacheMaxAge(0); return $this->keyValue->get('access', FALSE) ? AccessResult::allowed()->setCacheMaxAge(0) : AccessResult::forbidden()->setCacheMaxAge(0); } /** Loading core/modules/block/tests/src/Functional/BlockTest.php +1 −1 Original line number Diff line number Diff line Loading @@ -576,7 +576,7 @@ public function testBlockAccess(): void { $this->drupalGet('<front>'); $this->assertSession()->pageTextNotContains('Hello test world'); \Drupal::state()->set('test_block_access', TRUE); \Drupal::keyValue('block_test')->set('access', TRUE); $this->drupalGet('<front>'); $this->assertSession()->pageTextContains('Hello test world'); } Loading core/modules/layout_builder/tests/src/Functional/LayoutSectionTest.php +2 −2 Original line number Diff line number Diff line Loading @@ -197,7 +197,7 @@ public function testLayoutSectionFormatterAccess(): void { ]); // Restrict access to the block. $this->container->get('state')->set('test_block_access', FALSE); $this->container->get('keyvalue')->get('block_test')->set('access', FALSE); $this->drupalGet($node->toUrl('canonical')); $this->assertLayoutSection('.layout--onecol', NULL, '', '', 'UNCACHEABLE (poor cacheability)'); Loading @@ -205,7 +205,7 @@ public function testLayoutSectionFormatterAccess(): void { $this->assertSession()->pageTextNotContains('Hello test world'); // Grant access to the block, and ensure it was rendered. $this->container->get('state')->set('test_block_access', TRUE); $this->container->get('keyvalue')->get('block_test')->set('access', TRUE); $this->drupalGet($node->toUrl('canonical')); $this->assertLayoutSection('.layout--onecol', 'Hello test world', '', '', 'UNCACHEABLE (poor cacheability)'); } Loading Loading
core/modules/block/tests/modules/block_test/src/Plugin/Block/TestAccessBlock.php +6 −13 Original line number Diff line number Diff line Loading @@ -8,9 +8,9 @@ use Drupal\Core\Block\Attribute\Block; use Drupal\Core\Block\BlockBase; use Drupal\Core\Cache\Cache; use Drupal\Core\KeyValueStore\KeyValueStoreInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\State\StateInterface; use Drupal\Core\StringTranslation\TranslatableMarkup; use Symfony\Component\DependencyInjection\ContainerInterface; Loading @@ -23,11 +23,6 @@ )] class TestAccessBlock extends BlockBase implements ContainerFactoryPluginInterface { /** * The state service. */ protected StateInterface $state; /** * Tests the test access block. * Loading @@ -41,13 +36,11 @@ class TestAccessBlock extends BlockBase implements ContainerFactoryPluginInterfa * The plugin ID for the plugin instance. * @param mixed $plugin_definition * The plugin implementation definition. * @param \Drupal\Core\State\StateInterface $state * The state. * @param \Drupal\Core\KeyValueStore\KeyValueStoreInterface $keyValue * The key value store. */ public function __construct(array $configuration, $plugin_id, $plugin_definition, StateInterface $state) { public function __construct(array $configuration, $plugin_id, $plugin_definition, protected KeyValueStoreInterface $keyValue) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->state = $state; } /** Loading @@ -58,7 +51,7 @@ public static function create(ContainerInterface $container, array $configuratio $configuration, $plugin_id, $plugin_definition, $container->get('state') $container->get('keyvalue')->get('block_test') ); } Loading @@ -66,7 +59,7 @@ public static function create(ContainerInterface $container, array $configuratio * {@inheritdoc} */ protected function blockAccess(AccountInterface $account) { return $this->state->get('test_block_access', FALSE) ? AccessResult::allowed()->setCacheMaxAge(0) : AccessResult::forbidden()->setCacheMaxAge(0); return $this->keyValue->get('access', FALSE) ? AccessResult::allowed()->setCacheMaxAge(0) : AccessResult::forbidden()->setCacheMaxAge(0); } /** Loading
core/modules/block/tests/src/Functional/BlockTest.php +1 −1 Original line number Diff line number Diff line Loading @@ -576,7 +576,7 @@ public function testBlockAccess(): void { $this->drupalGet('<front>'); $this->assertSession()->pageTextNotContains('Hello test world'); \Drupal::state()->set('test_block_access', TRUE); \Drupal::keyValue('block_test')->set('access', TRUE); $this->drupalGet('<front>'); $this->assertSession()->pageTextContains('Hello test world'); } Loading
core/modules/layout_builder/tests/src/Functional/LayoutSectionTest.php +2 −2 Original line number Diff line number Diff line Loading @@ -197,7 +197,7 @@ public function testLayoutSectionFormatterAccess(): void { ]); // Restrict access to the block. $this->container->get('state')->set('test_block_access', FALSE); $this->container->get('keyvalue')->get('block_test')->set('access', FALSE); $this->drupalGet($node->toUrl('canonical')); $this->assertLayoutSection('.layout--onecol', NULL, '', '', 'UNCACHEABLE (poor cacheability)'); Loading @@ -205,7 +205,7 @@ public function testLayoutSectionFormatterAccess(): void { $this->assertSession()->pageTextNotContains('Hello test world'); // Grant access to the block, and ensure it was rendered. $this->container->get('state')->set('test_block_access', TRUE); $this->container->get('keyvalue')->get('block_test')->set('access', TRUE); $this->drupalGet($node->toUrl('canonical')); $this->assertLayoutSection('.layout--onecol', 'Hello test world', '', '', 'UNCACHEABLE (poor cacheability)'); } Loading