5 * Test file for privatemsg.module
9 class PrivatemsgTestCase
extends DrupalWebTestCase
{
11 * Implementation of getInfo().
16 // 'name' should start with what is being tested (menu item) followed by what about it
17 // is being tested (creation/deletion).
18 'name' => t('Privatemsg functionality.'),
19 // 'description' should be one or more complete sentences that provide more details on what
20 // exactly is being tested.
21 'description' => t('Test sending, receiving, listing, deleting messages and other features.'),
22 // 'group' should be a logical grouping of test cases, like a category. In most cases, that
23 // is the module the test case is for.
24 'group' => t('Privatemsg'),
29 * Implementation of setUp().
32 parent
::setUp('privatemsg');
36 * Test user access to /messages
37 * Create user with no 'read privatemsg' permission. Try to access mailbox and see if it gives access denied error
38 * Create user with 'read privatemsg' permission. Try to access mailbox and see if it gives allows access
40 function testPrivatemsgReadPrivatemsgPermission() {
41 $user_no_read_msg = $this->drupalCreateUser(); // set up user with default permissions (meaning: no read privatemsg permission
42 $this->drupalLogin($user_no_read_msg);
43 $this->drupalGet('messages');
44 $this->assertResponse(403, t('HTTP Response 403: Access to mailbox was blocked to user without "<em>read privatemsg</em>" permission'));
46 $user_read_msg = $this->drupalCreateUser(array('read privatemsg')); // set up user with default permissions (meaning: no read privatemsg permission
47 $this->drupalLogin($user_read_msg);
48 $this->drupalGet('messages');
49 $this->assertResponse(200, t('HTTP Response 200: Access to mailbox was authorized to user with "<em>read privatemsg</em>" permission'));
52 * Test user access to /messages/new
53 * Create user with no 'write privatemsg' permission. Try to access Write New Message page and see if it gives access denied error
54 * Create user with 'write privatemsg' permission. Try to access Write New Message page and see if it gives allows access
56 function testPrivatemsgWritePrivatemsgPermission() {
57 $user_no_write_msg = $this->drupalCreateUser(); // set up user with default permissions (meaning: no read privatemsg permission
58 $this->drupalLogin($user_no_write_msg);
59 $this->drupalGet('messages/new');
60 $this->assertResponse(403, t('HTTP Response 403: Access to Write New Message page was blocked to user without "<em>write privatemsg</em>" permission'));
62 $user_write_msg = $this->drupalCreateUser(array('write privatemsg')); // set up user with default permissions (meaning: no read privatemsg permission
63 $this->drupalLogin($user_write_msg);
64 $this->drupalGet('messages/new');
65 $this->assertResponse(200, t('HTTP Response 200: Access to Write New Message page was authorized to user with "<em>write privatemsg</em>" permission'));
69 * Test sending message from the /messages/new page between two people
71 function testPrivatemsgWriteNewPrivatemsgFormSubmit() {
73 * create an author and recipient users
75 $author = $this->drupalCreateUser(array('write privatemsg'));
76 $recipient = $this->drupalCreateUser(array('read privatemsg'));
80 * Fill navigate to privatemsg/new form, fill it out and submit
82 $this->drupalLogin($author);
83 $this->drupalGet('messages/new');
85 //assert if form is present //submit the form only if we found it
86 $xpath = '//form[@id="privatemsg-new"]';
87 if ( $this->assertTrue($this->xpath($xpath), 'Write New Message form successfuly found.', 'privatemsg') ) {
88 $edit = array( //create new message
89 'recipient' => $recipient->name
,
90 'subject' => $this->randomName(20),
91 'body' => $this->randomName(100),
94 $this->drupalPost('messages/new', $edit, t('Send message'));
95 //check if we got successful confirmation
96 if ( $this->assertText(t('A message has been sent to @recipients.', array('@recipients' => $recipient->name
)), 'Message sent confirmation displayed', 'privatemsg') ) {
98 * Login using recipient and try to read the message by going to inbox first
99 * We do the test inside this conditional block because sending messages test much pass before we proceed to reading messages
101 $this->drupalLogin($recipient);
102 $this->drupalGet('messages');
104 //assert if we see the subject of the message
105 if ( $this->assertText(t('@text', array('@text' => $edit['subject'])), 'Sent Message subject found.', 'privatemsg') ) {
106 $this->clickLink(t('@text', array('@text' => $edit['subject']))); //navigate into the message
107 $this->assertText($edit['body'], 'Found Message body.', 'privatemsg'); //confirm that we can read the message that was sent
115 * Implementation of tearDown().
117 function tearDown() {
118 //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.