diff --git a/modules/block/block.module b/modules/block/block.module index 70f55e5..f9704df 100644 --- a/modules/block/block.module +++ b/modules/block/block.module @@ -910,7 +910,7 @@ function _block_get_cache_id($block) { // it brings too many chances of having unwanted output get in the cache // and later be served to other users. We therefore exclude user 1 from // block caching. - if (variable_get('block_cache', FALSE) && !in_array($block->cache, array(DRUPAL_NO_CACHE, DRUPAL_CACHE_CUSTOM)) && $user->uid != 1) { + if (variable_get('block_cache', FALSE) && $block->cache != DRUPAL_NO_CACHE && $block->cache != DRUPAL_CACHE_CUSTOM && $user->uid != 1) { // Start with common sub-patterns: block identification, theme, language. $cid_parts[] = $block->module; $cid_parts[] = $block->delta; diff --git a/modules/contextual/contextual.module b/modules/contextual/contextual.module index 0f371a5..3098f83 100644 --- a/modules/contextual/contextual.module +++ b/modules/contextual/contextual.module @@ -167,3 +167,18 @@ function contextual_pre_render_links($element) { return $element; } +/** + * Implements hook_block_info_alter(). + */ +function contextual_block_info_alter(&$blocks) { + foreach ($blocks as &$module_blocks) { + foreach ($module_blocks as &$block) { + // Blocks that may be cached need to be cached at least per role. + // @see _block_get_cache_id() + // @see drupal_render_cid_parts() + if (isset($block->cache) && $block->cache != DRUPAL_NO_CACHE && $block->cache != DRUPAL_CACHE_CUSTOM) { + $block->cache |= DRUPAL_CACHE_PER_ROLE; + } + } + } +} diff --git a/modules/contextual/contextual.test b/modules/contextual/contextual.test index 79eedb8..46bad38 100644 --- a/modules/contextual/contextual.test +++ b/modules/contextual/contextual.test @@ -47,3 +47,46 @@ class ContextualDynamicContextTestCase extends DrupalWebTestCase { $this->assertRaw('node/' . $node3->nid . '/edit', 'Edit link for most recent article node showing.'); } } + +/** + * Tests to ensure that contextual links in blocks are cached properly. + */ +class ContextualBlockCacheTestCase extends DrupalWebTestCase { + protected $profile = 'testing'; + + public static function getInfo() { + return array( + 'name' => 'Contextual link block caching', + 'description' => 'Tests if contextual links in blocks are cached properly and not shown to users without the correct permissions.', + 'group' => 'Contextual', + ); + } + + function setUp() { + parent::setUp(array('contextual', 'block', 'forum')); + $web_user = $this->drupalCreateUser(array('access content', 'access contextual links', 'administer blocks')); + $this->drupalLogin($web_user); + } + + /** + * Tests contextual links on Active forum topics block with different users. + */ + function testForumBlock() { + // Set the forum_active block to the content region. + $edit = array(); + $edit['blocks[forum_active][region]'] = 'content'; + $edit['blocks[system_main][region]'] = 'content'; + $this->drupalPost('admin/structure/block', $edit, t('Save blocks')); + $this->assertText(t('The block settings have been updated.'), 'Active forum topics block successfully moved to content region.'); + + // Make sure the block is visible on the front page to the logged in user. + $this->drupalGet('node'); + $this->assertLinkByHref('admin/structure/block/manage/forum/active/configure', 0, 'Configure block link for Active forum topics block is displayed.'); + + // Log out and make sure the "Configure block" link is not visible to + // anonymous users. + $this->drupalLogout(); + $this->drupalGet('node'); + $this->assertNoLinkByHref('admin/structure/block/manage/forum/active/configure', 'Configure block link for Active forum topics block is not displayed for logged out user.'); + } +} diff --git a/modules/forum/forum.module b/modules/forum/forum.module index 8083233..26c5352 100644 --- a/modules/forum/forum.module +++ b/modules/forum/forum.module @@ -694,8 +694,11 @@ function forum_block_view($delta = '') { } $block['subject'] = $title; - // Cache based on the altered query. Enables us to cache with node access enabled. - $block['content'] = drupal_render_cache_by_query($query, 'forum_block_view'); + // Cache based on the altered query. Enables us to cache with node access + // enabled. However, contextual links on the block need to be cached at least + // per role. + // @see contextual_block_info_alter() + $block['content'] = drupal_render_cache_by_query($query, 'forum_block_view', CACHE_TEMPORARY, DRUPAL_CACHE_PER_ROLE); $block['content']['#access'] = user_access('access content'); return $block; }