-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #498 from localgovdrupal/fix/1.x/497-disabled-bloc…
…ks-get-rendered Stops blocks that are assigned to the disabled region being rendered.
- Loading branch information
Showing
9 changed files
with
199 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
modules/localgov_base_test_support/localgov_base_test_support.info.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
8 changes: 8 additions & 0 deletions
8
modules/localgov_base_test_support/localgov_base_test_support.routing.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
5 changes: 5 additions & 0 deletions
5
modules/localgov_base_test_support/localgov_base_test_support.services.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' } |
21 changes: 21 additions & 0 deletions
21
modules/localgov_base_test_support/src/Controller/TestController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!'), | ||
]; | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
modules/localgov_base_test_support/src/EventSubscriber/RenderTestBlockResponseSubscriber.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
modules/localgov_base_test_support/src/Plugin/Block/RenderTestBlock.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.', | ||
]; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |