You are here

function BlockTest::testBlockVisibility 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::testBlockVisibility()

Tests block visibility.

File

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

Class

BlockTest
Tests basic block functionality.

Namespace

Drupal\block\Tests

Code

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 = array(
    'id' => strtolower($this
      ->randomMachineName(8)),
    'region' => 'sidebar_first',
    'settings[label]' => $title,
  );

  // 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
    ->assertFieldChecked('edit-visibility-request-path-negate-0');
  $this
    ->drupalPostForm(NULL, $edit, t('Save block'));
  $this
    ->assertText('The block configuration has been saved.', 'Block was saved');
  $this
    ->clickLink('Configure');
  $this
    ->assertFieldChecked('edit-visibility-request-path-negate-1');
  $this
    ->drupalGet('');
  $this
    ->assertText($title, 'Block was displayed on the front page.');
  $this
    ->drupalGet('user');
  $this
    ->assertNoText($title, 'Block was not displayed according to block visibility rules.');

  // Confirm that the block is not displayed to anonymous users.
  $this
    ->drupalLogout();
  $this
    ->drupalGet('');
  $this
    ->assertNoText($title, 'Block was not displayed to anonymous users.');

  // Confirm that an empty block is not displayed.
  $this
    ->assertNoText('Powered by Drupal', 'Empty block not displayed.');
  $this
    ->assertNoRaw('sidebar-first', 'Empty sidebar-first region is not displayed.');
}