forum.test 15.1 KB
Newer Older
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
<?php
// $Id$

class ForumTestCase extends DrupalWebTestCase {
  protected $big_user;
  protected $own_user;
  protected $any_user;
  protected $nid_user;
  protected $container;
  protected $forum;
  protected $root_forum;
  protected $nids;

  function getInfo() {
    return array(
      'name' => t('Forum functionality'),
      'description' => t('Create, view, edit, delete, and change forum entries and verify its consistency in the database.'),
      'group' => t('Forum'),
    );
  }

  /**
   * Enable modules and create users with specific permissions.
   */
  function setUp() {
    parent::setUp('taxonomy', 'comment', 'forum');
    // Create users.
    $this->big_user = $this->drupalCreateUser(array('administer blocks', 'administer forums', 'administer menu', 'administer taxonomy', 'create forum content')); // 'access administration pages'));
    $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.
    $this->doAdminTests($this->big_user);
    // Generate topics to populate the active forum block.
    $this->generateForumTopics($this->forum);

    // Login the nid user to view the forum topics and generate an Active forum topics list (FAILS).
    $this->drupalLogin($this->nid_user);
    // View the forum topics.
    $this->viewForumTopics($this->nids);

    // Do basic tests for the any forum user.
    $this->doBasicTests($this->any_user, TRUE);
    // Create another forum node for the any forum user.
//    $node = $this->drupalCreateNode(array('type' => 'forum', 'uid' => $this->any_user->uid));
    $node = $this->createForumTopic($this->forum, FALSE);

    // Do basic tests for the own forum user.
    $this->doBasicTests($this->own_user, FALSE);
    // 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->drupalCreateNode(array('type' => 'forum', 'uid' => $this->own_user->uid));
    $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);
  }

  /**
   * 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();
    $edit['forum_active[region]'] = 'right';
    $this->drupalPost('admin/build/block', $edit, t('Save blocks'));
    $this->assertResponse(200);
    $this->assertText(t('The block settings have been updated.'), t('[Active forum topics] Forum block was enabled'));

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

    // Retrieve forum menu id.
    $mlid = db_result(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));

    // Add forum to navigation menu.
    $edit = array();
    $this->drupalPost('admin/build/menu-customize/navigation', $edit, t('Save configuration'));
    $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']);
106
107
108
109
    // Create second forum in container.
    $this->delete_forum = $this->createForum('forum', $this->container['tid']);
    // Delete this second form.
    $this->deleteForum($this->delete_forum['tid']);
110
111
112
113
114
115
116
117
118
119
    // 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', '');
120
    $original_settings = taxonomy_vocabulary_load($vid);
121
122
123
124
125
126
127
128
129
130
131
132
133

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

    $edit = array(
      'name' => $title,
      'description' => $description,
      'help' => '',
      'weight' => -10
    );

    // Edit the vocabulary.
134
    $this->drupalPost('admin/content/taxonomy/' . $vid, $edit, t('Save'));
135
136
137
138
    $this->assertResponse(200);
    $this->assertRaw(t('Updated vocabulary %name.', array('%name' => $title)), t('Vocabulary was edited'));

    // Grab the newly edited vocabulary.
139
    $current_settings = taxonomy_vocabulary_load($vid, TRUE);
140
141

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

145
    // Restore the original vocabulary.
146
    taxonomy_vocabulary_save($original_settings);
147
    $current_settings = taxonomy_vocabulary_load($vid, TRUE);
148
    $this->assertEqual($current_settings->name, $original_settings->name, 'The original vocabulary settings were restored');
149
150
151
152
153
154
155
  }

  /**
   * Create a forum container or a forum.
   *
   * @param string $type Forum type (forum container or forum).
   * @param integer $parent Forum parent (default = 0 = a root forum; >0 = a forum container or another forum).
156
   * @return object Taxonomy_term_data created.
157
158
159
160
161
162
163
164
165
166
167
168
169
170
   */
  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.
171
    $this->drupalPost('admin/build/forum/add/' . $type, $edit, t('Save'));
172
173
    $this->assertResponse(200);
    $type = ($type == 'container') ? 'forum container' : 'forum';
174
    $this->assertRaw(t('Created new @type %term.', array('%term' => $name, '@type' => t($type))), t(ucfirst($type) . ' was created'));
175
176

    // Verify forum.
177
    $term = db_fetch_array(db_query("SELECT * FROM {taxonomy_term_data} t WHERE t.vid = %d AND t.name = '%s' AND t.description = '%s'", variable_get('forum_nav_vocabulary', ''), $name, $description));
178
    $this->assertTrue(!empty($term), 'The ' . $type . ' exists in the database');
179
180
181

    // Verify forum hierarchy.
    $tid = $term['tid'];
182
    $parent_tid = db_result(db_query("SELECT t.parent FROM {taxonomy_term_hierarchy} t WHERE t.tid = %d", $tid));
183
    $this->assertTrue($parent == $parent_tid, 'The ' . $type . ' is linked to its container');
184
185
186
187

    return $term;
  }

188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
  /**
   * Delete a forum.
   *
   * @param integer $tid Forum id.
   */
  function deleteForum($tid) {
    // Delete the forum id.
    $this->drupalPost('admin/build/forum/edit/forum/'. $tid, array(), t('Delete'));
    $this->drupalPost(NULL, NULL, t('Delete'));
    
    // Assert that the forum no longer exists.
    $this->drupalGet('forum/'. $tid);
    $this->assertRaw(t('No forums defined'), 'The forum was not found');
  }

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
  /**
   * Run basic tests on the indicated user.
   *
   * @param object $user The logged in user.
   * @param boolean $admin User has 'access administration pages' privilege.
   */
  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);
    $tid = $forum['tid']; // Without this being set, post variable equals the first non-blank in select items list.

    $edit = array(
      'title' => $title,
      'body' => $body,
      'taxonomy[1]' => $tid
    );

    // 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.

    // Create forum topic.
244
//    $this->drupalPost('node/add/forum/' . $forum['tid'], $edit, t('Save'));
245
246
247
248
249
250
251
252
253
254
255
256
257
    $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'));
      $this->assertRaw(t('The item %title is only a container for forums.', array('%title' => $forum['name'])), t('Error message was shown'));
      return;
    }
    else {
      $this->assertRaw(t('@type %title has been created.', array('%title' => $title, '@type' => $type)), t('Forum topic was created'));
      $this->assertNoRaw(t('The item %title is only a container for forums.', array('%title' => $forum['name'])), t('No error message was shown'));
    }

    // Retrieve node object.
258
    $node = $this->drupalGetNodeByTitle($title);
259
260
261
    $this->assertTrue($node != null, t('Node @title was loaded', array('@title' => $title)));

    // View forum topic.
262
    $this->drupalGet('node/' . $node->nid);
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
    $this->assertRaw($title, t('Subject was found'));
    $this->assertRaw($body, t('Body was found'));

    return $node;
  }

  /**
   * Verify the logged in user has the desired access to the various forum nodes.
   *
   * @param object $node_user The user who creates the node.
   * @param object $node Node.
   * @param boolean $admin User has 'access administration pages' privilege.
   * @param integer $response HTTP response code.
   */
  private function verifyForums($node_user, $node, $admin, $response = 200) {
    $crumb = '›';
    $quote = '&#039;';

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

    // View forum help node.
    $this->drupalGet('admin/help/forum');
    $this->assertResponse($response2);
    if ($response2 == 200) {
      $this->assertTitle(t('Forum | Drupal'), t('Forum help node was displayed'));
      $this->assertText(t('Forum'), t('Forum help node was displayed'));
289
      $this->assertText(t('Home ' . $crumb . ' Administer ' . $crumb . ' Help'), t('Breadcrumbs were displayed'));
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
    }

    // Verify the forum blocks were displayed.
    $this->drupalGet('');
    $this->assertResponse(200);
    // This block never seems to display?
//    $this->assertText(t('Active forum topics'), t('[Active forum topics] Forum block was displayed'));
    $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.
307
    $this->drupalGet('node/' . $node->nid);
308
    $this->assertResponse(200);
309
310
    $this->assertTitle($node->title . ' | Drupal', t('Forum node was displayed'));
    $this->assertText(t('Home ' . $crumb . ' Forums ' . $crumb . ' @container ' . $crumb . ' @forum', array('@container' => $this->container['name'], '@forum' => $this->forum['name'])), t('Breadcrumbs were displayed'));
311
312

    // View forum edit node.
313
    $this->drupalGet('node/' . $node->nid . '/edit');
314
315
    $this->assertResponse($response);
    if ($response == 200) {
316
      $this->assertTitle('Edit Forum topic ' . $node->title . ' | Drupal', t('Forum edit node was displayed'));
317
      $this->assertText(t('Home ' . $crumb . ' @title', array('@title' => $node->title)), t('Breadcrumbs were displayed'));
318
319
320
321
322
323
324
325
326
    }

    if ($response == 200) {
      // Edit forum node (including moving it to another forum).
      $edit = array();
      $edit['title'] = 'node/' . $node->nid;
      $edit['body'] = $this->randomName(256);
      $edit['taxonomy[1]'] = $this->root_forum['tid']; // Assumes the topic is initially associated with $forum.
      $edit['shadow'] = TRUE;
327
      $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
328
329
330
331
332
333
334
      $this->assertRaw(t('Forum topic %title has been updated.', array('%title' => $edit['title'])), t('Forum node was edited'));

      // Verify topic was moved to a different forum.
      $forum_tid = db_result(db_query("SELECT tid FROM {forum} WHERE nid = %d AND vid = %d", $node->nid, $node->vid));
      $this->assertTrue($forum_tid == $this->root_forum['tid'], 'The forum topic is linked to a different forum');

      // Delete forum node.
335
      $this->drupalPost('node/' . $node->nid . '/delete', array(), t('Delete'));
336
337
338
339
340
341
342
343
      $this->assertResponse($response);
      $this->assertRaw(t('Forum topic %title has been deleted.', array('%title' => $edit['title'])), t('Forum node was deleted'));
    }
  }

  /**
   * Verify display of forum page.
   *
344
   * @param array $forum Forum array (a row from taxonomy_term_data table).
345
346
347
348
349
   */
  private function verifyForumView($forum, $parent = NULL) {
    $crumb = '›';

    // View forum page.
350
    $this->drupalGet('forum/' . $forum['tid']);
351
    $this->assertResponse(200);
352
    $this->assertTitle($forum['name'] . ' | Drupal', t('Forum node was displayed'));
353
    if (isset($parent)) {
354
      $this->assertText(t('Home ' . $crumb . ' Forums ' . $crumb . ' @name', array('@name' => $parent['name'])), t('Breadcrumbs were displayed'));
355
356
    }
    else {
357
      $this->assertText(t('Home ' . $crumb . ' Forums'), t('Breadcrumbs were displayed'));
358
359
360
361
362
363
    }
  }

  /**
   * Generate forum topics to test display of active forum block.
   *
364
   * @param array $forum Forum array (a row from taxonomy_term_data table).
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
   */
  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.
   *
   * @param array $nids Forum node id array.
   */
  private function viewForumTopics($nids) {
    $crumb = '›';

    for ($i = 0; $i < 2; $i++) {
      foreach ($nids as $nid) {
384
385
386
        $this->drupalGet('node/' . $nid);
        $this->drupalGet('node/' . $nid);
        $this->drupalGet('node/' . $nid);
387
388
389
390
      }
    }
  }
}