function BlockTest::testBlockVisibility

Same name in other branches
  1. 9 core/modules/block/tests/src/Functional/BlockTest.php \Drupal\Tests\block\Functional\BlockTest::testBlockVisibility()
  2. 8.9.x core/modules/block/tests/src/Functional/BlockTest.php \Drupal\Tests\block\Functional\BlockTest::testBlockVisibility()
  3. 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 28

Class

BlockTest
Tests basic block functionality.

Namespace

Drupal\Tests\block\Functional

Code

public function testBlockVisibility() : void {
    $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' => $this->randomMachineName(8),
        'region' => 'sidebar_first',
        'settings[label]' => $title,
        'settings[label_display]' => TRUE,
    ];
    // Set the block to be hidden on any user path, to be shown only to
    // authenticated users, and to be shown only on 200 and 404 responses.
    $edit['visibility[request_path][pages]'] = '/user*';
    $edit['visibility[request_path][negate]'] = TRUE;
    $edit['visibility[user_role][roles][' . RoleInterface::AUTHENTICATED_ID . ']'] = TRUE;
    $edit['visibility[response_status][status_codes][200]'] = 200;
    $edit['visibility[response_status][status_codes][404]'] = 404;
    $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()
        ->statusMessageContains('The block configuration has been saved.', 'status');
    $this->clickLink('Configure');
    $this->assertSession()
        ->checkboxChecked('edit-visibility-request-path-negate-1');
    $this->assertSession()
        ->checkboxChecked('edit-visibility-response-status-status-codes-200');
    $this->assertSession()
        ->checkboxChecked('edit-visibility-response-status-status-codes-404');
    // Confirm that the block is displayed on the front page (200 response).
    $this->drupalGet('');
    $this->assertSession()
        ->pageTextContains($title);
    // Confirm that the block is not displayed according to path visibility
    // rules.
    $this->drupalGet('user');
    $this->assertSession()
        ->pageTextNotContains($title);
    // Confirm that the block is displayed on a 404 response.
    $this->drupalGet('/0/null');
    $this->assertSession()
        ->pageTextContains($title);
    // Confirm that the block is not displayed on a 403 response.
    $this->drupalGet('/admin/config/system/cron');
    $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');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.