Chris@0: adminUser = $this->drupalCreateUser([ Chris@0: 'administer blocks', Chris@0: 'access administration pages', Chris@0: ]); Chris@0: $this->drupalLogin($this->adminUser); Chris@0: Chris@0: // Enable some test blocks. Chris@0: $this->blockValues = [ Chris@0: [ Chris@0: 'label' => 'Tools', Chris@0: 'tr' => '5', Chris@0: 'plugin_id' => 'system_menu_block:tools', Chris@0: 'settings' => ['region' => 'sidebar_second', 'id' => 'tools'], Chris@0: 'test_weight' => '-1', Chris@0: ], Chris@0: [ Chris@0: 'label' => 'Powered by Drupal', Chris@0: 'tr' => '16', Chris@0: 'plugin_id' => 'system_powered_by_block', Chris@0: 'settings' => ['region' => 'footer', 'id' => 'powered'], Chris@0: 'test_weight' => '0', Chris@0: ], Chris@0: ]; Chris@0: $this->blocks = []; Chris@0: foreach ($this->blockValues as $values) { Chris@0: $this->blocks[] = $this->drupalPlaceBlock($values['plugin_id'], $values['settings']); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test block demo page exists and functions correctly. Chris@0: */ Chris@0: public function testBlockDemoUiPage() { Chris@0: $this->drupalPlaceBlock('help_block', ['region' => 'help']); Chris@0: $this->drupalGet('admin/structure/block'); Chris@0: $this->clickLink(t('Demonstrate block regions (@theme)', ['@theme' => 'Classy'])); Chris@0: $elements = $this->xpath('//div[contains(@class, "region-highlighted")]/div[contains(@class, "block-region") and contains(text(), :title)]', [':title' => 'Highlighted']); Chris@0: $this->assertTrue(!empty($elements), 'Block demo regions are shown.'); Chris@0: Chris@0: \Drupal::service('theme_handler')->install(['test_theme']); Chris@0: $this->drupalGet('admin/structure/block/demo/test_theme'); Chris@0: $this->assertEscaped('Test theme'); Chris@0: Chris@0: \Drupal::service('theme_handler')->install(['stable']); Chris@0: $this->drupalGet('admin/structure/block/demo/stable'); Chris@0: $this->assertResponse(404, 'Hidden themes that are not the default theme are not supported by the block demo screen'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test block admin page exists and functions correctly. Chris@0: */ Chris@0: public function testBlockAdminUiPage() { Chris@0: // Visit the blocks admin ui. Chris@0: $this->drupalGet('admin/structure/block'); Chris@0: // Look for the blocks table. Chris@0: $blocks_table = $this->xpath("//table[@id='blocks']"); Chris@0: $this->assertTrue(!empty($blocks_table), 'The blocks table is being rendered.'); Chris@0: // Look for test blocks in the table. Chris@0: foreach ($this->blockValues as $delta => $values) { Chris@0: $block = $this->blocks[$delta]; Chris@0: $label = $block->label(); Chris@0: $element = $this->xpath('//*[@id="blocks"]/tbody/tr[' . $values['tr'] . ']/td[1]/text()'); Chris@0: $this->assertEquals($element[0]->getText(), $label, 'The "' . $label . '" block title is set inside the ' . $values['settings']['region'] . ' region.'); Chris@0: // Look for a test block region select form element. Chris@0: $this->assertField('blocks[' . $values['settings']['id'] . '][region]', 'The block "' . $values['label'] . '" has a region assignment field.'); Chris@0: // Move the test block to the header region. Chris@0: $edit['blocks[' . $values['settings']['id'] . '][region]'] = 'header'; Chris@0: // Look for a test block weight select form element. Chris@0: $this->assertField('blocks[' . $values['settings']['id'] . '][weight]', 'The block "' . $values['label'] . '" has a weight assignment field.'); Chris@0: // Change the test block's weight. Chris@0: $edit['blocks[' . $values['settings']['id'] . '][weight]'] = $values['test_weight']; Chris@0: } Chris@0: $this->drupalPostForm('admin/structure/block', $edit, t('Save blocks')); Chris@0: foreach ($this->blockValues as $values) { Chris@0: // Check if the region and weight settings changes have persisted. Chris@0: $this->assertOptionSelected( Chris@0: 'edit-blocks-' . $values['settings']['id'] . '-region', Chris@0: 'header', Chris@0: 'The block "' . $label . '" has the correct region assignment (header).' Chris@0: ); Chris@0: $this->assertOptionSelected( Chris@0: 'edit-blocks-' . $values['settings']['id'] . '-weight', Chris@0: $values['test_weight'], Chris@0: 'The block "' . $label . '" has the correct weight assignment (' . $values['test_weight'] . ').' Chris@0: ); Chris@0: } Chris@0: Chris@0: // Add a block with a machine name the same as a region name. Chris@0: $this->drupalPlaceBlock('system_powered_by_block', ['region' => 'header', 'id' => 'header']); Chris@0: $this->drupalGet('admin/structure/block'); Chris@0: $element = $this->xpath('//tr[contains(@class, :class)]', [':class' => 'region-title-header']); Chris@0: $this->assertTrue(!empty($element)); Chris@0: Chris@0: // Ensure hidden themes do not appear in the UI. Enable another non base Chris@0: // theme and place the local tasks block. Chris@0: $this->assertTrue(\Drupal::service('theme_handler')->themeExists('classy'), 'The classy base theme is enabled'); Chris@0: $this->drupalPlaceBlock('local_tasks_block', ['region' => 'header']); Chris@0: \Drupal::service('theme_installer')->install(['stable', 'stark']); Chris@0: $this->drupalGet('admin/structure/block'); Chris@0: $theme_handler = \Drupal::service('theme_handler'); Chris@0: $this->assertLink($theme_handler->getName('classy')); Chris@0: $this->assertLink($theme_handler->getName('stark')); Chris@0: $this->assertNoLink($theme_handler->getName('stable')); Chris@0: Chris@0: $this->drupalGet('admin/structure/block/list/stable'); Chris@0: $this->assertResponse(404, 'Placing blocks through UI is not possible for a hidden base theme.'); Chris@0: Chris@0: \Drupal::configFactory()->getEditable('system.theme')->set('admin', 'stable')->save(); Chris@0: \Drupal::service('router.builder')->rebuildIfNeeded(); Chris@0: $this->drupalPlaceBlock('local_tasks_block', ['region' => 'header', 'theme' => 'stable']); Chris@0: $this->drupalGet('admin/structure/block'); Chris@0: $this->assertLink($theme_handler->getName('stable')); Chris@0: $this->drupalGet('admin/structure/block/list/stable'); Chris@0: $this->assertResponse(200, 'Placing blocks through UI is possible for a hidden base theme that is the admin theme.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the block categories on the listing page. Chris@0: */ Chris@0: public function testCandidateBlockList() { Chris@0: $arguments = [ Chris@0: ':title' => 'Display message', Chris@0: ':category' => 'Block test', Chris@0: ':href' => 'admin/structure/block/add/test_block_instantiation/classy', Chris@0: ]; Chris@0: $pattern = '//tr[.//td/div[text()=:title] and .//td[text()=:category] and .//td//a[contains(@href, :href)]]'; Chris@0: Chris@0: $this->drupalGet('admin/structure/block'); Chris@0: $this->clickLink('Place block'); Chris@0: $elements = $this->xpath($pattern, $arguments); Chris@0: $this->assertTrue(!empty($elements), 'The test block appears in the category for its module.'); Chris@0: Chris@0: // Trigger the custom category addition in block_test_block_alter(). Chris@0: $this->container->get('state')->set('block_test_info_alter', TRUE); Chris@0: $this->container->get('plugin.manager.block')->clearCachedDefinitions(); Chris@0: Chris@0: $this->drupalGet('admin/structure/block'); Chris@0: $this->clickLink('Place block'); Chris@0: $arguments[':category'] = 'Custom category'; Chris@0: $elements = $this->xpath($pattern, $arguments); Chris@0: $this->assertTrue(!empty($elements), 'The test block appears in a custom category controlled by block_test_block_alter().'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the behavior of unsatisfied context-aware blocks. Chris@0: */ Chris@0: public function testContextAwareUnsatisfiedBlocks() { Chris@0: $arguments = [ Chris@0: ':category' => 'Block test', Chris@0: ':href' => 'admin/structure/block/add/test_context_aware_unsatisfied/classy', Chris@0: ':text' => 'Test context-aware unsatisfied block', Chris@0: ]; Chris@0: Chris@0: $this->drupalGet('admin/structure/block'); Chris@0: $this->clickLink('Place block'); Chris@0: $elements = $this->xpath('//tr[.//td/div[text()=:text] and .//td[text()=:category] and .//td//a[contains(@href, :href)]]', $arguments); Chris@0: $this->assertTrue(empty($elements), 'The context-aware test block does not appear.'); Chris@0: Chris@0: $definition = \Drupal::service('plugin.manager.block')->getDefinition('test_context_aware_unsatisfied'); Chris@0: $this->assertTrue(!empty($definition), 'The context-aware test block does not exist.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the behavior of context-aware blocks. Chris@0: */ Chris@0: public function testContextAwareBlocks() { Chris@0: $expected_text = '
' . \Drupal::currentUser()->getUsername() . '
'; Chris@0: $this->drupalGet(''); Chris@0: $this->assertNoText('Test context-aware block'); Chris@0: $this->assertNoRaw($expected_text); Chris@0: Chris@0: $block_url = 'admin/structure/block/add/test_context_aware/classy'; Chris@0: $arguments = [ Chris@0: ':title' => 'Test context-aware block', Chris@0: ':category' => 'Block test', Chris@0: ':href' => $block_url, Chris@0: ]; Chris@0: $pattern = '//tr[.//td/div[text()=:title] and .//td[text()=:category] and .//td//a[contains(@href, :href)]]'; Chris@0: Chris@0: $this->drupalGet('admin/structure/block'); Chris@0: $this->clickLink('Place block'); Chris@0: $elements = $this->xpath($pattern, $arguments); Chris@0: $this->assertTrue(!empty($elements), 'The context-aware test block appears.'); Chris@0: $definition = \Drupal::service('plugin.manager.block')->getDefinition('test_context_aware'); Chris@0: $this->assertTrue(!empty($definition), 'The context-aware test block exists.'); Chris@0: $edit = [ Chris@0: 'region' => 'content', Chris@0: 'settings[context_mapping][user]' => '@block_test.multiple_static_context:userB', Chris@0: ]; Chris@0: $this->drupalPostForm($block_url, $edit, 'Save block'); Chris@0: Chris@0: $this->drupalGet(''); Chris@0: $this->assertText('Test context-aware block'); Chris@0: $this->assertText('User context found.'); Chris@0: $this->assertRaw($expected_text); Chris@0: Chris@0: // Test context mapping allows empty selection for optional contexts. Chris@0: $this->drupalGet('admin/structure/block/manage/testcontextawareblock'); Chris@0: $edit = [ Chris@0: 'settings[context_mapping][user]' => '', Chris@0: ]; Chris@0: $this->drupalPostForm(NULL, $edit, 'Save block'); Chris@0: $this->drupalGet(''); Chris@0: $this->assertText('No context mapping selected.'); Chris@0: $this->assertNoText('User context found.'); Chris@0: Chris@0: // Tests that conditions with missing context are not displayed. Chris@0: $this->drupalGet('admin/structure/block/manage/testcontextawareblock'); Chris@0: $this->assertNoRaw('No existing type'); Chris@0: $this->assertNoFieldByXPath('//*[@name="visibility[condition_test_no_existing_type][negate]"]'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that the BlockForm populates machine name correctly. Chris@0: */ Chris@0: public function testMachineNameSuggestion() { Chris@0: $url = 'admin/structure/block/add/test_block_instantiation/classy'; Chris@0: $this->drupalGet($url); Chris@0: $this->assertFieldByName('id', 'displaymessage', 'Block form uses raw machine name suggestion when no instance already exists.'); Chris@0: $edit = ['region' => 'content']; Chris@0: $this->drupalPostForm($url, $edit, 'Save block'); Chris@0: $this->assertText('The block configuration has been saved.'); Chris@0: Chris@0: // Now, check to make sure the form starts by autoincrementing correctly. Chris@0: $this->drupalGet($url); Chris@0: $this->assertFieldByName('id', 'displaymessage_2', 'Block form appends _2 to plugin-suggested machine name when an instance already exists.'); Chris@0: $this->drupalPostForm($url, $edit, 'Save block'); Chris@0: $this->assertText('The block configuration has been saved.'); Chris@0: Chris@0: // And verify that it continues working beyond just the first two. Chris@0: $this->drupalGet($url); Chris@0: $this->assertFieldByName('id', 'displaymessage_3', 'Block form appends _3 to plugin-suggested machine name when two instances already exist.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the block placement indicator. Chris@0: */ Chris@0: public function testBlockPlacementIndicator() { Chris@0: // Select the 'Powered by Drupal' block to be placed. Chris@0: $block = []; Chris@0: $block['id'] = strtolower($this->randomMachineName()); Chris@0: $block['theme'] = 'classy'; Chris@0: $block['region'] = 'content'; Chris@0: Chris@0: // After adding a block, it will indicate which block was just added. Chris@0: $this->drupalPostForm('admin/structure/block/add/system_powered_by_block', $block, t('Save block')); Chris@0: $this->assertUrl('admin/structure/block/list/classy?block-placement=' . Html::getClass($block['id'])); Chris@0: Chris@0: // Resaving the block page will remove the block indicator. Chris@0: $this->drupalPostForm(NULL, [], t('Save blocks')); Chris@0: $this->assertUrl('admin/structure/block/list/classy'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests if validation errors are passed plugin form to the parent form. Chris@0: */ Chris@0: public function testBlockValidateErrors() { Chris@0: $this->drupalPostForm('admin/structure/block/add/test_settings_validation/classy', ['region' => 'content', 'settings[digits]' => 'abc'], t('Save block')); Chris@0: Chris@0: $arguments = [':message' => 'Only digits are allowed']; Chris@0: $pattern = '//div[contains(@class,"messages messages--error")]/div[contains(text()[2],:message)]'; Chris@0: $elements = $this->xpath($pattern, $arguments); Chris@0: $this->assertTrue($elements, 'Plugin error message found in parent form.'); Chris@0: Chris@0: $error_class_pattern = '//div[contains(@class,"form-item-settings-digits")]/input[contains(@class,"error")]'; Chris@0: $error_class = $this->xpath($error_class_pattern); Chris@0: $this->assertTrue($error_class, 'Plugin error class found in parent form.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that the enable/disable routes are protected from CSRF. Chris@0: */ Chris@0: public function testRouteProtection() { Chris@0: // Get the first block generated in our setUp method. Chris@0: /** @var \Drupal\block\BlockInterface $block */ Chris@0: $block = reset($this->blocks); Chris@0: // Ensure that the enable and disable routes are protected. Chris@0: $this->drupalGet('admin/structure/block/manage/' . $block->id() . '/disable'); Chris@0: $this->assertResponse(403); Chris@0: $this->drupalGet('admin/structure/block/manage/' . $block->id() . '/enable'); Chris@0: $this->assertResponse(403); Chris@0: } Chris@0: Chris@0: }