You are here

public function BlockUiTest::testContextAwareBlocks in Drupal 9

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

Tests the behavior of context-aware blocks.

File

core/modules/block/tests/src/Functional/BlockUiTest.php, line 216

Class

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

Namespace

Drupal\Tests\block\Functional

Code

public function testContextAwareBlocks() {
  $expected_text = '<div id="test_context_aware--username">' . \Drupal::currentUser()
    ->getAccountName() . '</div>';
  $this
    ->drupalGet('');
  $this
    ->assertSession()
    ->pageTextNotContains('Test context-aware block');
  $this
    ->assertSession()
    ->responseNotContains($expected_text);
  $block_url = 'admin/structure/block/add/test_context_aware/classy';
  $arguments = [
    ':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
    ->clickLink('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:userB',
  ];
  $this
    ->drupalGet($block_url);
  $this
    ->submitForm($edit, 'Save block');
  $this
    ->drupalGet('');
  $this
    ->assertSession()
    ->pageTextContains('Test context-aware block');
  $this
    ->assertSession()
    ->pageTextContains('User context found.');
  $this
    ->assertSession()
    ->responseContains($expected_text);

  // Test context mapping form element is not visible if there are no valid
  // context options for the block (the test_context_aware_no_valid_context_options
  // block has one context defined which is not available for it on the
  // Block Layout interface).
  $this
    ->drupalGet('admin/structure/block/add/test_context_aware_no_valid_context_options/classy');
  $this
    ->assertSession()
    ->fieldNotExists('edit-settings-context-mapping-email');

  // Test context mapping allows empty selection for optional contexts.
  $this
    ->drupalGet('admin/structure/block/manage/testcontextawareblock');
  $edit = [
    'settings[context_mapping][user]' => '',
  ];
  $this
    ->submitForm($edit, 'Save block');
  $this
    ->drupalGet('');
  $this
    ->assertSession()
    ->pageTextContains('No context mapping selected.');
  $this
    ->assertSession()
    ->pageTextNotContains('User context found.');

  // Tests that conditions with missing context are not displayed.
  $this
    ->drupalGet('admin/structure/block/manage/testcontextawareblock');
  $this
    ->assertSession()
    ->responseNotContains('No existing type');
  $this
    ->assertSession()
    ->elementNotExists('xpath', '//*[@name="visibility[condition_test_no_existing_type][negate]"]');
}