ContactStoragePersonalTest.php 4.5 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\contact_storage\Functional;
4

5
use Drupal\Component\Render\FormattableMarkup;
6 7
use Drupal\Component\Render\PlainTextOutput;
use Drupal\Core\Session\AccountInterface;
8 9
use Drupal\Core\Test\AssertMailTrait;
use Drupal\Tests\BrowserTestBase;
10 11 12 13 14 15

/**
 * Tests personal contact form functionality.
 *
 * @group contact
 */
16
class ContactStoragePersonalTest extends BrowserTestBase {
17

18 19 20
  use AssertMailTrait {
    getMails as drupalGetMails;
  }
21 22 23 24 25
  /**
   * Modules to enable.
   *
   * @var array
   */
26
  public static $modules = ['contact', 'contact_storage', 'dblog'];
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

  /**
   * A user with some administrative permissions.
   *
   * @var \Drupal\user\UserInterface
   */
  private $adminUser;

  /**
   * A user with permission to view profiles and access user contact forms.
   *
   * @var \Drupal\user\UserInterface
   */
  private $webUser;

  /**
   * A user without any permissions.
   *
   * @var \Drupal\user\UserInterface
   */
  private $contactUser;

  protected function setUp() {
    parent::setUp();

    // Create an admin user.
53
    $this->adminUser = $this->drupalCreateUser(['administer contact forms', 'administer users', 'administer account settings', 'access site reports']);
54 55 56

    // Create some normal users with their contact forms enabled by default.
    $this->config('contact.settings')->set('user_default_enabled', TRUE)->save();
57
    $this->webUser = $this->drupalCreateUser(['access user profiles', 'access user contact forms']);
58 59 60 61 62 63
    $this->contactUser = $this->drupalCreateUser();
  }

  /**
   * Tests that mails for contact messages are correctly sent.
   */
64
  public function testSendPersonalContactMessage() {
65
    // Ensure that the web user's email needs escaping.
66
    $mail = $this->webUser->getAccountName() . '&escaped@example.com';
67 68 69 70 71 72 73 74 75 76 77 78 79
    $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');
80
    $variables = [
81 82 83
      '@site-name' => $this->config('system.site')->get('name'),
      '@subject' => $message['subject[0][value]'],
      '@recipient-name' => $this->contactUser->getDisplayName(),
84
    ];
85 86 87 88 89 90 91 92 93 94 95
    $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->getDisplayName()) !== FALSE, 'Sender name is in sent message.');
    $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->adminUser);
    // Verify that the correct watchdog message has been logged.
    $this->drupalGet('/admin/reports/dblog');
96
    $placeholders = [
97 98
      '@sender_name' => $this->webUser->username,
      '@sender_email' => $this->webUser->getEmail(),
99
      '@recipient_name' => $this->contactUser->getDisplayName(),
100
    ];
101
    $this->assertRaw(new FormattableMarkup('@sender_name (@sender_email) sent @recipient_name an email.', $placeholders));
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
    // Ensure an unescaped version of the email does not exist anywhere.
    $this->assertNoRaw($this->webUser->getEmail());
  }

  /**
   * Fills out a user's personal contact form and submits it.
   *
   * @param \Drupal\Core\Session\AccountInterface $account
   *   A user object of the user being contacted.
   * @param array $message
   *   (optional) An array with the form fields being used. Defaults to an empty
   *   array.
   *
   * @return array
   *   An array with the form fields being used.
   */
118 119
  protected function submitPersonalContact(AccountInterface $account, array $message = []) {
    $message += [
120 121
      'subject[0][value]' => $this->randomMachineName(16),
      'message[0][value]' => $this->randomMachineName(64),
122
    ];
123 124 125
    $this->drupalPostForm('user/' . $account->id() . '/contact', $message, t('Send message'));
    return $message;
  }
126

127
}