Issue #2407765 by Wim Leers, borisson_: Wherever simple config is used to render output to end user, associate their cache tags

8.0.x
Nathaniel Catchpole 2015-03-04 15:13:24 +00:00
parent fa29e0b5ca
commit 468646e3b7
18 changed files with 79 additions and 14 deletions

View File

@ -51,6 +51,7 @@ class BlockSystemBrandingTest extends BlockTestBase {
$this->assertTrue(!empty($site_logo_element), 'The branding block logo was found.'); $this->assertTrue(!empty($site_logo_element), 'The branding block logo was found.');
$this->assertTrue(!empty($site_name_element), 'The branding block site name was found.'); $this->assertTrue(!empty($site_name_element), 'The branding block site name was found.');
$this->assertTrue(!empty($site_slogan_element), 'The branding block slogan was found.'); $this->assertTrue(!empty($site_slogan_element), 'The branding block slogan was found.');
$this->assertCacheTag('config:system.site');
// Turn just the logo off. // Turn just the logo off.
$this->config('block.block.site-branding') $this->config('block.block.site-branding')
@ -64,6 +65,7 @@ class BlockSystemBrandingTest extends BlockTestBase {
$this->assertTrue(empty($site_logo_element), 'The branding block logo was disabled.'); $this->assertTrue(empty($site_logo_element), 'The branding block logo was disabled.');
$this->assertTrue(!empty($site_name_element), 'The branding block site name was found.'); $this->assertTrue(!empty($site_name_element), 'The branding block site name was found.');
$this->assertTrue(!empty($site_slogan_element), 'The branding block slogan was found.'); $this->assertTrue(!empty($site_slogan_element), 'The branding block slogan was found.');
$this->assertCacheTag('config:system.site');
// Turn just the site name off. // Turn just the site name off.
$this->config('block.block.site-branding') $this->config('block.block.site-branding')
@ -78,6 +80,7 @@ class BlockSystemBrandingTest extends BlockTestBase {
$this->assertTrue(!empty($site_logo_element), 'The branding block logo was found.'); $this->assertTrue(!empty($site_logo_element), 'The branding block logo was found.');
$this->assertTrue(empty($site_name_element), 'The branding block site name was disabled.'); $this->assertTrue(empty($site_name_element), 'The branding block site name was disabled.');
$this->assertTrue(!empty($site_slogan_element), 'The branding block slogan was found.'); $this->assertTrue(!empty($site_slogan_element), 'The branding block slogan was found.');
$this->assertCacheTag('config:system.site');
// Turn just the site slogan off. // Turn just the site slogan off.
$this->config('block.block.site-branding') $this->config('block.block.site-branding')
@ -92,6 +95,7 @@ class BlockSystemBrandingTest extends BlockTestBase {
$this->assertTrue(!empty($site_logo_element), 'The branding block logo was found.'); $this->assertTrue(!empty($site_logo_element), 'The branding block logo was found.');
$this->assertTrue(!empty($site_name_element), 'The branding block site name was found.'); $this->assertTrue(!empty($site_name_element), 'The branding block site name was found.');
$this->assertTrue(empty($site_slogan_element), 'The branding block slogan was disabled.'); $this->assertTrue(empty($site_slogan_element), 'The branding block slogan was disabled.');
$this->assertCacheTag('config:system.site');
// Turn the site name and the site slogan off. // Turn the site name and the site slogan off.
$this->config('block.block.site-branding') $this->config('block.block.site-branding')
@ -106,6 +110,7 @@ class BlockSystemBrandingTest extends BlockTestBase {
$this->assertTrue(!empty($site_logo_element), 'The branding block logo was found.'); $this->assertTrue(!empty($site_logo_element), 'The branding block logo was found.');
$this->assertTrue(empty($site_name_element), 'The branding block site name was disabled.'); $this->assertTrue(empty($site_name_element), 'The branding block site name was disabled.');
$this->assertTrue(empty($site_slogan_element), 'The branding block slogan was disabled.'); $this->assertTrue(empty($site_slogan_element), 'The branding block slogan was disabled.');
$this->assertCacheTag('config:system.site');
} }
} }

View File

@ -317,7 +317,8 @@ class BookManager implements BookManagerInterface {
* A parent selection form element. * A parent selection form element.
*/ */
protected function addParentSelectFormElements(array $book_link) { protected function addParentSelectFormElements(array $book_link) {
if ($this->configFactory->get('book.settings')->get('override_parent_selector')) { $config = $this->configFactory->get('book.settings');
if ($config->get('override_parent_selector')) {
return array(); return array();
} }
// Offer a message or a drop-down to choose a different parent page. // Offer a message or a drop-down to choose a different parent page.
@ -352,6 +353,7 @@ class BookManager implements BookManagerInterface {
'#suffix' => '</div>', '#suffix' => '</div>',
); );
} }
$form['#cache']['tags'] = Cache::mergeTags(isset($form['#cache']['tags']) ? $form['#cache']['tags'] : [], $config->getCacheTags());
return $form; return $form;
} }

View File

@ -143,6 +143,11 @@ class BookTest extends WebTestBase {
$this->drupalLogout(); $this->drupalLogout();
$this->drupalLogin($this->bookAuthor); $this->drupalLogin($this->bookAuthor);
// Check the presence of expected cache tags.
$this->drupalGet('node/add/book');
$this->assertCacheTag('config:book.settings');
/* /*
* Add Node 5 under Node 3. * Add Node 5 under Node 3.
* Book * Book
@ -153,8 +158,6 @@ class BookTest extends WebTestBase {
* |- Node 5 * |- Node 5
* |- Node 4 * |- Node 4
*/ */
$nodes[] = $this->createBookNode($book->id(), $nodes[3]->book['nid']); // Node 5. $nodes[] = $this->createBookNode($book->id(), $nodes[3]->book['nid']); // Node 5.
$this->drupalLogout(); $this->drupalLogout();
$this->drupalLogin($this->webUser); $this->drupalLogin($this->webUser);

View File

@ -10,6 +10,7 @@ namespace Drupal\comment;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\Component\Utility\String; use Drupal\Component\Utility\String;
use Drupal\Component\Utility\Unicode; use Drupal\Component\Utility\Unicode;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Datetime\DrupalDateTime; use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Entity\ContentEntityForm; use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Entity\EntityManagerInterface;
@ -78,6 +79,7 @@ class CommentForm extends ContentEntityForm {
$entity = $this->entityManager->getStorage($comment->getCommentedEntityTypeId())->load($comment->getCommentedEntityId()); $entity = $this->entityManager->getStorage($comment->getCommentedEntityTypeId())->load($comment->getCommentedEntityId());
$field_name = $comment->getFieldName(); $field_name = $comment->getFieldName();
$field_definition = $this->entityManager->getFieldDefinitions($entity->getEntityTypeId(), $entity->bundle())[$comment->getFieldName()]; $field_definition = $this->entityManager->getFieldDefinitions($entity->getEntityTypeId(), $entity->bundle())[$comment->getFieldName()];
$config = $this->config('user.settings');
// Use #comment-form as unique jump target, regardless of entity type. // Use #comment-form as unique jump target, regardless of entity type.
$form['#id'] = drupal_html_id('comment_form'); $form['#id'] = drupal_html_id('comment_form');
@ -147,7 +149,7 @@ class CommentForm extends ContentEntityForm {
); );
if ($is_admin) { if ($is_admin) {
$form['author']['name']['#title'] = $this->t('Authored by'); $form['author']['name']['#title'] = $this->t('Authored by');
$form['author']['name']['#description'] = $this->t('Leave blank for %anonymous.', array('%anonymous' => $this->config('user.settings')->get('anonymous'))); $form['author']['name']['#description'] = $this->t('Leave blank for %anonymous.', array('%anonymous' => $config->get('anonymous')));
$form['author']['name']['#autocomplete_route_name'] = 'user.autocomplete'; $form['author']['name']['#autocomplete_route_name'] = 'user.autocomplete';
} }
elseif ($this->currentUser->isAuthenticated()) { elseif ($this->currentUser->isAuthenticated()) {
@ -157,7 +159,7 @@ class CommentForm extends ContentEntityForm {
$form['author']['name']['#account'] = $this->currentUser; $form['author']['name']['#account'] = $this->currentUser;
} }
elseif($this->currentUser->isAnonymous()) { elseif($this->currentUser->isAnonymous()) {
$form['author']['name']['#attributes']['data-drupal-default-value'] = $this->config('user.settings')->get('anonymous'); $form['author']['name']['#attributes']['data-drupal-default-value'] = $config->get('anonymous');
} }
// Add author email and homepage fields depending on the current user. // Add author email and homepage fields depending on the current user.
@ -201,6 +203,8 @@ class CommentForm extends ContentEntityForm {
'#access' => $is_admin, '#access' => $is_admin,
); );
$form['#cache']['tags'] = Cache::mergeTags(isset($form['#cache']['tags']) ? $form['#cache']['tags'] : [], $config->getCacheTags());
return parent::form($form, $form_state, $comment); return parent::form($form, $form_state, $comment);
} }

View File

@ -7,6 +7,7 @@
namespace Drupal\comment; namespace Drupal\comment;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface; use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityViewBuilder; use Drupal\Core\Entity\EntityViewBuilder;
@ -102,7 +103,9 @@ class CommentViewBuilder extends EntityViewBuilder {
} }
$account = comment_prepare_author($entity); $account = comment_prepare_author($entity);
if (\Drupal::config('user.settings')->get('signatures') && $account->getSignature()) { $config = \Drupal::config('user.settings');
$build['#cache']['tags'] = Cache::mergeTags(isset($build['#cache']['tags']) ? $build['#cache']['tags'] : [], $config->getCacheTags());
if ($config->get('signatures') && $account->getSignature()) {
$build[$id]['signature'] = array( $build[$id]['signature'] = array(
'#type' => 'processed_text', '#type' => 'processed_text',
'#text' => $account->getSignature(), '#text' => $account->getSignature(),

View File

@ -56,6 +56,9 @@ class CommentAnonymousTest extends CommentTestBase {
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment'); $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
$this->assertTrue($this->commentContactInfoAvailable(), 'Contact information available.'); $this->assertTrue($this->commentContactInfoAvailable(), 'Contact information available.');
// Check the presence of expected cache tags.
$this->assertCacheTag('config:user.settings');
$anonymous_comment2 = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName()); $anonymous_comment2 = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName());
$this->assertTrue($this->commentExists($anonymous_comment2), 'Anonymous comment with contact info (optional) found.'); $this->assertTrue($this->commentExists($anonymous_comment2), 'Anonymous comment with contact info (optional) found.');

View File

@ -110,6 +110,7 @@ class CommentDefaultFormatterCacheTagsTest extends EntityUnitTestBase {
'comment_view', 'comment_view',
'comment:' . $comment->id(), 'comment:' . $comment->id(),
'config:filter.format.plain_text', 'config:filter.format.plain_text',
'config:user.settings',
'user_view', 'user_view',
'user:2', 'user:2',
); );

View File

@ -7,6 +7,7 @@
namespace Drupal\contact\Controller; namespace Drupal\contact\Controller;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Datetime\DateFormatter; use Drupal\Core\Datetime\DateFormatter;
use Drupal\Core\Flood\FloodInterface; use Drupal\Core\Flood\FloodInterface;
@ -77,12 +78,13 @@ class ContactController extends ControllerBase {
if (!$this->currentUser()->hasPermission('administer contact forms')) { if (!$this->currentUser()->hasPermission('administer contact forms')) {
$this->contactFloodControl(); $this->contactFloodControl();
} }
$config = $this->config('contact.settings');
// Use the default form if no form has been passed. // Use the default form if no form has been passed.
if (empty($contact_form)) { if (empty($contact_form)) {
$contact_form = $this->entityManager() $contact_form = $this->entityManager()
->getStorage('contact_form') ->getStorage('contact_form')
->load($this->config('contact.settings')->get('default_form')); ->load($config->get('default_form'));
// If there are no forms, do not display the form. // If there are no forms, do not display the form.
if (empty($contact_form)) { if (empty($contact_form)) {
if ($this->currentUser()->hasPermission('administer contact forms')) { if ($this->currentUser()->hasPermission('administer contact forms')) {
@ -104,6 +106,7 @@ class ContactController extends ControllerBase {
$form = $this->entityFormBuilder()->getForm($message); $form = $this->entityFormBuilder()->getForm($message);
$form['#title'] = String::checkPlain($contact_form->label()); $form['#title'] = String::checkPlain($contact_form->label());
$form['#cache']['tags'] = Cache::mergeTags(isset($form['#cache']['tags']) ? $form['#cache']['tags'] : [], $config->getCacheTags());
return $form; return $form;
} }

View File

@ -54,6 +54,10 @@ class ContactSitewideTest extends WebTestBase {
)); ));
$this->drupalLogin($admin_user); $this->drupalLogin($admin_user);
// Check the presence of expected cache tags.
$this->drupalGet('contact');
$this->assertCacheTag('config:contact.settings');
$flood_limit = 3; $flood_limit = 3;
$this->config('contact.settings') $this->config('contact.settings')
->set('flood.limit', $flood_limit) ->set('flood.limit', $flood_limit)

View File

@ -7,6 +7,7 @@
namespace Drupal\forum\Controller; namespace Drupal\forum\Controller;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityAccessControlHandlerInterface; use Drupal\Core\Entity\EntityAccessControlHandlerInterface;
use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\EntityStorageInterface;
@ -190,6 +191,7 @@ class ForumController extends ControllerBase {
if (empty($term->forum_container->value)) { if (empty($term->forum_container->value)) {
$build['#attached']['feed'][] = array('taxonomy/term/' . $term->id() . '/feed', 'RSS - ' . $term->getName()); $build['#attached']['feed'][] = array('taxonomy/term/' . $term->id() . '/feed', 'RSS - ' . $term->getName());
} }
$build['#cache']['tags'] = Cache::mergeTags(isset($build['#cache']['tags']) ? $build['#cache']['tags'] : [], $config->getCacheTags());
return [ return [
'action' => $this->buildActionLinks($config->get('vocabulary'), $term), 'action' => $this->buildActionLinks($config->get('vocabulary'), $term),

View File

@ -119,6 +119,12 @@ class ForumTest extends WebTestBase {
// Look for the "General discussion" default forum // Look for the "General discussion" default forum
$this->assertRaw(t('<a href="'. Url::fromRoute('forum.page', ['taxonomy_term' => 1])->toString() .'">General discussion</a>'), "Found the default forum at the /forum listing"); $this->assertRaw(t('<a href="'. Url::fromRoute('forum.page', ['taxonomy_term' => 1])->toString() .'">General discussion</a>'), "Found the default forum at the /forum listing");
// Check the presence of expected cache tags.
$this->assertCacheTag('config:forum.settings');
$this->drupalGet(Url::fromRoute('forum.page', ['taxonomy_term' => 1]));
$this->assertCacheTag('config:forum.settings');
// Do the admin tests. // Do the admin tests.
$this->doAdminTests($this->admin_user); $this->doAdminTests($this->admin_user);

View File

@ -2733,4 +2733,16 @@ abstract class WebTestBase extends TestBase {
return $this->getAbsoluteUrl($path); return $this->getAbsoluteUrl($path);
} }
} }
/**
* Asserts whether an expected cache tag was present in the last response.
*
* @param string $expected_cache_tag
* The expected cache tag.
*/
protected function assertCacheTag($expected_cache_tag) {
$cache_tags = explode(' ', $this->drupalGetHeader('X-Drupal-Cache-Tags'));
$this->assertTrue(in_array($expected_cache_tag, $cache_tags));
}
} }

View File

@ -8,6 +8,7 @@
namespace Drupal\system\Plugin\Block; namespace Drupal\system\Plugin\Block;
use Drupal\Core\Block\BlockBase; use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
@ -189,11 +190,10 @@ class SystemBrandingBlock extends BlockBase implements ContainerFactoryPluginInt
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCacheTags() { public function getCacheTags() {
// The theme-specific cache tag is set automatically for each block, but the return Cache::mergeTags(
// output of this block also depends on the global theme settings. parent::getCacheTags(),
$cache_tags = parent::getCacheTags(); $this->configFactory->get('system.site')->getCacheTags()
$cache_tags[] = 'theme_global_setting'; );
return $cache_tags;
} }
} }

View File

@ -99,6 +99,7 @@ class PageCacheTagsIntegrationTest extends WebTestBase {
'config:system.menu.tools', 'config:system.menu.tools',
'config:system.menu.footer', 'config:system.menu.footer',
'config:system.menu.main', 'config:system.menu.main',
'config:system.site',
)); ));
// Full node page 2. // Full node page 2.
@ -132,6 +133,7 @@ class PageCacheTagsIntegrationTest extends WebTestBase {
'config:system.menu.tools', 'config:system.menu.tools',
'config:system.menu.footer', 'config:system.menu.footer',
'config:system.menu.main', 'config:system.menu.main',
'config:system.site',
)); ));
} }

View File

@ -74,6 +74,7 @@ abstract class AccountForm extends ContentEntityForm {
$account = $this->entity; $account = $this->entity;
$user = $this->currentUser(); $user = $this->currentUser();
$config = \Drupal::config('user.settings'); $config = \Drupal::config('user.settings');
$form['#cache']['tags'] = $config->getCacheTags();
$language_interface = \Drupal::languageManager()->getCurrentLanguage(); $language_interface = \Drupal::languageManager()->getCurrentLanguage();
$register = $account->isAnonymous(); $register = $account->isAnonymous();

View File

@ -7,6 +7,7 @@
namespace Drupal\user\Form; namespace Drupal\user\Form;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Flood\FloodInterface; use Drupal\Core\Flood\FloodInterface;
use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
@ -78,13 +79,15 @@ class UserLoginForm extends FormBase {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function buildForm(array $form, FormStateInterface $form_state) { public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config('system.site');
// Display login form: // Display login form:
$form['name'] = array( $form['name'] = array(
'#type' => 'textfield', '#type' => 'textfield',
'#title' => $this->t('Username'), '#title' => $this->t('Username'),
'#size' => 60, '#size' => 60,
'#maxlength' => USERNAME_MAX_LENGTH, '#maxlength' => USERNAME_MAX_LENGTH,
'#description' => $this->t('Enter your @s username.', array('@s' => $this->config('system.site')->get('name'))), '#description' => $this->t('Enter your @s username.', array('@s' => $config->get('name'))),
'#required' => TRUE, '#required' => TRUE,
'#attributes' => array( '#attributes' => array(
'autocorrect' => 'off', 'autocorrect' => 'off',
@ -109,6 +112,8 @@ class UserLoginForm extends FormBase {
$form['#validate'][] = '::validateAuthentication'; $form['#validate'][] = '::validateAuthentication';
$form['#validate'][] = '::validateFinal'; $form['#validate'][] = '::validateFinal';
$form['#cache']['tags'] = Cache::mergeTags(isset($form['#cache']['tags']) ? $form['#cache']['tags'] : [], $config->getCacheTags());
return $form; return $form;
} }

View File

@ -16,10 +16,16 @@ use Drupal\Core\Password\PhpassHashedPassword;
* @group user * @group user
*/ */
class UserLoginTest extends WebTestBase { class UserLoginTest extends WebTestBase {
/** /**
* Tests login with destination. * Tests login with destination.
*/ */
function testLoginDestination() { function testLoginCacheTagsAndDestination() {
$this->drupalGet('user/login');
// The user login form says "Enter your <site name> username.", hence it
// depends on config:system.site, and its cache tags should be present.
$this->assertCacheTag('config:system.site');
$user = $this->drupalCreateUser(array()); $user = $this->drupalCreateUser(array());
$this->drupalGet('user/login', array('query' => array('destination' => 'foo'))); $this->drupalGet('user/login', array('query' => array('destination' => 'foo')));
$edit = array('name' => $user->getUserName(), 'pass' => $user->pass_raw); $edit = array('name' => $user->getUserName(), 'pass' => $user->pass_raw);

View File

@ -169,6 +169,9 @@ class UserRegistrationTest extends WebTestBase {
$this->drupalGet('user/register'); $this->drupalGet('user/register');
$this->assertNoRaw('<details id="edit-account"><summary>Account information</summary>'); $this->assertNoRaw('<details id="edit-account"><summary>Account information</summary>');
// Check the presence of expected cache tags.
$this->assertCacheTag('config:user.settings');
$edit = array(); $edit = array();
$edit['name'] = $name = $this->randomMachineName(); $edit['name'] = $name = $this->randomMachineName();
$edit['mail'] = $mail = $edit['name'] . '@example.com'; $edit['mail'] = $mail = $edit['name'] . '@example.com';