public function BlockTest::testBlockVisibility in Drupal 9
Same name and namespace in other branches
- 8 core/modules/block/tests/src/Functional/BlockTest.php \Drupal\Tests\block\Functional\BlockTest::testBlockVisibility()
- 10 core/modules/block/tests/src/Functional/BlockTest.php \Drupal\Tests\block\Functional\BlockTest::testBlockVisibility()
Tests block visibility.
File
- core/
modules/ block/ tests/ src/ Functional/ BlockTest.php, line 26
Class
- BlockTest
- Tests basic block functionality.
Namespace
Drupal\Tests\block\FunctionalCode
public function testBlockVisibility() {
$block_name = 'system_powered_by_block';
// Create a random title for the block.
$title = $this
->randomMachineName(8);
// Enable a standard block.
$default_theme = $this
->config('system.theme')
->get('default');
$edit = [
'id' => strtolower($this
->randomMachineName(8)),
'region' => 'sidebar_first',
'settings[label]' => $title,
'settings[label_display]' => TRUE,
];
// Set the block to be hidden on any user path, and to be shown only to
// authenticated users.
$edit['visibility[request_path][pages]'] = '/user*';
$edit['visibility[request_path][negate]'] = TRUE;
$edit['visibility[user_role][roles][' . RoleInterface::AUTHENTICATED_ID . ']'] = TRUE;
$this
->drupalGet('admin/structure/block/add/' . $block_name . '/' . $default_theme);
$this
->assertSession()
->checkboxChecked('edit-visibility-request-path-negate-0');
$this
->submitForm($edit, 'Save block');
$this
->assertSession()
->pageTextContains('The block configuration has been saved.');
$this
->clickLink('Configure');
$this
->assertSession()
->checkboxChecked('edit-visibility-request-path-negate-1');
// Confirm that the block is displayed on the front page.
$this
->drupalGet('');
$this
->assertSession()
->pageTextContains($title);
// Confirm that the block is not displayed according to block visibility
// rules.
$this
->drupalGet('user');
$this
->assertSession()
->pageTextNotContains($title);
// Confirm that the block is not displayed to anonymous users.
$this
->drupalLogout();
$this
->drupalGet('');
$this
->assertSession()
->pageTextNotContains($title);
// Confirm that an empty block is not displayed.
$this
->assertSession()
->pageTextNotContains('Powered by Drupal');
$this
->assertSession()
->responseNotContains('sidebar-first');
}