Newer
Older
<?php
/**
* @file
Alex Pott
committed
* Contains \Drupal\contact\Tests\ContactPersonalTest.
*/
namespace Drupal\contact\Tests;
use Drupal\Component\Utility\String;
Alex Pott
committed
use Drupal\Core\Session\AccountInterface;
use Drupal\simpletest\WebTestBase;
/**
* Tests personal contact form functionality.
*
* @group contact
*/
class ContactPersonalTest extends WebTestBase {
/**
* Set to TRUE to strict check all configuration saved.
*
* @see \Drupal\Core\Config\Testing\ConfigSchemaChecker
*
* @var bool
*/
protected $strictConfigSchema = TRUE;
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('contact', 'dblog');
/**
* A user with some administrative permissions.
*
* @var object
*/
private $admin_user;
/**
* A user with 'access user contact forms' permission.
*
* @var object
*/
private $web_user;
/**
* A user without any permissions.
*
* @var object
*/
private $contact_user;
Alex Pott
committed
protected function setUp() {
parent::setUp();
// Create an admin user.
$this->admin_user = $this->drupalCreateUser(array('administer contact forms', 'administer users', 'administer account settings', 'access site reports'));
// Create some normal users with their contact forms enabled by default.
Alex Pott
committed
\Drupal::config('contact.settings')->set('user_default_enabled', TRUE)->save();
$this->web_user = $this->drupalCreateUser(array('access user contact forms'));
$this->contact_user = $this->drupalCreateUser();
}
Alex Pott
committed
/**
* Tests that mails for contact messages are correctly sent.
*/
function testSendPersonalContactMessage() {
$this->drupalLogin($this->web_user);
$message = $this->submitPersonalContact($this->contact_user);
$mails = $this->drupalGetMails();
$this->assertEqual(1, count($mails));
$mail = $mails[0];
$this->assertEqual($mail['to'], $this->contact_user->getEmail());
$this->assertEqual($mail['from'], \Drupal::config('system.site')->get('mail'));
$this->assertEqual($mail['reply-to'], $this->web_user->getEmail());
$this->assertEqual($mail['key'], 'user_mail');
$variables = array(
Angie Byron
committed
'!site-name' => \Drupal::config('system.site')->get('name'),
Alex Pott
committed
'!subject' => $message['subject[0][value]'],
'!recipient-name' => $this->contact_user->getUsername(),
);
$this->assertEqual($mail['subject'], t('[!site-name] !subject', $variables), 'Subject is in sent message.');
$this->assertTrue(strpos($mail['body'], t('Hello !recipient-name,', $variables)) !== FALSE, 'Recipient name is in sent message.');
$this->assertTrue(strpos($mail['body'], $this->web_user->getUsername()) !== FALSE, 'Sender name is in sent message.');
Alex Pott
committed
$this->assertTrue(strpos($mail['body'], $message['message[0][value]']) !== FALSE, 'Message body is in sent message.');
// Check there was no problems raised during sending.
$this->drupalLogout();
$this->drupalLogin($this->admin_user);
// Verify that the correct watchdog message has been logged.
$this->drupalGet('/admin/reports/dblog');
$placeholders = array(
'@sender_name' => $this->web_user->username,
'@sender_email' => $this->web_user->getEmail(),
'@recipient_name' => $this->contact_user->getUsername()
);
$this->assertText(String::format('@sender_name (@sender_email) sent @recipient_name an email.', $placeholders));
Alex Pott
committed
}
/**
* Tests access to the personal contact form.
*/
function testPersonalContactAccess() {
Dries Buytaert
committed
// Test allowed access to admin user's contact form.
$this->drupalLogin($this->web_user);
Dries Buytaert
committed
$this->drupalGet('user/' . $this->admin_user->id() . '/contact');
Dries Buytaert
committed
$this->assertResponse(200);
// Check the page title is properly displayed.
$this->assertRaw(t('Contact @username', array('@username' => $this->admin_user->getUsername())));
Dries Buytaert
committed
// Test denied access to admin user's own contact form.
$this->drupalLogout();
$this->drupalLogin($this->admin_user);
Dries Buytaert
committed
$this->drupalGet('user/' . $this->admin_user->id() . '/contact');
Dries Buytaert
committed
$this->assertResponse(403);
// Test allowed access to user with contact form enabled.
$this->drupalLogin($this->web_user);
Dries Buytaert
committed
$this->drupalGet('user/' . $this->contact_user->id() . '/contact');
$this->assertResponse(200);
// Test denied access to the user's own contact form.
Dries Buytaert
committed
$this->drupalGet('user/' . $this->web_user->id() . '/contact');
$this->assertResponse(403);
// Test always denied access to the anonymous user contact form.
$this->drupalGet('user/0/contact');
$this->assertResponse(403);
// Test that anonymous users can access the contact form.
$this->drupalLogout();
user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array('access user contact forms'));
Dries Buytaert
committed
$this->drupalGet('user/' . $this->contact_user->id() . '/contact');
$this->assertResponse(200);
Dries Buytaert
committed
// Test that anonymous users can access admin user's contact form.
Dries Buytaert
committed
$this->drupalGet('user/' . $this->admin_user->id() . '/contact');
Dries Buytaert
committed
$this->assertResponse(200);
// Revoke the personal contact permission for the anonymous user.
user_role_revoke_permissions(DRUPAL_ANONYMOUS_RID, array('access user contact forms'));
Dries Buytaert
committed
$this->drupalGet('user/' . $this->contact_user->id() . '/contact');
$this->assertResponse(403);
Dries Buytaert
committed
$this->drupalGet('user/' . $this->admin_user->id() . '/contact');
Dries Buytaert
committed
$this->assertResponse(403);
// Disable the personal contact form.
$this->drupalLogin($this->admin_user);
$edit = array('contact_default_status' => FALSE);
$this->drupalPostForm('admin/config/people/accounts', $edit, t('Save configuration'));
Dries Buytaert
committed
$this->assertText(t('The configuration options have been saved.'), 'Setting successfully saved.');
$this->drupalLogout();
// Re-create our contacted user with personal contact forms disabled by
// default.
$this->contact_user = $this->drupalCreateUser();
// Test denied access to a user with contact form disabled.
$this->drupalLogin($this->web_user);
Dries Buytaert
committed
$this->drupalGet('user/' . $this->contact_user->id() . '/contact');
$this->assertResponse(403);
// Test allowed access for admin user to a user with contact form disabled.
$this->drupalLogin($this->admin_user);
Dries Buytaert
committed
$this->drupalGet('user/' . $this->contact_user->id() . '/contact');
$this->assertResponse(200);
// Re-create our contacted user as a blocked user.
$this->contact_user = $this->drupalCreateUser();
$this->contact_user->block();
$this->contact_user->save();
// Test that blocked users can still be contacted by admin.
Dries Buytaert
committed
$this->drupalGet('user/' . $this->contact_user->id() . '/contact');
$this->assertResponse(200);
// Test that blocked users cannot be contacted by non-admins.
$this->drupalLogin($this->web_user);
Dries Buytaert
committed
$this->drupalGet('user/' . $this->contact_user->id() . '/contact');
$this->assertResponse(403);
Alex Pott
committed
// Test enabling and disabling the contact page through the user profile
// form.
$this->drupalGet('user/' . $this->web_user->id() . '/edit');
$this->assertNoFieldChecked('edit-contact--2');
$this->assertFalse(\Drupal::service('user.data')->get('contact', $this->web_user->id(), 'enabled'), 'Personal contact form disabled');
$this->drupalPostForm(NULL, array('contact' => TRUE), t('Save'));
Alex Pott
committed
$this->assertFieldChecked('edit-contact--2');
$this->assertTrue(\Drupal::service('user.data')->get('contact', $this->web_user->id(), 'enabled'), 'Personal contact form enabled');
}
/**
* Tests the personal contact form flood protection.
*/
function testPersonalContactFlood() {
$flood_limit = 3;
Angie Byron
committed
\Drupal::config('contact.settings')->set('flood.limit', $flood_limit)->save();
// Clear flood table in preparation for flood test and allow other checks to complete.
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->web_user);
// Submit contact form with correct values and check flood interval.
for ($i = 0; $i < $flood_limit; $i++) {
$this->submitPersonalContact($this->contact_user);
$this->assertText(t('Your message has been sent.'), 'Message sent.');
}
// Submit contact form one over limit.
Dries Buytaert
committed
$this->drupalGet('user/' . $this->contact_user->id(). '/contact');
Alex Pott
committed
$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(\Drupal::config('contact.settings')->get('flood.interval')))), 'Normal user denied access to flooded contact form.');
// Test that the admin user can still access the contact form even though
// the flood limit was reached.
$this->drupalLogin($this->admin_user);
$this->assertNoText('Try again later.', 'Admin user not denied access to flooded contact form.');
}
Alex Pott
committed
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/**
* Tests the personal contact form based access when an admin adds users.
*/
function testAdminContact() {
user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array('access user contact forms'));
$this->checkContactAccess(200);
$this->checkContactAccess(403, FALSE);
$config = \Drupal::config('contact.settings');
$config->set('user_default_enabled', FALSE);
$config->save();
$this->checkContactAccess(403);
}
/**
* Creates a user and then checks contact form access.
*
* @param integer $response
* The expected response code.
* @param boolean $contact_value
* (optional) The value the contact field should be set too.
*/
protected function checkContactAccess($response, $contact_value = NULL) {
$this->drupalLogin($this->admin_user);
$this->drupalGet('admin/people/create');
if (\Drupal::config('contact.settings')->get('user_default_enabled', TRUE)) {
$this->assertFieldChecked('edit-contact--2');
}
else {
$this->assertNoFieldChecked('edit-contact--2');
}
Alex Pott
committed
$name = $this->randomMachineName();
Alex Pott
committed
$edit = array(
'name' => $name,
Alex Pott
committed
'mail' => $this->randomMachineName() . '@example.com',
Alex Pott
committed
'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);
}
/**
* Fills out a user's personal contact form and submits it.
*
Alex Pott
committed
* @param \Drupal\Core\Session\AccountInterface $account
* A user object of the user being contacted.
Alex Pott
committed
* @param array $message
* (optional) An array with the form fields being used. Defaults to an empty
* array.
Alex Pott
committed
*
* @return array
* An array with the form fields being used.
Alex Pott
committed
protected function submitPersonalContact(AccountInterface $account, array $message = array()) {
$message += array(
Alex Pott
committed
'subject[0][value]' => $this->randomMachineName(16),
'message[0][value]' => $this->randomMachineName(64),
$this->drupalPostForm('user/' . $account->id() . '/contact', $message, t('Send message'));
Alex Pott
committed
return $message;
Alex Pott
committed