3 namespace Drupal\Tests\contact\Functional;
5 use Drupal\Component\Utility\SafeMarkup;
6 use Drupal\Component\Render\PlainTextOutput;
7 use Drupal\Core\Session\AccountInterface;
8 use Drupal\Core\Test\AssertMailTrait;
9 use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait;
10 use Drupal\Tests\BrowserTestBase;
11 use Drupal\user\RoleInterface;
14 * Tests personal contact form functionality.
18 class ContactPersonalTest extends BrowserTestBase {
21 use AssertPageCacheContextsAndTagsTrait;
28 public static $modules = ['contact', 'dblog'];
31 * A user with some administrative permissions.
33 * @var \Drupal\user\UserInterface
38 * A user with permission to view profiles and access user contact forms.
40 * @var \Drupal\user\UserInterface
45 * A user without any permissions.
47 * @var \Drupal\user\UserInterface
51 protected function setUp() {
54 // Create an admin user.
55 $this->adminUser = $this->drupalCreateUser(['administer contact forms', 'administer users', 'administer account settings', 'access site reports']);
57 // Create some normal users with their contact forms enabled by default.
58 $this->config('contact.settings')->set('user_default_enabled', TRUE)->save();
59 $this->webUser = $this->drupalCreateUser(['access user profiles', 'access user contact forms']);
60 $this->contactUser = $this->drupalCreateUser();
64 * Tests that mails for contact messages are correctly sent.
66 public function testSendPersonalContactMessage() {
67 // Ensure that the web user's email needs escaping.
68 $mail = $this->webUser->getUsername() . '&escaped@example.com';
69 $this->webUser->setEmail($mail)->save();
70 $this->drupalLogin($this->webUser);
72 $this->drupalGet('user/' . $this->contactUser->id() . '/contact');
73 $this->assertEscaped($mail);
74 $message = $this->submitPersonalContact($this->contactUser);
75 $mails = $this->getMails();
76 $this->assertEqual(1, count($mails));
78 $this->assertEqual($mail['to'], $this->contactUser->getEmail());
79 $this->assertEqual($mail['from'], $this->config('system.site')->get('mail'));
80 $this->assertEqual($mail['reply-to'], $this->webUser->getEmail());
81 $this->assertEqual($mail['key'], 'user_mail');
83 '@site-name' => $this->config('system.site')->get('name'),
84 '@subject' => $message['subject[0][value]'],
85 '@recipient-name' => $this->contactUser->getDisplayName(),
87 $subject = PlainTextOutput::renderFromHtml(t('[@site-name] @subject', $variables));
88 $this->assertEqual($mail['subject'], $subject, 'Subject is in sent message.');
89 $this->assertTrue(strpos($mail['body'], 'Hello ' . $variables['@recipient-name']) !== FALSE, 'Recipient name is in sent message.');
90 $this->assertTrue(strpos($mail['body'], $this->webUser->getDisplayName()) !== FALSE, 'Sender name is in sent message.');
91 $this->assertTrue(strpos($mail['body'], $message['message[0][value]']) !== FALSE, 'Message body is in sent message.');
93 // Check there was no problems raised during sending.
94 $this->drupalLogout();
95 $this->drupalLogin($this->adminUser);
96 // Verify that the correct watchdog message has been logged.
97 $this->drupalGet('/admin/reports/dblog');
99 '@sender_name' => $this->webUser->username,
100 '@sender_email' => $this->webUser->getEmail(),
101 '@recipient_name' => $this->contactUser->getUsername()
103 $this->assertRaw(SafeMarkup::format('@sender_name (@sender_email) sent @recipient_name an email.', $placeholders));
104 // Ensure an unescaped version of the email does not exist anywhere.
105 $this->assertNoRaw($this->webUser->getEmail());
109 * Tests access to the personal contact form.
111 public function testPersonalContactAccess() {
112 // Test allowed access to admin user's contact form.
113 $this->drupalLogin($this->webUser);
114 $this->drupalGet('user/' . $this->adminUser->id() . '/contact');
115 $this->assertResponse(200);
116 // Check the page title is properly displayed.
117 $this->assertRaw(t('Contact @username', ['@username' => $this->adminUser->getDisplayName()]));
119 // Test denied access to admin user's own contact form.
120 $this->drupalLogout();
121 $this->drupalLogin($this->adminUser);
122 $this->drupalGet('user/' . $this->adminUser->id() . '/contact');
123 $this->assertResponse(403);
125 // Test allowed access to user with contact form enabled.
126 $this->drupalLogin($this->webUser);
127 $this->drupalGet('user/' . $this->contactUser->id() . '/contact');
128 $this->assertResponse(200);
130 // Test that there is no access to personal contact forms for users
131 // without an email address configured.
132 $original_email = $this->contactUser->getEmail();
133 $this->contactUser->setEmail(FALSE)->save();
134 $this->drupalGet('user/' . $this->contactUser->id() . '/contact');
135 $this->assertResponse(404, 'Not found (404) returned when visiting a personal contact form for a user with no email address');
137 // Test that the 'contact tab' does not appear on the user profiles
138 // for users without an email address configured.
139 $this->drupalGet('user/' . $this->contactUser->id());
140 $contact_link = '/user/' . $this->contactUser->id() . '/contact';
141 $this->assertResponse(200);
142 $this->assertNoLinkByHref ($contact_link, 'The "contact" tab is hidden on profiles for users with no email address');
144 // Restore original email address.
145 $this->contactUser->setEmail($original_email)->save();
147 // Test denied access to the user's own contact form.
148 $this->drupalGet('user/' . $this->webUser->id() . '/contact');
149 $this->assertResponse(403);
151 // Test always denied access to the anonymous user contact form.
152 $this->drupalGet('user/0/contact');
153 $this->assertResponse(403);
155 // Test that anonymous users can access the contact form.
156 $this->drupalLogout();
157 user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['access user contact forms']);
158 $this->drupalGet('user/' . $this->contactUser->id() . '/contact');
159 $this->assertResponse(200);
161 // Test that anonymous users can access admin user's contact form.
162 $this->drupalGet('user/' . $this->adminUser->id() . '/contact');
163 $this->assertResponse(200);
164 $this->assertCacheContext('user');
166 // Revoke the personal contact permission for the anonymous user.
167 user_role_revoke_permissions(RoleInterface::ANONYMOUS_ID, ['access user contact forms']);
168 $this->drupalGet('user/' . $this->contactUser->id() . '/contact');
169 $this->assertResponse(403);
170 $this->assertCacheContext('user');
171 $this->drupalGet('user/' . $this->adminUser->id() . '/contact');
172 $this->assertResponse(403);
174 // Disable the personal contact form.
175 $this->drupalLogin($this->adminUser);
176 $edit = ['contact_default_status' => FALSE];
177 $this->drupalPostForm('admin/config/people/accounts', $edit, t('Save configuration'));
178 $this->assertText(t('The configuration options have been saved.'), 'Setting successfully saved.');
179 $this->drupalLogout();
181 // Re-create our contacted user with personal contact forms disabled by
183 $this->contactUser = $this->drupalCreateUser();
185 // Test denied access to a user with contact form disabled.
186 $this->drupalLogin($this->webUser);
187 $this->drupalGet('user/' . $this->contactUser->id() . '/contact');
188 $this->assertResponse(403);
190 // Test allowed access for admin user to a user with contact form disabled.
191 $this->drupalLogin($this->adminUser);
192 $this->drupalGet('user/' . $this->contactUser->id() . '/contact');
193 $this->assertResponse(200);
195 // Re-create our contacted user as a blocked user.
196 $this->contactUser = $this->drupalCreateUser();
197 $this->contactUser->block();
198 $this->contactUser->save();
200 // Test that blocked users can still be contacted by admin.
201 $this->drupalGet('user/' . $this->contactUser->id() . '/contact');
202 $this->assertResponse(200);
204 // Test that blocked users cannot be contacted by non-admins.
205 $this->drupalLogin($this->webUser);
206 $this->drupalGet('user/' . $this->contactUser->id() . '/contact');
207 $this->assertResponse(403);
209 // Test enabling and disabling the contact page through the user profile
211 $this->drupalGet('user/' . $this->webUser->id() . '/edit');
212 $this->assertNoFieldChecked('edit-contact--2');
213 $this->assertFalse(\Drupal::service('user.data')->get('contact', $this->webUser->id(), 'enabled'), 'Personal contact form disabled');
214 $this->drupalPostForm(NULL, ['contact' => TRUE], t('Save'));
215 $this->assertFieldChecked('edit-contact--2');
216 $this->assertTrue(\Drupal::service('user.data')->get('contact', $this->webUser->id(), 'enabled'), 'Personal contact form enabled');
218 // Test with disabled global default contact form in combination with a user
219 // that has the contact form enabled.
220 $this->config('contact.settings')->set('user_default_enabled', FALSE)->save();
221 $this->contactUser = $this->drupalCreateUser();
222 \Drupal::service('user.data')->set('contact', $this->contactUser->id(), 'enabled', 1);
224 $this->drupalGet('user/' . $this->contactUser->id() . '/contact');
225 $this->assertResponse(200);
229 * Tests the personal contact form flood protection.
231 public function testPersonalContactFlood() {
233 $this->config('contact.settings')->set('flood.limit', $flood_limit)->save();
235 $this->drupalLogin($this->webUser);
237 // Submit contact form with correct values and check flood interval.
238 for ($i = 0; $i < $flood_limit; $i++) {
239 $this->submitPersonalContact($this->contactUser);
240 $this->assertText(t('Your message has been sent.'), 'Message sent.');
243 // Submit contact form one over limit.
244 $this->submitPersonalContact($this->contactUser);
245 $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.');
247 // Test that the admin user can still access the contact form even though
248 // the flood limit was reached.
249 $this->drupalLogin($this->adminUser);
250 $this->assertNoText('Try again later.', 'Admin user not denied access to flooded contact form.');
254 * Tests the personal contact form based access when an admin adds users.
256 public function testAdminContact() {
257 user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['access user contact forms']);
258 $this->checkContactAccess(200);
259 $this->checkContactAccess(403, FALSE);
260 $config = $this->config('contact.settings');
261 $config->set('user_default_enabled', FALSE);
263 $this->checkContactAccess(403);
267 * Creates a user and then checks contact form access.
269 * @param int $response
270 * The expected response code.
271 * @param bool $contact_value
272 * (optional) The value the contact field should be set too.
274 protected function checkContactAccess($response, $contact_value = NULL) {
275 $this->drupalLogin($this->adminUser);
276 $this->drupalGet('admin/people/create');
277 if ($this->config('contact.settings')->get('user_default_enabled', TRUE)) {
278 $this->assertFieldChecked('edit-contact--2');
281 $this->assertNoFieldChecked('edit-contact--2');
283 $name = $this->randomMachineName();
286 'mail' => $this->randomMachineName() . '@example.com',
287 'pass[pass1]' => $pass = $this->randomString(),
288 'pass[pass2]' => $pass,
291 if (isset($contact_value)) {
292 $edit['contact'] = $contact_value;
294 $this->drupalPostForm('admin/people/create', $edit, t('Create new account'));
295 $user = user_load_by_name($name);
296 $this->drupalLogout();
298 $this->drupalGet('user/' . $user->id() . '/contact');
299 $this->assertResponse($response);
303 * Fills out a user's personal contact form and submits it.
305 * @param \Drupal\Core\Session\AccountInterface $account
306 * A user object of the user being contacted.
307 * @param array $message
308 * (optional) An array with the form fields being used. Defaults to an empty
312 * An array with the form fields being used.
314 protected function submitPersonalContact(AccountInterface $account, array $message = []) {
316 'subject[0][value]' => $this->randomMachineName(16),
317 'message[0][value]' => $this->randomMachineName(64),
319 $this->drupalPostForm('user/' . $account->id() . '/contact', $message, t('Send message'));