View source
<?php
namespace Drupal\Tests\block\Functional;
use Drupal\Component\Utility\Html;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl;
use Drupal\Tests\BrowserTestBase;
class BlockUiTest extends BrowserTestBase {
protected static $modules = [
'block',
'block_test',
'help',
'condition_test',
];
protected $defaultTheme = 'classy';
protected $regions;
protected $blockValues;
protected $blocks;
protected $adminUser;
protected function setUp() : void {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser([
'administer blocks',
'access administration pages',
]);
$this
->drupalLogin($this->adminUser);
$this->blockValues = [
[
'label' => 'Tools',
'tr' => '5',
'plugin_id' => 'system_menu_block:tools',
'settings' => [
'region' => 'sidebar_second',
'id' => 'tools',
],
'test_weight' => '-1',
],
[
'label' => 'Powered by Drupal',
'tr' => '16',
'plugin_id' => 'system_powered_by_block',
'settings' => [
'region' => 'footer',
'id' => 'powered',
],
'test_weight' => '0',
],
];
$this->blocks = [];
foreach ($this->blockValues as $values) {
$this->blocks[] = $this
->drupalPlaceBlock($values['plugin_id'], $values['settings']);
}
}
public function testBlockDemoUiPage() {
$this
->drupalPlaceBlock('help_block', [
'region' => 'help',
]);
$this
->drupalGet('admin/structure/block');
$this
->clickLink('Demonstrate block regions (Classy)');
$this
->assertSession()
->elementExists('xpath', '//div[contains(@class, "region-highlighted")]/div[contains(@class, "block-region") and contains(text(), "Highlighted")]');
\Drupal::service('theme_installer')
->install([
'test_theme',
]);
$this
->drupalGet('admin/structure/block/demo/test_theme');
$this
->assertSession()
->assertEscaped('<strong>Test theme</strong>');
\Drupal::service('theme_installer')
->install([
'stable',
]);
$this
->drupalGet('admin/structure/block/demo/stable');
$this
->assertSession()
->statusCodeEquals(404);
}
public function testBlockAdminUiPage() {
$this
->drupalGet('admin/structure/block');
$blocks_table = $this
->xpath("//table[@id='blocks']");
$this
->assertTrue(!empty($blocks_table), 'The blocks table is being rendered.');
foreach ($this->blockValues as $delta => $values) {
$block = $this->blocks[$delta];
$label = $block
->label();
$element = $this
->xpath('//*[@id="blocks"]/tbody/tr[' . $values['tr'] . ']/td[1]/text()');
$this
->assertEquals($element[0]
->getText(), $label, 'The "' . $label . '" block title is set inside the ' . $values['settings']['region'] . ' region.');
$this
->assertSession()
->fieldExists('blocks[' . $values['settings']['id'] . '][region]');
$edit['blocks[' . $values['settings']['id'] . '][region]'] = 'header';
$this
->assertSession()
->fieldExists('blocks[' . $values['settings']['id'] . '][weight]');
$edit['blocks[' . $values['settings']['id'] . '][weight]'] = $values['test_weight'];
}
$this
->drupalGet('admin/structure/block');
$this
->submitForm($edit, 'Save blocks');
foreach ($this->blockValues as $values) {
$this
->assertTrue($this
->assertSession()
->optionExists('edit-blocks-' . $values['settings']['id'] . '-region', 'header')
->isSelected());
$this
->assertTrue($this
->assertSession()
->optionExists('edit-blocks-' . $values['settings']['id'] . '-weight', $values['test_weight'])
->isSelected());
}
$this
->drupalPlaceBlock('system_powered_by_block', [
'region' => 'header',
'id' => 'header',
]);
$this
->drupalGet('admin/structure/block');
$element = $this
->xpath('//tr[contains(@class, :class)]', [
':class' => 'region-title-header',
]);
$this
->assertTrue(!empty($element));
$this
->assertTrue(\Drupal::service('theme_handler')
->themeExists('classy'), 'The classy base theme is enabled');
$this
->drupalPlaceBlock('local_tasks_block', [
'region' => 'header',
]);
\Drupal::service('theme_installer')
->install([
'stable',
'stark',
]);
$this
->drupalGet('admin/structure/block');
$theme_handler = \Drupal::service('theme_handler');
$this
->assertSession()
->linkExists($theme_handler
->getName('classy'));
$this
->assertSession()
->linkExists($theme_handler
->getName('stark'));
$this
->assertSession()
->linkNotExists($theme_handler
->getName('stable'));
$this
->drupalGet('admin/structure/block/list/stable');
$this
->assertSession()
->statusCodeEquals(404);
\Drupal::configFactory()
->getEditable('system.theme')
->set('admin', 'stable')
->save();
\Drupal::service('router.builder')
->rebuildIfNeeded();
$this
->drupalPlaceBlock('local_tasks_block', [
'region' => 'header',
'theme' => 'stable',
]);
$this
->drupalGet('admin/structure/block');
$this
->assertSession()
->linkExists($theme_handler
->getName('stable'));
$this
->drupalGet('admin/structure/block/list/stable');
$this
->assertSession()
->statusCodeEquals(200);
}
public function testCandidateBlockList() {
$arguments = [
':title' => 'Display message',
':category' => 'Block test',
':href' => 'admin/structure/block/add/test_block_instantiation/classy',
];
$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 test block appears in the category for its module.');
$this->container
->get('state')
->set('block_test_info_alter', TRUE);
$this->container
->get('plugin.manager.block')
->clearCachedDefinitions();
$this
->drupalGet('admin/structure/block');
$this
->clickLink('Place block');
$arguments[':category'] = 'Custom category';
$elements = $this
->xpath($pattern, $arguments);
$this
->assertTrue(!empty($elements), 'The test block appears in a custom category controlled by block_test_block_alter().');
}
public function testContextAwareUnsatisfiedBlocks() {
$this
->drupalGet('admin/structure/block');
$this
->clickLink('Place block');
$this
->assertSession()
->elementNotExists('xpath', '//tr[.//td/div[text()="Test context-aware unsatisfied block"] and .//td[text()="Block test"] and .//td//a[contains(@href, "admin/structure/block/add/test_context_aware_unsatisfied/classy")]]');
$definition = \Drupal::service('plugin.manager.block')
->getDefinition('test_context_aware_unsatisfied');
$this
->assertTrue(!empty($definition), 'The context-aware test block does not exist.');
}
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);
$this
->drupalGet('admin/structure/block/add/test_context_aware_no_valid_context_options/classy');
$this
->assertSession()
->fieldNotExists('edit-settings-context-mapping-email');
$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.');
$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]"]');
}
public function testMachineNameSuggestion() {
$url = 'admin/structure/block/add/test_block_instantiation/classy';
$this
->drupalGet($url);
$this
->assertSession()
->fieldValueEquals('id', 'displaymessage');
$edit = [
'region' => 'content',
];
$this
->drupalGet($url);
$this
->submitForm($edit, 'Save block');
$this
->assertSession()
->pageTextContains('The block configuration has been saved.');
$this
->drupalGet($url);
$this
->assertSession()
->fieldValueEquals('id', 'displaymessage_2');
$this
->drupalGet($url);
$this
->submitForm($edit, 'Save block');
$this
->assertSession()
->pageTextContains('The block configuration has been saved.');
$this
->drupalGet($url);
$this
->assertSession()
->fieldValueEquals('id', 'displaymessage_3');
}
public function testBlockPlacementIndicator() {
\Drupal::service('module_installer')
->install([
'language',
'locale',
]);
$this->container = \Drupal::getContainer();
ConfigurableLanguage::createFromLangcode('it')
->save();
$config = $this
->config('language.types');
$config
->set('negotiation.language_interface.enabled', [
LanguageNegotiationUrl::METHOD_ID => -10,
]);
$config
->save();
$config = $this
->config('language.negotiation');
$config
->set('url.source', LanguageNegotiationUrl::CONFIG_DOMAIN);
$config
->set('url.domains', [
'en' => \Drupal::request()
->getHost(),
'it' => 'it.example.com',
]);
$config
->save();
$block = [];
$block['id'] = strtolower($this
->randomMachineName());
$block['theme'] = 'classy';
$block['region'] = 'content';
$this
->drupalGet('admin/structure/block/add/system_powered_by_block');
$this
->submitForm($block, 'Save block');
$this
->assertSession()
->addressEquals('admin/structure/block/list/classy?block-placement=' . Html::getClass($block['id']));
$this
->submitForm([], 'Save blocks');
$this
->assertSession()
->addressEquals('admin/structure/block/list/classy');
$this
->clickLink('Place block');
$this
->clickLink('Place block');
$this
->submitForm([], 'Save block');
$this
->assertSession()
->addressEquals('admin/structure/block/list/classy?block-placement=scriptalertxsssubjectscript');
$this
->clickLink('Remove');
$this
->submitForm([], 'Remove');
$this
->assertSession()
->addressEquals('admin/structure/block');
}
public function testBlockValidateErrors() {
$this
->drupalGet('admin/structure/block/add/test_settings_validation/classy');
$this
->submitForm([
'region' => 'content',
'settings[digits]' => 'abc',
], 'Save block');
$arguments = [
':message' => 'Only digits are allowed',
];
$pattern = '//div[contains(@class,"messages messages--error")]/div[contains(text()[2],:message)]';
$elements = $this
->xpath($pattern, $arguments);
$this
->assertNotEmpty($elements, 'Plugin error message found in parent form.');
$error_class_pattern = '//div[contains(@class,"form-item-settings-digits")]/input[contains(@class,"error")]';
$error_class = $this
->xpath($error_class_pattern);
$this
->assertNotEmpty($error_class, 'Plugin error class found in parent form.');
}
public function testRouteProtection() {
$block = reset($this->blocks);
$this
->drupalGet('admin/structure/block/manage/' . $block
->id() . '/disable');
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalGet('admin/structure/block/manage/' . $block
->id() . '/enable');
$this
->assertSession()
->statusCodeEquals(403);
}
public function testBrokenBlockVisibility() {
$assert_session = $this
->assertSession();
$block = $this
->drupalPlaceBlock('broken');
$this
->drupalGet('admin/structure/block/manage/' . $block
->id());
$assert_session
->statusCodeEquals(200);
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('');
$assert_session
->statusCodeEquals(200);
$assert_session
->pageTextContains('This block is broken or missing. You may be missing content or you might need to enable the original module.');
$this
->drupalLogout();
$this
->drupalGet('');
$assert_session
->statusCodeEquals(200);
$assert_session
->pageTextNotContains('This block is broken or missing. You may be missing content or you might need to enable the original module.');
$this
->drupalLogin($this
->drupalCreateUser([
'access administration pages',
]));
$this
->drupalGet('');
$assert_session
->statusCodeEquals(200);
$assert_session
->pageTextNotContains('This block is broken or missing. You may be missing content or you might need to enable the original module.');
}
}