Projet

Général

Profil

Paste
Télécharger (11,6 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / privatemsg / privatemsg_roles / privatemsg_roles.test @ 13755f8d

1
<?php
2

    
3
/**
4
 * @file
5
 * This file contains tests for the privatemsg roles module
6
 */
7

    
8
/**
9
 * Test cases for the privatemsg_roles module.
10
 */
11
class PrivatemsgRolesTestCase extends PrivatemsgBaseTestCase {
12
  public static function getInfo() {
13
    return array(
14
      'name' => 'Privatemsg Roles functionality',
15
      'description' => 'Tests sending messages to roles',
16
      'group' => 'Privatemsg',
17
    );
18
  }
19

    
20
  function setUp() {
21
    parent::setUp('privatemsg', 'privatemsg_roles', 'privatemsg_filter', 'pm_block_user');
22
  }
23

    
24
  function testSendMessagetoRoleAPI() {
25
    $admin = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg', 'write privatemsg to roles'));
26
    $user1 = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg'));
27
    $user2 = $this->drupalCreateUser();
28

    
29
    // Add role of user 1 to user 2;
30
    $edit = array('roles' => $user2->roles + $user1->roles);
31
    user_save($user2, $edit);
32

    
33
    $recipient = user_role_load($user1->roles[4]);
34
    $recipient->recipient = $recipient->rid;
35
    $recipient->type = 'role';
36
    privatemsg_new_thread(array($recipient), $subject = $this->randomName(10), $body = $this->randomName(50), array('author' => $admin));
37

    
38
    $this->drupalLogin($user2);
39
    $this->drupalGet('messages');
40
    $this->assertRaw($subject . '</a> <span class="marker">new</span>', t('Message is displayed as new'));
41

    
42
    $this->clickLink($subject);
43
    $this->assertText($body, t('Thread starter body displayed.'));
44
  }
45

    
46
  function testSendMessagetoRoleCron() {
47
    $admin = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg', 'write privatemsg to roles'));
48
    $user1 = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg'));
49

    
50
    variable_set('privatemsg_recipient_small_threshold', 20);
51
    variable_set('privatemgs_cron_recipient_per_run', 20);
52

    
53
    // Create 26 users (more than are allowed to be process directly).
54
    $users = array();
55
    for ($i = 0; $i < 25; $i++) {
56
      $users[$i] = $this->drupalCreateUser();
57
      // Add role of user 1 to user 2;
58
      $edit = array('roles' => $users[$i]->roles + $user1->roles);
59
      user_save($users[$i], $edit);
60
    }
61

    
62
    $recipient = user_role_load($user1->roles[4]);
63
    $recipient->recipient = $recipient->rid;
64
    $recipient->type = 'role';
65
    privatemsg_new_thread(array($recipient), $subject = $this->randomName(10), $body = $this->randomName(50), array('author' => $admin));
66

    
67
    // Run cron.
68
    privatemsg_cron();
69

    
70
    // Test a few recipients to see if they recieved the message.
71
    foreach (array(0, 5, 18) as $uid) {
72
      $this->drupalLogin($users[$uid]);
73
      $this->drupalGet('messages');
74
      $this->assertRaw($subject . '</a> <span class="marker">new</span>', t('Message is displayed as new'));
75

    
76
      $this->clickLink($subject);
77
      $this->assertText($body, t('Thread starter body displayed.'));
78
    }
79

    
80
    // Make sure that user 20 has not yet recieved the message.
81
    $this->drupalLogin($users[20]);
82
    $this->drupalGet('messages');
83
    $this->assertNoText($subject, t('Message is not yet displayed for this user'));
84

    
85
    // Run cron again.
86
    privatemsg_cron();
87

    
88
    // Test that the remaining recipients do now see the message too.
89
    foreach (array(20, 24) as $uid) {
90
      $this->drupalLogin($users[$uid]);
91
      $this->drupalGet('messages');
92
      $this->assertRaw($subject . '</a> <span class="marker">new</span>', t('Message is displayed as new'));
93

    
94
      $this->clickLink($subject);
95
      $this->assertText($body, t('Thread starter body displayed.'));
96
    }
97
  }
98

    
99
  function testSendMessagetoRoleBatch() {
100
    $admin = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg', 'write privatemsg to roles'));
101
    $user1 = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg'));
102

    
103
    variable_set('privatemsg_recipient_small_threshold', 20);
104
    // Create 25 users (more than are allowed to be process directly).
105
    $users = array();
106
    for ($i = 0; $i < 25; $i++) {
107
      $users[$i] = $this->drupalCreateUser();
108
      // Add role of user 1 to user 2;
109
      $edit = array('roles' => $users[$i]->roles + $user1->roles);
110
      user_save($users[$i], $edit);
111
    }
112
    $this->drupalLogin($admin);
113

    
114
    // Send a message to the role shared by all users.
115
    $edit = array(
116
      'recipient' => user_role_load($user1->roles[4])->name . '[role]',
117
      'subject' => $this->randomName(10),
118
      'body[value]' => $this->randomName(50),
119
    );
120
    $this->drupalPost('messages/new', $edit, t('Send message'));
121
    $this->assertText(t('A message has been sent to @role (role).', array('@role' => user_role_load($user1->roles[4])->name)));
122

    
123
    // Test a few recipients to see if they all recieved the message.
124
    foreach ($users as $user) {
125
      $this->drupalLogin($user);
126
      $this->drupalGet('messages');
127
      $this->assertRaw($edit['subject'] . '</a> <span class="marker">new</span>', t('Message is displayed as new'));
128

    
129
      $this->clickLink($edit['subject']);
130
      $this->assertText($edit['body[value]'], t('Thread starter body displayed.'));
131
    }
132
  }
133

    
134
  function testPermission() {
135
    $user1 = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg'));
136
    $user2 = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg'));
137
    $this->drupalLogin($user1);
138

    
139
    // Send a message to the role of user 1 and 2.
140
    $edit = array(
141
      'recipient' => $user2->roles[4] . '[role]',
142
      'subject' => $this->randomName(10),
143
      'body[value]' => $this->randomName(50),
144
    );
145
    $this->drupalPost('messages/new', $edit, t('Send message'));
146
    $this->assertText(t('You must include at least one valid recipient.'));
147
  }
148

    
149
  function testSendMessageToRole() {
150
    $admin = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg', 'write privatemsg to roles'));
151
    $user1 = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg'));
152
    $user2 = $this->drupalCreateUser(array('view roles recipients'));
153

    
154
    // Add role of user 1 to user 2;
155
    $edit = array('roles' => $user2->roles + $user1->roles);
156
    user_save($user2, $edit);
157
    $this->drupalLogin($admin);
158

    
159
    // Verify autocomplete feature.
160
    $role_name = user_role_load($user1->roles[4])->name;
161
    $json = $this->drupalGet('messages/autocomplete/' . drupal_substr($role_name, 0, 2));
162
    $autocomplete = (array)json_decode($json);
163
    $this->assertEqual($autocomplete[$role_name . ', '], $role_name);
164

    
165
    // Access the form through a url that pre-fills the recipient field.
166
    $this->drupalGet('messages/new/role_4');
167

    
168
    // Send a message to the role of user 1 and 2.
169
    $edit = array(
170
      'subject' => $this->randomName(10),
171
      'body[value]' => $this->randomName(50),
172
    );
173
    $this->drupalPost(NULL, $edit, t('Send message'));
174
    $this->assertText(t('A message has been sent to @role (role).', array('@role' => user_role_load($user1->roles[4])->name)));
175

    
176
    // Log in as user1 and check that the message is listed, is marked as new
177
    // and can be marked as read.
178
    $this->drupalLogin($user1);
179
    $this->drupalGet('messages');
180
    $this->assertRaw($edit['subject'] . '</a> <span class="marker">new</span>', t('Message is displayed as new'));
181

    
182
    $this->clickLink($edit['subject']);
183
    $this->assertText($edit['body[value]'], t('Thread starter body displayed.'));
184

    
185
    // Make sure that the user does not see the role.
186
    $this->assertNoText(t('@role (role)', array('@role' => $role_name)));
187

    
188
    // Reply to the message, only admin should see this.
189
    $reply1 = array(
190
      'body[value]' => $this->randomName(50),
191
    );
192
    $this->drupalPost(NULL, $reply1, t('Send message'));
193

    
194
    $this->drupalGet('messages');
195
    $this->assertNoRaw($edit['subject'] . '</a> <span class="marker">new</span>', t('Message is displayed as read'));
196

    
197
    // Login as admin again and check if he sees the response.
198
    $this->drupalLogin($admin);
199
    $this->drupalGet('messages');
200
    $this->assertRaw($edit['subject'] . '</a> <span class="marker">new</span>', t('Message is displayed as new'));
201
    $this->clickLink($edit['subject']);
202

    
203
    $this->assertText($reply1['body[value]'], t('Reply1 is displayed'));
204

    
205
    // Reply to the message, all recipients should see this.
206
    $reply2 = array(
207
      'body[value]' => $this->randomName(50),
208
    );
209
    $this->drupalPost(NULL, $reply2, t('Send message'));
210

    
211
    // Log in as user2 and check that he only sees the messages from admin.
212
    $this->drupalLogin($user2);
213
    $this->drupalGet('messages');
214
    $this->assertRaw($edit['subject'] . '</a> <span class="marker">new</span>', t('Message is displayed as new'));
215
    $this->clickLink($edit['subject']);
216
    $this->assertText(t('@role (role)', array('@role' => $role_name)));
217

    
218
    $this->assertText($edit['body[value]'], t('Thread starter body is displayed'));
219
    $this->assertNoText($reply1['body[value]'], t('Reply1 is not displayed'));
220
    $this->assertText($reply2['body[value]'], t('Reply2 is displayed'));
221
  }
222

    
223
  function testSendMessageToRoleBlocked() {
224
    $admin = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg', 'write privatemsg to roles'));
225
    $user1 = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg'));
226
    $user2 = $this->drupalCreateUser();
227

    
228
    // Add role of user 1 to user 2;
229
    $edit = array('roles' => $user2->roles + $user1->roles);
230
    user_save($user2, $edit);
231

    
232
    // Block admin as user 2.
233
    $this->drupalLogin($user2);
234
    $this->drupalPost('messages/blocked', array('name' => $admin->name), t('Block user'));
235

    
236
    $this->drupalLogin($admin);
237

    
238
    // Send a message to the role of user 1 and 2.
239
    $edit = array(
240
      'recipient' => user_role_load($user1->roles[4])->name . '[role]',
241
      'subject' => $this->randomName(10),
242
      'body[value]' => $this->randomName(50),
243
    );
244
    $this->drupalPost('messages/new', $edit, t('Send message'));
245
    $this->assertText(t('A message has been sent to @role (role).', array('@role' => user_role_load($user1->roles[4])->name)));
246

    
247
    // Log in as user1 and check that the message is listed, is marked as new
248
    // and can be marked as read.
249
    $this->drupalLogin($user1);
250
    $this->drupalGet('messages');
251
    $this->assertRaw($edit['subject'] . '</a> <span class="marker">new</span>', t('Message is displayed as new'));
252

    
253
    $this->clickLink($edit['subject']);
254
    $this->assertText($edit['body[value]'], t('Thread starter body displayed.'));
255

    
256
    // Make sure that the user doesn't see the role recipient.
257
    $this->assertNoText(t('@role (role)', array('@role' => user_role_load($user1->roles[4])->name)));
258

    
259
    // Log in as user2 and make sure that he didn't received the messages
260
    // as he blocks admin.
261
    $this->drupalLogin($user2);
262
    $this->drupalGet('messages');
263
    $this->assertNoText($edit['subject'], t('Message is not displayed'));
264
  }
265

    
266
  function testNewUser() {
267
    $admin = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg', 'write privatemsg to roles'));
268
    $user1 = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg'));
269

    
270
    // Send a message to the role of user 1.
271
    $this->drupalLogin($admin);
272
    $edit = array(
273
      'recipient' => user_role_load($user1->roles[4])->name . '[role]',
274
      'subject' => $this->randomName(10),
275
      'body[value]' => $this->randomName(50),
276
    );
277
    $this->drupalPost('messages/new', $edit, t('Send message'));
278
    $this->assertText(t('A message has been sent to @role (role).', array('@role' => user_role_load($user1->roles[4])->name)));
279

    
280
    // Add user 2 with the same role now. The user should not see the message.
281
    $user2 = $this->drupalCreateUser();
282

    
283
    // Add role of user 1 to user 2;
284
    $edit_roles = array('roles' => $user2->roles + $user1->roles);
285
    user_save($user2, $edit_roles);
286
    $this->drupalLogin($user2);
287

    
288
    $this->drupalGet('messages');
289
    $this->assertNoText($edit['subject'], t('Newly added user does not see the old message sent to his role'));
290
  }
291
}