Skip to content

Commit

Permalink
Merge pull request #234 from localgovdrupal/fix/2.x/229-deleted-servi…
Browse files Browse the repository at this point in the history
…ce-status-being-cached

Add cache tags for all status nodes to service status message block
  • Loading branch information
finnlewis authored May 14, 2024
2 parents f5cb449 + fb9e056 commit 78f389f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
12 changes: 0 additions & 12 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ jobs:
fail-fast: false
matrix:
include:
- localgov-version: '2.x'
drupal-version: '~9.4'
php-version: '8.1'
- localgov-version: '3.x'
drupal-version: '~10.0'
php-version: '8.1'
Expand Down Expand Up @@ -109,9 +106,6 @@ jobs:
fail-fast: false
matrix:
include:
- localgov-version: '2.x'
drupal-version: '~9.4'
php-version: '8.1'
- localgov-version: '3.x'
drupal-version: '~10.0'
php-version: '8.1'
Expand Down Expand Up @@ -145,9 +139,6 @@ jobs:
fail-fast: false
matrix:
include:
- localgov-version: '2.x'
drupal-version: '~9.4'
php-version: '8.1'
- localgov-version: '3.x'
drupal-version: '~10.0'
php-version: '8.1'
Expand Down Expand Up @@ -180,9 +171,6 @@ jobs:
fail-fast: false
matrix:
include:
- localgov-version: '2.x'
drupal-version: '~9.4'
php-version: '8.1'
- localgov-version: '3.x'
drupal-version: '~10.0'
php-version: '8.1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ class ServiceStatusMessage extends BlockBase implements ContainerFactoryPluginIn
*/
protected $cacheContexts = [];

/**
* Cache tags for visible service statuses.
*
* @var string[]
*/
protected $cacheTags = [];

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -101,6 +108,7 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition
$condition = $this->pluginManagerCondition->createInstance($condition_id, $values);
$conditions[] = $condition;
$this->cacheContexts = Cache::mergeContexts($this->cacheContexts, $condition->getCacheContexts());
$this->cacheTags = Cache::mergeTags($this->cacheTags, $condition->getCacheTags());
}

if (ConditionAccessResolver::checkAccess($conditions, 'or')) {
Expand Down Expand Up @@ -129,9 +137,9 @@ public function build() {
*/
protected function blockAccess(AccountInterface $account) {
if (empty($this->statusNodes)) {
return AccessResult::neutral();
return AccessResult::neutral()->addCacheableDependency($this);
}
return AccessResult::allowed();
return AccessResult::allowed()->addCacheableDependency($this);
}

/**
Expand All @@ -146,7 +154,7 @@ public function getCacheContexts() {
*/
public function getCacheTags() {
// Invalidate cache on changes to localgov_services_status nodes.
return Cache::mergeTags(parent::getCacheTags(), ['node_list:localgov_services_status']);
return Cache::mergeTags(parent::getCacheTags(), $this->cacheTags, ['node_list:localgov_services_status']);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,6 @@ public function testServiceStatusMessageVisibility() {
],
]);

// This is only necessary in tests.
// Block visibility is fine when doing this manually.
drupal_flush_all_caches();

// Check visibility on homepage.
$this->drupalGet('<front>');
$this->assertSession()->elementExists('css', '.service-status-messages');
Expand All @@ -119,6 +115,16 @@ public function testServiceStatusMessageVisibility() {
$this->assertSession()->elementTextContains('css', '.service-status-messages', $summary);
$this->assertSession()->elementTextContains('css', '.service-status-messages', $summary2);
$this->assertSession()->linkByHrefExists(\Drupal::service('path_alias.manager')->getAliasByPath('/node/' . $status2->id()));

// Check visibility of deleted status.
$status2->delete();
$this->drupalGet($landing_path);
$this->assertSession()->elementTextContains('css', '.service-status-messages', $summary);
$this->assertSession()->elementTextNotContains('css', '.service-status-messages', $summary2);

// Check visibility on homepage.
$this->drupalGet('<front>');
$this->assertSession()->elementNotExists('css', '.service-status-messages');
}

}

0 comments on commit 78f389f

Please sign in to comment.