View source
<?php
namespace Drupal\Tests\contact\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Utility\Html;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Test\AssertMailTrait;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait;
use Drupal\user\RoleInterface;
class ContactPersonalTest extends BrowserTestBase {
use AssertMailTrait;
use AssertPageCacheContextsAndTagsTrait;
protected static $modules = [
'contact',
'dblog',
'mail_html_test',
];
protected $defaultTheme = 'stark';
private $adminUser;
private $webUser;
private $contactUser;
protected function setUp() : void {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser([
'administer contact forms',
'administer users',
'administer account settings',
'access site reports',
]);
$this
->config('contact.settings')
->set('user_default_enabled', TRUE)
->save();
$this->webUser = $this
->drupalCreateUser([
'access user profiles',
'access user contact forms',
]);
$this->contactUser = $this
->drupalCreateUser();
}
public function testSendPersonalContactMessage() {
$mail = $this->webUser
->getAccountName() . '&escaped@example.com';
$this->webUser
->setEmail($mail)
->save();
$this
->drupalLogin($this->webUser);
$this
->drupalGet('user/' . $this->contactUser
->id() . '/contact');
$this
->assertSession()
->assertEscaped($mail);
$message = $this
->submitPersonalContact($this->contactUser);
$mails = $this
->getMails();
$this
->assertCount(1, $mails);
$mail = $mails[0];
$this
->assertEquals($this->contactUser
->getEmail(), $mail['to']);
$this
->assertEquals($this
->config('system.site')
->get('mail'), $mail['from']);
$this
->assertEquals($this->webUser
->getEmail(), $mail['reply-to']);
$this
->assertEquals('user_mail', $mail['key']);
$subject = '[' . $this
->config('system.site')
->get('name') . '] ' . $message['subject[0][value]'];
$this
->assertEquals($subject, $mail['subject'], 'Subject is in sent message.');
$this
->assertStringContainsString('Hello ' . $this->contactUser
->getDisplayName(), $mail['body'], 'Recipient name is in sent message.');
$this
->assertStringContainsString($this->webUser
->getDisplayName(), $mail['body'], 'Sender name is in sent message.');
$this
->assertStringContainsString($message['message[0][value]'], $mail['body'], 'Message body is in sent message.');
$this
->drupalLogout();
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('/admin/reports/dblog');
$placeholders = [
'@sender_name' => $this->webUser
->getAccountName(),
'@sender_email' => $this->webUser
->getEmail(),
'@recipient_name' => $this->contactUser
->getAccountName(),
];
$this
->assertSession()
->responseContains(new FormattableMarkup('@sender_name (@sender_email) sent @recipient_name an email.', $placeholders));
$this
->assertSession()
->responseNotContains($this->webUser
->getEmail());
$mail_config = $this
->config('system.mail');
$mail_config
->set('interface.default', 'test_html_mail_collector');
$mail_config
->save();
$this
->drupalLogin($this->webUser);
$message['message[0][value]'] = 'This <i>is</i> a more <b>specific</b> <sup>test</sup>, the emails are formatted now.';
$message = $this
->submitPersonalContact($this->contactUser, $message);
$this
->assertMailString('body', 'Hello ' . $this->contactUser
->getDisplayName(), 1);
$this
->assertMailString('body', $this->webUser
->getDisplayName(), 1);
$this
->assertMailString('body', Html::Escape($message['message[0][value]']), 1);
}
public function testPersonalContactAccess() {
$this
->drupalLogin($this->webUser);
$this
->drupalGet('user/' . $this->adminUser
->id() . '/contact');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains('Contact ' . $this->adminUser
->getDisplayName());
$this
->drupalLogout();
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('user/' . $this->adminUser
->id() . '/contact');
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalLogin($this->webUser);
$this
->drupalGet('user/' . $this->contactUser
->id() . '/contact');
$this
->assertSession()
->statusCodeEquals(200);
$original_email = $this->contactUser
->getEmail();
$this->contactUser
->setEmail(FALSE)
->save();
$this
->drupalGet('user/' . $this->contactUser
->id() . '/contact');
$this
->assertSession()
->statusCodeEquals(404);
$this
->drupalGet('user/' . $this->contactUser
->id());
$contact_link = '/user/' . $this->contactUser
->id() . '/contact';
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->linkByHrefNotExists($contact_link, 'The "contact" tab is hidden on profiles for users with no email address');
$this->contactUser
->setEmail($original_email)
->save();
$this
->drupalGet('user/' . $this->webUser
->id() . '/contact');
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalGet('user/0/contact');
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalLogout();
user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, [
'access user contact forms',
]);
$this
->drupalGet('user/' . $this->contactUser
->id() . '/contact');
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet('user/' . $this->adminUser
->id() . '/contact');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertCacheContext('user');
user_role_revoke_permissions(RoleInterface::ANONYMOUS_ID, [
'access user contact forms',
]);
$this
->drupalGet('user/' . $this->contactUser
->id() . '/contact');
$this
->assertSession()
->statusCodeEquals(403);
$this
->assertCacheContext('user');
$this
->drupalGet('user/' . $this->adminUser
->id() . '/contact');
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalLogin($this->adminUser);
$edit = [
'contact_default_status' => FALSE,
];
$this
->drupalGet('admin/config/people/accounts');
$this
->submitForm($edit, 'Save configuration');
$this
->assertSession()
->pageTextContains('The configuration options have been saved.');
$this
->drupalLogout();
$this->contactUser = $this
->drupalCreateUser();
$this
->drupalLogin($this->webUser);
$this
->drupalGet('user/' . $this->contactUser
->id() . '/contact');
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('user/' . $this->contactUser
->id() . '/contact');
$this
->assertSession()
->statusCodeEquals(200);
$this->contactUser = $this
->drupalCreateUser();
$this->contactUser
->block();
$this->contactUser
->save();
$this
->drupalGet('user/' . $this->contactUser
->id() . '/contact');
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalLogin($this->webUser);
$this
->drupalGet('user/' . $this->contactUser
->id() . '/contact');
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalGet('user/' . $this->webUser
->id() . '/edit');
$this
->assertSession()
->checkboxNotChecked('edit-contact--2');
$this
->assertNull(\Drupal::service('user.data')
->get('contact', $this->webUser
->id(), 'enabled'), 'Personal contact form disabled');
$this
->submitForm([
'contact' => TRUE,
], 'Save');
$this
->assertSession()
->checkboxChecked('edit-contact--2');
$this
->assertNotEmpty(\Drupal::service('user.data')
->get('contact', $this->webUser
->id(), 'enabled'), 'Personal contact form enabled');
$this
->config('contact.settings')
->set('user_default_enabled', FALSE)
->save();
$this->contactUser = $this
->drupalCreateUser();
\Drupal::service('user.data')
->set('contact', $this->contactUser
->id(), 'enabled', 1);
$this
->drupalGet('user/' . $this->contactUser
->id() . '/contact');
$this
->assertSession()
->statusCodeEquals(200);
}
public function testPersonalContactFlood() {
$flood_limit = 3;
$this
->config('contact.settings')
->set('flood.limit', $flood_limit)
->save();
$this
->drupalLogin($this->webUser);
for ($i = 0; $i < $flood_limit; $i++) {
$this
->submitPersonalContact($this->contactUser);
$this
->assertSession()
->pageTextContains('Your message has been sent.');
}
$this
->submitPersonalContact($this->contactUser);
$interval = \Drupal::service('date.formatter')
->formatInterval($this
->config('contact.settings')
->get('flood.interval'));
$this
->assertSession()
->pageTextContains("You cannot send more than 3 messages in {$interval}. Try again later.");
$this
->drupalLogin($this->adminUser);
$this
->assertSession()
->pageTextNotContains('Try again later.');
}
public function testAdminContact() {
user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, [
'access user contact forms',
]);
$this
->checkContactAccess(200);
$this
->checkContactAccess(403, FALSE);
$config = $this
->config('contact.settings');
$config
->set('user_default_enabled', FALSE);
$config
->save();
$this
->checkContactAccess(403);
}
protected function checkContactAccess($response, $contact_value = NULL) {
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/people/create');
if ($this
->config('contact.settings')
->get('user_default_enabled', TRUE)) {
$this
->assertSession()
->checkboxChecked('edit-contact--2');
}
else {
$this
->assertSession()
->checkboxNotChecked('edit-contact--2');
}
$name = $this
->randomMachineName();
$edit = [
'name' => $name,
'mail' => $this
->randomMachineName() . '@example.com',
'pass[pass1]' => $pass = $this
->randomString(),
'pass[pass2]' => $pass,
'notify' => FALSE,
];
if (isset($contact_value)) {
$edit['contact'] = $contact_value;
}
$this
->drupalGet('admin/people/create');
$this
->submitForm($edit, 'Create new account');
$user = user_load_by_name($name);
$this
->drupalLogout();
$this
->drupalGet('user/' . $user
->id() . '/contact');
$this
->assertSession()
->statusCodeEquals($response);
}
protected function submitPersonalContact(AccountInterface $account, array $message = []) {
$message += [
'subject[0][value]' => $this
->randomMachineName(16) . '< " =+ >',
'message[0][value]' => $this
->randomMachineName(64) . '< " =+ >',
];
$this
->drupalGet('user/' . $account
->id() . '/contact');
$this
->submitForm($message, 'Send message');
return $message;
}
}