forum.test 16.3 KB
Newer Older
1
2
3
4
<?php
// $Id$

class ForumTestCase extends DrupalWebTestCase {
5
  protected $admin_user;
6
7
8
9
10
11
12
13
  protected $own_user;
  protected $any_user;
  protected $nid_user;
  protected $container;
  protected $forum;
  protected $root_forum;
  protected $nids;

14
  public static function getInfo() {
15
    return array(
16
17
18
      'name' => 'Forum functionality',
      'description' => 'Create, view, edit, delete, and change forum entries and verify its consistency in the database.',
      'group' => 'Forum',
19
20
21
22
23
24
25
26
27
    );
  }

  /**
   * Enable modules and create users with specific permissions.
   */
  function setUp() {
    parent::setUp('taxonomy', 'comment', 'forum');
    // Create users.
28
    $this->admin_user = $this->drupalCreateUser(array('administer blocks', 'administer forums', 'administer menu', 'administer taxonomy', 'create forum content')); // 'access administration pages'));
29
30
31
32
33
34
35
36
37
38
    $this->own_user = $this->drupalCreateUser(array('create forum content', 'edit own forum content', 'delete own forum content'));
    $this->any_user = $this->drupalCreateUser(array('create forum content', 'edit any forum content', 'delete any forum content', 'access administration pages'));
    $this->nid_user = $this->drupalCreateUser(array());
  }

  /**
   * Login users, create forum nodes, and test forum functionality through the admin and user interfaces.
   */
  function testForum() {
    // Do the admin tests.
39
    $this->doAdminTests($this->admin_user);
40
41
42
    // Generate topics to populate the active forum block.
    $this->generateForumTopics($this->forum);

43
44
    // Login the nid user to view the forum topics and generate an active forum
    // topics list.
45
46
47
48
49
    $this->drupalLogin($this->nid_user);
    $this->viewForumTopics($this->nids);

    // Do basic tests for the any forum user.
    $this->doBasicTests($this->any_user, TRUE);
50

51
52
53
54
55
    // Create another forum node for the any forum user.
    $node = $this->createForumTopic($this->forum, FALSE);

    // Do basic tests for the own forum user.
    $this->doBasicTests($this->own_user, FALSE);
56

57
58
59
60
61
62
63
64
65
    // Verify the own forum user only has access to the forum view node.
    $this->verifyForums($this->any_user, $node, FALSE, 403);
    // Create another forum node for the own forum user.
    $node = $this->createForumTopic($this->forum, FALSE);

    // Login the any forum user.
    $this->drupalLogin($this->any_user);
    // Verify the any forum user has access to all the forum nodes.
    $this->verifyForums($this->own_user, $node, TRUE);
66
67
68
69
70

    // Verify the topic and post counts on the forum page.
    $this->drupalGet('forum');
    $this->assertRaw("<td class=\"topics\">\n          6                  </td>");
    $this->assertRaw('<td class="posts">6</td>');
71
72
73
74
75
76
77

    // Test loading multiple forum nodes on the front page.
    $this->drupalLogin($this->drupalCreateUser(array('administer content types', 'create forum content')));
    $this->drupalPost('admin/structure/types/manage/forum', array('node_options[promote]' => 'promote'), t('Save content type'));
    $this->createForumTopic($this->forum, FALSE);
    $this->createForumTopic($this->forum, FALSE);
    $this->drupalGet('node');
78
79
80
81
82
83
84
85
86
87

    // Test adding a comment to a forum topic.
    $node = $this->createForumTopic($this->forum, FALSE);
    $this->drupalPost("node/$node->nid", array('comment' => $this->randomName()), t('Save'));
    $this->assertResponse(200);

    // Test editing a forum topic that has a comment.
    $this->drupalLogin($this->any_user);
    $this->drupalPost("node/$node->nid/edit", array(), t('Save'));
    $this->assertResponse(200);
88
89
90
91
92
93
94
95
96
97
98
99
100
  }

  /**
   * Run admin tests on the admin user.
   *
   * @param object $user The logged in user.
   */
  private function doAdminTests($user) {
    // Login the user.
    $this->drupalLogin($user);

    // Enable the active forum block.
    $edit = array();
101
    $edit['forum_active[region]'] = 'sidebar_second';
102
    $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
103
    $this->assertResponse(200);
104
    $this->assertText(t('The block settings have been updated.'), t('Active forum topics forum block was enabled'));
105
106
107

    // Enable the new forum block.
    $edit = array();
108
    $edit['forum_new[region]'] = 'sidebar_second';
109
    $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
110
111
112
113
    $this->assertResponse(200);
    $this->assertText(t('The block settings have been updated.'), t('[New forum topics] Forum block was enabled'));

    // Retrieve forum menu id.
114
    $mlid = db_query_range("SELECT mlid FROM {menu_links} WHERE link_path = 'forum' AND menu_name = 'navigation' AND module = 'system' ORDER BY mlid ASC", 0, 1)->fetchField();
115
116
117

    // Add forum to navigation menu.
    $edit = array();
118
    $this->drupalPost('admin/structure/menu/manage/navigation', $edit, t('Save configuration'));
119
120
121
122
123
124
125
126
127
    $this->assertResponse(200);

    // Edit forum taxonomy.
    // Restoration of the settings fails and causes subsequent tests to fail.
    $this->container = $this->editForumTaxonomy();
    // Create forum container.
    $this->container = $this->createForum('container');
    // Create forum inside the forum container.
    $this->forum = $this->createForum('forum', $this->container['tid']);
128
129
130
131
    // Create second forum in container.
    $this->delete_forum = $this->createForum('forum', $this->container['tid']);
    // Delete this second form.
    $this->deleteForum($this->delete_forum['tid']);
132
133
134
135
136
137
138
139
140
141
    // Create forum at the top (root) level.
    $this->root_forum = $this->createForum('forum');
  }

  /**
   * Edit the forum taxonomy.
   */
  function editForumTaxonomy() {
    // Backup forum taxonomy.
    $vid = variable_get('forum_nav_vocabulary', '');
142
    $original_settings = taxonomy_vocabulary_load($vid);
143
144
145
146
147
148
149
150

    // Generate a random name/description.
    $title = $this->randomName(10);
    $description = $this->randomName(100);

    $edit = array(
      'name' => $title,
      'description' => $description,
151
      'machine_name' => drupal_strtolower(drupal_substr($this->randomName(), 3, 9)),
152
153
154
    );

    // Edit the vocabulary.
155
    $this->drupalPost('admin/structure/taxonomy/' . $vid . '/edit', $edit, t('Save'));
156
157
158
159
    $this->assertResponse(200);
    $this->assertRaw(t('Updated vocabulary %name.', array('%name' => $title)), t('Vocabulary was edited'));

    // Grab the newly edited vocabulary.
160
    entity_get_controller('taxonomy_vocabulary')->resetCache();
161
    $current_settings = taxonomy_vocabulary_load($vid);
162
163

    // Make sure we actually edited the vocabulary properly.
164
165
    $this->assertEqual($current_settings->name, $title, t('The name was updated'));
    $this->assertEqual($current_settings->description, $description, t('The description was updated'));
166

167
    // Restore the original vocabulary.
168
    taxonomy_vocabulary_save($original_settings);
169
170
    drupal_static_reset('taxonomy_vocabulary_load');
    $current_settings = taxonomy_vocabulary_load($vid);
171
    $this->assertEqual($current_settings->name, $original_settings->name, 'The original vocabulary settings were restored');
172
173
174
175
176
  }

  /**
   * Create a forum container or a forum.
   *
177
178
179
180
181
182
183
   * @param $type
   *   Forum type (forum container or forum).
   * @param $parent
   *   Forum parent (default = 0 = a root forum; >0 = a forum container or
   *   another forum).
   * @return
   *   taxonomy_term_data created.
184
185
186
187
188
189
190
191
192
193
194
195
196
197
   */
  function createForum($type, $parent = 0) {
    // Generate a random name/description.
    $name = $this->randomName(10);
    $description = $this->randomName(100);

    $edit = array(
      'name' => $name,
      'description' => $description,
      'parent[0]' => $parent,
      'weight' => '0',
    );

    // Create forum.
198
    $this->drupalPost('admin/structure/forum/add/' . $type, $edit, t('Save'));
199
200
    $this->assertResponse(200);
    $type = ($type == 'container') ? 'forum container' : 'forum';
201
    $this->assertRaw(t('Created new @type %term.', array('%term' => $name, '@type' => t($type))), t(ucfirst($type) . ' was created'));
202
203

    // Verify forum.
204
    $term = db_query("SELECT * FROM {taxonomy_term_data} t WHERE t.vid = :vid AND t.name = :name AND t.description = :desc", array(':vid' => variable_get('forum_nav_vocabulary', ''), ':name' => $name, ':desc' => $description))->fetchAssoc();
205
    $this->assertTrue(!empty($term), 'The ' . $type . ' exists in the database');
206
207
208

    // Verify forum hierarchy.
    $tid = $term['tid'];
209
    $parent_tid = db_query("SELECT t.parent FROM {taxonomy_term_hierarchy} t WHERE t.tid = :tid", array(':tid' => $tid))->fetchField();
210
    $this->assertTrue($parent == $parent_tid, 'The ' . $type . ' is linked to its container');
211
212
213
214

    return $term;
  }

215
216
217
  /**
   * Delete a forum.
   *
218
219
   * @param $tid
   *   The forum ID.
220
221
   */
  function deleteForum($tid) {
222
    // Delete the forum.
223
    $this->drupalPost('admin/structure/forum/edit/forum/' . $tid, array(), t('Delete'));
224
    $this->drupalPost(NULL, array(), t('Delete'));
225

226
    // Assert that the forum no longer exists.
227
    $this->drupalGet('forum/' . $tid);
228
229
230
    $this->assertRaw(t('No forums defined'), 'The forum was not found');
  }

231
232
233
  /**
   * Run basic tests on the indicated user.
   *
234
235
236
237
   * @param $user
   *   The logged in user.
   * @param $admin
   *   User has 'access administration pages' privilege.
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
   */
  private function doBasicTests($user, $admin) {
    // Login the user.
    $this->drupalLogin($user);
    // Attempt to create forum topic under a container.
    $this->createForumTopic($this->container, TRUE);
    // Create forum node.
    $node = $this->createForumTopic($this->forum, FALSE);
    // Verify the user has access to all the forum nodes.
    $this->verifyForums($user, $node, $admin);
  }

  /**
   * Create forum topic.
   *
   * @param array $forum Forum array.
   * @param boolean $container True if $forum is a container.
   * @return object Topic node created.
   */
  function createForumTopic($forum, $container = FALSE) {
    // Generate a random subject/body.
    $title = $this->randomName(20);
    $body = $this->randomName(200);
261
262
263
264

    // Without this being set, post variable equals the first non-blank in
    // select items list.
    $tid = $forum['tid'];
265

266
    $langcode = FIELD_LANGUAGE_NONE;
267
    $edit = array(
268
      "title[$langcode][0][value]" => $title,
269
      "body[$langcode][0][value]" => $body,
270
      "taxonomy_forums[$langcode]" => $tid,
271
272
    );

273
274
275
276
    // TODO The taxonomy select value is set by drupal code when the tid is part
    // of the url. However, unless a tid is passed in the edit array, when
    // drupalPost() runs, the select value is not preserved. Instead, the post
    // variables seem to pick up the first non-blank value in the select list.
277
278
279
280
281
    // Create forum topic.
    $this->drupalPost('node/add/forum/', $edit, t('Save'));
    $type = t('Forum topic');
    if ($container) {
      $this->assertNoRaw(t('@type %title has been created.', array('@type' => $type, '%title' => $title)), t('Forum topic was not created'));
282
      $this->assertRaw(t('The item %title is only a forum container.', array('%title' => $forum['name'])), t('Error message was shown'));
283
284
285
286
      return;
    }
    else {
      $this->assertRaw(t('@type %title has been created.', array('%title' => $title, '@type' => $type)), t('Forum topic was created'));
287
      $this->assertNoRaw(t('The item %title is only a forum container.', array('%title' => $forum['name'])), t('No error message was shown'));
288
289
290
    }

    // Retrieve node object.
291
    $node = $this->drupalGetNodeByTitle($title);
292
    $this->assertTrue($node != NULL, t('Node @title was loaded', array('@title' => $title)));
293
294

    // View forum topic.
295
    $this->drupalGet('node/' . $node->nid);
296
297
298
299
300
301
302
    $this->assertRaw($title, t('Subject was found'));
    $this->assertRaw($body, t('Body was found'));

    return $node;
  }

  /**
303
   * Verify the logged in user has access to a forum nodes.
304
   *
305
306
307
308
309
310
311
312
   * @param $node_user
   *   The user who creates the node.
   * @param $node
   *   The node being checked.
   * @param $admin
   *   Boolean to indicate whether the user can 'access administration pages'.
   * @param $response
   *   The exptected HTTP response code.
313
   */
314
  private function verifyForums($node_user, $node, $admin, $response = 200) {
315
316
317
318
319
320
321
322
323
    $crumb = '›';
    $quote = '&#039;';

    $response2 = ($admin) ? 200 : 403;

    // View forum help node.
    $this->drupalGet('admin/help/forum');
    $this->assertResponse($response2);
    if ($response2 == 200) {
324
      $this->assertTitle(t('Forum | Drupal'), t('Forum help title was displayed'));
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
      $this->assertText(t('Forum'), t('Forum help node was displayed'));
    }

    // Verify the forum blocks were displayed.
    $this->drupalGet('');
    $this->assertResponse(200);
    $this->assertText(t('New forum topics'), t('[New forum topics] Forum block was displayed'));

    // View forum container page.
    $this->verifyForumView($this->container);
    // View forum page.
    $this->verifyForumView($this->forum, $this->container);
    // View root forum page.
    $this->verifyForumView($this->root_forum);

    // View forum node.
341
    $this->drupalGet('node/' . $node->nid);
342
    $this->assertResponse(200);
343
    $this->assertTitle($node->title[FIELD_LANGUAGE_NONE][0]['value'] . ' | Drupal', t('Forum node was displayed'));
344
    $this->assertText(t('Home ' . $crumb . ' Forums ' . $crumb . ' @container ' . $crumb . ' @forum', array('@container' => $this->container['name'], '@forum' => $this->forum['name'])), t('Breadcrumbs were displayed'));
345
346

    // View forum edit node.
347
    $this->drupalGet('node/' . $node->nid . '/edit');
348
349
    $this->assertResponse($response);
    if ($response == 200) {
350
      $this->assertTitle('Edit Forum topic ' . $node->title[FIELD_LANGUAGE_NONE][0]['value'] . ' | Drupal', t('Forum edit node was displayed'));
351
352
353
354
355
    }

    if ($response == 200) {
      // Edit forum node (including moving it to another forum).
      $edit = array();
356
      $langcode = FIELD_LANGUAGE_NONE;
357
      $edit["title[$langcode][0][value]"] = 'node/' . $node->nid;
358
      $edit["body[$langcode][0][value]"] = $this->randomName(256);
359
      // Assume the topic is initially associated with $forum.
360
      $edit["taxonomy_forums[$langcode]"] = $this->root_forum['tid'];
361
      $edit['shadow'] = TRUE;
362
      $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
363
      $this->assertRaw(t('Forum topic %title has been updated.', array('%title' => $edit["title[$langcode][0][value]"])), t('Forum node was edited'));
364
365

      // Verify topic was moved to a different forum.
366
367
368
369
      $forum_tid = db_query("SELECT tid FROM {forum} WHERE nid = :nid AND vid = :vid", array(
        ':nid' => $node->nid,
        ':vid' => $node->vid,
      ))->fetchField();
370
371
372
      $this->assertTrue($forum_tid == $this->root_forum['tid'], 'The forum topic is linked to a different forum');

      // Delete forum node.
373
      $this->drupalPost('node/' . $node->nid . '/delete', array(), t('Delete'));
374
      $this->assertResponse($response);
375
      $this->assertRaw(t('Forum topic %title has been deleted.', array('%title' => $edit["title[$langcode][0][value]"])), t('Forum node was deleted'));
376
377
378
379
380
381
    }
  }

  /**
   * Verify display of forum page.
   *
382
383
   * @param $forum
   *   A row from taxonomy_term_data table in array.
384
385
386
387
388
   */
  private function verifyForumView($forum, $parent = NULL) {
    $crumb = '›';

    // View forum page.
389
    $this->drupalGet('forum/' . $forum['tid']);
390
    $this->assertResponse(200);
391
    $this->assertTitle($forum['name'] . ' | Drupal', t('Forum name was displayed'));
392
    if (isset($parent)) {
393
      $this->assertText(t('Home ' . $crumb . ' Forums ' . $crumb . ' @name', array('@name' => $parent['name'])), t('Breadcrumbs were displayed'));
394
395
    }
    else {
396
      $this->assertText(t('Home ' . $crumb . ' Forums'), t('Breadcrumbs were displayed'));
397
398
399
400
401
402
    }
  }

  /**
   * Generate forum topics to test display of active forum block.
   *
403
   * @param array $forum Forum array (a row from taxonomy_term_data table).
404
405
406
407
408
409
410
411
412
413
414
415
   */
  private function generateForumTopics($forum) {
    $this->nids = array();
    for ($i = 0; $i < 5; $i++) {
      $node = $this->createForumTopic($this->forum, FALSE);
      $this->nids[] = $node->nid;
    }
  }

  /**
   * View forum topics to test display of active forum block.
   *
416
417
418
419
420
421
   * @todo The logic here is completely incorrect, since the active
   * forum topics block is determined by comments on the node, not by views.
   * @todo DIE
   *
   * @param $nids
   *   An array of forum node IDs.
422
423
424
425
426
427
   */
  private function viewForumTopics($nids) {
    $crumb = '›';

    for ($i = 0; $i < 2; $i++) {
      foreach ($nids as $nid) {
428
429
430
        $this->drupalGet('node/' . $nid);
        $this->drupalGet('node/' . $nid);
        $this->drupalGet('node/' . $nid);
431
432
433
434
      }
    }
  }
}