ContactPersonalTest.php 9.26 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 14 15 16
use Drupal\simpletest\WebTestBase;

/**
 * Tests the personal contact form.
 */
class ContactPersonalTest extends WebTestBase {
17 18 19 20 21 22

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

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

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

  /**
   * A user without any permissions.
   *
   * @var object
   */
44 45 46 47 48 49 50 51 52 53 54
  private $contact_user;

  public static function getInfo() {
    return array(
      'name' => 'Personal contact form',
      'description' => 'Tests personal contact form functionality.',
      'group' => 'Contact',
    );
  }

  function setUp() {
55
    parent::setUp();
56 57

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

    // Create some normal users with their contact forms enabled by default.
61
    \Drupal::config('contact.settings')->set('user_default_enabled', 1)->save();
62 63 64 65
    $this->web_user = $this->drupalCreateUser(array('access user contact forms'));
    $this->contact_user = $this->drupalCreateUser();
  }

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

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

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

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

120 121
    // Test allowed access to user with contact form enabled.
    $this->drupalLogin($this->web_user);
122
    $this->drupalGet('user/' . $this->contact_user->id() . '/contact');
123 124 125
    $this->assertResponse(200);

    // Test denied access to the user's own contact form.
126
    $this->drupalGet('user/' . $this->web_user->id() . '/contact');
127 128 129 130 131 132 133 134 135
    $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'));
136
    $this->drupalGet('user/' . $this->contact_user->id() . '/contact');
137 138
    $this->assertResponse(200);

139
    // Test that anonymous users can access admin user's contact form.
140
    $this->drupalGet('user/' . $this->admin_user->id() . '/contact');
141 142
    $this->assertResponse(200);

143 144
    // Revoke the personal contact permission for the anonymous user.
    user_role_revoke_permissions(DRUPAL_ANONYMOUS_RID, array('access user contact forms'));
145
    $this->drupalGet('user/' . $this->contact_user->id() . '/contact');
146
    $this->assertResponse(403);
147
    $this->drupalGet('user/' . $this->admin_user->id() . '/contact');
148
    $this->assertResponse(403);
149 150 151 152

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

    // Test allowed access for admin user to a user with contact form disabled.
    $this->drupalLogin($this->admin_user);
168
    $this->drupalGet('user/' . $this->contact_user->id() . '/contact');
169 170 171 172
    $this->assertResponse(200);

    // Re-create our contacted user as a blocked user.
    $this->contact_user = $this->drupalCreateUser();
173
    $this->contact_user->block();
174 175 176
    $this->contact_user->save();

    // Test that blocked users can still be contacted by admin.
177
    $this->drupalGet('user/' . $this->contact_user->id() . '/contact');
178 179 180 181
    $this->assertResponse(200);

    // Test that blocked users cannot be contacted by non-admins.
    $this->drupalLogin($this->web_user);
182
    $this->drupalGet('user/' . $this->contact_user->id() . '/contact');
183
    $this->assertResponse(403);
184 185 186 187 188 189

    // 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');
190
    $this->drupalPostForm(NULL, array('contact' => TRUE), t('Save'));
191 192
    $this->assertFieldChecked('edit-contact--2');
    $this->assertTrue(\Drupal::service('user.data')->get('contact', $this->web_user->id(), 'enabled'), 'Personal contact form enabled');
193 194 195 196 197 198 199
  }

  /**
   * Tests the personal contact form flood protection.
   */
  function testPersonalContactFlood() {
    $flood_limit = 3;
200
    \Drupal::config('contact.settings')->set('flood.limit', $flood_limit)->save();
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215

    // 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.
216
    $this->drupalGet('user/' . $this->contact_user->id(). '/contact');
217
    $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.');
218 219 220 221 222 223 224 225 226 227 228 229 230

    // 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
231 232
   *   (optional) An array with the form fields being used. Defaults to an empty
   *   array.
233 234 235 236 237 238
   */
  protected function submitPersonalContact($account, array $message = array()) {
    $message += array(
      'subject' => $this->randomName(16),
      'message' => $this->randomName(64),
    );
239
    $this->drupalPostForm('user/' . $account->id() . '/contact', $message, t('Send message'));
240
    return $message;
241 242
  }
}