Commit 650bc9f8 authored by alexpott's avatar alexpott

Issue #2241267 by Wim Leers: Make tours set cache tags.

parent 304d50a7
<?php
/**
* @file
* Contains \Drupal\tour\Tests\TourCacheTagsTest.
*/
namespace Drupal\tour\Tests;
use Drupal\system\Tests\Cache\PageCacheTagsTestBase;
use Drupal\tour\Entity\Tour;
use Drupal\user\Entity\Role;
/**
* Tests the Tour entity's cache tags.
*
* @group tour
*/
class TourCacheTagsTest extends PageCacheTagsTestBase {
/**
* {@inheritdoc}
*/
public static $modules = array('tour', 'tour_test');
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
// Give anonymous users permission to view nodes, so that we can verify the
// cache tags of cached versions of node pages.
Role::load(DRUPAL_ANONYMOUS_RID)->grantPermission('access tour')
->save();
}
/**
* Tests cache tags presence and invalidation of the Tour entity.
*
* Tests the following cache tags:
* - ['tour' => '<tour ID>']
*/
public function testRenderedTour() {
$path = 'tour-test-1';
// Prime the page cache.
$this->verifyPageCache($path, 'MISS');
// Verify a cache hit, but also the presence of the correct cache tags.
$expected_tags = array(
'theme:stark',
'theme_global_settings:1',
'tour:tour-test',
'rendered:1',
);
$this->verifyPageCache($path, 'HIT', $expected_tags);
// Verify that after modifying the tour, there is a cache miss.
$this->pass('Test modification of tour.', 'Debug');
Tour::load('tour-test')->save();
$this->verifyPageCache($path, 'MISS');
// Verify a cache hit.
$this->verifyPageCache($path, 'HIT', $expected_tags);
// Verify that after deleting the tour, there is a cache miss.
$this->pass('Test deletion of tour.', 'Debug');
Tour::load('tour-test')->delete();
$this->verifyPageCache($path, 'MISS');
// Verify a cache hit.
$this->verifyPageCache($path, 'HIT', array('rendered:1', 'theme:stark', 'theme_global_settings:1'));
}
}
......@@ -2,7 +2,7 @@
/**
* @file
* Contains \Drupal\tour\Entity\TourInterface.
* Contains \Drupal\tour\TourInterface.
*/
namespace Drupal\tour;
......
......@@ -19,6 +19,7 @@ class TourViewBuilder extends EntityViewBuilder {
* {@inheritdoc}
*/
public function viewMultiple(array $entities = array(), $view_mode = 'full', $langcode = NULL) {
/** @var \Drupal\tour\TourInterface[] $entities */
$build = array();
foreach ($entities as $entity_id => $entity) {
$tips = $entity->getTips();
......@@ -63,6 +64,9 @@ public function viewMultiple(array $entities = array(), $view_mode = 'full', $la
'hidden',
),
),
'#cache' => [
'tags' => $entity->getCacheTag(),
],
);
}
}
......
......@@ -75,9 +75,9 @@ function tour_toolbar() {
}
/**
* Implements hook_preprocess_HOOK() for page templates.
* Implements hook_page_build().
*/
function tour_preprocess_page(&$variables) {
function tour_page_build(&$page) {
if (!\Drupal::currentUser()->hasPermission('access tour')) {
return;
}
......@@ -97,7 +97,7 @@ function tour_preprocess_page(&$variables) {
}
}
if (!empty($tours)) {
$variables['page']['help']['tour'] = entity_view_multiple($tours, 'full');
$page['help']['tour'] = entity_view_multiple($tours, 'full');
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment