function BlockTest::testBlockToggleVisibility

Same name in other branches
  1. 9 core/modules/block/tests/src/Functional/BlockTest.php \Drupal\Tests\block\Functional\BlockTest::testBlockToggleVisibility()
  2. 8.9.x core/modules/block/tests/src/Functional/BlockTest.php \Drupal\Tests\block\Functional\BlockTest::testBlockToggleVisibility()
  3. 11.x core/modules/block/tests/src/Functional/BlockTest.php \Drupal\Tests\block\Functional\BlockTest::testBlockToggleVisibility()

Tests that visibility can be properly toggled.

File

core/modules/block/tests/src/Functional/BlockTest.php, line 89

Class

BlockTest
Tests basic block functionality.

Namespace

Drupal\Tests\block\Functional

Code

public function testBlockToggleVisibility() : 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,
    ];
    $block_id = $edit['id'];
    // Set the block to be shown only to authenticated users.
    $edit['visibility[user_role][roles][' . RoleInterface::AUTHENTICATED_ID . ']'] = TRUE;
    $this->drupalGet('admin/structure/block/add/' . $block_name . '/' . $default_theme);
    $this->submitForm($edit, 'Save block');
    $this->clickLink('Configure');
    $this->assertSession()
        ->checkboxChecked('edit-visibility-user-role-roles-authenticated');
    $edit = [
        'visibility[user_role][roles][' . RoleInterface::AUTHENTICATED_ID . ']' => FALSE,
    ];
    $this->submitForm($edit, 'Save block');
    $this->clickLink('Configure');
    $this->assertSession()
        ->checkboxNotChecked('edit-visibility-user-role-roles-authenticated');
    // Ensure that no visibility is configured.
    
    /** @var \Drupal\block\BlockInterface $block */
    $block = Block::load($block_id);
    $visibility_config = $block->getVisibilityConditions()
        ->getConfiguration();
    $this->assertSame([], $visibility_config);
    $this->assertSame([], $block->get('visibility'));
}

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