You are here

function PrivatemsgTestCase::testWriteReplyPrivatemsg in Privatemsg 7.2

Same name and namespace in other branches
  1. 6.2 privatemsg.test \PrivatemsgTestCase::testWriteReplyPrivatemsg()
  2. 6 privatemsg.test \PrivatemsgTestCase::testWriteReplyPrivatemsg()
  3. 7 privatemsg.test \PrivatemsgTestCase::testWriteReplyPrivatemsg()

Test sending message from the /messages/new page between two people

File

./privatemsg.test, line 245
Test file for privatemsg.module

Class

PrivatemsgTestCase

Code

function testWriteReplyPrivatemsg() {

  // Create an author and two recipients.
  $author = $this
    ->drupalCreateUser(array(
    'read privatemsg',
    'write privatemsg',
    'select text format for privatemsg',
    filter_permission_name(filter_format_load('full_html')),
  ));
  $recipient = $this
    ->drupalCreateUser(array(
    'read privatemsg',
  ));
  $recipient2 = $this
    ->drupalCreateUser(array(
    'read privatemsg',
    'write privatemsg',
  ));

  // Set up a user with "read/write privatemsg" permissions.
  $blocked_recipient = $this
    ->drupalCreateUser(array(
    'read privatemsg',
    'write privatemsg',
  ));

  // Block this recipient to test users who cancelled their accounts.
  user_save($blocked_recipient, array(
    'status' => 0,
  ));

  // Login author and go to new message form.
  $this
    ->drupalLogin($author);
  $this
    ->drupalGet('messages/new');

  // Prepare edit arrays, single recipient with [user].
  $edit = array(
    'recipient' => $recipient->name . ' [user]',
    'subject' => $this
      ->randomName(20),
    'body[value]' => $this
      ->randomName(100),
  );

  // Two recipients.
  $edit2 = array(
    'recipient' => $recipient->name . ', ' . $recipient2->name,
    'subject' => $this
      ->randomName(20),
    'body[value]' => $this
      ->randomName(100),
  );

  // No recipients.
  $editnone = array(
    'recipient' => '',
    'subject' => $this
      ->randomName(20),
    'body[value]' => $this
      ->randomName(100),
  );

  // Invalid recipient.
  $editinvalid = array(
    'recipient' => $this
      ->randomName(5),
    'subject' => $this
      ->randomName(20),
    'body[value]' => $this
      ->randomName(100),
  );

  // Blocked recipient.
  $editrecipientblocked = array(
    'recipient' => $blocked_recipient->name,
    'subject' => $this
      ->randomName(20),
    'body[value]' => $this
      ->randomName(100),
  );

  // Message for which the author will be blocked later.
  $editauthorblocked = array(
    'recipient' => $recipient2->name,
    'subject' => $this
      ->randomName(20),
    'body[value]' => $this
      ->randomName(100),
  );

  // Empty body.
  $editnobody = array(
    'recipient' => $recipient->name,
    'subject' => $this
      ->randomName(20),
    'body[value]' => '',
  );

  // Empty subject.
  $editnosubject = array(
    'recipient' => $recipient->name,
    'subject' => '',
    'body[value]' => $this
      ->randomName(100),
  );

  // Empty subject and body.
  $editempty = array(
    'recipient' => $recipient->name,
    'subject' => '',
    'body[value]' => '',
  );

  // Empty subject and body.
  $editonlyspace = array(
    'recipient' => $recipient2->name,
    'subject' => ' ',
    'body[value]' => $this
      ->randomName(10),
  );

  // Invalid and valid recipient
  $editmixed = array(
    'recipient' => ($invalidmixed = $this
      ->randomName(5)) . ', ' . $recipient->name,
    'subject' => $this
      ->randomName(20),
    'body[value]' => $this
      ->randomName(100),
  );

  // message with a bold part, not allowed with default format
  $editformatted = array(
    'recipient' => $recipient2->name,
    'subject' => $this
      ->randomName(20),
    'body[value]' => $this
      ->randomName(100) . '<b>formatted message #1</b>',
    'body[format]' => 'full_html',
  );

  // Submit the messages.
  $this
    ->drupalPost('messages/new', $edit, t('Send message'));
  $this
    ->assertText(t('A message has been sent to @recipients.', array(
    '@recipients' => $recipient->name,
  )), 'Message sent confirmation displayed.');
  $this
    ->drupalPost('messages/new', $edit2, t('Send message'));
  $this
    ->assertText(t('A message has been sent to @recipients.', array(
    '@recipients' => implode(', ', array(
      $recipient->name,
      $recipient2->name,
    )),
  )), 'Message sent confirmation displayed.');
  $this
    ->drupalPost('messages/new', $editnone, t('Send message'));
  $this
    ->assertText(t('To field is required.'), 'Message was not sent.');
  $this
    ->drupalPost('messages/new', $editinvalid, t('Send message'));
  $this
    ->assertText(t('You must include at least one valid recipient.'), 'Message was not sent.');
  $this
    ->assertText(t('The following recipients will not receive this private message: @recipients.', array(
    '@recipients' => $editinvalid['recipient'],
  )), 'Message about non-existing user displayed.');
  $this
    ->drupalPost('messages/new', $editrecipientblocked, t('Send message'));
  $this
    ->assertText(t('@recipients has disabled his or her account.', array(
    '@recipients' => $blocked_recipient->name,
  )), 'Message about blocked user displayed.');
  $this
    ->assertText(t('You are not allowed to send this message because all recipients are blocked.'), 'Message was not sent.');

  // We will block the author later to test whether the reply form appears.
  $this
    ->drupalPost('messages/new', $editauthorblocked, t('Send message'));
  $this
    ->assertText(t('A message has been sent to @recipients.', array(
    '@recipients' => $recipient2->name,
  )), 'Message sent confirmation displayed.');
  $this
    ->drupalPost('messages/new', $editnobody, t('Send message'));
  $this
    ->assertText(t('A message has been sent to @recipients.', array(
    '@recipients' => $recipient->name,
  )), 'Message sent confirmation displayed.');
  $this
    ->drupalPost('messages/new', $editnosubject, t('Send message'));
  $this
    ->assertText(t('A message has been sent to @recipients.', array(
    '@recipients' => $recipient->name,
  )), 'Message sent confirmation displayed.');
  $this
    ->drupalPost('messages/new', $editempty, t('Send message'));
  $this
    ->assertText(t('You must include a subject line or a message.'), 'Empty subject message displayed.');
  $this
    ->drupalPost('messages/new', $editonlyspace, t('Send message'));
  $this
    ->assertText(t('A message has been sent to @recipients.', array(
    '@recipients' => $recipient2->name,
  )), 'Message sent confirmation displayed.');
  $this
    ->drupalPost('messages/new', $editmixed, t('Send message'));
  $this
    ->assertText(t('A message has been sent to @recipients.', array(
    '@recipients' => $recipient->name,
  )), 'Message sent confirmation displayed.');
  $this
    ->assertText(t('The following recipients will not receive this private message: @recipients.', array(
    '@recipients' => $invalidmixed,
  )), 'Message about non-existing user displayed.');
  $this
    ->drupalPost('messages/new', $editformatted, t('Send message'));
  $this
    ->assertText(t('A message has been sent to @recipients.', array(
    '@recipients' => $recipient2->name,
  )), 'Message sent confirmation displayed.');

  // Login as recipient2 and try to write some replies.
  $this
    ->drupalLogin($recipient2);
  $this
    ->drupalGet('messages');

  // Check that the message with only a space in the subject uses the body
  // as subject.
  $this
    ->clickLink($editonlyspace['body[value]']);
  $this
    ->drupalGet('messages');
  $this
    ->assertNoText($edit['subject'], 'Message sent to other recipient not found.');
  $this
    ->assertText($edit2['subject'], 'Sent message subject found.');
  $this
    ->clickLink($edit2['subject']);
  $this
    ->assertText($edit2['body[value]'], 'Found message body.');

  // Prepare replies.
  $reply = array(
    'body[value]' => $this
      ->randomName(100),
  );

  // Empty body.
  $replyempty = array(
    'body[value]' => '',
  );
  $this
    ->drupalPost(NULL, $reply, t('Send message'));
  $this
    ->assertText($reply['body[value]'], 'New message body displayed.');
  $this
    ->drupalPost(NULL, $replyempty, t('Send message'));
  $this
    ->assertText(t('You must include a message in your reply.'));

  // reply with a bold part, not allowed with default format
  $replyformatted = array(
    'body[value]' => $this
      ->randomName(100) . '<b>formatted message #2</b>',
  );
  $this
    ->drupalGet('messages');
  $this
    ->clickLink($editformatted['subject']);
  $this
    ->assertRaw($editformatted['body[value]'], 'Found formatted message body.');
  $this
    ->drupalPost(NULL, $replyformatted, t('Send message'));
  $this
    ->assertNoRaw($replyformatted['body[value]'], 'Did not find formatted reply body.');
  $this
    ->assertText(check_plain($replyformatted['body[value]']), 'New reply body displayed.');

  // Login using recipient and try to read the message by going to inbox first.
  $this
    ->drupalLogin($recipient);
  $this
    ->drupalGet('messages');

  // Assert if we see the subject of the messages.
  $this
    ->assertText($edit['subject'], 'Sent message subject found.');
  $this
    ->assertText($edit2['subject'], 'Sent message subject found.');
  $this
    ->assertText($editnobody['subject'], 'Sent message subject found.');
  $this
    ->assertText(trim(truncate_utf8(strip_tags($editnosubject['body[value]']), 50, TRUE, TRUE)), 'Sent message subject found.');
  $this
    ->assertText($editmixed['subject'], 'Sent message subject found.');

  // Assert that we don't see those that were invalid.
  $this
    ->assertNoText($editnone['subject'], 'Invalid message subject not found.');
  $this
    ->assertNoText($editinvalid['subject'], 'Invalid message subject not found.');

  // Navigate into the message.
  $this
    ->clickLink($edit['subject']);

  // Confirm that we can read the message that was sent.
  $this
    ->assertText($edit['body[value]'], 'Found message body.');
  $this
    ->assertNoText(t('Reply to thread:'), 'Reply form is not displayed.');

  // Verify that the participants information is correct.
  $this
    ->assertText(t('Between you and @author', array(
    '@author' => $author->name,
  )));

  // Navigate into the message.
  $this
    ->drupalGet('messages');
  $this
    ->clickLink($edit2['subject']);

  // Confirm that we can read the message that was sent.
  $this
    ->assertText($edit2['body[value]'], 'Found message body.');

  // Confirm that we can read the reply that was sent.
  $this
    ->assertText($reply['body[value]'], 'Found reply body.');

  // Block the author.
  user_save($author, array(
    'status' => 0,
  ));
  $this
    ->drupalLogin($recipient2);

  // Navigate into the message.
  $this
    ->drupalGet('messages');
  $this
    ->clickLink($editauthorblocked['subject']);

  // Confirm that the reply form is not shown.
  $this
    ->assertNoText(t('Reply'), 'Reply form is not displayed.');
  $this
    ->assertText(t('You can not reply to this conversation because all recipients are blocked.'));
}