You are here

function PrivatemsgTestCase::testWriteReplyPrivatemsg in Privatemsg 6

Same name and namespace in other branches
  1. 6.2 privatemsg.test \PrivatemsgTestCase::testWriteReplyPrivatemsg()
  2. 7.2 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 180
Test file for privatemsg.module

Class

PrivatemsgTestCase
@file Test file for privatemsg.module

Code

function testWriteReplyPrivatemsg() {

  // Create a author and two recipients.
  $author = $this
    ->drupalCreateUser(array(
    'write privatemsg',
    'administer filters',
  ));
  $recipient = $this
    ->drupalCreateUser(array(
    'read privatemsg',
  ));
  $recipient2 = $this
    ->drupalCreateUser(array(
    'read privatemsg',
    'write privatemsg',
  ));

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

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

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

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

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

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

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

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

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

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

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

  // 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('Disallowed to send a message without at least one valid recipient'), 'Message was not sent.');
  $this
    ->drupalPost('messages/new', $editinvalid, t('Send message'));
  $this
    ->assertText(t('Disallowed to send a message without at least one valid recipient'), 'Message was not sent.');
  $this
    ->assertText(t('The following users will not receive this private message: @recipients', array(
    '@recipients' => $editinvalid['recipient'],
  )), 'Message about non-existing user 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('Disallowed to send a message without subject'), '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 users 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']);
  $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'], 'Found message body.');

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

  // Empty body.
  $replyempty = array(
    'body' => '',
  );
  $this
    ->drupalPost(NULL, $reply, t('Send message'));
  $this
    ->assertText($reply['body'], 'New message body displayed.');
  $this
    ->drupalPost(NULL, $replyempty, t('Send message'));
  $this
    ->assertText(t('Disallowed to send reply without a message.'));

  // reply with a bold part, not allowed with default format
  $replyformatted = array(
    'body' => $this
      ->randomName(100) . '<b>formatted message #2</b>',
  );
  $this
    ->drupalGet('messages');
  $this
    ->clickLink($editformatted['subject']);
  $this
    ->assertRaw($editformatted['body'], 'Found formatted message body.');
  $this
    ->drupalPost(NULL, $replyformatted, t('Send message'));
  $this
    ->assertNoRaw($replyformatted['body'], 'Did not find formatted reply body.');
  $this
    ->assertText(strip_tags($replyformatted['body']), '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']), 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'], 'Found message body.');
  $this
    ->assertNoText(t('Reply to thread:'), 'Reply form is not displayed.');

  // 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'], 'Found message body.');

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