You are here

function PrivatemsgAPITestCase::testPrivatemsgApiNewThread in Privatemsg 6.2

Same name and namespace in other branches
  1. 6 tests/privatemsgapi.test \PrivatemsgAPITestCase::testPrivatemsgApiNewThread()
  2. 7.2 privatemsg.test \PrivatemsgAPITestCase::testPrivatemsgApiNewThread()
  3. 7 privatemsg.test \PrivatemsgAPITestCase::testPrivatemsgApiNewThread()

Tests sending new private messages.

File

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

Class

PrivatemsgAPITestCase
Tests for API functions.

Code

function testPrivatemsgApiNewThread() {
  $author = $this
    ->drupalCreateUser(array(
    'write privatemsg',
    'read privatemsg',
  ));
  $recipient1 = $this
    ->drupalCreateUser(array(
    'read privatemsg',
  ));
  $recipient2 = $this
    ->drupalCreateUser(array(
    'read privatemsg',
  ));
  $recipient3 = $this
    ->drupalCreateUser(array(
    'read privatemsg',
  ));

  // Reset user_access cache
  user_access('', $author, TRUE);
  $resultok1 = privatemsg_new_thread(array(
    $recipient1,
    $recipient2,
    $recipient3,
  ), 'normal message', 'Body text', array(
    'author' => $author,
  ));
  $this
    ->assertTrue($resultok1['success'], 'Private message could be sent successfully');
  $message = $this
    ->getMessageFromSubject('normal message');
  $this
    ->assertFalse(empty($message), 'Message was saved in database');
  $this
    ->assertEqual($message['author'], $author->uid, 'Message was sent by author');
  $resultok2 = privatemsg_new_thread(array(
    $recipient1,
    $recipient2,
    $recipient3,
  ), 'empty body', '', array(
    'author' => $author,
  ));
  $this
    ->assertTrue($resultok2['success'], 'API allowed to send message without body');
  $resultf1 = privatemsg_new_thread(array(
    $recipient1,
    $recipient2,
    $recipient3,
  ), '', 'No subject', array(
    'author' => $author,
  ));
  $this
    ->assertEqual('A subject or message must be included.', $resultf1['messages']['error'][0], 'API denied to send message without a subject');
  $resultf2 = privatemsg_new_thread(array(), 'no recipients', 'Body text', array(
    'author' => $author,
  ));
  $this
    ->assertEqual('At least one valid recipient must be included with the message.', $resultf2['messages']['error'][0], 'API denied to send message without recipients');
  $message = $this
    ->getMessageFromSubject('no recipients');
  $this
    ->assertTrue(empty($message), 'Message was not saved in database');
  $resultf3 = privatemsg_new_thread(array(
    $recipient1,
    $recipient2,
    $recipient3,
  ), 'not allowed', 'Body text', array(
    'author' => $recipient1,
  ));
  $errormessage = t('@user is not allowed to write messages.', array(
    '@user' => $recipient1->name,
  ));
  $this
    ->assertEqual($errormessage, $resultf3['messages']['error'][0], 'API denied to send message from user without permission');
  $message = $this
    ->getMessageFromSubject('not allowed');
  $this
    ->assertTrue(empty($message), 'Message was not saved in database');

  // Test with an input format that the author is not allowed to use.
  $resultf4 = privatemsg_new_thread(array(
    $recipient1,
    $recipient2,
    $recipient3,
  ), 'input filter not allowed', 'Body text', array(
    'author' => $author,
    'format' => 2,
  ));
  $errormessage = t('@user is not allowed to use the specified input format.', array(
    '@user' => $author->name,
  ));
  $this
    ->assertEqual($errormessage, $resultf4['messages']['error'][0], t('User is not allowed to use the specified input format.'));
  $message = $this
    ->getMessageFromSubject('input filter not allowed');
  $this
    ->assertTrue(empty($message), 'Message was not saved in database');

  // Send a message through the api to the same user and check if it marked
  // as new.
  privatemsg_new_thread(array(
    $author,
  ), $subject = $this
    ->randomName(10), $this
    ->randomString(20), array(
    'author' => $author,
  ));
  $this
    ->drupalLogin($author);
  $this
    ->drupalGet('messages');
  $this
    ->clickLink($subject);
  $this
    ->assertText(t('New'), t('Message is marked as new'));
}