You are here

public function VisibilityTest::testSingleConditions in Block Visibility Groups 8

File

src/Tests/VisibilityTest.php, line 45

Class

VisibilityTest
Tests the block_visibility_groups Visibility Settings.

Namespace

Drupal\block_visibility_groups\Tests

Code

public function testSingleConditions() {

  // @todo Condition with node doesn't work for some reason.
  $config = [
    'id' => 'node_type',
    'bundles' => [
      'page',
    ],
    'negate' => 0,
    'context_mapping' => [
      'node' => '@node.node_route_context:node',
    ],
  ];
  $configs['request'] = [
    'id' => 'request_path',
    'pages' => '/node/*',
    'negate' => 0,
  ];
  $group = $this
    ->createGroup($configs);
  $block_title = $this
    ->randomMachineName();
  $block_id = $this
    ->placeBlockInGroupUI('system_powered_by_block', $group
    ->id(), $block_title);
  $page_node = $this
    ->drupalCreateNode();
  $this
    ->drupalGet('node/' . $page_node
    ->id());
  $this
    ->assertText($block_title, 'Block shows up on page node when added via UI.');
  $this
    ->drupalGet('user');
  $this
    ->assertNoText($block_title, 'Block does not show up on user page when added via UI.');

  // Try updating, verify that the user get's redirected to the block layout
  // of the used visibility group.
  $this
    ->updateBlockInGroupUI($block_id, $group
    ->id());
  $default_theme = $this
    ->config('system.theme')
    ->get('default');
  $option = Url::fromRoute('block.admin_display_theme', [
    'theme' => $default_theme,
  ], [
    'query' => [
      'block_visibility_group' => $group
        ->id(),
    ],
  ])
    ->toString();
  $this
    ->assertOptionSelected('edit-select', $option, "User gets redirected to the selected group's block layout page.");
  $block = $this
    ->placeBlockInGroup('system_powered_by_block', $group
    ->id());
  $this
    ->drupalGet('node/' . $page_node
    ->id());
  $this
    ->assertText($block
    ->label(), 'Block shows up on page node.');
  $this
    ->drupalGet('user');
  $this
    ->assertNoText($block
    ->label(), 'Block does not show up on user page.');
  $this->container
    ->get('module_installer')
    ->uninstall([
    'block_visibility_groups',
  ]);

  // After uninstall conditions will not apply.
  $this
    ->drupalGet('node/' . $page_node
    ->id());
  $this
    ->assertText($block
    ->label(), 'Block shows up on page node.');
  $this
    ->drupalGet('user');
  $this
    ->assertText($block
    ->label(), 'Block shows up on user node.');
}