summaryrefslogtreecommitdiffstats
path: root/privatemsg.test
blob: ac1f225c63a4598a3dee733e453280aa4a753930 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php
// $Id$
/**
 * @file
 * Test file for privatemsg.module
 *
 */

class PrivatemsgTestCase extends DrupalWebTestCase {
  /**
   * Implementation of getInfo().
   */
  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 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 "<em>read privatemsg</em>" 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 "<em>read privatemsg</em>" 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 "<em>write privatemsg</em>" 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 "<em>write privatemsg</em>" 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();
  }

}