View source
<?php
namespace Drupal\Tests\block_permissions\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Url;
class BlockFormRoutesAccessTest extends BlockPermissionsBrowserTestBase {
protected $coreBlocksUser;
protected $systemBlocksUser;
protected $secondThemeUser;
protected function setUp() : void {
parent::setUp();
$this->coreBlocksUser = $this
->drupalCreateUser([
'administer blocks',
"administer block settings for theme {$this->defaultTheme}",
'administer blocks provided by core',
]);
$this->systemBlocksUser = $this
->drupalCreateUser([
'administer blocks',
"administer block settings for theme {$this->defaultTheme}",
'administer blocks provided by system',
]);
$this->secondThemeUser = $this
->drupalCreateUser([
'administer blocks',
"administer block settings for theme {$this->secondTheme}",
'administer blocks provided by core',
'administer blocks provided by system',
]);
}
public function testBlockAddFormAccess() {
$this
->drupalLogin($this->coreBlocksUser);
$this
->drupalGet($this
->getBlockAdminAddUrl('page_title_block', $this->defaultTheme));
$this
->assertBlockFormPageHasAccess();
$this
->drupalGet($this
->getBlockAdminAddUrl('system_branding_block', $this->defaultTheme));
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalLogin($this->systemBlocksUser);
$this
->drupalGet($this
->getBlockAdminAddUrl('page_title_block', $this->defaultTheme));
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalGet($this
->getBlockAdminAddUrl('system_branding_block', $this->defaultTheme));
$this
->assertBlockFormPageHasAccess();
$this
->drupalLogin($this->secondThemeUser);
$this
->drupalGet($this
->getBlockAdminAddUrl('page_title_block', $this->secondTheme));
$this
->assertBlockFormPageHasAccess();
$this
->drupalGet($this
->getBlockAdminAddUrl('system_branding_block', $this->secondTheme));
$this
->assertBlockFormPageHasAccess();
$this
->drupalGet($this
->getBlockAdminAddUrl('page_title_block', $this->defaultTheme));
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalGet($this
->getBlockAdminAddUrl('system_branding_block', $this->defaultTheme));
$this
->assertSession()
->statusCodeEquals(403);
}
public function testBlockEditFormAccess() {
$page_title_block_edit = $this
->getBlockEditFormUrl($this->pageTitleBlock
->id());
$system_branding_block_edit = $this
->getBlockEditFormUrl($this->systemBrandingBlock
->id());
$this
->drupalLogin($this->coreBlocksUser);
$this
->drupalGet($page_title_block_edit);
$this
->assertBlockFormPageHasAccess();
$this
->drupalGet($system_branding_block_edit);
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalLogin($this->systemBlocksUser);
$this
->drupalGet($page_title_block_edit);
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalGet($system_branding_block_edit);
$this
->assertBlockFormPageHasAccess();
}
public function testBlockDeleteFormAccess() {
$page_title_block_delete = $this
->getBlockDeleteFormUrl($this->pageTitleBlock
->id());
$system_branding_block_delete = $this
->getBlockDeleteFormUrl($this->systemBrandingBlock
->id());
$this
->drupalLogin($this->coreBlocksUser);
$this
->drupalGet($page_title_block_delete);
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains($this
->getBlockDeleteFormTitle($this->pageTitleBlock
->label()));
$this
->drupalGet($system_branding_block_delete);
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalLogin($this->systemBlocksUser);
$this
->drupalGet($page_title_block_delete);
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalGet($system_branding_block_delete);
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains($this
->getBlockDeleteFormTitle($this->systemBrandingBlock
->label()));
}
protected function assertBlockFormPageHasAccess() {
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains('Configure block');
}
protected function getBlockEditFormUrl($id) {
return Url::fromRoute('entity.block.edit_form', [
'block' => $id,
]);
}
protected function getBlockDeleteFormUrl($id) {
return Url::fromRoute('entity.block.delete_form', [
'block' => $id,
]);
}
protected function getBlockAdminAddUrl($id, $theme) {
return Url::fromRoute('block.admin_add', [
'plugin_id' => $id,
'theme' => $theme,
]);
}
protected function getBlockDeleteFormTitle($name) {
return new FormattableMarkup('Are you sure you want to remove the block @name?', [
'@name' => $name,
]);
}
}