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 = '