You are here

function PrivatemsgAPITestCase::testPrivatemsgApiNewThread in Privatemsg 6

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

File

tests/privatemsgapi.test, line 33
Privatemsg API tests

Class

PrivatemsgAPITestCase
@file Privatemsg API tests

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('Disallowed to send a message without subject', $resultf1['messages']['error'][0], 'API denied to send message without subject');
  $resultf2 = privatemsg_new_thread(array(), 'no recipients', 'Body text', array(
    'author' => $author,
  ));
  $this
    ->assertEqual('Disallowed to send a message without at least one valid recipient', $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 = 'User ' . $recipient1->name . ' is not allowed to write messages';
  $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 @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'));
}