t('Privatemsg 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.'), // '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'), ); } /** * Implementation of setUp(). */ function setUp() { parent::setUp('privatemsg'); } /** * Test user access to /messages * Create user with no 'read privatemsg' permission. Try to access mailbox and see if it gives access denied error * Create user with 'read privatemsg' permission. Try to access mailbox and see if it gives allows access */ function testPrivatemsgReadPrivatemsgPermission() { $user_no_read_msg = $this->drupalCreateUser(); // set up user with default permissions (meaning: no read privatemsg permission $this->drupalLogin($user_no_read_msg); $this->drupalGet('messages'); $this->assertResponse(403, t('HTTP Response 403: Access to mailbox was blocked to user without "read privatemsg" permission')); $user_read_msg = $this->drupalCreateUser(array('read privatemsg')); // set up user with default permissions (meaning: no read privatemsg permission $this->drupalLogin($user_read_msg); $this->drupalGet('messages'); $this->assertResponse(200, t('HTTP Response 200: Access to mailbox was authorized to user with "read privatemsg" permission')); } /** * Test user access to /messages/new * Create user with no 'write privatemsg' permission. Try to access Write New Message page and see if it gives access denied error * Create user with 'write privatemsg' permission. Try to access Write New Message page and see if it gives allows access */ function testPrivatemsgWritePrivatemsgPermission() { $user_no_write_msg = $this->drupalCreateUser(); // set up user with default permissions (meaning: no read privatemsg permission $this->drupalLogin($user_no_write_msg); $this->drupalGet('messages/new'); $this->assertResponse(403, t('HTTP Response 403: Access to Write New Message page was blocked to user without "write privatemsg" permission')); $user_write_msg = $this->drupalCreateUser(array('write privatemsg')); // set up user with default permissions (meaning: no read privatemsg permission $this->drupalLogin($user_write_msg); $this->drupalGet('messages/new'); $this->assertResponse(200, t('HTTP Response 200: Access to Write New Message page was authorized to user with "write privatemsg" permission')); } /** * Test sending message from the /messages/new page between two people */ function testPrivatemsgWriteNewPrivatemsgFormSubmit() { /** * create an author and recipient users */ $author = $this->drupalCreateUser(array('write privatemsg')); $recipient = $this->drupalCreateUser(array('read privatemsg')); /** * login using author * Fill navigate to privatemsg/new form, fill it out and submit */ $this->drupalLogin($author); $this->drupalGet('messages/new'); //assert if form is present //submit the form only if we found it $xpath = '//form[@id="privatemsg-new"]'; if ( $this->assertTrue($this->xpath($xpath), 'Write New Message form successfuly found.', 'privatemsg') ) { $edit = array( //create new message 'recipient' => $recipient->name, 'subject' => $this->randomName(20), 'body' => $this->randomName(100), ); //submit our message $this->drupalPost('messages/new', $edit, t('Send message')); //check if we got successful confirmation if ( $this->assertText(t('A message has been sent to @recipients.', array('@recipients' => $recipient->name)), 'Message sent confirmation displayed', 'privatemsg') ) { /** * Login using recipient and try to read the message by going to inbox first * We do the test inside this conditional block because sending messages test much pass before we proceed to reading messages */ $this->drupalLogin($recipient); $this->drupalGet('messages'); //assert if we see the subject of the message if ( $this->assertText(t('@text', array('@text' => $edit['subject'])), 'Sent Message subject found.', 'privatemsg') ) { $this->clickLink(t('@text', array('@text' => $edit['subject']))); //navigate into the message $this->assertText($edit['body'], 'Found Message body.', 'privatemsg'); //confirm that we can read the message that was sent } } } } /** * Test correct handling of read all permissions. */ function testReadAllPermission() { $author = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg')); $recipient = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg')); $admin = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg', 'read all private messages')); // Create new message. $edit = array( 'recipient' => $recipient->name, 'subject' => $this->randomName(20), 'body' => $this->randomName(100), ); $this->drupalLogin($author); $this->drupalPost('messages/new', $edit, t('Send message')); $this->assertText(t('A message has been sent to @recipients.', array('@recipients' => $recipient->name)), t('Message sent confirmation displayed')); $this->drupalLogin($admin); $this->drupalGet('messages/view/1'); $this->assertText(t('This conversation is being viewed with escalated priviledges and may not be the same as shown to normal users.'), t('Notice about read all mode displayed.')); // Send a first response. $admin_edit = array( 'body' => $this->randomName(100), ); $this->drupalPost('messages/view/1', $admin_edit, t('Send message')); // Make sure that the notice is not displayed anymore. // @tod: Commented out because this does not work as expected. $this->assertNoText(t('This conversation is being viewed with escalated priviledges and may not be the same as shown to normal users.'), t('Notice about read all mode not displayed.')); // Make sure that both the existing message body and the new one are displayed. $this->assertText($edit['body'], t('First message body displayed.')); $this->assertText($admin_edit['body'], t('New message body displayed.')); $admin_recipient_count = db_result(db_query("SELECT COUNT(*) FROM {pm_index} WHERE uid = %d AND thread_id = %d", $admin->uid, 1)); $this->assertEqual($admin_recipient_count, 2, t('Admin is listed as recipient for every message once.')); // Send a second response. $admin_edit2 = array( 'body' => $this->randomName(100), ); $this->drupalPost('messages/view/1', $admin_edit2, t('Send message')); // Make sure that both the existing message body and the new one are displayed. $this->assertText($edit['body'], t('First message body displayed.')); $this->assertText($admin_edit['body'], t('Second response body displayed.')); $this->assertText($admin_edit2['body'], t('Third message body displayed.')); $admin_recipient_count = db_result(db_query("SELECT COUNT(*) FROM {pm_index} WHERE uid = %d AND thread_id = %d", $admin->uid, 1)); $this->assertEqual($admin_recipient_count, 3, t('Admin is listed as recipient for every message once.')); } /** * Tests for the flush feature */ function testPrivatemsgFlush() { $author = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg')); $recipient = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg')); // Send 10 messages. for ($i = 0; $i < 10; $i++) { privatemsg_new_thread(array($recipient), 'Message #'. $i, 'This is the body', array('author' => $author)); } // Delete message 1, 3, 4, 6, 9 for author. foreach (array(1, 3, 4, 6, 9) as $pmid) { privatemsg_message_change_delete($pmid, TRUE, $author); } // Delete message 1, 2, 4, 6, 8 for recipient. foreach (array(1, 3, 4, 6, 9) as $pmid) { privatemsg_message_change_delete($pmid, TRUE, $recipient); } // Now, mid 1, 4 and 6 have been deleted by both. // Flush configuration, enable, delay is default, 30 days variable_set('privatemsg_flush_enabled', TRUE); // Set back the deleted timestamp 35 days back of mid 4. db_query('UPDATE {pm_index} SET deleted = %d WHERE mid = 4', time() - 35 * 86400); // Set back the deleted timestamp of mid 6, but only 20 back. db_query('UPDATE {pm_index} SET deleted = %d WHERE mid = 6', time() - 20 * 86400); // Run flush. privatemsg_cron(); // Check if the undeleted messages are still there. foreach (array(2, 3, 5, 7, 8, 9, 10) as $pmid) { $message = _privatemsg_load($pmid, $author); $this->assertTrue(!empty($message), t('Undeleted message #%id is still in the system', array('%id' => $pmid))); } // Check if the "recently" deleted messages are still there. foreach (array(1, 6) as $pmid) { $message = _privatemsg_load($pmid, $author); $this->assertTrue(!empty($message), t('Deleted message #%id is still in the system', array('%id' => $pmid))); } // Mid 4 should have been flushed. $message = _privatemsg_load(4, $author); $this->assertTrue(empty($message), t('Message #4 has been flushed')); } /** * Implementation of tearDown(). */ function tearDown() { //we dont really need to do this. i'm adding it just to keep it in front of my eyes so i can memorize it. parent::tearDown(); } }