Skip to content

Commit

Permalink
Merge pull request #498 from localgovdrupal/fix/1.x/497-disabled-bloc…
Browse files Browse the repository at this point in the history
…ks-get-rendered

Stops blocks that are assigned to the disabled region being rendered.
  • Loading branch information
finnlewis authored Nov 20, 2023
2 parents 15909a6 + 69fafa1 commit 2ee7bdc
Show file tree
Hide file tree
Showing 9 changed files with 199 additions and 22 deletions.
24 changes: 12 additions & 12 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ jobs:
fail-fast: false
matrix:
localgov-version:
- '2.x'
- '3.x'
drupal-version:
- '~9.3'
- '~10.0'
php-version:
- '7.4'
- '8.1'
- '8.2'

steps:

Expand Down Expand Up @@ -109,12 +109,12 @@ jobs:
fail-fast: false
matrix:
localgov-version:
- '2.x'
- '3.x'
drupal-version:
- '~9.3'
- '~10.0'
php-version:
- '7.4'
- '8.1'
- '8.2'

steps:

Expand Down Expand Up @@ -145,12 +145,12 @@ jobs:
fail-fast: false
matrix:
localgov-version:
- '2.x'
- '3.x'
drupal-version:
- '~9.3'
- '~10.0'
php-version:
- '7.4'
- '8.1'
- '8.2'

steps:

Expand Down Expand Up @@ -180,12 +180,12 @@ jobs:
fail-fast: false
matrix:
localgov-version:
- '2.x'
- '3.x'
drupal-version:
- '~9.3'
- '~10.0'
php-version:
- '7.4'
- '8.1'
- '8.2'

steps:

Expand Down
18 changes: 8 additions & 10 deletions localgov_base.theme
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,20 @@ function localgov_base_preprocess_page(&$variables) {
// @see https://www.drupal.org/node/953034
// @see https://www.drupal.org/forum/support/module-development-and-code-questions/2016-04-07/drupal-8-regions-with-and-empty#comment-12149518
$active_theme = \Drupal::service('theme.manager')->getActiveTheme()->getName();
$system_region = system_region_list($active_theme, $show = REGIONS_ALL);
$regions = array_keys(system_region_list($active_theme, REGIONS_VISIBLE));

foreach ($system_region as $key => $value) {
$region = $key;
$has = 'has_' . $region;
$excluded_regions = [
'messages',
'disabled',
];

$excluded_regions = [
'messages',
];
foreach ($regions as $region) {
if (in_array($region, $excluded_regions)) {
continue;
}

$copy = $variables['page'][$region];
$rendered = Drupal::service('renderer')->renderPlain($copy);
$variables[$has] = !empty(trim(strip_tags($rendered, '<drupal-render-placeholder><embed><hr><iframe><img><input><link><object><script><source><style><video>')));
$rendered = \Drupal::service('renderer')->renderPlain($copy);
$variables['has_' . $region] = !empty(trim(strip_tags($rendered, '<drupal-render-placeholder><embed><hr><iframe><img><input><link><object><script><source><style><video>')));
}
$variables['has_sidebars'] = $variables['has_sidebar_first'] || $variables['has_sidebar_second'];

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: "LocalGov Base Test Support"
description: "Things to help test localgov_base that have to live in a module."
type: module
core_version_requirement: ^8.8 || ^9 || ^10
hidden: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
localgov_base_test_support.test_page:
path: '/test-page'
defaults:
_controller: '\Drupal\localgov_base_test_support\Controller\TestController::testPage'
_title: 'Test page'
requirements:
# No access check, as this module is just for testing.
_access: 'TRUE'
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:
localgov_base_test_support.render_test_block_response_subscriber:
class: '\Drupal\localgov_base_test_support\EventSubscriber\RenderTestBlockResponseSubscriber'
tags:
- { name: 'event_subscriber' }
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Drupal\localgov_base_test_support\Controller;

use Drupal\Core\Controller\ControllerBase;

/**
* A test controller.
*/
class TestController extends ControllerBase {

/**
* Returns a render array for a test page.
*/
public function testPage() {
return [
'#markup' => $this->t('Hello World!'),
];
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Drupal\localgov_base_test_support\EventSubscriber;

use Drupal\localgov_base_test_support\Plugin\Block\RenderTestBlock;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;

/**
* Adds debug info to the Response headers for testing.
*/
class RenderTestBlockResponseSubscriber implements EventSubscriberInterface {

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return [
KernelEvents::RESPONSE => 'addRenderTestBlockHeader',
];
}

/**
* Adds a response header to indicate if the RenderTestBlock got built.
*/
public function addRenderTestBlockHeader(ResponseEvent $event) {
$headerName = 'X-Render-Test-Block';
$headerValue = RenderTestBlock::$built ? 'TRUE' : 'FALSE';
$event->getResponse()->headers->set($headerName, $headerValue);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Drupal\localgov_base_test_support\Plugin\Block;

use Drupal\Core\Block\BlockBase;

/**
* Provides a render test block, used by RegionRenderTest.
*
* @Block(
* id = "render_test_block",
* admin_label = @Translation("Render test block")
* )
*/
class RenderTestBlock extends BlockBase {

/**
* Records if a block of this class has ever been built.
*
* This is public so it can be read by RenderTestBlockResponseSubscriber.
*
* @var bool
*/
public static bool $built = FALSE;

/**
* {@inheritdoc}
*/
public function build() {

self::$built = TRUE;
return [
'#markup' => 'This is the output from the render test block.',
];
}

}
70 changes: 70 additions & 0 deletions tests/Functional/RegionRenderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Drupal\Tests\block_content\Functional;

use Drupal\Tests\BrowserTestBase;

/**
* Tests the code in localgov_base_preprocess_page() that renders regions.
*
* @group localgov_base
*/
class RegionRenderTest extends BrowserTestBase {

/**
* {@inheritdoc}
*/
protected static $modules = [
'localgov_base_test_support',
];

/**
* {@inheritdoc}
*/
protected $profile = 'localgov';

/**
* {@inheritdoc}
*/
protected $defaultTheme = 'localgov_base';

/**
* Data provider for ::testRegionRender().
*/
public function blockRegionProvider() {
return [
[
'region' => 'disabled',
'rendered' => 'FALSE',
],
[
'region' => 'sidebar_first',
'rendered' => 'TRUE',
],
];
}

/**
* Tests that blocks in regions that aren't printed don't get rendered.
*
* To do this, we place a block provided by the localgov_base_test_support
* module into one of the regions. We then load a page, which is a callback
* provided by a controller in the same module, that will always load, and
* show blocks. Our block class records if an instance of it was built when
* the page loads. The module registers an event listener to add if the block
* was rendered to the response headers.
*
* We do all this because the page render runs in a different process to this
* test, so we can't just read the value from the class directly.
*
* @dataProvider blockRegionProvider
*/
public function testRegionRender($region, $rendered) {
$this->drupalPlaceBlock('render_test_block', [
'region' => $region,
]);
$this->drupalGet('/test-page');
$this->assertSession()->responseHeaderContains('X-Render-Test-Block', $rendered);
}

}

0 comments on commit 2ee7bdc

Please sign in to comment.