Projet

Général

Profil

Paste
Télécharger (32,1 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / forum_access / tests / forum_access_test_base.php @ 76df55b7

1
<?php
2

    
3
/**
4
 * @file
5
 * Base class with auxiliary functions for forum access module tests.
6
 */
7

    
8
/**
9
 * Base test class for the Forum Access module.
10
 */
11
class ForumAccessBaseTestCase extends ForumTestCase {
12

    
13
  protected $admin_rid;
14
  protected $webmaster_rid;
15
  protected $forum_admin_rid;
16
  protected $edndel_any_content_rid;
17
  protected $edndel_own_content_rid;
18
  protected $edit_any_content_rid;
19
  protected $edit_own_content_rid;
20
  protected $delete_any_content_rid;
21
  protected $delete_own_content_rid;
22
  protected $create_content_rid;
23
  protected $anon_rid;
24
  protected $auth_rid;
25
  protected $user1;
26
  protected $webmaster_user;
27
  protected $forum_admin_user;
28
  protected $edndel_any_content_user;
29
  protected $edndel_own_content_user;
30
  protected $edit_any_content_user;
31
  protected $edit_own_content_user;
32
  protected $delete_any_content_user;
33
  protected $delete_own_content_user;
34
  protected $create_content_user;
35
  protected $moderator;
36
  protected $time;
37
  protected $accounts;
38
  protected $rids;
39
  protected $accesses;
40

    
41
  /**
42
   * Implements setUp().
43
   */
44
  function setUp($modules = array()) {
45
    if (!isset($this->time)) {
46
      $this->time = time();
47
    }
48
    $this->timeLimit = 2345;
49
    $this->pass("timeLimit set to $this->timeLimit.");
50
    parent::setUp();
51
    if (!module_exists('forum_access')) {
52
      module_enable(array('acl', 'chain_menu_access', 'forum_access'), FALSE);
53
    }
54
    $this->assertTrue(module_exists('acl'), t('Module %module enabled!', array('%module' => 'acl')), 'Setup');
55
    $this->assertTrue(module_exists('chain_menu_access'), t('Module %module enabled!', array('%module' => 'chain_menu_access')), 'Setup');
56
    $this->assertTrue(module_exists('forum_access'), t('Module %module enabled!', array('%module' => 'forum_access')), 'Setup');
57
    $modules = array('devel', 'devel_node_access') + $modules;
58
    $files = system_rebuild_module_data();
59
    $available_modules = array();
60
    foreach ($modules as $module) {
61
      if (!empty($files[$module]) && !module_exists($module)) {
62
        $available_modules[] = $module;
63
      }
64
    }
65
    if (!empty($available_modules)) {
66
      module_enable($available_modules);
67
    }
68
    parent::resetAll();
69
    $this->accesses = array('view', 'create', 'update', 'delete');
70
  }
71

    
72
  /*
73
   * Implements additional set-up tasks.
74
   *
75
   * We cannot keep the test driver from calling setUp() for the inherited
76
   * tests, so keep setUp() as short as possible and manually call setUp2()
77
   * at the start of each test.
78
   */
79
  function setUp2() {
80
    $this->user1 = user_load(1);
81
    // Update uid 1's name and password so we know it.
82
    $password = user_password();
83
    require_once DRUPAL_ROOT . '/' . variable_get('password_inc', 'includes/password.inc');
84
    $account = array(
85
      'name' => 'user1',
86
      'pass' => user_hash_password(trim($password)),
87
    );
88
    // We cannot use user_save() here or the password would be hashed again.
89
    db_update('users')
90
      ->fields($account)
91
      ->condition('uid', 1)
92
      ->execute();
93
    // Reload and log in uid 1.
94
    $this->user1 = user_load(1, TRUE);
95
    $this->user1->pass_raw = $password;
96

    
97
    // Rebuild content access permissions
98
    $this->drupalLogin($this->user1);
99
    $this->drupalPost('admin/reports/status/rebuild', array(), t('Rebuild permissions'));
100

    
101
    if (module_exists('devel_node_access')) {
102
      // Enable Devel Node Access.
103
      $this->drupalGet('admin/config/development/devel');
104
      $this->assertResponse(200);
105
      $this->drupalPost('admin/config/development/devel', array(
106
        'devel_node_access_debug_mode' => '1',
107
      ), t('Save configuration'));
108
      $this->assertResponse(200, 'Devel Node Access configuration saved.');
109

    
110
      // Enable the second DNA block, too.
111
      $this->drupalPost('admin/structure/block/list', array(
112
        'blocks[devel_node_access_dna_user][region]' => 'footer',
113
      ), t('Save blocks'));
114
    }
115
    if (module_exists('devel')) {
116
      $this->drupalPost('admin/config/development/devel', array(
117
        'devel_error_handlers[]' => array(1, 2, 4),
118
      ), t('Save configuration'));
119
      $this->assertResponse(200, 'Devel configuration saved.');
120
      $this->drupalPost('admin/people/permissions/list', array(
121
        '1[access devel information]' => 'access devel information',
122
        '2[access devel information]' => 'access devel information',
123
      ), t('Save permissions'));
124
      $this->assertResponse(200, 'Devel permissions saved.');
125
    }
126
    /*
127
      The base class creates the following users:
128
      $this->user1                = user 1
129
      $this->admin_user           = array('administer blocks', 'administer forums', 'administer menu', 'administer taxonomy', 'create forum content')); // 'access administration pages')
130
      $this->edit_any_topics_user = array('create forum content', 'edit any forum content', 'delete any forum content', 'access administration pages')
131
      $this->edit_own_topics_user = array('create forum content', 'edit own forum content', 'delete own forum content')
132
      $this->web_user             = array()
133

134
      Remove these users and roles and create the ones we need.
135
    */
136
    user_role_delete((int) reset($this->admin_user->roles));
137
    user_role_delete((int) reset($this->edit_any_topics_user->roles));
138
    user_role_delete((int) reset($this->edit_own_topics_user->roles));
139
    user_delete($this->admin_user->uid);
140
    user_delete($this->edit_any_topics_user->uid);
141
    user_delete($this->edit_own_topics_user->uid);
142
    user_delete($this->web_user->uid);
143
    unset($this->web_user);
144

    
145
    // Get rids and uids up to 10/9.
146
    for ($i = 0; $i < 3; ++$i) {
147
      $dummy_rid = (int) $this->drupalCreateRole(array(), 'dummy');
148
      $dummy_user = $this->drupalCreateNamedUser('Dummy', array($dummy_rid));
149
      user_role_delete($dummy_rid);
150
      user_delete($dummy_user->uid);
151
    }
152

    
153
    // Create our roles.
154
    $this->admin_rid = 3;
155
    $this->webmaster_rid = (int) $this->drupalCreateRole(array('administer blocks', 'administer forums', 'administer nodes', 'administer comments', 'administer menu', 'administer taxonomy', 'create forum content', 'access content overview', 'access administration pages', 'view revisions', 'revert revisions', 'delete revisions'), '11 webmaster');
156
    $this->forum_admin_rid = (int) $this->drupalCreateRole(array('administer forums', 'create forum content', 'edit any forum content', 'delete any forum content', /* 'access content overview', 'access administration pages', */), '12 forum admin');
157
    $this->edndel_any_content_rid = (int) $this->drupalCreateRole(array('create forum content', 'edit any forum content', 'delete any forum content', 'view own unpublished content'), '13 edndel any content');
158
    $this->edndel_own_content_rid = (int) $this->drupalCreateRole(array('create forum content', 'edit own forum content', 'delete own forum content'), '14 edndel own content');
159
    $this->edit_any_content_rid = (int) $this->drupalCreateRole(array('create forum content', 'edit any forum content', 'view own unpublished content'), '15 edit any content');
160
    $this->edit_own_content_rid = (int) $this->drupalCreateRole(array('create forum content', 'edit own forum content', 'edit own comments'), '16 edit own content');
161
    $this->delete_any_content_rid = (int) $this->drupalCreateRole(array('create forum content', 'delete any forum content', 'view own unpublished content'), '17 delete any content');
162
    $this->delete_own_content_rid = (int) $this->drupalCreateRole(array('create forum content', 'delete own forum content', 'edit own comments'), '18 delete own content');  // EOC should not make any difference!
163
    $this->create_content_rid = (int) $this->drupalCreateRole(array('create forum content'), '19 create content');
164
    $this->anon_rid = DRUPAL_ANONYMOUS_RID;
165
    $this->auth_rid = DRUPAL_AUTHENTICATED_RID;
166

    
167
    // Create our users.
168
    $this->admin_user = $this->drupalCreateNamedUser('10_Administrator', array($this->admin_rid));
169
    $this->webmaster_user = $this->drupalCreateNamedUser('11_Webmaster', array($this->webmaster_rid));
170
    $this->forum_admin_user = $this->drupalCreateNamedUser('12_Forum_admin', array($this->forum_admin_rid));
171
    $this->edndel_any_content_user = $this->drupalCreateNamedUser('13_EdNDel_any_content', array($this->edndel_any_content_rid));
172
    $this->edndel_own_content_user = $this->drupalCreateNamedUser('14_EdNDel_own_content', array($this->edndel_own_content_rid));
173
    $this->edit_any_content_user = $this->drupalCreateNamedUser('15_Edit_any_content', array($this->edit_any_content_rid));
174
    $this->edit_own_content_user = $this->drupalCreateNamedUser('16_Edit_own_content', array($this->edit_own_content_rid));
175
    $this->delete_any_content_user = $this->drupalCreateNamedUser('17_Delete_any_content', array($this->delete_any_content_rid));
176
    $this->delete_own_content_user = $this->drupalCreateNamedUser('18_Delete_own_content', array($this->delete_own_content_rid));
177
    $this->create_content_user = $this->drupalCreateNamedUser('19_Create_content', array($this->create_content_rid));
178
    $this->auth_user = $this->drupalCreateNamedUser('20_Auth_only', array());
179
    $this->moderator = $this->drupalCreateNamedUser('21_Moderator', array($this->create_content_rid));
180

    
181
    $anon = drupal_anonymous_user();
182
    $anon->name = check_plain(format_username($anon));
183
    $this->accounts = array(
184
      $this->user1, $this->admin_user, $this->webmaster_user, $this->forum_admin_user,
185
      $this->edndel_any_content_user, $this->edndel_own_content_user,
186
      $this->edit_any_content_user, $this->edit_own_content_user,
187
      $this->delete_any_content_user, $this->delete_own_content_user,
188
      $this->create_content_user, $this->auth_user, $this->moderator,
189
    );
190
    $this->rids = array(
191
      $this->anon_rid, $this->auth_rid, $this->admin_rid,
192
      $this->webmaster_rid, $this->forum_admin_rid, $this->edndel_any_content_rid, $this->edndel_own_content_rid,
193
      $this->edit_any_content_rid, $this->edit_own_content_rid,
194
      $this->delete_any_content_rid, $this->delete_own_content_rid, $this->create_content_rid,
195
    );
196
    // Show settings for reference.
197
    $this->drupalGet('admin/people/permissions/list');
198
    $this->assertResponse(200, '^^^ Permissions');
199
    $this->drupalGet('admin/people', array('query' => array('sort' => 'asc', 'order' => drupal_encode_path(t('Username')))));
200
    $this->assertResponse(200, '^^^ Users');
201
  }
202

    
203
  function testForum() {
204
    // Skip test in the base class; too bad we can't skip the unnecessary setUp() call...
205
    $this->pass('==========================<br />Empty testForum() @' . (time() - $this->time));
206
  }
207

    
208
  function testAddOrphanTopic() {
209
    // Skip test in the base class; too bad we can't skip the unnecessary setUp() call...
210
    $this->pass('==========================<br />Empty testAddOrphanTopic() @' . (time() - $this->time));
211
  }
212

    
213
  function testEnableForumField() {
214
    // Skip test in the base class; too bad we can't skip the unnecessary setUp() call...
215
    $this->pass('==========================<br />Empty testEnableForumField() @' . (time() - $this->time));
216
  }
217

    
218

    
219
  /**
220
   * Asserts that a field in the current page is enabled.
221
   *
222
   * @param $id
223
   *   Id of field to assert.
224
   * @param $message
225
   *   Message to display.
226
   * @return
227
   *   TRUE on pass, FALSE on fail.
228
   */
229
  protected function assertFieldEnabled($id, $message = '') {
230
    $elements = $this->xpath('//input[@id=:id]', array(':id' => $id));
231
    return $this->assertTrue(isset($elements[0]) && empty($elements[0]['disabled']), $message ? $message : t('Field @id is enabled.', array('@id' => $id)), t('Browser'));
232
  }
233

    
234
  /**
235
   * Asserts that a field in the current page is disabled.
236
   *
237
   * @param $id
238
   *   Id of field to assert.
239
   * @param $message
240
   *   Message to display.
241
   * @return
242
   *   TRUE on pass, FALSE on fail.
243
   */
244
  protected function assertFieldDisabled($id, $message = '') {
245
    $elements = $this->xpath('//input[@id=:id]', array(':id' => $id));
246
    return $this->assertTrue(isset($elements[0]) && !empty($elements[0]['disabled']), $message ? $message : t('Field @id is disabled.', array('@id' => $id)), t('Browser'));
247
  }
248

    
249
  /**
250
   * Pass if a button with the specified label is found, and optional with the
251
   * specified index.
252
   *
253
   * @param $label
254
   *   Text in the value attribute.
255
   * @param $index
256
   *   Link position counting from zero.
257
   * @param $message
258
   *   Message to display.
259
   * @param $group
260
   *   The group this message belongs to, defaults to 'Other'.
261
   * @return
262
   *   TRUE if the assertion succeeded, FALSE otherwise.
263
   */
264
  protected function assertButton($label, $index = 0, $message = '', $group = 'Other') {
265
    $links = $this->xpath('//input[contains(@type, submit)][contains(@value, :label)]', array(':label' => $label));
266
    $message = ($message ?  $message : t('Button with label %label found.', array('%label' => $label)));
267
    return $this->assert(isset($links[$index]), $message, $group);
268
  }
269

    
270
  /**
271
   * Pass if a button with the specified label is not found.
272
   *
273
   * @param $label
274
   *   Text in the value attribute.
275
   * @param $message
276
   *   Message to display.
277
   * @param $group
278
   *   The group this message belongs to, defaults to 'Other'.
279
   * @return
280
   *   TRUE if the assertion succeeded, FALSE otherwise.
281
   */
282
  protected function assertNoButton($label, $message = '', $group = 'Other') {
283
    $links = $this->xpath('//input[contains(@type, submit)][contains(@value, :label)]', array(':label' => $label));
284
    $message = ($message ?  $message : t('Button with label %label found.', array('%label' => $label)));
285
    return $this->assert(empty($links), $message, $group);
286
  }
287

    
288
  /**
289
   * Extend drupalCreateUser() base method to accept a name as well as
290
   * multiple roles (rather than permissions).
291
   *
292
   * @param $name
293
   *   Name to assign to the user
294
   * @param $rids
295
   *   Array of Role IDs to assign to user.
296
   * @return
297
   *   A fully loaded user object with pass_raw property, or FALSE if account
298
   *   creation fails.
299
   */
300
  protected function drupalCreateNamedUser($name, $rids = array()) {
301
    // Create a user.
302
    $rids2 = array();
303
    foreach ($rids as $rid) {
304
      $rids2[$rid] = $rid;
305
    }
306
    $edit = array();
307
    $edit['name']   = $name;
308
    $edit['mail']   = $edit['name'] . '@example.com';
309
    $edit['roles']  = $rids2;
310
    $edit['pass']   = user_password();
311
    $edit['status'] = 1;
312

    
313
    $account = user_save(drupal_anonymous_user(), $edit);
314

    
315
    $this->assertTrue(!empty($account->uid), t('User %name created, uid=%uid.', array('%name' => $edit['name'], '%uid' => $account->uid)), t('User login'));
316
    if (empty($account->uid)) {
317
      return FALSE;
318
    }
319

    
320
    // Add the raw password so that we can log in as this user.
321
    $account->pass_raw = $edit['pass'];
322
    return $account;
323
  }
324

    
325
  protected function createForumTopicWithTitle($forum, $title) {
326
    $node = $this->createForumTopic((array) $forum);
327
    $node = node_load($node->nid);
328
    $node->title = $title;
329
    $node->shadow = FALSE;
330
    node_save($node);
331
    return $node;
332
  }
333

    
334
  protected function createForumCommentWithText($node, $text) {
335
    static $cid = 0;
336
    $this->drupalPost("node/$node->nid", array(
337
      'comment_body[' . LANGUAGE_NONE . '][0][value]' => $text,
338
    ), t('Save'));
339
    $this->assertResponse(200);
340
    $this->assertText($text, "Comment '$text' found, too.");
341
    return comment_load(++$cid);
342
  }
343

    
344
  protected function isFieldChecked($id) {
345
    $elements = $this->xpath('//input[@id=:id]', array(':id' => $id));
346
    return isset($elements[0]) && !empty($elements[0]['checked']);
347
  }
348

    
349

    
350
  /**
351
   * Check the set of permissions in one forum.
352
   *
353
   * @param $forum
354
   *  An associative array describing the forum.
355
   * @param $is_default
356
   *  Set to TRUE if this is the default forum (without any moderator).
357
   */
358
  function checkForum($forum, $is_default = FALSE) {
359
    $this->drupalLogin($this->user1);
360

    
361
    $this->drupalGet("admin/structure/forum/edit/forum/$forum->tid");
362
    $this->assertResponse(200, "^^^ '$forum->name' exists.");
363

    
364
    foreach ($this->accounts as $key => $account) {
365
      // Retrieve the access settings for this account.
366
      $account->access = array();
367
      foreach ($account->roles as $rid => $role_name) {
368
        foreach ($this->accesses as $access) {
369
          if ($this->isFieldChecked("edit-forum-access-grants-checkboxes-$access-$rid")) {
370
            $account->access[$access] = $access;
371
          }
372
        }
373
      }
374
    }
375

    
376
    foreach ($this->accounts as $key => $account) {
377
      // Create a topic and a comment for this account to experiment with.
378
      $account->node = $this->createForumTopicWithTitle($forum, "Topic for $account->name");
379
      $account->comment = $this->createForumCommentWithText($account->node, "Comment for $account->name");
380
    }
381
    // Show the topic list.
382
    $this->drupalGet("forum/$forum->tid");
383
    $this->assertResponse(200, "^^^ '$forum->name' initial topics.");
384

    
385
    foreach ($this->accounts as $key => $account) {
386
      $is_super_user = user_access('bypass node access', $account) || ($account->uid == $this->moderator->uid && !$is_default);
387

    
388
      if (!empty($account->uid)) {
389
        $this->drupalLogin($account);
390
      }
391
      else {
392
        $this->drupalLogout();
393
      }
394

    
395
      // Check whether we have an 'Add new Forum topic' link.
396
      $this->drupalGet('forum');
397

    
398
      if (empty($account->access['view']) && !$is_super_user) {
399
        $this->assertResponse(403, "^^^ $account->name cannot see the Forum Overview");
400
      }
401
      elseif ((empty($account->access['create']) || !user_access('create forum content', $account)) && !$is_super_user) {
402
        $this->assertResponse(200, 'Forum Overview');
403
        $this->assertNoLink(t('Add new Forum topic'), "^^^ $account->name cannot post in the '$forum->name'.");
404
      }
405
      else {
406
        $this->assertResponse(200, 'Forum Overview');
407
        $this->assertLink($forum->name, 0, "^^^ $account->name can see the '$forum->name'.");
408
        $this->assertLink(t('Add new Forum topic'), 0, "^^^ $account->name can post in the '$forum->name'.");
409
      }
410

    
411
      foreach (array('any', 'own') as $test_type) {
412

    
413
        // Check whether we can View our topic.
414
        $comment = &$account->comment;
415
        $node = &$account->node;
416
        if ((empty($account->access['view']) || !user_access('access content', $account)) && !$is_super_user) {
417
          $this->drupalGet("forum/$forum->tid");
418
          $this->assertResponse(404, "^^^ $account->name cannot access '$forum->name'.");
419
          $this->drupalGet("node/$node->nid");
420
          $this->assertResponse(403, "^^^ $account->name cannot access $test_type topic.");
421
          $this->drupalGet("node/$node->nid/edit");
422
          $this->assertResponse(403, "$account->name cannot edit $test_type topic (not accessible).");
423
          $this->drupalGet("comment/$comment->cid");
424
          $this->assertResponse(403, "^^^ $account->name cannot access comment '$comment->subject'.");
425
        }
426
        else {
427
          $this->drupalGet("forum/$forum->tid");
428
          $this->assertResponse(200, "^^^ '$forum->name' as $account->name.");
429

    
430
          $this->assertLink($node->title);
431
          $this->clickLink($node->title);
432
          $this->assertResponse(200, "^^^ $account->name can access $test_type topic.");
433
          $this->assertText($comment->subject, "Comment '$comment->subject' found, too.");
434

    
435

    
436
          // Check comment visibility.
437
          if (!$is_super_user && (!user_access('access comments', $account) || empty($account->access['view'])) && !user_access('administer comments', $account)) {
438
            $this->assertNoLinkByHref("/comment/$comment->cid#comment-$comment->cid");
439
            $this->drupalGet("comment/$comment->cid");
440
            $this->assertResponse(403, "^^^ $account->name cannot see comment '$comment->subject'.");
441
          }
442
          else {
443
            $this->assertLinkByHref(url("comment/$comment->cid", array('fragment' => "comment-$comment->cid")));
444
            // Check post comment / reply link.
445
            if (((!user_access('post comments', $account) && !user_access('post comments without approval', $account)) || empty($account->access['create'])) && !$is_super_user) {
446
              if (!$account->uid) {
447
                $this->assertLinkByHref("/user/login?destination=node/$node->nid#comment-form");
448
              }
449
              $this->assertNoLink(t('Add new comment'));
450
              $this->assertNoText(t('Add new comment'));
451
              $this->assertNoLink(t('reply'));
452
              $this->drupalGet("comment/$comment->cid");
453
              $this->assertResponse(200, '^^^ ' . "Comment '$comment->subject' is visible to $account->name'.");
454
              $this->drupalGet("comment/reply/$node->nid");
455
              $this->assertResponse(403);
456
              $this->drupalGet("comment/reply/$node->nid/$comment->cid");
457
              $this->assertResponse(403);
458
            }
459
            else {
460
              $this->assertText(t('Add new comment'));
461
              $this->assertLink(t('reply'));
462
              $this->assertLinkByHref("comment/reply/$node->nid/$comment->cid");
463
              $this->drupalGet("comment/reply/$node->nid/$comment->cid");
464
              $this->assertResponse(200);
465
            }
466

    
467
            // Check comment edit links.
468
            global $user;
469
            drupal_save_session(FALSE);
470
            $user_save = $user;
471
            $user = $account;
472
            // We ignore the 'edit own comments' permission!
473
            $comment_access_edit = FALSE;  // comment_access('edit', $comment);
474
            $user = $user_save;
475
            drupal_save_session(TRUE);
476
            $this->drupalGet("comment/$comment->cid");
477
            $this->assertResponse(200);
478
            if (empty($account->access['update']) && !$is_super_user && !$comment_access_edit && !user_access('administer comments', $account) && !user_access('edit any forum content', $account) && !($account->uid == $comment->uid && user_access('edit own forum content', $account))) {
479
              $this->assertNoLink(t('edit'));
480
              $this->drupalGet("comment/$comment->cid/edit");
481
              $this->assertResponse(403);
482
            }
483
            else {
484
              $this->assertLink(t('edit'));
485
              $this->clickLink(t('edit'));
486
              $this->assertResponse(200);
487
              $this->drupalGet("comment/$comment->cid/edit");
488
              $this->assertResponse(200);
489
              $this->assertText($comment->subject);
490
              $comment->title .= ' (updated)';
491
              $this->drupalPost("comment/$comment->cid/edit", array(
492
                'subject' => $comment->subject,
493
              ), t('Save'));
494
              $this->assertText(t("Your comment has been posted."));  // It ought to say 'updated'!
495
            }
496

    
497
            // Check comment delete links.
498
            $this->drupalGet("comment/$comment->cid");
499
            if ((empty($account->access['delete'])) && !$is_super_user && !user_access('administer comments', $account) && !user_access('delete any forum content', $account) && !($account->uid == $comment->uid && user_access('delete own forum content', $account))) {
500
              $this->assertNoLink(t('delete'));
501
              $this->drupalGet("comment/$comment->cid/delete");
502
              $this->assertResponse(403);
503
            }
504
            else {
505
              $this->assertText($comment->subject);
506
              $this->assertLink(t('delete'));
507
              $this->clickLink(t('delete'));
508
              $this->assertResponse(200);
509
              $this->drupalGet("comment/$comment->cid/delete");
510
              $this->assertResponse(200);
511
              $this->drupalPost("comment/$comment->cid/delete", array(), t('Delete'));
512
              $this->assertText(t('The comment and all its replies have been deleted.'));
513
              $this->assertNoText($comment->subject);
514
              unset($account->comment);
515
            }
516
          }
517

    
518
          // Check whether we can Edit our topic.
519
          $this->drupalGet("node/$node->nid");
520
          $this->assertResponse(200);
521
          if (empty($account->access['update']) && !user_access('edit any forum content', $account) &&
522
              !(user_access('edit own forum content', $account) && $node->uid == $account->uid) &&
523
              !$is_super_user) {
524
            $this->assertNoLink(t('Edit'));
525
            $this->drupalGet("node/$node->nid/edit");
526
            $this->assertResponse(403, "$account->name cannot edit $test_type topic.");
527
          }
528
          else {
529
            $this->assertLink(t('Edit'));
530
            $this->clickLink(t('Edit'));
531
            $this->assertResponse(200, "^^^ $account->name can edit $test_type topic.");
532

    
533
            // Check that moderator gets administrator properties.
534
            if ($is_super_user || user_access('administer nodes', $account)) {
535
              $this->assertText(t('Revision information'), "$account->name sees Revision information.");
536
              $this->assertText(t('Comment settings'), "$account->name sees Comment settings.");
537
              $this->assertText(t('Publishing options'), "$account->name sees Publishing options.");
538
              if (user_access('administer nodes', $account)) {
539
                $this->assertText(t('Menu settings'), "$account->name sees Menu settings.");
540
                $this->assertText(t('Authoring information'), "$account->name sees Authoring information.");
541
              }
542
              else {
543
                $this->assertNoText(t('Menu settings'), "$account->name does not see Menu settings.");
544
                $this->assertNoText(t('Authoring information'), "$account->name does not see Authoring information.");
545
              }
546
            }
547
            else {
548
              $this->assertNoText(t('Revision information'), "$account->name does not see Revision information.");
549
              $this->assertNoText(t('Comment settings'), "$account->name does not see Comment settings.");
550
              $this->assertNoText(t('Publishing options'), "$account->name does not see Publishing options.");
551
              $this->assertNoText(t('Menu settings'), "$account->name does not see Menu settings.");
552
              $this->assertNoText(t('Authoring information'), "$account->name does not see Authoring information.");
553
            }
554

    
555
            // Check whether we can Delete our topic.
556
            if (empty($account->access['delete']) && !user_access('delete any forum content', $account) &&
557
                !(user_access('delete own forum content', $account) && $node->uid == $account->uid) &&
558
                !$is_super_user) {
559
              $this->assertNoButton(t('Delete'), 0, $account->name . ' has no Delete button.');
560
            }
561
            else {
562
              $this->assertButton(t('Delete'), 0, $account->name . ' has a Delete button.');
563
            }
564

    
565
            // Change the title.
566
            $node->title = $node->title . ' (changed)';
567
            $this->drupalPost("node/$node->nid/edit", array(
568
              'title' => $node->title,
569
            ), t('Save'));
570
            $this->assertText(t('Forum topic !title has been updated.', array('!title' => $node->title)));
571
          }
572

    
573
          // Check whether we can delete the topic.
574
          if (empty($account->access['delete']) && !user_access('delete any forum content', $account) &&
575
              !(user_access('delete own forum content', $account) && $node->uid == $account->uid) &&
576
              !$is_super_user) {
577
            $this->drupalGet("node/$node->nid/delete");
578
            $this->assertResponse(403, "$account->name cannot delete $test_type topic.");
579
          }
580
          else {
581
            $this->drupalPost("node/$node->nid/delete", array(), t('Delete'));
582
            $this->assertText(t('Forum topic !title has been deleted.', array('!title' => $node->title)));
583
          }
584
        }
585

    
586
        if ($test_type == 'any' && (!empty($account->access['view']) || $is_super_user)) {
587
          // Check whether we can create a topic.
588
          if ((empty($account->access['create']) || !user_access('create forum content', $account)) && !$is_super_user) {
589
            $this->drupalGet('forum');
590
            if (empty($account->uid)) {
591
              $this->assertLinkByHref('/user/login?destination=forum');
592
            }
593
            else {
594
              $this->assertResponse(200, "^^^ $account->name can see the Forum Overview, but...");
595
              $this->assertText(t('You are not allowed to post new content in the forum.'));
596
            }
597
            $this->drupalGet("node/add/forum/$forum->tid");
598
            $this->assertResponse(403, "^^^ $account->name cannot create a forum topic in '$forum->name'.");
599
            break;
600
          }
601
          else {
602
            $this->drupalGet('forum');
603
            $this->assertNoText(t('You are not allowed to post new content in the forum.'));
604
            $this->assertLink(t('Add new Forum topic'));
605
            $this->clickLink(t('Add new Forum topic'));
606
            $this->assertResponse(200, "^^^ $account->name can create a forum topic.");
607
            $this->drupalGet("node/add/forum/$forum->tid");
608
            $this->assertResponse(200, "^^^ $account->name can create a forum topic in '$forum->name'.");
609
            $this->drupalGet('forum');
610
            $this->assertLink(t('Add new Forum topic'));
611
            $this->drupalPost("node/add/forum/$forum->tid", array(
612
              'title' => "Topic 1 by $account->name",
613
            ), t('Save'));
614
            $node = $account->node = $this->createForumTopicWithTitle($forum, "Topic 2 by $account->name");
615
            $this->drupalGet('node/' . $node->nid );
616

    
617
            $account->comment = $this->createForumCommentWithText($node, "Comment by $account->name");
618

    
619
            $this->drupalGet("forum/$forum->tid");
620
            $this->assertResponse(200, "^^^ '$forum->name' as $account->name (own topic).");
621
          }
622
        }
623
      }
624
    }
625

    
626
    $this->drupalLogin($this->user1);
627
    $this->drupalGet("forum/$forum->tid");
628
    $this->assertResponse(200, "^^^ '$forum->name' remaining topics.");
629
  }
630

    
631

    
632
  function createFAForum($id, $tag, $description, array $accesses) {
633
    $edit = array(
634
      'name' => "Forum $id $tag",
635
      'description' => $description,
636
      'parent[0]' => 0,
637
      'weight' => '0',
638
    );
639
    $forum = (object) ($edit + array('tid' => 2));
640
    $edit["forum_access[grants][checkboxes][view][1]"] = FALSE;
641
    $edit["forum_access[grants][checkboxes][view][2]"] = FALSE;
642
    $edit["forum_access[grants][checkboxes][create][2]"] = FALSE;
643
    foreach (array($this->webmaster_rid, $this->forum_admin_rid, $this->edit_any_content_rid, $this->edit_own_content_rid, $this->create_content_rid, $this->admin_rid, $this->anon_rid, $this->auth_rid) as $rid) {
644
      foreach ($accesses as $access) {
645
        $key = "$access-$rid";
646
        if (array_search($key, array('update-3', 'delete-3')) === FALSE) {
647
          $edit["forum_access[grants][checkboxes][$access][$rid]"] = TRUE;
648
        }
649
      }
650
    }
651
    $this->drupalPost('admin/structure/forum/add/forum', $edit, t('Save'));
652
    $this->assertResponse(200, "'$forum->name' added.");
653

    
654
    // Set moderator.
655
    $acl_id = _forum_access_get_acl($forum->tid);
656
    $this->assertNotNull($acl_id);
657
    acl_add_user($acl_id, $this->moderator->uid);
658
    $this->assertTrue(acl_has_user($acl_id, $this->moderator->uid), t('User %moderator is moderator.', array('%user' => $this->moderator->uid)));
659

    
660
    // Show the result.
661
    $this->drupalGet("admin/structure/forum/edit/forum/$forum->tid");
662
    $this->assertResponse(200, "^^^ '$forum->name' exists, with these settings.");
663
    $this->drupalGet('forum');
664
    $this->assertResponse(200, 'Forum Overview');
665
    return $forum;
666
  }
667

    
668
  function createAndCheckForum($id, $tag, $description, array $accesses) {
669
    $this->setUp2();
670
    taxonomy_term_delete(1);
671
    $this->pass("#########################<br />#$id - $tag Configuration test @" . (time() - $this->time), '<a id="jump1" href="#jump2">/\<br />######<br />\/</a>');
672
    $forum = $this->createFAForum($id, $tag, $description, $accesses);
673
    $this->checkForum($forum);
674
    $this->pass("#########################<br />#$id - END $tag Configuration test @" . (time() - $this->time), '<a id="jump2" href="#jump3">/\<br />######<br />\/</a>');
675
  }
676

    
677
  /**
678
   * Makes Devel's dpm() work inside this test, if Devel is installed.
679
   */
680
  function dpm($input, $name = NULL) {
681
    if (module_exists('devel') && user_access('access devel information')) {
682
      $export = kprint_r($input, TRUE, $name);
683
      $trigger_error = 'trigger_error';
684
      $trigger_error($export);
685
    }
686
  }
687

    
688
  /**
689
   * Writes a krumo entry into the watchdog log, if Devel is installed.
690
   */
691
  function wpm($input, $name = NULL) {
692
    if (module_exists('devel') && user_access('access devel information')) {
693
      $export = kprint_r($input, TRUE, $name);
694
      watchdog('debug', $export);
695
    }
696
  }
697
}