Skip to content

Commit

Permalink
Amend the visibility test to account for banner caching
Browse files Browse the repository at this point in the history
See #327.

Extends visibility test by providing actual pages for the banner to display on.
This also provides an extra banner to test the correct banner is visible per page.
  • Loading branch information
andybroomfield committed Sep 12, 2024
1 parent dec1fc0 commit 69e4167
Showing 1 changed file with 61 additions and 2 deletions.
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 69e4167

Please sign in to comment.