View source
<?php
namespace Drupal\contact\Tests;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Component\Render\PlainTextOutput;
use Drupal\Core\Session\AccountInterface;
use Drupal\simpletest\WebTestBase;
use Drupal\user\RoleInterface;
class ContactPersonalTest extends WebTestBase {
public static $modules = array(
'contact',
'dblog',
);
private $adminUser;
private $webUser;
private $contactUser;
protected function setUp() {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser(array(
'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(array(
'access user profiles',
'access user contact forms',
));
$this->contactUser = $this
->drupalCreateUser();
}
function testSendPersonalContactMessage() {
$mail = $this->webUser
->getUsername() . '&escaped@example.com';
$this->webUser
->setEmail($mail)
->save();
$this
->drupalLogin($this->webUser);
$this
->drupalGet('user/' . $this->contactUser
->id() . '/contact');
$this
->assertEscaped($mail);
$message = $this
->submitPersonalContact($this->contactUser);
$mails = $this
->drupalGetMails();
$this
->assertEqual(1, count($mails));
$mail = $mails[0];
$this
->assertEqual($mail['to'], $this->contactUser
->getEmail());
$this
->assertEqual($mail['from'], $this
->config('system.site')
->get('mail'));
$this
->assertEqual($mail['reply-to'], $this->webUser
->getEmail());
$this
->assertEqual($mail['key'], 'user_mail');
$variables = array(
'@site-name' => $this
->config('system.site')
->get('name'),
'@subject' => $message['subject[0][value]'],
'@recipient-name' => $this->contactUser
->getUsername(),
);
$subject = PlainTextOutput::renderFromHtml(t('[@site-name] @subject', $variables));
$this
->assertEqual($mail['subject'], $subject, 'Subject is in sent message.');
$this
->assertTrue(strpos($mail['body'], 'Hello ' . $variables['@recipient-name']) !== FALSE, 'Recipient name is in sent message.');
$this
->assertTrue(strpos($mail['body'], $this->webUser
->getUsername()) !== FALSE, 'Sender name is in sent message.');
$this
->assertTrue(strpos($mail['body'], $message['message[0][value]']) !== FALSE, 'Message body is in sent message.');
$this
->drupalLogout();
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('/admin/reports/dblog');
$placeholders = array(
'@sender_name' => $this->webUser->username,
'@sender_email' => $this->webUser
->getEmail(),
'@recipient_name' => $this->contactUser
->getUsername(),
);
$this
->assertRaw(SafeMarkup::format('@sender_name (@sender_email) sent @recipient_name an email.', $placeholders));
$this
->assertNoRaw($this->webUser
->getEmail());
}
function testPersonalContactAccess() {
$this
->drupalLogin($this->webUser);
$this
->drupalGet('user/' . $this->adminUser
->id() . '/contact');
$this
->assertResponse(200);
$this
->assertRaw(t('Contact @username', array(
'@username' => $this->adminUser
->getUsername(),
)));
$this
->drupalLogout();
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('user/' . $this->adminUser
->id() . '/contact');
$this
->assertResponse(403);
$this
->drupalLogin($this->webUser);
$this
->drupalGet('user/' . $this->contactUser
->id() . '/contact');
$this
->assertResponse(200);
$original_email = $this->contactUser
->getEmail();
$this->contactUser
->setEmail(FALSE)
->save();
$this
->drupalGet('user/' . $this->contactUser
->id() . '/contact');
$this
->assertResponse(404, 'Not found (404) returned when visiting a personal contact form for a user with no email address');
$this
->drupalGet('user/' . $this->contactUser
->id());
$contact_link = '/user/' . $this->contactUser
->id() . '/contact';
$this
->assertResponse(200);
$this
->assertNoLinkByHref($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
->assertResponse(403);
$this
->drupalGet('user/0/contact');
$this
->assertResponse(403);
$this
->drupalLogout();
user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, array(
'access user contact forms',
));
$this
->drupalGet('user/' . $this->contactUser
->id() . '/contact');
$this
->assertResponse(200);
$this
->drupalGet('user/' . $this->adminUser
->id() . '/contact');
$this
->assertResponse(200);
$this
->assertCacheContext('user');
user_role_revoke_permissions(RoleInterface::ANONYMOUS_ID, array(
'access user contact forms',
));
$this
->drupalGet('user/' . $this->contactUser
->id() . '/contact');
$this
->assertResponse(403);
$this
->assertCacheContext('user');
$this
->drupalGet('user/' . $this->adminUser
->id() . '/contact');
$this
->assertResponse(403);
$this
->drupalLogin($this->adminUser);
$edit = array(
'contact_default_status' => FALSE,
);
$this
->drupalPostForm('admin/config/people/accounts', $edit, t('Save configuration'));
$this
->assertText(t('The configuration options have been saved.'), 'Setting successfully saved.');
$this
->drupalLogout();
$this->contactUser = $this
->drupalCreateUser();
$this
->drupalLogin($this->webUser);
$this
->drupalGet('user/' . $this->contactUser
->id() . '/contact');
$this
->assertResponse(403);
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('user/' . $this->contactUser
->id() . '/contact');
$this
->assertResponse(200);
$this->contactUser = $this
->drupalCreateUser();
$this->contactUser
->block();
$this->contactUser
->save();
$this
->drupalGet('user/' . $this->contactUser
->id() . '/contact');
$this
->assertResponse(200);
$this
->drupalLogin($this->webUser);
$this
->drupalGet('user/' . $this->contactUser
->id() . '/contact');
$this
->assertResponse(403);
$this
->drupalGet('user/' . $this->webUser
->id() . '/edit');
$this
->assertNoFieldChecked('edit-contact--2');
$this
->assertFalse(\Drupal::service('user.data')
->get('contact', $this->webUser
->id(), 'enabled'), 'Personal contact form disabled');
$this
->drupalPostForm(NULL, array(
'contact' => TRUE,
), t('Save'));
$this
->assertFieldChecked('edit-contact--2');
$this
->assertTrue(\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
->assertResponse(200);
}
function testPersonalContactFlood() {
$flood_limit = 3;
$this
->config('contact.settings')
->set('flood.limit', $flood_limit)
->save();
db_delete('flood')
->execute();
$num_records_flood = db_query("SELECT COUNT(*) FROM {flood}")
->fetchField();
$this
->assertIdentical($num_records_flood, '0', 'Flood table emptied.');
$this
->drupalLogin($this->webUser);
for ($i = 0; $i < $flood_limit; $i++) {
$this
->submitPersonalContact($this->contactUser);
$this
->assertText(t('Your message has been sent.'), 'Message sent.');
}
$this
->submitPersonalContact($this->contactUser);
$this
->assertRaw(t('You cannot send more than %number messages in @interval. Try again later.', array(
'%number' => $flood_limit,
'@interval' => \Drupal::service('date.formatter')
->formatInterval($this
->config('contact.settings')
->get('flood.interval')),
)), 'Normal user denied access to flooded contact form.');
$this
->drupalLogin($this->adminUser);
$this
->assertNoText('Try again later.', 'Admin user not denied access to flooded contact form.');
}
function testAdminContact() {
user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, array(
'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
->assertFieldChecked('edit-contact--2');
}
else {
$this
->assertNoFieldChecked('edit-contact--2');
}
$name = $this
->randomMachineName();
$edit = array(
'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
->drupalPostForm('admin/people/create', $edit, t('Create new account'));
$user = user_load_by_name($name);
$this
->drupalLogout();
$this
->drupalGet('user/' . $user
->id() . '/contact');
$this
->assertResponse($response);
}
protected function submitPersonalContact(AccountInterface $account, array $message = array()) {
$message += array(
'subject[0][value]' => $this
->randomMachineName(16),
'message[0][value]' => $this
->randomMachineName(64),
);
$this
->drupalPostForm('user/' . $account
->id() . '/contact', $message, t('Send message'));
return $message;
}
}