function BlockUiTest::testContextAwareBlocks

Same name and namespace in other branches
  1. 9 core/modules/block/tests/src/Functional/BlockUiTest.php \Drupal\Tests\block\Functional\BlockUiTest::testContextAwareBlocks()
  2. 8.9.x core/modules/block/tests/src/Functional/BlockUiTest.php \Drupal\Tests\block\Functional\BlockUiTest::testContextAwareBlocks()
  3. 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 225

Class

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

Namespace

Drupal\Tests\block\Functional

Code

public function testContextAwareBlocks() : void {
    $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/stark';
    $this->drupalGet('admin/structure/block');
    $this->clickLink('Place block');
    $this->assertSession()
        ->elementExists('xpath', '//tr[.//td/div[text()="Test context-aware block"] and .//td[text()="Block test"] and .//td//a[contains(@href, "' . $block_url . '")]]');
    $definition = \Drupal::service('plugin.manager.block')->getDefinition('test_context_aware');
    $this->assertNotEmpty($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/stark');
    $this->assertSession()
        ->fieldNotExists('edit-settings-context-mapping-email');
    // Test context mapping allows empty selection for optional contexts.
    $this->drupalGet('admin/structure/block/manage/stark_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/stark_testcontextawareblock');
    $this->assertSession()
        ->responseNotContains('No existing type');
    $this->assertSession()
        ->elementNotExists('xpath', '//*[@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.