Chris@0: adminUser = $this->drupalCreateUser(['administer contact forms', 'administer users', 'administer account settings', 'access site reports']); Chris@0: Chris@0: // Create some normal users with their contact forms enabled by default. Chris@0: $this->config('contact.settings')->set('user_default_enabled', TRUE)->save(); Chris@0: $this->webUser = $this->drupalCreateUser(['access user profiles', 'access user contact forms']); Chris@0: $this->contactUser = $this->drupalCreateUser(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that mails for contact messages are correctly sent. Chris@0: */ Chris@0: public function testSendPersonalContactMessage() { Chris@0: // Ensure that the web user's email needs escaping. Chris@18: $mail = $this->webUser->getAccountName() . '&escaped@example.com'; Chris@0: $this->webUser->setEmail($mail)->save(); Chris@0: $this->drupalLogin($this->webUser); Chris@0: Chris@0: $this->drupalGet('user/' . $this->contactUser->id() . '/contact'); Chris@0: $this->assertEscaped($mail); Chris@0: $message = $this->submitPersonalContact($this->contactUser); Chris@0: $mails = $this->getMails(); Chris@0: $this->assertEqual(1, count($mails)); Chris@0: $mail = $mails[0]; Chris@0: $this->assertEqual($mail['to'], $this->contactUser->getEmail()); Chris@0: $this->assertEqual($mail['from'], $this->config('system.site')->get('mail')); Chris@0: $this->assertEqual($mail['reply-to'], $this->webUser->getEmail()); Chris@0: $this->assertEqual($mail['key'], 'user_mail'); Chris@0: $variables = [ Chris@0: '@site-name' => $this->config('system.site')->get('name'), Chris@0: '@subject' => $message['subject[0][value]'], Chris@0: '@recipient-name' => $this->contactUser->getDisplayName(), Chris@0: ]; Chris@0: $subject = PlainTextOutput::renderFromHtml(t('[@site-name] @subject', $variables)); Chris@0: $this->assertEqual($mail['subject'], $subject, 'Subject is in sent message.'); Chris@0: $this->assertTrue(strpos($mail['body'], 'Hello ' . $variables['@recipient-name']) !== FALSE, 'Recipient name is in sent message.'); Chris@0: $this->assertTrue(strpos($mail['body'], $this->webUser->getDisplayName()) !== FALSE, 'Sender name is in sent message.'); Chris@0: $this->assertTrue(strpos($mail['body'], $message['message[0][value]']) !== FALSE, 'Message body is in sent message.'); Chris@0: Chris@0: // Check there was no problems raised during sending. Chris@0: $this->drupalLogout(); Chris@0: $this->drupalLogin($this->adminUser); Chris@0: // Verify that the correct watchdog message has been logged. Chris@0: $this->drupalGet('/admin/reports/dblog'); Chris@0: $placeholders = [ Chris@0: '@sender_name' => $this->webUser->username, Chris@0: '@sender_email' => $this->webUser->getEmail(), Chris@18: '@recipient_name' => $this->contactUser->getAccountName(), Chris@0: ]; Chris@17: $this->assertRaw(new FormattableMarkup('@sender_name (@sender_email) sent @recipient_name an email.', $placeholders)); Chris@0: // Ensure an unescaped version of the email does not exist anywhere. Chris@0: $this->assertNoRaw($this->webUser->getEmail()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests access to the personal contact form. Chris@0: */ Chris@0: public function testPersonalContactAccess() { Chris@0: // Test allowed access to admin user's contact form. Chris@0: $this->drupalLogin($this->webUser); Chris@0: $this->drupalGet('user/' . $this->adminUser->id() . '/contact'); Chris@0: $this->assertResponse(200); Chris@0: // Check the page title is properly displayed. Chris@0: $this->assertRaw(t('Contact @username', ['@username' => $this->adminUser->getDisplayName()])); Chris@0: Chris@0: // Test denied access to admin user's own contact form. Chris@0: $this->drupalLogout(); Chris@0: $this->drupalLogin($this->adminUser); Chris@0: $this->drupalGet('user/' . $this->adminUser->id() . '/contact'); Chris@0: $this->assertResponse(403); Chris@0: Chris@0: // Test allowed access to user with contact form enabled. Chris@0: $this->drupalLogin($this->webUser); Chris@0: $this->drupalGet('user/' . $this->contactUser->id() . '/contact'); Chris@0: $this->assertResponse(200); Chris@0: Chris@0: // Test that there is no access to personal contact forms for users Chris@0: // without an email address configured. Chris@0: $original_email = $this->contactUser->getEmail(); Chris@0: $this->contactUser->setEmail(FALSE)->save(); Chris@0: $this->drupalGet('user/' . $this->contactUser->id() . '/contact'); Chris@0: $this->assertResponse(404, 'Not found (404) returned when visiting a personal contact form for a user with no email address'); Chris@0: Chris@0: // Test that the 'contact tab' does not appear on the user profiles Chris@0: // for users without an email address configured. Chris@0: $this->drupalGet('user/' . $this->contactUser->id()); Chris@0: $contact_link = '/user/' . $this->contactUser->id() . '/contact'; Chris@0: $this->assertResponse(200); Chris@0: $this->assertNoLinkByHref($contact_link, 'The "contact" tab is hidden on profiles for users with no email address'); Chris@0: Chris@0: // Restore original email address. Chris@0: $this->contactUser->setEmail($original_email)->save(); Chris@0: Chris@0: // Test denied access to the user's own contact form. Chris@0: $this->drupalGet('user/' . $this->webUser->id() . '/contact'); Chris@0: $this->assertResponse(403); Chris@0: Chris@0: // Test always denied access to the anonymous user contact form. Chris@0: $this->drupalGet('user/0/contact'); Chris@0: $this->assertResponse(403); Chris@0: Chris@0: // Test that anonymous users can access the contact form. Chris@0: $this->drupalLogout(); Chris@0: user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['access user contact forms']); Chris@0: $this->drupalGet('user/' . $this->contactUser->id() . '/contact'); Chris@0: $this->assertResponse(200); Chris@0: Chris@0: // Test that anonymous users can access admin user's contact form. Chris@0: $this->drupalGet('user/' . $this->adminUser->id() . '/contact'); Chris@0: $this->assertResponse(200); Chris@0: $this->assertCacheContext('user'); Chris@0: Chris@0: // Revoke the personal contact permission for the anonymous user. Chris@0: user_role_revoke_permissions(RoleInterface::ANONYMOUS_ID, ['access user contact forms']); Chris@0: $this->drupalGet('user/' . $this->contactUser->id() . '/contact'); Chris@0: $this->assertResponse(403); Chris@0: $this->assertCacheContext('user'); Chris@0: $this->drupalGet('user/' . $this->adminUser->id() . '/contact'); Chris@0: $this->assertResponse(403); Chris@0: Chris@0: // Disable the personal contact form. Chris@0: $this->drupalLogin($this->adminUser); Chris@0: $edit = ['contact_default_status' => FALSE]; Chris@0: $this->drupalPostForm('admin/config/people/accounts', $edit, t('Save configuration')); Chris@0: $this->assertText(t('The configuration options have been saved.'), 'Setting successfully saved.'); Chris@0: $this->drupalLogout(); Chris@0: Chris@0: // Re-create our contacted user with personal contact forms disabled by Chris@0: // default. Chris@0: $this->contactUser = $this->drupalCreateUser(); Chris@0: Chris@0: // Test denied access to a user with contact form disabled. Chris@0: $this->drupalLogin($this->webUser); Chris@0: $this->drupalGet('user/' . $this->contactUser->id() . '/contact'); Chris@0: $this->assertResponse(403); Chris@0: Chris@0: // Test allowed access for admin user to a user with contact form disabled. Chris@0: $this->drupalLogin($this->adminUser); Chris@0: $this->drupalGet('user/' . $this->contactUser->id() . '/contact'); Chris@0: $this->assertResponse(200); Chris@0: Chris@0: // Re-create our contacted user as a blocked user. Chris@0: $this->contactUser = $this->drupalCreateUser(); Chris@0: $this->contactUser->block(); Chris@0: $this->contactUser->save(); Chris@0: Chris@0: // Test that blocked users can still be contacted by admin. Chris@0: $this->drupalGet('user/' . $this->contactUser->id() . '/contact'); Chris@0: $this->assertResponse(200); Chris@0: Chris@0: // Test that blocked users cannot be contacted by non-admins. Chris@0: $this->drupalLogin($this->webUser); Chris@0: $this->drupalGet('user/' . $this->contactUser->id() . '/contact'); Chris@0: $this->assertResponse(403); Chris@0: Chris@0: // Test enabling and disabling the contact page through the user profile Chris@0: // form. Chris@0: $this->drupalGet('user/' . $this->webUser->id() . '/edit'); Chris@0: $this->assertNoFieldChecked('edit-contact--2'); Chris@0: $this->assertFalse(\Drupal::service('user.data')->get('contact', $this->webUser->id(), 'enabled'), 'Personal contact form disabled'); Chris@0: $this->drupalPostForm(NULL, ['contact' => TRUE], t('Save')); Chris@0: $this->assertFieldChecked('edit-contact--2'); Chris@0: $this->assertTrue(\Drupal::service('user.data')->get('contact', $this->webUser->id(), 'enabled'), 'Personal contact form enabled'); Chris@0: Chris@0: // Test with disabled global default contact form in combination with a user Chris@0: // that has the contact form enabled. Chris@0: $this->config('contact.settings')->set('user_default_enabled', FALSE)->save(); Chris@0: $this->contactUser = $this->drupalCreateUser(); Chris@0: \Drupal::service('user.data')->set('contact', $this->contactUser->id(), 'enabled', 1); Chris@0: Chris@0: $this->drupalGet('user/' . $this->contactUser->id() . '/contact'); Chris@0: $this->assertResponse(200); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the personal contact form flood protection. Chris@0: */ Chris@0: public function testPersonalContactFlood() { Chris@0: $flood_limit = 3; Chris@0: $this->config('contact.settings')->set('flood.limit', $flood_limit)->save(); Chris@0: Chris@0: $this->drupalLogin($this->webUser); Chris@0: Chris@0: // Submit contact form with correct values and check flood interval. Chris@0: for ($i = 0; $i < $flood_limit; $i++) { Chris@0: $this->submitPersonalContact($this->contactUser); Chris@0: $this->assertText(t('Your message has been sent.'), 'Message sent.'); Chris@0: } Chris@0: Chris@0: // Submit contact form one over limit. Chris@0: $this->submitPersonalContact($this->contactUser); Chris@0: $this->assertRaw(t('You cannot send more than %number messages in @interval. Try again later.', ['%number' => $flood_limit, '@interval' => \Drupal::service('date.formatter')->formatInterval($this->config('contact.settings')->get('flood.interval'))]), 'Normal user denied access to flooded contact form.'); Chris@0: Chris@0: // Test that the admin user can still access the contact form even though Chris@0: // the flood limit was reached. Chris@0: $this->drupalLogin($this->adminUser); Chris@0: $this->assertNoText('Try again later.', 'Admin user not denied access to flooded contact form.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the personal contact form based access when an admin adds users. Chris@0: */ Chris@0: public function testAdminContact() { Chris@0: user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['access user contact forms']); Chris@0: $this->checkContactAccess(200); Chris@0: $this->checkContactAccess(403, FALSE); Chris@0: $config = $this->config('contact.settings'); Chris@0: $config->set('user_default_enabled', FALSE); Chris@0: $config->save(); Chris@0: $this->checkContactAccess(403); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Creates a user and then checks contact form access. Chris@0: * Chris@0: * @param int $response Chris@0: * The expected response code. Chris@0: * @param bool $contact_value Chris@0: * (optional) The value the contact field should be set too. Chris@0: */ Chris@0: protected function checkContactAccess($response, $contact_value = NULL) { Chris@0: $this->drupalLogin($this->adminUser); Chris@0: $this->drupalGet('admin/people/create'); Chris@0: if ($this->config('contact.settings')->get('user_default_enabled', TRUE)) { Chris@0: $this->assertFieldChecked('edit-contact--2'); Chris@0: } Chris@0: else { Chris@0: $this->assertNoFieldChecked('edit-contact--2'); Chris@0: } Chris@0: $name = $this->randomMachineName(); Chris@0: $edit = [ Chris@0: 'name' => $name, Chris@0: 'mail' => $this->randomMachineName() . '@example.com', Chris@0: 'pass[pass1]' => $pass = $this->randomString(), Chris@0: 'pass[pass2]' => $pass, Chris@0: 'notify' => FALSE, Chris@0: ]; Chris@0: if (isset($contact_value)) { Chris@0: $edit['contact'] = $contact_value; Chris@0: } Chris@0: $this->drupalPostForm('admin/people/create', $edit, t('Create new account')); Chris@0: $user = user_load_by_name($name); Chris@0: $this->drupalLogout(); Chris@0: Chris@0: $this->drupalGet('user/' . $user->id() . '/contact'); Chris@0: $this->assertResponse($response); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Fills out a user's personal contact form and submits it. Chris@0: * Chris@0: * @param \Drupal\Core\Session\AccountInterface $account Chris@0: * A user object of the user being contacted. Chris@0: * @param array $message Chris@0: * (optional) An array with the form fields being used. Defaults to an empty Chris@0: * array. Chris@0: * Chris@0: * @return array Chris@0: * An array with the form fields being used. Chris@0: */ Chris@0: protected function submitPersonalContact(AccountInterface $account, array $message = []) { Chris@0: $message += [ Chris@0: 'subject[0][value]' => $this->randomMachineName(16), Chris@0: 'message[0][value]' => $this->randomMachineName(64), Chris@0: ]; Chris@0: $this->drupalPostForm('user/' . $account->id() . '/contact', $message, t('Send message')); Chris@0: return $message; Chris@0: } Chris@0: Chris@0: }