You are here

public function BlockTest::testBlockToggleVisibility in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/block/src/Tests/BlockTest.php \Drupal\block\Tests\BlockTest::testBlockToggleVisibility()

Tests that visibility can be properly toggled.

File

core/modules/block/src/Tests/BlockTest.php, line 69
Contains \Drupal\block\Tests\BlockTest.

Class

BlockTest
Tests basic block functionality.

Namespace

Drupal\block\Tests

Code

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 = array(
    '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
    ->drupalPostForm('admin/structure/block/add/' . $block_name . '/' . $default_theme, $edit, t('Save block'));
  $this
    ->clickLink('Configure');
  $this
    ->assertFieldChecked('edit-visibility-user-role-roles-authenticated');
  $edit = [
    'visibility[user_role][roles][' . RoleInterface::AUTHENTICATED_ID . ']' => FALSE,
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'Save block');
  $this
    ->clickLink('Configure');
  $this
    ->assertNoFieldChecked('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
    ->assertIdentical([], $visibility_config);
  $this
    ->assertIdentical([], $block
    ->get('visibility'));
}