ContactPersonalTest.php 9.09 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Definition of Drupal\contact\Tests\ContactPersonalTest.
 */

namespace Drupal\contact\Tests;

10
use Drupal\Component\Utility\String;
11 12 13
use Drupal\simpletest\WebTestBase;

/**
14 15 16
 * Tests personal contact form functionality.
 *
 * @group contact
17 18
 */
class ContactPersonalTest extends WebTestBase {
19 20 21 22 23 24

  /**
   * Modules to enable.
   *
   * @var array
   */
25
  public static $modules = array('contact', 'dblog');
26

27 28 29 30 31
  /**
   * A user with some administrative permissions.
   *
   * @var object
   */
32
  private $admin_user;
33 34 35 36 37 38

  /**
   * A user with 'access user contact forms' permission.
   *
   * @var object
   */
39
  private $web_user;
40 41 42 43 44 45

  /**
   * A user without any permissions.
   *
   * @var object
   */
46 47 48
  private $contact_user;

  function setUp() {
49
    parent::setUp();
50 51

    // Create an admin user.
52
    $this->admin_user = $this->drupalCreateUser(array('administer contact forms', 'administer users', 'administer account settings', 'access site reports'));
53 54

    // Create some normal users with their contact forms enabled by default.
55
    \Drupal::config('contact.settings')->set('user_default_enabled', 1)->save();
56 57 58 59
    $this->web_user = $this->drupalCreateUser(array('access user contact forms'));
    $this->contact_user = $this->drupalCreateUser();
  }

60 61 62 63 64 65 66 67 68 69
  /**
   * 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];
70
    $this->assertEqual($mail['to'], $this->contact_user->getEmail());
71 72
    $this->assertEqual($mail['from'], \Drupal::config('system.site')->get('mail'));
    $this->assertEqual($mail['reply-to'], $this->web_user->getEmail());
73 74
    $this->assertEqual($mail['key'], 'user_mail');
    $variables = array(
75
      '!site-name' => \Drupal::config('system.site')->get('name'),
76
      '!subject' => $message['subject'],
77
      '!recipient-name' => $this->contact_user->getUsername(),
78 79 80
    );
    $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.');
81
    $this->assertTrue(strpos($mail['body'], $this->web_user->getUsername()) !== FALSE, 'Sender name is in sent message.');
82
    $this->assertTrue(strpos($mail['body'], $message['message']) !== FALSE, 'Message body is in sent message.');
83 84 85 86 87 88 89 90 91 92 93

    // 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()
    );
94
    $this->assertText(String::format('@sender_name (@sender_email) sent @recipient_name an email.', $placeholders));
95 96
  }

97 98 99 100
  /**
   * Tests access to the personal contact form.
   */
  function testPersonalContactAccess() {
101 102
    // Test allowed access to admin user's contact form.
    $this->drupalLogin($this->web_user);
103
    $this->drupalGet('user/' . $this->admin_user->id() . '/contact');
104
    $this->assertResponse(200);
105 106
    // Check the page title is properly displayed.
    $this->assertRaw(t('Contact @username', array('@username' => $this->admin_user->getUsername())));
107 108 109 110

    // Test denied access to admin user's own contact form.
    $this->drupalLogout();
    $this->drupalLogin($this->admin_user);
111
    $this->drupalGet('user/' . $this->admin_user->id() . '/contact');
112 113
    $this->assertResponse(403);

114 115
    // Test allowed access to user with contact form enabled.
    $this->drupalLogin($this->web_user);
116
    $this->drupalGet('user/' . $this->contact_user->id() . '/contact');
117 118 119
    $this->assertResponse(200);

    // Test denied access to the user's own contact form.
120
    $this->drupalGet('user/' . $this->web_user->id() . '/contact');
121 122 123 124 125 126 127 128 129
    $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'));
130
    $this->drupalGet('user/' . $this->contact_user->id() . '/contact');
131 132
    $this->assertResponse(200);

133
    // Test that anonymous users can access admin user's contact form.
134
    $this->drupalGet('user/' . $this->admin_user->id() . '/contact');
135 136
    $this->assertResponse(200);

137 138
    // Revoke the personal contact permission for the anonymous user.
    user_role_revoke_permissions(DRUPAL_ANONYMOUS_RID, array('access user contact forms'));
139
    $this->drupalGet('user/' . $this->contact_user->id() . '/contact');
140
    $this->assertResponse(403);
141
    $this->drupalGet('user/' . $this->admin_user->id() . '/contact');
142
    $this->assertResponse(403);
143 144 145 146

    // Disable the personal contact form.
    $this->drupalLogin($this->admin_user);
    $edit = array('contact_default_status' => FALSE);
147
    $this->drupalPostForm('admin/config/people/accounts', $edit, t('Save configuration'));
148
    $this->assertText(t('The configuration options have been saved.'), 'Setting successfully saved.');
149 150 151 152 153 154 155 156
    $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);
157
    $this->drupalGet('user/' . $this->contact_user->id() . '/contact');
158 159 160 161
    $this->assertResponse(403);

    // Test allowed access for admin user to a user with contact form disabled.
    $this->drupalLogin($this->admin_user);
162
    $this->drupalGet('user/' . $this->contact_user->id() . '/contact');
163 164 165 166
    $this->assertResponse(200);

    // Re-create our contacted user as a blocked user.
    $this->contact_user = $this->drupalCreateUser();
167
    $this->contact_user->block();
168 169 170
    $this->contact_user->save();

    // Test that blocked users can still be contacted by admin.
171
    $this->drupalGet('user/' . $this->contact_user->id() . '/contact');
172 173 174 175
    $this->assertResponse(200);

    // Test that blocked users cannot be contacted by non-admins.
    $this->drupalLogin($this->web_user);
176
    $this->drupalGet('user/' . $this->contact_user->id() . '/contact');
177
    $this->assertResponse(403);
178 179 180 181 182 183

    // 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');
184
    $this->drupalPostForm(NULL, array('contact' => TRUE), t('Save'));
185 186
    $this->assertFieldChecked('edit-contact--2');
    $this->assertTrue(\Drupal::service('user.data')->get('contact', $this->web_user->id(), 'enabled'), 'Personal contact form enabled');
187 188 189 190 191 192 193
  }

  /**
   * Tests the personal contact form flood protection.
   */
  function testPersonalContactFlood() {
    $flood_limit = 3;
194
    \Drupal::config('contact.settings')->set('flood.limit', $flood_limit)->save();
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209

    // 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.
210
    $this->drupalGet('user/' . $this->contact_user->id(). '/contact');
211
    $this->assertRaw(t('You cannot send more than %number messages in @interval. Try again later.', array('%number' => $flood_limit, '@interval' => format_interval(\Drupal::config('contact.settings')->get('flood.interval')))), 'Normal user denied access to flooded contact form.');
212 213 214 215 216 217 218 219 220 221 222 223 224

    // 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.');
  }

  /**
   * Fills out a user's personal contact form and submits it.
   *
   * @param $account
   *   A user object of the user being contacted.
   * @param $message
225 226
   *   (optional) An array with the form fields being used. Defaults to an empty
   *   array.
227 228 229 230 231 232
   */
  protected function submitPersonalContact($account, array $message = array()) {
    $message += array(
      'subject' => $this->randomName(16),
      'message' => $this->randomName(64),
    );
233
    $this->drupalPostForm('user/' . $account->id() . '/contact', $message, t('Send message'));
234
    return $message;
235 236
  }
}