function BlockUiTest::testContextAwareBlocks

Same name in other branches
  1. 9 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()
  3. 11.x 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 223

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->assertNoText('Test context-aware block');
    $this->assertNoRaw($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->drupalPostForm($block_url, $edit, 'Save block');
    $this->drupalGet('');
    $this->assertText('Test context-aware block');
    $this->assertText('User context found.');
    $this->assertRaw($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->drupalPostForm(NULL, $edit, 'Save block');
    $this->drupalGet('');
    $this->assertText('No context mapping selected.');
    $this->assertNoText('User context found.');
    // Tests that conditions with missing context are not displayed.
    $this->drupalGet('admin/structure/block/manage/testcontextawareblock');
    $this->assertNoRaw('No existing type');
    $this->assertNoFieldByXPath('//*[@name="visibility[condition_test_no_existing_type][negate]"]');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.