annotate core/modules/block/tests/src/Functional/BlockUiTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\block\Functional;
Chris@0 4
Chris@0 5 use Drupal\Component\Utility\Html;
Chris@17 6 use Drupal\language\Entity\ConfigurableLanguage;
Chris@17 7 use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl;
Chris@0 8 use Drupal\Tests\BrowserTestBase;
Chris@0 9
Chris@0 10 /**
Chris@0 11 * Tests that the block configuration UI exists and stores data correctly.
Chris@0 12 *
Chris@0 13 * @group block
Chris@0 14 */
Chris@0 15 class BlockUiTest extends BrowserTestBase {
Chris@0 16
Chris@0 17 /**
Chris@0 18 * Modules to install.
Chris@0 19 *
Chris@0 20 * @var array
Chris@0 21 */
Chris@0 22 public static $modules = ['block', 'block_test', 'help', 'condition_test'];
Chris@0 23
Chris@0 24 protected $regions;
Chris@0 25
Chris@0 26 /**
Chris@0 27 * The submitted block values used by this test.
Chris@0 28 *
Chris@0 29 * @var array
Chris@0 30 */
Chris@0 31 protected $blockValues;
Chris@0 32
Chris@0 33 /**
Chris@0 34 * The block entities used by this test.
Chris@0 35 *
Chris@0 36 * @var \Drupal\block\BlockInterface[]
Chris@0 37 */
Chris@0 38 protected $blocks;
Chris@0 39
Chris@0 40 /**
Chris@0 41 * An administrative user to configure the test environment.
Chris@0 42 */
Chris@0 43 protected $adminUser;
Chris@0 44
Chris@0 45 protected function setUp() {
Chris@0 46 parent::setUp();
Chris@0 47 // Create and log in an administrative user.
Chris@0 48 $this->adminUser = $this->drupalCreateUser([
Chris@0 49 'administer blocks',
Chris@0 50 'access administration pages',
Chris@0 51 ]);
Chris@0 52 $this->drupalLogin($this->adminUser);
Chris@0 53
Chris@0 54 // Enable some test blocks.
Chris@0 55 $this->blockValues = [
Chris@0 56 [
Chris@0 57 'label' => 'Tools',
Chris@0 58 'tr' => '5',
Chris@0 59 'plugin_id' => 'system_menu_block:tools',
Chris@0 60 'settings' => ['region' => 'sidebar_second', 'id' => 'tools'],
Chris@0 61 'test_weight' => '-1',
Chris@0 62 ],
Chris@0 63 [
Chris@0 64 'label' => 'Powered by Drupal',
Chris@0 65 'tr' => '16',
Chris@0 66 'plugin_id' => 'system_powered_by_block',
Chris@0 67 'settings' => ['region' => 'footer', 'id' => 'powered'],
Chris@0 68 'test_weight' => '0',
Chris@0 69 ],
Chris@0 70 ];
Chris@0 71 $this->blocks = [];
Chris@0 72 foreach ($this->blockValues as $values) {
Chris@0 73 $this->blocks[] = $this->drupalPlaceBlock($values['plugin_id'], $values['settings']);
Chris@0 74 }
Chris@0 75 }
Chris@0 76
Chris@0 77 /**
Chris@0 78 * Test block demo page exists and functions correctly.
Chris@0 79 */
Chris@0 80 public function testBlockDemoUiPage() {
Chris@0 81 $this->drupalPlaceBlock('help_block', ['region' => 'help']);
Chris@0 82 $this->drupalGet('admin/structure/block');
Chris@0 83 $this->clickLink(t('Demonstrate block regions (@theme)', ['@theme' => 'Classy']));
Chris@0 84 $elements = $this->xpath('//div[contains(@class, "region-highlighted")]/div[contains(@class, "block-region") and contains(text(), :title)]', [':title' => 'Highlighted']);
Chris@0 85 $this->assertTrue(!empty($elements), 'Block demo regions are shown.');
Chris@0 86
Chris@0 87 \Drupal::service('theme_handler')->install(['test_theme']);
Chris@0 88 $this->drupalGet('admin/structure/block/demo/test_theme');
Chris@0 89 $this->assertEscaped('<strong>Test theme</strong>');
Chris@0 90
Chris@0 91 \Drupal::service('theme_handler')->install(['stable']);
Chris@0 92 $this->drupalGet('admin/structure/block/demo/stable');
Chris@0 93 $this->assertResponse(404, 'Hidden themes that are not the default theme are not supported by the block demo screen');
Chris@0 94 }
Chris@0 95
Chris@0 96 /**
Chris@0 97 * Test block admin page exists and functions correctly.
Chris@0 98 */
Chris@0 99 public function testBlockAdminUiPage() {
Chris@0 100 // Visit the blocks admin ui.
Chris@0 101 $this->drupalGet('admin/structure/block');
Chris@0 102 // Look for the blocks table.
Chris@0 103 $blocks_table = $this->xpath("//table[@id='blocks']");
Chris@0 104 $this->assertTrue(!empty($blocks_table), 'The blocks table is being rendered.');
Chris@0 105 // Look for test blocks in the table.
Chris@0 106 foreach ($this->blockValues as $delta => $values) {
Chris@0 107 $block = $this->blocks[$delta];
Chris@0 108 $label = $block->label();
Chris@0 109 $element = $this->xpath('//*[@id="blocks"]/tbody/tr[' . $values['tr'] . ']/td[1]/text()');
Chris@0 110 $this->assertEquals($element[0]->getText(), $label, 'The "' . $label . '" block title is set inside the ' . $values['settings']['region'] . ' region.');
Chris@0 111 // Look for a test block region select form element.
Chris@0 112 $this->assertField('blocks[' . $values['settings']['id'] . '][region]', 'The block "' . $values['label'] . '" has a region assignment field.');
Chris@0 113 // Move the test block to the header region.
Chris@0 114 $edit['blocks[' . $values['settings']['id'] . '][region]'] = 'header';
Chris@0 115 // Look for a test block weight select form element.
Chris@0 116 $this->assertField('blocks[' . $values['settings']['id'] . '][weight]', 'The block "' . $values['label'] . '" has a weight assignment field.');
Chris@0 117 // Change the test block's weight.
Chris@0 118 $edit['blocks[' . $values['settings']['id'] . '][weight]'] = $values['test_weight'];
Chris@0 119 }
Chris@0 120 $this->drupalPostForm('admin/structure/block', $edit, t('Save blocks'));
Chris@0 121 foreach ($this->blockValues as $values) {
Chris@0 122 // Check if the region and weight settings changes have persisted.
Chris@0 123 $this->assertOptionSelected(
Chris@0 124 'edit-blocks-' . $values['settings']['id'] . '-region',
Chris@0 125 'header',
Chris@0 126 'The block "' . $label . '" has the correct region assignment (header).'
Chris@0 127 );
Chris@0 128 $this->assertOptionSelected(
Chris@0 129 'edit-blocks-' . $values['settings']['id'] . '-weight',
Chris@0 130 $values['test_weight'],
Chris@0 131 'The block "' . $label . '" has the correct weight assignment (' . $values['test_weight'] . ').'
Chris@0 132 );
Chris@0 133 }
Chris@0 134
Chris@0 135 // Add a block with a machine name the same as a region name.
Chris@0 136 $this->drupalPlaceBlock('system_powered_by_block', ['region' => 'header', 'id' => 'header']);
Chris@0 137 $this->drupalGet('admin/structure/block');
Chris@0 138 $element = $this->xpath('//tr[contains(@class, :class)]', [':class' => 'region-title-header']);
Chris@0 139 $this->assertTrue(!empty($element));
Chris@0 140
Chris@0 141 // Ensure hidden themes do not appear in the UI. Enable another non base
Chris@0 142 // theme and place the local tasks block.
Chris@0 143 $this->assertTrue(\Drupal::service('theme_handler')->themeExists('classy'), 'The classy base theme is enabled');
Chris@0 144 $this->drupalPlaceBlock('local_tasks_block', ['region' => 'header']);
Chris@0 145 \Drupal::service('theme_installer')->install(['stable', 'stark']);
Chris@0 146 $this->drupalGet('admin/structure/block');
Chris@0 147 $theme_handler = \Drupal::service('theme_handler');
Chris@0 148 $this->assertLink($theme_handler->getName('classy'));
Chris@0 149 $this->assertLink($theme_handler->getName('stark'));
Chris@0 150 $this->assertNoLink($theme_handler->getName('stable'));
Chris@0 151
Chris@0 152 $this->drupalGet('admin/structure/block/list/stable');
Chris@0 153 $this->assertResponse(404, 'Placing blocks through UI is not possible for a hidden base theme.');
Chris@0 154
Chris@0 155 \Drupal::configFactory()->getEditable('system.theme')->set('admin', 'stable')->save();
Chris@0 156 \Drupal::service('router.builder')->rebuildIfNeeded();
Chris@0 157 $this->drupalPlaceBlock('local_tasks_block', ['region' => 'header', 'theme' => 'stable']);
Chris@0 158 $this->drupalGet('admin/structure/block');
Chris@0 159 $this->assertLink($theme_handler->getName('stable'));
Chris@0 160 $this->drupalGet('admin/structure/block/list/stable');
Chris@0 161 $this->assertResponse(200, 'Placing blocks through UI is possible for a hidden base theme that is the admin theme.');
Chris@0 162 }
Chris@0 163
Chris@0 164 /**
Chris@0 165 * Tests the block categories on the listing page.
Chris@0 166 */
Chris@0 167 public function testCandidateBlockList() {
Chris@0 168 $arguments = [
Chris@0 169 ':title' => 'Display message',
Chris@0 170 ':category' => 'Block test',
Chris@0 171 ':href' => 'admin/structure/block/add/test_block_instantiation/classy',
Chris@0 172 ];
Chris@0 173 $pattern = '//tr[.//td/div[text()=:title] and .//td[text()=:category] and .//td//a[contains(@href, :href)]]';
Chris@0 174
Chris@0 175 $this->drupalGet('admin/structure/block');
Chris@0 176 $this->clickLink('Place block');
Chris@0 177 $elements = $this->xpath($pattern, $arguments);
Chris@0 178 $this->assertTrue(!empty($elements), 'The test block appears in the category for its module.');
Chris@0 179
Chris@0 180 // Trigger the custom category addition in block_test_block_alter().
Chris@0 181 $this->container->get('state')->set('block_test_info_alter', TRUE);
Chris@0 182 $this->container->get('plugin.manager.block')->clearCachedDefinitions();
Chris@0 183
Chris@0 184 $this->drupalGet('admin/structure/block');
Chris@0 185 $this->clickLink('Place block');
Chris@0 186 $arguments[':category'] = 'Custom category';
Chris@0 187 $elements = $this->xpath($pattern, $arguments);
Chris@0 188 $this->assertTrue(!empty($elements), 'The test block appears in a custom category controlled by block_test_block_alter().');
Chris@0 189 }
Chris@0 190
Chris@0 191 /**
Chris@0 192 * Tests the behavior of unsatisfied context-aware blocks.
Chris@0 193 */
Chris@0 194 public function testContextAwareUnsatisfiedBlocks() {
Chris@0 195 $arguments = [
Chris@0 196 ':category' => 'Block test',
Chris@0 197 ':href' => 'admin/structure/block/add/test_context_aware_unsatisfied/classy',
Chris@0 198 ':text' => 'Test context-aware unsatisfied block',
Chris@0 199 ];
Chris@0 200
Chris@0 201 $this->drupalGet('admin/structure/block');
Chris@0 202 $this->clickLink('Place block');
Chris@0 203 $elements = $this->xpath('//tr[.//td/div[text()=:text] and .//td[text()=:category] and .//td//a[contains(@href, :href)]]', $arguments);
Chris@0 204 $this->assertTrue(empty($elements), 'The context-aware test block does not appear.');
Chris@0 205
Chris@0 206 $definition = \Drupal::service('plugin.manager.block')->getDefinition('test_context_aware_unsatisfied');
Chris@0 207 $this->assertTrue(!empty($definition), 'The context-aware test block does not exist.');
Chris@0 208 }
Chris@0 209
Chris@0 210 /**
Chris@0 211 * Tests the behavior of context-aware blocks.
Chris@0 212 */
Chris@0 213 public function testContextAwareBlocks() {
Chris@18 214 $expected_text = '<div id="test_context_aware--username">' . \Drupal::currentUser()->getAccountName() . '</div>';
Chris@0 215 $this->drupalGet('');
Chris@0 216 $this->assertNoText('Test context-aware block');
Chris@0 217 $this->assertNoRaw($expected_text);
Chris@0 218
Chris@0 219 $block_url = 'admin/structure/block/add/test_context_aware/classy';
Chris@0 220 $arguments = [
Chris@0 221 ':title' => 'Test context-aware block',
Chris@0 222 ':category' => 'Block test',
Chris@0 223 ':href' => $block_url,
Chris@0 224 ];
Chris@0 225 $pattern = '//tr[.//td/div[text()=:title] and .//td[text()=:category] and .//td//a[contains(@href, :href)]]';
Chris@0 226
Chris@0 227 $this->drupalGet('admin/structure/block');
Chris@0 228 $this->clickLink('Place block');
Chris@0 229 $elements = $this->xpath($pattern, $arguments);
Chris@0 230 $this->assertTrue(!empty($elements), 'The context-aware test block appears.');
Chris@0 231 $definition = \Drupal::service('plugin.manager.block')->getDefinition('test_context_aware');
Chris@0 232 $this->assertTrue(!empty($definition), 'The context-aware test block exists.');
Chris@0 233 $edit = [
Chris@0 234 'region' => 'content',
Chris@0 235 'settings[context_mapping][user]' => '@block_test.multiple_static_context:userB',
Chris@0 236 ];
Chris@0 237 $this->drupalPostForm($block_url, $edit, 'Save block');
Chris@0 238
Chris@0 239 $this->drupalGet('');
Chris@0 240 $this->assertText('Test context-aware block');
Chris@0 241 $this->assertText('User context found.');
Chris@0 242 $this->assertRaw($expected_text);
Chris@0 243
Chris@17 244 // Test context mapping form element is not visible if there are no valid
Chris@17 245 // context options for the block (the test_context_aware_no_valid_context_options
Chris@17 246 // block has one context defined which is not available for it on the
Chris@17 247 // Block Layout interface).
Chris@17 248 $this->drupalGet('admin/structure/block/add/test_context_aware_no_valid_context_options/classy');
Chris@17 249 $this->assertSession()->fieldNotExists('edit-settings-context-mapping-email');
Chris@17 250
Chris@0 251 // Test context mapping allows empty selection for optional contexts.
Chris@0 252 $this->drupalGet('admin/structure/block/manage/testcontextawareblock');
Chris@0 253 $edit = [
Chris@0 254 'settings[context_mapping][user]' => '',
Chris@0 255 ];
Chris@0 256 $this->drupalPostForm(NULL, $edit, 'Save block');
Chris@0 257 $this->drupalGet('');
Chris@0 258 $this->assertText('No context mapping selected.');
Chris@0 259 $this->assertNoText('User context found.');
Chris@0 260
Chris@0 261 // Tests that conditions with missing context are not displayed.
Chris@0 262 $this->drupalGet('admin/structure/block/manage/testcontextawareblock');
Chris@0 263 $this->assertNoRaw('No existing type');
Chris@0 264 $this->assertNoFieldByXPath('//*[@name="visibility[condition_test_no_existing_type][negate]"]');
Chris@0 265 }
Chris@0 266
Chris@0 267 /**
Chris@0 268 * Tests that the BlockForm populates machine name correctly.
Chris@0 269 */
Chris@0 270 public function testMachineNameSuggestion() {
Chris@0 271 $url = 'admin/structure/block/add/test_block_instantiation/classy';
Chris@0 272 $this->drupalGet($url);
Chris@0 273 $this->assertFieldByName('id', 'displaymessage', 'Block form uses raw machine name suggestion when no instance already exists.');
Chris@0 274 $edit = ['region' => 'content'];
Chris@0 275 $this->drupalPostForm($url, $edit, 'Save block');
Chris@0 276 $this->assertText('The block configuration has been saved.');
Chris@0 277
Chris@0 278 // Now, check to make sure the form starts by autoincrementing correctly.
Chris@0 279 $this->drupalGet($url);
Chris@0 280 $this->assertFieldByName('id', 'displaymessage_2', 'Block form appends _2 to plugin-suggested machine name when an instance already exists.');
Chris@0 281 $this->drupalPostForm($url, $edit, 'Save block');
Chris@0 282 $this->assertText('The block configuration has been saved.');
Chris@0 283
Chris@0 284 // And verify that it continues working beyond just the first two.
Chris@0 285 $this->drupalGet($url);
Chris@0 286 $this->assertFieldByName('id', 'displaymessage_3', 'Block form appends _3 to plugin-suggested machine name when two instances already exist.');
Chris@0 287 }
Chris@0 288
Chris@0 289 /**
Chris@0 290 * Tests the block placement indicator.
Chris@0 291 */
Chris@0 292 public function testBlockPlacementIndicator() {
Chris@17 293 // Test the block placement indicator with using the domain as URL language
Chris@17 294 // indicator. This causes destination query parameters to be absolute URLs.
Chris@17 295 \Drupal::service('module_installer')->install(['language', 'locale']);
Chris@17 296 $this->container = \Drupal::getContainer();
Chris@17 297 ConfigurableLanguage::createFromLangcode('it')->save();
Chris@17 298 $config = $this->config('language.types');
Chris@17 299 $config->set('negotiation.language_interface.enabled', [
Chris@17 300 LanguageNegotiationUrl::METHOD_ID => -10,
Chris@17 301 ]);
Chris@17 302 $config->save();
Chris@17 303 $config = $this->config('language.negotiation');
Chris@17 304 $config->set('url.source', LanguageNegotiationUrl::CONFIG_DOMAIN);
Chris@17 305 $config->set('url.domains', [
Chris@17 306 'en' => \Drupal::request()->getHost(),
Chris@17 307 'it' => 'it.example.com',
Chris@17 308 ]);
Chris@17 309 $config->save();
Chris@17 310
Chris@0 311 // Select the 'Powered by Drupal' block to be placed.
Chris@0 312 $block = [];
Chris@0 313 $block['id'] = strtolower($this->randomMachineName());
Chris@0 314 $block['theme'] = 'classy';
Chris@0 315 $block['region'] = 'content';
Chris@0 316
Chris@0 317 // After adding a block, it will indicate which block was just added.
Chris@0 318 $this->drupalPostForm('admin/structure/block/add/system_powered_by_block', $block, t('Save block'));
Chris@17 319 $this->assertSession()->addressEquals('admin/structure/block/list/classy?block-placement=' . Html::getClass($block['id']));
Chris@0 320
Chris@17 321 // Resaving the block page will remove the block placement indicator.
Chris@0 322 $this->drupalPostForm(NULL, [], t('Save blocks'));
Chris@17 323 $this->assertSession()->addressEquals('admin/structure/block/list/classy');
Chris@17 324
Chris@17 325 // Place another block and test the remove functionality works with the
Chris@17 326 // block placement indicator. Click the first 'Place block' link to bring up
Chris@17 327 // the list of blocks to place in the first available region.
Chris@17 328 $this->clickLink('Place block');
Chris@17 329 // Select the first available block.
Chris@17 330 $this->clickLink('Place block');
Chris@17 331 $block = [];
Chris@17 332 $block['id'] = strtolower($this->randomMachineName());
Chris@17 333 $block['theme'] = 'classy';
Chris@17 334 $this->submitForm([], 'Save block');
Chris@17 335 $this->assertSession()->addressEquals('admin/structure/block/list/classy?block-placement=' . Html::getClass($block['id']));
Chris@17 336
Chris@17 337 // Removing a block will remove the block placement indicator.
Chris@17 338 $this->clickLink('Remove');
Chris@17 339 $this->submitForm([], 'Remove');
Chris@17 340 // @todo https://www.drupal.org/project/drupal/issues/2980527 this should be
Chris@17 341 // 'admin/structure/block/list/classy' but there is a bug.
Chris@17 342 $this->assertSession()->addressEquals('admin/structure/block');
Chris@0 343 }
Chris@0 344
Chris@0 345 /**
Chris@0 346 * Tests if validation errors are passed plugin form to the parent form.
Chris@0 347 */
Chris@0 348 public function testBlockValidateErrors() {
Chris@0 349 $this->drupalPostForm('admin/structure/block/add/test_settings_validation/classy', ['region' => 'content', 'settings[digits]' => 'abc'], t('Save block'));
Chris@0 350
Chris@0 351 $arguments = [':message' => 'Only digits are allowed'];
Chris@0 352 $pattern = '//div[contains(@class,"messages messages--error")]/div[contains(text()[2],:message)]';
Chris@0 353 $elements = $this->xpath($pattern, $arguments);
Chris@0 354 $this->assertTrue($elements, 'Plugin error message found in parent form.');
Chris@0 355
Chris@0 356 $error_class_pattern = '//div[contains(@class,"form-item-settings-digits")]/input[contains(@class,"error")]';
Chris@0 357 $error_class = $this->xpath($error_class_pattern);
Chris@0 358 $this->assertTrue($error_class, 'Plugin error class found in parent form.');
Chris@0 359 }
Chris@0 360
Chris@0 361 /**
Chris@0 362 * Tests that the enable/disable routes are protected from CSRF.
Chris@0 363 */
Chris@0 364 public function testRouteProtection() {
Chris@0 365 // Get the first block generated in our setUp method.
Chris@0 366 /** @var \Drupal\block\BlockInterface $block */
Chris@0 367 $block = reset($this->blocks);
Chris@0 368 // Ensure that the enable and disable routes are protected.
Chris@0 369 $this->drupalGet('admin/structure/block/manage/' . $block->id() . '/disable');
Chris@0 370 $this->assertResponse(403);
Chris@0 371 $this->drupalGet('admin/structure/block/manage/' . $block->id() . '/enable');
Chris@0 372 $this->assertResponse(403);
Chris@0 373 }
Chris@0 374
Chris@0 375 }