function BlockTest::testBlockToggleVisibility
Tests that visibility can be properly toggled.
File
- 
              core/modules/ block/ tests/ src/ Functional/ BlockTest.php, line 74 
Class
- BlockTest
- Tests basic block functionality.
Namespace
Drupal\Tests\block\FunctionalCode
public function testBlockToggleVisibility() {
  $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,
  ];
  $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.
