You are here

public function BlockUiTest::testContextAwareBlocks in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/block/src/Tests/BlockUiTest.php \Drupal\block\Tests\BlockUiTest::testContextAwareBlocks()

Tests the behavior of context-aware blocks.

File

core/modules/block/src/Tests/BlockUiTest.php, line 216
Contains \Drupal\block\Tests\BlockUiTest.

Class

BlockUiTest
Tests that the block configuration UI exists and stores data correctly.

Namespace

Drupal\block\Tests

Code

public function testContextAwareBlocks() {
  $expected_text = '<div id="test_context_aware--username">' . \Drupal::currentUser()
    ->getUsername() . '</div>';
  $this
    ->drupalGet('');
  $this
    ->assertNoText('Test context-aware block');
  $this
    ->assertNoRaw($expected_text);
  $block_url = 'admin/structure/block/add/test_context_aware/classy';
  $arguments = array(
    ':title' => 'Test context-aware block',
    ':category' => 'Block test',
    ':href' => $block_url,
  );
  $pattern = '//tr[.//td/div[text()=:title] and .//td[text()=:category] and .//td//a[contains(@href, :href)]]';
  $this
    ->drupalGet('admin/structure/block');
  $this
    ->clickLinkPartialName('Place block');
  $elements = $this
    ->xpath($pattern, $arguments);
  $this
    ->assertTrue(!empty($elements), 'The context-aware test block appears.');
  $definition = \Drupal::service('plugin.manager.block')
    ->getDefinition('test_context_aware');
  $this
    ->assertTrue(!empty($definition), 'The context-aware test block exists.');
  $edit = [
    'region' => 'content',
    'settings[context_mapping][user]' => '@block_test.multiple_static_context:user2',
  ];
  $this
    ->drupalPostForm($block_url, $edit, 'Save block');
  $this
    ->drupalGet('');
  $this
    ->assertText('Test context-aware block');
  $this
    ->assertRaw($expected_text);

  // Test context mapping allows empty selection for optional contexts.
  $this
    ->drupalGet('admin/structure/block/manage/testcontextawareblock');
  $edit = [
    'settings[context_mapping][user]' => '',
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'Save block');
  $this
    ->drupalGet('');
  $this
    ->assertText('No context mapping selected.');
}