#600812 by Berdir | igorik, nbz: Fixed duplicate recipients bug and added tests.
[project/privatemsg.git] / privatemsg.test
1 <?php
2 // $Id$
3 /**
4 * @file
5 * Test file for privatemsg.module
6 *
7 */
8
9 class PrivatemsgTestCase extends DrupalWebTestCase {
10 /**
11 * Implementation of getInfo().
12 */
13 function getInfo() {
14 return array
15 (
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'),
25 );
26 }
27
28 /**
29 * Implementation of setUp().
30 */
31 function setUp() {
32 parent::setUp('privatemsg');
33 }
34
35 /**
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
39 */
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'));
45
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'));
50 }
51 /**
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
55 */
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'));
61
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'));
66 }
67
68 /**
69 * Test sending message from the /messages/new page between two people
70 */
71 function testPrivatemsgWriteNewPrivatemsgFormSubmit() {
72 /**
73 * create an author and recipient users
74 */
75 $author = $this->drupalCreateUser(array('write privatemsg'));
76 $recipient = $this->drupalCreateUser(array('read privatemsg'));
77
78 /**
79 * login using author
80 * Fill navigate to privatemsg/new form, fill it out and submit
81 */
82 $this->drupalLogin($author);
83 $this->drupalGet('messages/new');
84
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),
92 );
93 //submit our message
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') ) {
97 /**
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
100 */
101 $this->drupalLogin($recipient);
102 $this->drupalGet('messages');
103
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
108 }
109 }
110 }
111 }
112
113 /**
114 * Test correct handling of read all permissions.
115 */
116 function testReadAllPermission() {
117 $author = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg'));
118 $recipient = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg'));
119 $admin = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg', 'read all private messages'));
120
121 // Create new message.
122 $edit = array(
123 'recipient' => $recipient->name,
124 'subject' => $this->randomName(20),
125 'body' => $this->randomName(100),
126 );
127 $this->drupalLogin($author);
128 $this->drupalPost('messages/new', $edit, t('Send message'));
129
130 $this->assertText(t('A message has been sent to @recipients.', array('@recipients' => $recipient->name)), t('Message sent confirmation displayed'));
131
132 $this->drupalLogin($admin);
133 $this->drupalGet('messages/view/1');
134
135 $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.'));
136
137 // Send a first response.
138 $admin_edit = array(
139 'body' => $this->randomName(100),
140 );
141 $this->drupalPost('messages/view/1', $admin_edit, t('Send message'));
142
143 // Make sure that the notice is not displayed anymore.
144 // @tod: Commented out because this does not work as expected.
145 $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.'));
146
147 // Make sure that both the existing message body and the new one are displayed.
148 $this->assertText($edit['body'], t('First message body displayed.'));
149 $this->assertText($admin_edit['body'], t('New message body displayed.'));
150
151 $admin_recipient_count = db_result(db_query("SELECT COUNT(*) FROM {pm_index} WHERE uid = %d AND thread_id = %d", $admin->uid, 1));
152 $this->assertEqual($admin_recipient_count, 2, t('Admin is listed as recipient for every message once.'));
153
154
155 // Send a second response.
156 $admin_edit2 = array(
157 'body' => $this->randomName(100),
158 );
159 $this->drupalPost('messages/view/1', $admin_edit2, t('Send message'));
160
161 // Make sure that both the existing message body and the new one are displayed.
162 $this->assertText($edit['body'], t('First message body displayed.'));
163 $this->assertText($admin_edit['body'], t('Second response body displayed.'));
164 $this->assertText($admin_edit2['body'], t('Third message body displayed.'));
165
166 $admin_recipient_count = db_result(db_query("SELECT COUNT(*) FROM {pm_index} WHERE uid = %d AND thread_id = %d", $admin->uid, 1));
167 $this->assertEqual($admin_recipient_count, 3, t('Admin is listed as recipient for every message once.'));
168
169 }
170
171 /**
172 * Tests for the flush feature
173 */
174 function testPrivatemsgFlush()
175 {
176 $author = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg'));
177 $recipient = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg'));
178
179 // Send 10 messages.
180 for ($i = 0; $i < 10; $i++) {
181 privatemsg_new_thread(array($recipient), 'Message #'. $i, 'This is the body', array('author' => $author));
182 }
183
184 // Delete message 1, 3, 4, 6, 9 for author.
185 foreach (array(1, 3, 4, 6, 9) as $pmid) {
186 privatemsg_message_change_delete($pmid, TRUE, $author);
187 }
188
189 // Delete message 1, 2, 4, 6, 8 for recipient.
190 foreach (array(1, 3, 4, 6, 9) as $pmid) {
191 privatemsg_message_change_delete($pmid, TRUE, $recipient);
192 }
193
194 // Now, mid 1, 4 and 6 have been deleted by both.
195
196 // Flush configuration, enable, delay is default, 30 days
197 variable_set('privatemsg_flush_enabled', TRUE);
198
199 // Set back the deleted timestamp 35 days back of mid 4.
200 db_query('UPDATE {pm_index} SET deleted = %d WHERE mid = 4', time() - 35 * 86400);
201 // Set back the deleted timestamp of mid 6, but only 20 back.
202 db_query('UPDATE {pm_index} SET deleted = %d WHERE mid = 6', time() - 20 * 86400);
203
204 // Run flush.
205 privatemsg_cron();
206
207 // Check if the undeleted messages are still there.
208 foreach (array(2, 3, 5, 7, 8, 9, 10) as $pmid) {
209 $message = _privatemsg_load($pmid, $author);
210 $this->assertTrue(!empty($message), t('Undeleted message #%id is still in the system', array('%id' => $pmid)));
211 }
212
213 // Check if the "recently" deleted messages are still there.
214 foreach (array(1, 6) as $pmid) {
215 $message = _privatemsg_load($pmid, $author);
216 $this->assertTrue(!empty($message), t('Deleted message #%id is still in the system', array('%id' => $pmid)));
217 }
218
219 // Mid 4 should have been flushed.
220 $message = _privatemsg_load(4, $author);
221 $this->assertTrue(empty($message), t('Message #4 has been flushed'));
222 }
223
224 /**
225 * Implementation of tearDown().
226 */
227 function tearDown() {
228 //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.
229 parent::tearDown();
230 }
231
232 }
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250