core/modules/contact/src/MessageForm.php | 3 +++ .../contact/src/Tests/ContactAuthenticatedUserTest.php | 14 ++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/core/modules/contact/src/MessageForm.php b/core/modules/contact/src/MessageForm.php index 9cb5e54..c576a56 100644 --- a/core/modules/contact/src/MessageForm.php +++ b/core/modules/contact/src/MessageForm.php @@ -120,6 +120,7 @@ public function form(array $form, FormStateInterface $form_state) { '#title' => $this->t('Your email address'), '#required' => TRUE, ); + $form['#cache']['contexts'][] = 'user.roles:authenticated'; if ($user->isAnonymous()) { $form['#attached']['library'][] = 'core/drupal.form'; $form['#attributes']['data-user-info-from-browser'] = TRUE; @@ -131,11 +132,13 @@ public function form(array $form, FormStateInterface $form_state) { $form['name']['#value'] = $user->getUsername(); $form['name']['#required'] = FALSE; $form['name']['#plain_text'] = $user->getUsername(); + $form['name']['#cache']['contexts'][] = 'user'; $form['mail']['#type'] = 'item'; $form['mail']['#value'] = $user->getEmail(); $form['mail']['#required'] = FALSE; $form['mail']['#plain_text'] = $user->getEmail(); + $form['mail']['#cache']['contexts'][] = 'user'; } // The user contact form has a preset recipient. diff --git a/core/modules/contact/src/Tests/ContactAuthenticatedUserTest.php b/core/modules/contact/src/Tests/ContactAuthenticatedUserTest.php index 2adc4c2..923878e 100644 --- a/core/modules/contact/src/Tests/ContactAuthenticatedUserTest.php +++ b/core/modules/contact/src/Tests/ContactAuthenticatedUserTest.php @@ -21,19 +21,29 @@ class ContactAuthenticatedUserTest extends WebTestBase { * * @var array */ - public static $modules = array('contact'); + public static $modules = array('contact', 'contact_test'); /** * Tests that name and email fields are not present for authenticated users. */ function testContactSiteWideTextfieldsLoggedInTestCase() { - $this->drupalLogin($this->drupalCreateUser(array('access site-wide contact form'))); + $user1 = $this->drupalCreateUser(array('access site-wide contact form')); + $this->drupalLogin($user1); $this->drupalGet('contact'); + $this->assertResponse(200); + $this->assertCacheContext('user'); // Ensure that there is no textfield for name. $this->assertFalse($this->xpath('//input[@name=:name]', array(':name' => 'name'))); + $this->assertRaw($user1->getAccountName()); // Ensure that there is no textfield for email. $this->assertFalse($this->xpath('//input[@name=:name]', array(':name' => 'mail'))); + + // Log in as a different user and confirm that + $user2 = $this->drupalCreateUser(array('access site-wide contact form')); + $this->drupalLogin($user2); + $this->drupalGet('contact'); + $this->assertRaw($user2->getAccountName()); } }