Commit 468646e3 authored by catch's avatar catch

Issue #2407765 by Wim Leers, borisson_: Wherever simple config is used to...

Issue #2407765 by Wim Leers, borisson_: Wherever simple config is used to render output to end user, associate their cache tags
parent fa29e0b5
......@@ -51,6 +51,7 @@ public function testSystemBrandingSettings() {
$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_slogan_element), 'The branding block slogan was found.');
$this->assertCacheTag('config:system.site');
// Turn just the logo off.
$this->config('block.block.site-branding')
......@@ -64,6 +65,7 @@ public function testSystemBrandingSettings() {
$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_slogan_element), 'The branding block slogan was found.');
$this->assertCacheTag('config:system.site');
// Turn just the site name off.
$this->config('block.block.site-branding')
......@@ -78,6 +80,7 @@ public function testSystemBrandingSettings() {
$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_slogan_element), 'The branding block slogan was found.');
$this->assertCacheTag('config:system.site');
// Turn just the site slogan off.
$this->config('block.block.site-branding')
......@@ -92,6 +95,7 @@ public function testSystemBrandingSettings() {
$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_slogan_element), 'The branding block slogan was disabled.');
$this->assertCacheTag('config:system.site');
// Turn the site name and the site slogan off.
$this->config('block.block.site-branding')
......@@ -106,6 +110,7 @@ public function testSystemBrandingSettings() {
$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_slogan_element), 'The branding block slogan was disabled.');
$this->assertCacheTag('config:system.site');
}
}
......@@ -317,7 +317,8 @@ public function getBookParents(array $item, array $parent = array()) {
* A parent selection form element.
*/
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();
}
// Offer a message or a drop-down to choose a different parent page.
......@@ -352,6 +353,7 @@ protected function addParentSelectFormElements(array $book_link) {
'#suffix' => '</div>',
);
}
$form['#cache']['tags'] = Cache::mergeTags(isset($form['#cache']['tags']) ? $form['#cache']['tags'] : [], $config->getCacheTags());
return $form;
}
......
......@@ -143,6 +143,11 @@ function testBook() {
$this->drupalLogout();
$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.
* Book
......@@ -153,8 +158,6 @@ function testBook() {
* |- Node 5
* |- Node 4
*/
$nodes[] = $this->createBookNode($book->id(), $nodes[3]->book['nid']); // Node 5.
$this->drupalLogout();
$this->drupalLogin($this->webUser);
......
......@@ -10,6 +10,7 @@
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\Component\Utility\String;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Entity\EntityManagerInterface;
......@@ -78,6 +79,7 @@ public function form(array $form, FormStateInterface $form_state) {
$entity = $this->entityManager->getStorage($comment->getCommentedEntityTypeId())->load($comment->getCommentedEntityId());
$field_name = $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.
$form['#id'] = drupal_html_id('comment_form');
......@@ -147,7 +149,7 @@ public function form(array $form, FormStateInterface $form_state) {
);
if ($is_admin) {
$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';
}
elseif ($this->currentUser->isAuthenticated()) {
......@@ -157,7 +159,7 @@ public function form(array $form, FormStateInterface $form_state) {
$form['author']['name']['#account'] = $this->currentUser;
}
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.
......@@ -201,6 +203,8 @@ public function form(array $form, FormStateInterface $form_state) {
'#access' => $is_admin,
);
$form['#cache']['tags'] = Cache::mergeTags(isset($form['#cache']['tags']) ? $form['#cache']['tags'] : [], $config->getCacheTags());
return parent::form($form, $form_state, $comment);
}
......
......@@ -7,6 +7,7 @@
namespace Drupal\comment;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityViewBuilder;
......@@ -102,7 +103,9 @@ public function buildComponents(array &$build, array $entities, array $displays,
}
$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(
'#type' => 'processed_text',
'#text' => $account->getSignature(),
......
......@@ -56,6 +56,9 @@ function testAnonymous() {
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
$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());
$this->assertTrue($this->commentExists($anonymous_comment2), 'Anonymous comment with contact info (optional) found.');
......
......@@ -110,6 +110,7 @@ public function testCacheTags() {
'comment_view',
'comment:' . $comment->id(),
'config:filter.format.plain_text',
'config:user.settings',
'user_view',
'user:2',
);
......
......@@ -7,6 +7,7 @@
namespace Drupal\contact\Controller;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Datetime\DateFormatter;
use Drupal\Core\Flood\FloodInterface;
......@@ -77,12 +78,13 @@ public function contactSitePage(ContactFormInterface $contact_form = NULL) {
if (!$this->currentUser()->hasPermission('administer contact forms')) {
$this->contactFloodControl();
}
$config = $this->config('contact.settings');
// Use the default form if no form has been passed.
if (empty($contact_form)) {
$contact_form = $this->entityManager()
->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 (empty($contact_form)) {
if ($this->currentUser()->hasPermission('administer contact forms')) {
......@@ -104,6 +106,7 @@ public function contactSitePage(ContactFormInterface $contact_form = NULL) {
$form = $this->entityFormBuilder()->getForm($message);
$form['#title'] = String::checkPlain($contact_form->label());
$form['#cache']['tags'] = Cache::mergeTags(isset($form['#cache']['tags']) ? $form['#cache']['tags'] : [], $config->getCacheTags());
return $form;
}
......
......@@ -54,6 +54,10 @@ function testSiteWideContact() {
));
$this->drupalLogin($admin_user);
// Check the presence of expected cache tags.
$this->drupalGet('contact');
$this->assertCacheTag('config:contact.settings');
$flood_limit = 3;
$this->config('contact.settings')
->set('flood.limit', $flood_limit)
......
......@@ -7,6 +7,7 @@
namespace Drupal\forum\Controller;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityAccessControlHandlerInterface;
use Drupal\Core\Entity\EntityStorageInterface;
......@@ -190,6 +191,7 @@ protected function build($forums, TermInterface $term, $topics = array(), $paren
if (empty($term->forum_container->value)) {
$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 [
'action' => $this->buildActionLinks($config->get('vocabulary'), $term),
......
......@@ -119,6 +119,12 @@ function testForum() {
// 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");
// 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.
$this->doAdminTests($this->admin_user);
......
......@@ -2733,4 +2733,16 @@ protected function buildUrl($path, array $options = array()) {
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));
}
}
......@@ -8,6 +8,7 @@
namespace Drupal\system\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
......@@ -189,11 +190,10 @@ public function build() {
* {@inheritdoc}
*/
public function getCacheTags() {
// The theme-specific cache tag is set automatically for each block, but the
// output of this block also depends on the global theme settings.
$cache_tags = parent::getCacheTags();
$cache_tags[] = 'theme_global_setting';
return $cache_tags;
return Cache::mergeTags(
parent::getCacheTags(),
$this->configFactory->get('system.site')->getCacheTags()
);
}
}
......@@ -99,6 +99,7 @@ function testPageCacheTags() {
'config:system.menu.tools',
'config:system.menu.footer',
'config:system.menu.main',
'config:system.site',
));
// Full node page 2.
......@@ -132,6 +133,7 @@ function testPageCacheTags() {
'config:system.menu.tools',
'config:system.menu.footer',
'config:system.menu.main',
'config:system.site',
));
}
......
......@@ -74,6 +74,7 @@ public function form(array $form, FormStateInterface $form_state) {
$account = $this->entity;
$user = $this->currentUser();
$config = \Drupal::config('user.settings');
$form['#cache']['tags'] = $config->getCacheTags();
$language_interface = \Drupal::languageManager()->getCurrentLanguage();
$register = $account->isAnonymous();
......
......@@ -7,6 +7,7 @@
namespace Drupal\user\Form;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Flood\FloodInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
......@@ -78,13 +79,15 @@ public function getFormId() {
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config('system.site');
// Display login form:
$form['name'] = array(
'#type' => 'textfield',
'#title' => $this->t('Username'),
'#size' => 60,
'#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,
'#attributes' => array(
'autocorrect' => 'off',
......@@ -109,6 +112,8 @@ public function buildForm(array $form, FormStateInterface $form_state) {
$form['#validate'][] = '::validateAuthentication';
$form['#validate'][] = '::validateFinal';
$form['#cache']['tags'] = Cache::mergeTags(isset($form['#cache']['tags']) ? $form['#cache']['tags'] : [], $config->getCacheTags());
return $form;
}
......
......@@ -16,10 +16,16 @@
* @group user
*/
class UserLoginTest extends WebTestBase {
/**
* 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());
$this->drupalGet('user/login', array('query' => array('destination' => 'foo')));
$edit = array('name' => $user->getUserName(), 'pass' => $user->pass_raw);
......
......@@ -169,6 +169,9 @@ function testRegistrationDefaultValues() {
$this->drupalGet('user/register');
$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['name'] = $name = $this->randomMachineName();
$edit['mail'] = $mail = $edit['name'] . '@example.com';
......
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