function BlockTest::testBlockVisibility

Test block visibility.

File

drupal/core/modules/block/lib/Drupal/block/Tests/BlockTest.php, line 28
Contains \Drupal\block\Tests\BlockTest.

Class

BlockTest
Provides testing for basic block module functionality.

Namespace

Drupal\block\Tests

Code

function testBlockVisibility() {
  $block_name = 'system_powered_by_block';

  // Create a random title for the block.
  $title = $this
    ->randomName(8);

  // Enable a standard block.
  $default_theme = config('system.theme')
    ->get('default');
  $edit = array(
    'machine_name' => strtolower($this
      ->randomName(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[path][pages]'] = 'user*';
  $edit['visibility[role][roles][' . DRUPAL_AUTHENTICATED_RID . ']'] = TRUE;
  $this
    ->drupalPost('admin/structure/block/add/' . $block_name . '/' . $default_theme, $edit, t('Save block'));
  $this
    ->assertText('The block configuration has been saved.', 'Block was saved');
  $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.');
  $this
    ->drupalGet('USER/' . $this->adminUser->uid);
  $this
    ->assertNoText($title, 'Block was not displayed according to block visibility rules regardless of path case.');

  // 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.');
}