Skip to content
Snippets Groups Projects
Verified Commit 7025180d authored by Dave Long's avatar Dave Long
Browse files

Issue #3414800 by plopesc, smustgrave: Access check in AnnounceBlock does not...

Issue #3414800 by plopesc, smustgrave: Access check in AnnounceBlock does not take into account $return_as_object parameter
parent be90b320
No related branches found
No related tags found
No related merge requests found
......@@ -6,6 +6,7 @@
use Drupal\announcements_feed\AnnounceRenderer;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Access\AccessResultInterface;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
......@@ -34,37 +35,34 @@ class AnnounceBlock extends BlockBase implements ContainerFactoryPluginInterface
* The plugin implementation definition.
* @param \Drupal\announcements_feed\AnnounceRenderer $announceRenderer
* The AnnounceRenderer service.
* @param \Drupal\Core\Session\AccountInterface $currentUser
* The current user.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, protected AnnounceRenderer $announceRenderer, protected AccountInterface $currentUser) {
public function __construct(array $configuration, $plugin_id, $plugin_definition, protected AnnounceRenderer $announceRenderer) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): static {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('announcements_feed.renderer'),
$container->get('current_user')
$container->get('announcements_feed.renderer')
);
}
/**
* {@inheritdoc}
*/
public function access(AccountInterface $account, $return_as_object = FALSE) {
return AccessResult::allowedIfHasPermission($this->currentUser, 'access announcements');
public function blockAccess(AccountInterface $account): AccessResultInterface {
return AccessResult::allowedIfHasPermission($account, 'access announcements');
}
/**
* {@inheritdoc}
*/
public function build() {
public function build(): array {
return $this->announceRenderer->render();
}
......
......@@ -5,6 +5,10 @@
namespace Drupal\Tests\announcements_feed\FunctionalJavascript;
use Drupal\announce_feed_test\AnnounceTestHttpClientMiddleware;
use Drupal\block\BlockInterface;
use Drupal\Core\Access\AccessResultAllowed;
use Drupal\Core\Access\AccessResultNeutral;
use Drupal\Core\Session\AnonymousUserSession;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
/**
......@@ -30,9 +34,9 @@ class AnnounceBlockTest extends WebDriverTestBase {
/**
* The announce block instance.
*
* @var \Drupal\block\Entity\Block
* @var \Drupal\block\BlockInterface
*/
protected $announceBlock;
protected BlockInterface $announceBlock;
/**
* {@inheritdoc}
......@@ -48,11 +52,13 @@ protected function setUp(): void {
/**
* Testing announce feed block visibility.
*/
public function testAnnounceWithoutPermission() {
// User with "access announcements" permission.
public function testAnnounceWithoutPermission(): void {
// User with "access announcements" permission and anonymous session.
$account = $this->drupalCreateUser([
'access announcements',
]);
$anonymous_account = new AnonymousUserSession();
$this->drupalLogin($account);
$this->drupalGet('<front>');
......@@ -65,6 +71,12 @@ public function testAnnounceWithoutPermission() {
$this->drupalLogout();
$assert_session->pageTextNotContains('Announcements Feed');
// Test access() method return type.
$this->assertTrue($this->announceBlock->getPlugin()->access($account));
$this->assertInstanceOf(AccessResultAllowed::class, $this->announceBlock->getPlugin()->access($account, TRUE));
$this->assertFalse($this->announceBlock->getPlugin()->access($anonymous_account));
$this->assertInstanceOf(AccessResultNeutral::class, $this->announceBlock->getPlugin()->access($anonymous_account, TRUE));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment