class PrivatemsgAPITestCase

  1. 1 tests/privatemsgapi.test PrivatemsgAPITestCase
  2. 7-1 privatemsg.test PrivatemsgAPITestCase
  3. 7-2 privatemsg.test PrivatemsgAPITestCase
  4. 6-2 privatemsg.test PrivatemsgAPITestCase

Privatemsg API tests

Hierarchy

Expanded class hierarchy of PrivatemsgAPITestCase

File

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

View source
class PrivatemsgAPITestCase extends PrivatemsgBaseTestCase {
  /**
   * Implements getInfo().
   */
  public static function getInfo() {
    return array(
      // 'name' should start with what is being tested (menu item) followed by what about it
      // is being tested (creation/deletion).
      'name' => t('Privatemsg API functionality.'),
      // 'description' should be one or more complete sentences that provide more details on what
      // exactly is being tested.
      'description' => t('Test sending, receiving, listing, deleting messages and other features via API.'),
      // 'group' should be a logical grouping of test cases, like a category.  In most cases, that
      // is the module the test case is for.
      'group' => t('Privatemsg'),
    );
  }

  /**
   * Implements setUp().
   */
  function setUp() {
    parent::setUp('privatemsg', 'privatemsg_roles');

    // Create the full html format.
    $full_html_format = array(
      'format' => 'full_html',
      'name' => 'Full HTML',
      'weight' => 1,
      'filters' => array(
        // URL filter.
        'filter_url' => array(
          'weight' => 0,
          'status' => 1,
        ),
        // Line break filter.
        'filter_autop' => array(
          'weight' => 1,
          'status' => 1,
        ),
        // HTML corrector filter.
        'filter_htmlcorrector' => array(
          'weight' => 10,
          'status' => 1,
        ),
      ),
    );
    $full_html_format = (object) $full_html_format;
    filter_format_save($full_html_format);
    // Refresh permissions.
    $this->checkPermissions(array(), TRUE);
  }

  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' => 'full_html'));
    $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'));
  }

  function getMessageFromSubject($subject) {
    return db_query("SELECT * FROM {pm_message} WHERE subject = :subject", array(':subject' => $subject))->fetchObject();
  }

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

    // Reset user_access cache
    user_access('', $author, TRUE);

    $resultok = privatemsg_new_thread(array($recipient2, $recipient1, $recipient3), 'test reply', 'body text', array('author' => $author));
    $this->assertTrue($resultok['success'], 'Private message could be sent successfully');

    $thread_row = $this->getMessageFromSubject('test reply');

    $this->assertEqual($thread_row->reply_to_mid, 0, t('First message is not a reply to a thread.'));
    $resultok = privatemsg_reply($thread_row->mid, 'Test Body', array('author' => $author));
    $this->assertTrue($resultok['success'], 'Reply could be sent successfully');

    $reply_to_mid = db_query('SELECT reply_to_mid FROM {pm_message} WHERE mid = :mid', array(':mid' => $resultok['message']->mid))->fetchField();
    $this->assertEqual($reply_to_mid, $thread_row->mid, t('Reply is marked as reply of first message.'));

    $resultok = privatemsg_reply($thread_row->mid, 'Test Body', array('author' => $recipient1));
    $this->assertTrue($resultok['success'], 'Reply could be sent successfully');

    $reply_to_mid = db_query('SELECT reply_to_mid FROM {pm_message} WHERE mid = :mid', array(':mid' => $resultok['message']->mid))->fetchField();
    $this->assertEqual($reply_to_mid, $thread_row->mid, t('Reply is marked as reply of first message.'));

    $resultf1 = privatemsg_reply($thread_row->mid, '', array('author' => $recipient2));
    $this->assertFalse($resultf1['success'], 'API denied to send message without body.');
    $this->assertEqual($resultf1['messages']['error'][0], t('A message must be included in your reply.'), 'Correct error returned when replying with an empty body.');

    $resultf2 = privatemsg_reply($thread_row->mid, 'Test Body', array('author' => $recipient3));
    $errormessage = t('@user is not allowed to write messages.', array('@user' => $recipient3->name));
    $this->assertEqual($errormessage, $resultf2['messages']['error'][0], 'API denied to send message from user without permission');
  }

  /**
   * Test various use cases for privatemsg_get_link().
   */
  function testGetLink() {
    $author = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg', 'write privatemsg to all roles'));
    $author2 = $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', 'allow disabling privatemsg'));
    $recipient4 = $this->drupalCreateUser();

    // Create role with the same name as recipient 2.
    $role = $this->drupalCreateRole(array('read privatemsg'), $recipient2->name);

    $this->drupalLogin($author);

    $this->drupalGet(privatemsg_get_link(array($recipient1)));
    $this->assertFieldByName('recipient', $recipient1->name);

    // Because of there is a role with the same name like recipient 2,
    // add the [user] for recipient 2.
    $this->drupalGet(privatemsg_get_link(array($recipient1, $recipient2)));
    $this->assertFieldByName('recipient', $recipient1->name . ', ' . $recipient2->name . ' [user]');

    $this->drupalGet(privatemsg_get_link(array($recipient1, $recipient2), $author));
    $this->assertFieldByName('recipient', $recipient1->name . ', ' . $recipient2->name . ' [user]');

    $this->drupalGet(privatemsg_get_link(array($recipient1, $recipient2), $author, $subject = 'Str/"ang\\w3//'));
    $this->assertFieldByName('recipient', $recipient1->name . ', ' . $recipient2->name . ' [user]');
    $this->assertFieldByName('subject', $subject);

    // Disable privatemsg for recipient 3.
    privatemsg_set_setting('user', $recipient3->uid, 'disabled', 1);

    $this->assertFalse(privatemsg_get_link(array($recipient3), $author));

    $this->drupalGet(privatemsg_get_link(array($recipient1, $recipient3), $author));
    $this->assertFieldByName('recipient', $recipient1->name);

    // Disable links to self, verify that a link is only returned when the
    // author is not the only recipient.
    variable_set('privatemsg_display_link_self', FALSE);

    $this->assertFalse(privatemsg_get_link(array($author), $author));

    $this->drupalGet(privatemsg_get_link(array($recipient1, $author), $author));
    $this->assertFieldByName('recipient', $recipient1->name);

    // Verify that link is not shown when recipient doesn't have read
    // permission.
    $this->assertFalse(privatemsg_get_link(array($recipient4), $author));

    $this->drupalGet(privatemsg_get_link(array($recipient1, $recipient4), $author));
    $this->assertFieldByName('recipient', $recipient1->name);

    // Verify that link is not shown when author does not have write permission.
    $this->drupalLogin($recipient1);
    $this->assertFalse(privatemsg_get_link(array($author), $recipient1));

    // For user that has no permission to send message to role,
    // the [user] suffix will not appear although there is a role with the same name as user.
    $this->drupalLogin($author2);
    $this->drupalGet(privatemsg_get_link(array($recipient2)));
    $this->assertFieldByName('recipient', $recipient2->name);
  }

  /**
   * Tests for the privatemsg_*_setting() functions.
   */
  function testSettings() {

    $admin = user_load(1);
    $user = $this->drupalCreateUser(array('write privatemsg'));
    $user2 = $this->drupalCreateUser(array('write privatemsg'));

    // Create some global and role default settings.
    privatemsg_set_setting('global', 0, 'test', 1);
    privatemsg_set_setting('role', array_shift(array_keys($user->roles)), 'test', 2);

    // Add some user specific setting.
    privatemsg_set_setting('user', $admin->uid, 'test', 3);
    privatemsg_set_setting('user', $user->uid, 'test2', 4);
    privatemsg_set_setting('user', $user2->uid, 'test2', -1);

    // Clear the static cache.
    drupal_static_reset('privatemsg_settings');

    // Get the ids for each user.
    $admin_ids = privatemsg_get_default_setting_ids($admin);
    $user_ids = privatemsg_get_default_setting_ids($user);
    $user2_ids = privatemsg_get_default_setting_ids($user2);

    $this->assertEqual(privatemsg_get_setting('test', $admin_ids), 3, t('The admin has a user level setting.'));
    $this->assertEqual(privatemsg_get_setting('test', $user_ids), 2, t('The first user has role-level default.'));
    $this->assertEqual(privatemsg_get_setting('test', $user2_ids), 1, t('The second user defaults to the global.'));

    $this->assertEqual(privatemsg_get_setting('test2', $user_ids), 4, t('The value for another setting is read correctly.'));
    $this->assertEqual(privatemsg_get_setting('test2', $user2_ids), 0, t('Negative values are ignored.'));

    // Update existing settings, verify that the updated value is used now.
    privatemsg_set_setting('role', array_shift(array_keys($user->roles)), 'test', 5);
    privatemsg_set_setting('user', $admin->uid, 'test', 6);
    privatemsg_set_setting('user', $user2->uid, 'test', 7);

    $this->assertEqual(privatemsg_get_setting('test', $admin_ids), 6, t('The updated user level setting is used.'));
    $this->assertEqual(privatemsg_get_setting('test', $user_ids), 5, t('The updated role level setting is used.'));
    $this->assertEqual(privatemsg_get_setting('test', $user2_ids), 7, t('The second user uses the new setting.'));

    // Default some settings.
    privatemsg_del_setting('role', array_shift(array_keys($user->roles)), 'test');
    privatemsg_del_setting('user', $admin->uid, 'test');

    $this->assertEqual(privatemsg_get_setting('test', $admin_ids), 1, t('The user level setting was deleted, the default is now used.'));
    $this->assertEqual(privatemsg_get_setting('test', $user_ids), 1, t('The role level setting was deleted, the default is now used.'));

    // Test variable_get() fallback.
    variable_set('privatemsg_setting_test3', 10);
    privatemsg_set_setting('user', $admin->uid, 'test3', 11);
    $this->assertEqual(privatemsg_get_setting('test3', $user_ids), 10, t('The variable_get() fallback is used when no other value exists.'));
    $this->assertEqual(privatemsg_get_setting('test3', $admin_ids), 11, t('The updated user level setting is used.'));

    // Explicitly set a global default now.
    privatemsg_set_setting('global', 0, 'test3', 12);
    $this->assertEqual(privatemsg_get_setting('test3', $user_ids), 12, t('The variable_get() fallback is not used when other values exists.'));

    // Test argument fallback.
    privatemsg_set_setting('user', $admin->uid, 'test4', 14);
    $this->assertEqual(privatemsg_get_setting('test4', $user_ids, 13), 13, t('The argument fallback is used when no other value exists.'));
    $this->assertEqual(privatemsg_get_setting('test4', $admin_ids), 14, t('The user level setting is used.'));

    // Explicitly set a global default now.
    privatemsg_set_setting('global', 0, 'test4', 15);
    $this->assertEqual(privatemsg_get_setting('test4', $user_ids, 13), 15, t('The variable_get() fallback is not used when other values exists.'));
  }
}

Members

Contains filters are case sensitive
Namesort descending Modifiers Type Description
PrivatemsgAPITestCase::getInfo public static function Implements getInfo().
PrivatemsgAPITestCase::getMessageFromSubject function
PrivatemsgAPITestCase::setUp function Implements setUp().
PrivatemsgAPITestCase::testGetLink function Test various use cases for privatemsg_get_link().
PrivatemsgAPITestCase::testPrivatemsgApiNewThread function
PrivatemsgAPITestCase::testPrivatemsgApiReply function
PrivatemsgAPITestCase::testSettings function Tests for the privatemsg_*_setting() functions.
PrivatemsgBaseTestCase::$profile protected property Use testing profile.