Skip to content

Commit

Permalink
Merge pull request #355 from localgovdrupal/feature/1.x-354-move-visi…
Browse files Browse the repository at this point in the history
…ble-check

Move the visible check to alert banner manager
  • Loading branch information
andybroomfield authored Sep 16, 2024
2 parents 47ab0b9 + 69e4167 commit f59c9f3
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 10 deletions.
12 changes: 11 additions & 1 deletion src/AlertBannerManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function getCurrentAlertBanners(array $options): array {
// Set default options.
$default_options = [
'type' => [],
'check_visible' => FALSE,
];
$options = array_merge($default_options, $options);

Expand Down Expand Up @@ -71,7 +72,7 @@ public function getCurrentAlertBanners(array $options): array {
->execute();

// Load alert banners and add all.
// Visibility check happens in block build, so we get cache contexts on all.
// Visibility check happens separately, so we get cache contexts on all.
if (!empty($published_alert_banners)) {
foreach ($alert_banner_storage->loadMultiple($published_alert_banners) as $alert_banner) {
$alert_banner = $this->entityRepository->getTranslationFromContext($alert_banner);
Expand All @@ -83,6 +84,15 @@ public function getCurrentAlertBanners(array $options): array {
}
}

// Check visibility if specified.
// Should only be when banners are being displayed.
// @see #154.
if ($options['check_visible']) {
$current_alert_banners = array_filter($current_alert_banners, function ($alert_banner) {
return $alert_banner->isVisible();
});
}

return $current_alert_banners;
}

Expand Down
11 changes: 4 additions & 7 deletions src/Plugin/Block/AlertBannerBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ public function build() {

$options = [
'type' => $this->mapTypesConfigToQuery(),
'check_visible' => TRUE,
];

// Fetch the current published banner.
Expand All @@ -168,13 +169,8 @@ public function build() {
// Render the alert banner.
$build = [];
foreach ($published_alert_banners as $alert_banner) {

// Only add to the build if it is visible.
// @see #154.
if ($alert_banner->isVisible()) {
$build[] = $this->entityTypeManager->getViewBuilder('localgov_alert_banner')
->view($alert_banner);
}
$build[] = $this->entityTypeManager->getViewBuilder('localgov_alert_banner')
->view($alert_banner);
}
return $build;
}
Expand All @@ -199,6 +195,7 @@ public function getCacheContexts() {
$contexts = [];
$options = [
'type' => $this->mapTypesConfigToQuery(),
'check_visible' => FALSE,
];
foreach ($this->alertBannerManager->getCurrentAlertBanners($options) as $alert_banner) {
$contexts = Cache::mergeContexts($contexts, $alert_banner->getCacheContexts());
Expand Down
63 changes: 61 additions & 2 deletions tests/src/Functional/VisibilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

namespace Drupal\Tests\localgov_alert_banner\Functional;

use Drupal\node\NodeInterface;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\node\Traits\NodeCreationTrait;

/**
* Functional tests for LocalGov Drupal Alert banner admin view.
*/
class VisibilityTest extends BrowserTestBase {
use NodeCreationTrait;

/**
* {@inheritdoc}
Expand All @@ -19,6 +22,8 @@ class VisibilityTest extends BrowserTestBase {
*/
protected static $modules = [
'localgov_alert_banner',
'node',
'path_alias',
];

/**
Expand All @@ -35,15 +40,27 @@ protected function setUp(): void {
*/
public function testAlertBannerVisibility() {

// Create a page for the alert banner.
$this->createContentType(['type' => 'page']);
$node = $this->createNode([
'title' => 'Council tax',
'type' => 'page',
'status' => NodeInterface::PUBLISHED,
]);
$this->container->get('entity_type.manager')->getStorage('path_alias')->create([
'path' => '/node/' . $node->id(),
'alias' => '/council-tax',
])->save();

// Create an alert banner.
$title = $this->randomMachineName(8);
$title = 'Council tax - ' . $this->randomMachineName(8);
$alert_message = 'Alert message: ' . $this->randomMachineName(16);
$alert = $this->container->get('entity_type.manager')->getStorage('localgov_alert_banner')
->create([
'type' => 'localgov_alert_banner',
'title' => $title,
'short_description' => $alert_message,
'type_of_alert' => 'minor',
'type_of_alert' => '20--minor',
'moderation_state' => 'published',
'visibility' => [
'conditions' => [
Expand Down Expand Up @@ -71,6 +88,48 @@ public function testAlertBannerVisibility() {
// Check it's still on /council-tax.
$this->drupalGet('/council-tax');
$this->assertSession()->pageTextContains($title);

// Create a second page and banner to test banner display caches correctly.
// @See https://github.com/localgovdrupal/localgov_alert_banner/issues/327
$node2 = $this->createNode([
'title' => 'Adult social care',
'type' => 'page',
'status' => NodeInterface::PUBLISHED,
]);
$this->container->get('entity_type.manager')->getStorage('path_alias')->create([
'path' => '/node/' . $node2->id(),
'alias' => '/adult-social-care',
])->save();

$title2 = 'Adult social care - ' . $this->randomMachineName(8);
$alert_message2 = 'Alert message: ' . $this->randomMachineName(16);
$alert2 = $this->container->get('entity_type.manager')->getStorage('localgov_alert_banner')
->create([
'type' => 'localgov_alert_banner',
'title' => $title2,
'short_description' => $alert_message2,
'type_of_alert' => '20--minor',
'moderation_state' => 'published',
'visibility' => [
'conditions' => [
'request_path' => [
'pages' => '/adult-social-care',
'negate' => 0,
],
],
],
]);
$alert2->save();

// Check the correct alert is on /adult-social-care.
$this->drupalGet('/adult-social-care');
$this->assertSession()->pageTextNotContains($title);
$this->assertSession()->pageTextContains($title2);

// Check the correct alert is on /council-tax.
$this->drupalGet('/council-tax');
$this->assertSession()->pageTextContains($title);
$this->assertSession()->pageTextNotContains($title2);
}

}

0 comments on commit f59c9f3

Please sign in to comment.