ForumTest.php 26.6 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\forum\Functional;
4

5
use Drupal\Component\Render\FormattableMarkup;
6 7
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
8
use Drupal\Core\Entity\EntityInterface;
9
use Drupal\Core\Link;
10
use Drupal\Core\Url;
11
use Drupal\taxonomy\Entity\Vocabulary;
12
use Drupal\Tests\BrowserTestBase;
13

14
/**
15 16
 * Tests for forum.module.
 *
17 18 19 20
 * Create, view, edit, delete, and change forum entries and verify its
 * consistency in the database.
 *
 * @group forum
21
 */
22
class ForumTest extends BrowserTestBase {
23

24 25 26 27 28
  /**
   * Modules to enable.
   *
   * @var array
   */
29 30 31 32 33 34 35 36 37
  protected static $modules = [
    'taxonomy',
    'comment',
    'forum',
    'node',
    'block',
    'menu_ui',
    'help',
  ];
38

39 40 41 42 43
  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'classy';

44 45 46
  /**
   * A user with various administrative privileges.
   */
47
  protected $adminUser;
48 49 50 51

  /**
   * A user that can create forum topics and edit its own topics.
   */
52
  protected $editOwnTopicsUser;
53 54 55 56

  /**
   * A user that can create, edit, and delete forum topics.
   */
57
  protected $editAnyTopicsUser;
58 59 60 61

  /**
   * A user with no special privileges.
   */
62 63 64 65 66 67
  protected $webUser;

  /**
   * An administrative user who can bypass comment approval.
   */
  protected $postCommentUser;
68 69

  /**
70
   * An array representing a forum container.
71
   */
72
  protected $forumContainer;
73 74 75 76

  /**
   * An array representing a forum.
   */
77
  protected $forum;
78 79 80 81

  /**
   * An array representing a root forum.
   */
82
  protected $rootForum;
83 84 85 86

  /**
   * An array of forum topic node IDs.
   */
87 88
  protected $nids;

89 90 91
  /**
   * {@inheritdoc}
   */
92
  protected function setUp(): void {
93
    parent::setUp();
94
    $this->drupalPlaceBlock('system_breadcrumb_block');
95
    $this->drupalPlaceBlock('page_title_block');
96

97
    // Create users.
98
    $this->adminUser = $this->drupalCreateUser([
99
      'access administration pages',
100
      'administer modules',
101 102 103 104 105
      'administer blocks',
      'administer forums',
      'administer menu',
      'administer taxonomy',
      'create forum content',
106
      'access comments',
107 108
    ]);
    $this->editAnyTopicsUser = $this->drupalCreateUser([
109 110 111 112
      'access administration pages',
      'create forum content',
      'edit any forum content',
      'delete any forum content',
113 114
    ]);
    $this->editOwnTopicsUser = $this->drupalCreateUser([
115 116 117
      'create forum content',
      'edit own forum content',
      'delete own forum content',
118
    ]);
119
    $this->webUser = $this->drupalCreateUser();
120
    $this->postCommentUser = $this->drupalCreateUser([
121 122 123 124 125
      'administer content types',
      'create forum content',
      'post comments',
      'skip comment approval',
      'access comments',
126 127
    ]);
    $this->drupalPlaceBlock('help_block', ['region' => 'help']);
128
    $this->drupalPlaceBlock('local_actions_block');
129 130 131
  }

  /**
132
   * Tests forum functionality through the admin and user interfaces.
133
   */
134
  public function testForum() {
135
    // Check that the basic forum install creates a default forum topic
136
    $this->drupalGet('/forum');
137
    // Look for the "General discussion" default forum
138
    $this->assertRaw(Link::createFromRoute(t('General discussion'), 'forum.page', ['taxonomy_term' => 1])->toString(), "Found the default forum at the /forum listing");
139 140 141 142 143 144
    // Check the presence of expected cache tags.
    $this->assertCacheTag('config:forum.settings');

    $this->drupalGet(Url::fromRoute('forum.page', ['taxonomy_term' => 1]));
    $this->assertCacheTag('config:forum.settings');

145
    // Do the admin tests.
146
    $this->doAdminTests($this->adminUser);
147

148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
    // Check display order.
    $display = EntityViewDisplay::load('node.forum.default');
    $body = $display->getComponent('body');
    $comment = $display->getComponent('comment_forum');
    $taxonomy = $display->getComponent('taxonomy_forums');

    // Assert field order is body » taxonomy » comments.
    $this->assertTrue($taxonomy['weight'] < $body['weight']);
    $this->assertTrue($body['weight'] < $comment['weight']);

    // Check form order.
    $display = EntityFormDisplay::load('node.forum.default');
    $body = $display->getComponent('body');
    $comment = $display->getComponent('comment_forum');
    $taxonomy = $display->getComponent('taxonomy_forums');

    // Assert category comes before body in order.
    $this->assertTrue($taxonomy['weight'] < $body['weight']);

167
    $this->generateForumTopics();
168

169
    // Log in an unprivileged user to view the forum topics and generate an
170
    // active forum topics list.
171
    $this->drupalLogin($this->webUser);
172 173
    // Verify that this user is shown a message that they may not post content.
    $this->drupalGet('forum/' . $this->forum['tid']);
174
    $this->assertText(t('You are not allowed to post new content in the forum'), "Authenticated user without permission to post forum content is shown message in local tasks to that effect.");
175

176 177
    // Log in, and do basic tests for a user with permission to edit any forum
    // content.
178
    $this->doBasicTests($this->editAnyTopicsUser, TRUE);
179 180 181 182 183
    // Create a forum node authored by this user.
    $any_topics_user_node = $this->createForumTopic($this->forum, FALSE);

    // Log in, and do basic tests for a user with permission to edit only its
    // own forum content.
184
    $this->doBasicTests($this->editOwnTopicsUser, FALSE);
185 186 187
    // Create a forum node authored by this user.
    $own_topics_user_node = $this->createForumTopic($this->forum, FALSE);
    // Verify that this user cannot edit forum content authored by another user.
188
    $this->verifyForums($any_topics_user_node, FALSE, 403);
189

190 191 192 193 194 195
    // Verify that this user is shown a local task to add new forum content.
    $this->drupalGet('forum');
    $this->assertLink(t('Add new Forum topic'));
    $this->drupalGet('forum/' . $this->forum['tid']);
    $this->assertLink(t('Add new Forum topic'));

196
    // Log in a user with permission to edit any forum content.
197
    $this->drupalLogin($this->editAnyTopicsUser);
198
    // Verify that this user can edit forum content authored by another user.
199
    $this->verifyForums($own_topics_user_node, TRUE);
200 201 202

    // Verify the topic and post counts on the forum page.
    $this->drupalGet('forum');
203 204

    // Verify row for testing forum.
205
    $forum_arg = [':forum' => 'forum-list-' . $this->forum['tid']];
206 207

    // Topics cell contains number of topics and number of unread topics.
208
    $xpath = $this->buildXPathQuery('//tr[@id=:forum]//td[@class="forum__topics"]', $forum_arg);
209
    $topics = $this->xpath($xpath);
210 211 212 213
    $topics = trim($topics[0]->getText());
    // The extracted text contains the number of topics (6) and new posts
    // (also 6) in this table cell.
    $this->assertEquals('6 6 new posts in forum ' . $this->forum['name'], $topics, 'Number of topics found.');
214 215

    // Verify the number of unread topics.
216 217
    $elements = $this->xpath('//tr[@id=:forum]//td[@class="forum__topics"]//a', $forum_arg);
    $this->assertStringStartsWith('6 new posts', $elements[0]->getText(), 'Number of unread topics found.');
218
    // Verify that the forum name is in the unread topics text.
219
    $elements = $this->xpath('//tr[@id=:forum]//em[@class="placeholder"]', $forum_arg);
220
    $this->assertStringContainsString($this->forum['name'], $elements[0]->getText(), 'Forum name found in unread topics text.');
221 222

    // Verify total number of posts in forum.
223 224
    $elements = $this->xpath('//tr[@id=:forum]//td[@class="forum__posts"]', $forum_arg);
    $this->assertEquals('6', $elements[0]->getText(), 'Number of posts found.');
225 226

    // Test loading multiple forum nodes on the front page.
227 228
    $this->drupalLogin($this->drupalCreateUser(['administer content types', 'create forum content', 'post comments']));
    $this->drupalPostForm('admin/structure/types/manage/forum', ['options[promote]' => 'promote'], t('Save content type'));
229 230 231
    $this->createForumTopic($this->forum, FALSE);
    $this->createForumTopic($this->forum, FALSE);
    $this->drupalGet('node');
232 233 234

    // Test adding a comment to a forum topic.
    $node = $this->createForumTopic($this->forum, FALSE);
235
    $edit = [];
236
    $edit['comment_body[0][value]'] = $this->randomMachineName();
237
    $this->drupalPostForm('node/' . $node->id(), $edit, t('Save'));
238
    $this->assertSession()->statusCodeEquals(200);
239 240

    // Test editing a forum topic that has a comment.
241
    $this->drupalLogin($this->editAnyTopicsUser);
242
    $this->drupalGet('forum/' . $this->forum['tid']);
243
    $this->drupalPostForm('node/' . $node->id() . '/edit', [], t('Save'));
244
    $this->assertSession()->statusCodeEquals(200);
245 246 247

    // Test the root forum page title change.
    $this->drupalGet('forum');
248
    $this->assertCacheTag('config:taxonomy.vocabulary.' . $this->forum['vid']);
249
    $this->assertTitle('Forums | Drupal');
250
    $vocabulary = Vocabulary::load($this->forum['vid']);
251 252 253
    $vocabulary->set('name', 'Discussions');
    $vocabulary->save();
    $this->drupalGet('forum');
254
    $this->assertTitle('Discussions | Drupal');
255 256 257 258 259

    // Test anonymous action link.
    $this->drupalLogout();
    $this->drupalGet('forum/' . $this->forum['tid']);
    $this->assertLink(t('Log in to post new content in the forum.'));
260 261
  }

262
  /**
263 264 265 266
   * Tests that forum nodes can't be added without a parent.
   *
   * Verifies that forum nodes are not created without choosing "forum" from the
   * select list.
267
   */
268
  public function testAddOrphanTopic() {
269
    // Must remove forum topics to test creating orphan topics.
270
    $vid = $this->config('forum.settings')->get('vocabulary');
271 272 273
    $tids = \Drupal::entityQuery('taxonomy_term')
      ->condition('vid', $vid)
      ->execute();
274 275 276
    $term_storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
    $terms = $term_storage->loadMultiple($tids);
    $term_storage->delete($terms);
277 278

    // Create an orphan forum item.
279
    $edit = [];
280 281
    $edit['title[0][value]'] = $this->randomMachineName(10);
    $edit['body[0][value]'] = $this->randomMachineName(120);
282
    $this->drupalLogin($this->adminUser);
283
    $this->drupalPostForm('node/add/forum', $edit, t('Save'));
284

285 286 287 288 289 290
    $nid_count = $this->container->get('entity_type.manager')
      ->getStorage('node')
      ->getQuery()
      ->accessCheck(FALSE)
      ->count()
      ->execute();
291
    $this->assertEqual(0, $nid_count, 'A forum node was not created when missing a forum vocabulary.');
292 293

    // Reset the defaults for future tests.
294
    \Drupal::service('module_installer')->install(['forum']);
295 296
  }

297
  /**
298
   * Runs admin tests on the admin user.
299
   *
300 301
   * @param object $user
   *   The logged-in user.
302 303
   */
  private function doAdminTests($user) {
304
    // Log in the user.
305 306
    $this->drupalLogin($user);

307
    // Add forum to the Tools menu.
308
    $edit = [];
309
    $this->drupalPostForm('admin/structure/menu/manage/tools', $edit, t('Save'));
310
    $this->assertSession()->statusCodeEquals(200);
311 312 313

    // Edit forum taxonomy.
    // Restoration of the settings fails and causes subsequent tests to fail.
314
    $this->editForumVocabulary();
315
    // Create forum container.
316
    $this->forumContainer = $this->createForum('container');
317 318
    // Verify "edit container" link exists and functions correctly.
    $this->drupalGet('admin/structure/forum');
319 320
    // Verify help text is shown.
    $this->assertText(t('Forums contain forum topics. Use containers to group related forums'));
321 322 323
    // Verify action links are there.
    $this->assertLink('Add forum');
    $this->assertLink('Add container');
324
    $this->clickLink('edit container');
325
    $this->assertRaw('Edit container', 'Followed the link to edit the container');
326
    // Create forum inside the forum container.
327
    $this->forum = $this->createForum('forum', $this->forumContainer['tid']);
328 329 330
    // Verify the "edit forum" link exists and functions correctly.
    $this->drupalGet('admin/structure/forum');
    $this->clickLink('edit forum');
331
    $this->assertRaw('Edit forum', 'Followed the link to edit the forum');
332 333
    // Navigate back to forum structure page.
    $this->drupalGet('admin/structure/forum');
334 335
    // Create second forum in container, destined to be deleted below.
    $delete_forum = $this->createForum('forum', $this->forumContainer['tid']);
336
    // Save forum overview.
337
    $this->drupalPostForm('admin/structure/forum/', [], t('Save'));
338
    $this->assertRaw(t('The configuration options have been saved.'));
339
    // Delete this second forum.
340
    $this->deleteForum($delete_forum['tid']);
341
    // Create forum at the top (root) level.
342
    $this->rootForum = $this->createForum('forum');
343 344

    // Test vocabulary form alterations.
345
    $this->drupalGet('admin/structure/taxonomy/manage/forums');
346 347
    $this->assertSession()->buttonExists('Save');
    $this->assertSession()->buttonNotExists('Delete');
348 349

    // Test term edit form alterations.
350
    $this->drupalGet('taxonomy/term/' . $this->forumContainer['tid'] . '/edit');
351 352 353 354 355 356
    // Test parent field been hidden by forum module.
    $this->assertNoField('parent[]', 'Parent field not found.');

    // Create a default vocabulary named "Tags".
    $description = 'Use tags to group articles on similar topics into categories.';
    $help = 'Enter a comma-separated list of words to describe your content.';
357
    $vocabulary = Vocabulary::create([
358 359
      'name' => 'Tags',
      'description' => $description,
360
      'vid' => 'tags',
361
      'langcode' => \Drupal::languageManager()->getDefaultLanguage()->getId(),
362
      'help' => $help,
363
    ]);
364
    $vocabulary->save();
365
    // Test tags vocabulary form is not affected.
366
    $this->drupalGet('admin/structure/taxonomy/manage/tags');
367
    $this->assertSession()->buttonExists('Save');
368
    $this->assertLink(t('Delete'));
369
    // Test tags vocabulary term form is not affected.
370
    $this->drupalGet('admin/structure/taxonomy/manage/tags/add');
371
    $this->assertField('parent[]', 'Parent field found.');
372 373 374
    // Test relations widget exists.
    $relations_widget = $this->xpath("//details[@id='edit-relations']");
    $this->assertTrue(isset($relations_widget[0]), 'Relations widget element found.');
375 376 377
  }

  /**
378
   * Edits the forum taxonomy.
379
   */
380
  public function editForumVocabulary() {
381
    // Backup forum taxonomy.
382
    $vid = $this->config('forum.settings')->get('vocabulary');
383
    $original_vocabulary = Vocabulary::load($vid);
384

385
    // Generate a random name and description.
386
    $edit = [
387 388
      'name' => $this->randomMachineName(10),
      'description' => $this->randomMachineName(100),
389
    ];
390 391

    // Edit the vocabulary.
392
    $this->drupalPostForm('admin/structure/taxonomy/manage/' . $original_vocabulary->id(), $edit, t('Save'));
393
    $this->assertSession()->statusCodeEquals(200);
394
    $this->assertRaw(t('Updated vocabulary %name.', ['%name' => $edit['name']]), 'Vocabulary was edited');
395 396

    // Grab the newly edited vocabulary.
397
    $current_vocabulary = Vocabulary::load($vid);
398 399

    // Make sure we actually edited the vocabulary properly.
400 401
    $this->assertEqual($current_vocabulary->label(), $edit['name'], 'The name was updated');
    $this->assertEqual($current_vocabulary->getDescription(), $edit['description'], 'The description was updated');
402 403

    // Restore the original vocabulary's name and description.
404 405
    $current_vocabulary->set('name', $original_vocabulary->label());
    $current_vocabulary->set('description', $original_vocabulary->getDescription());
406 407
    $current_vocabulary->save();
    // Reload vocabulary to make sure changes are saved.
408
    $current_vocabulary = Vocabulary::load($vid);
409
    $this->assertEqual($current_vocabulary->label(), $original_vocabulary->label(), 'The original vocabulary settings were restored');
410 411 412
  }

  /**
413
   * Creates a forum container or a forum.
414
   *
415
   * @param string $type
416
   *   The forum type (forum container or forum).
417
   * @param int $parent
418 419
   *   The forum parent. This defaults to 0, indicating a root forum.
   *
420
   * @return \Drupal\Core\Database\StatementInterface
421
   *   The created taxonomy term data.
422
   */
423
  public function createForum($type, $parent = 0) {
424
    // Generate a random name/description.
425 426
    $name = $this->randomMachineName(10);
    $description = $this->randomMachineName(100);
427

428
    $edit = [
429 430
      'name[0][value]' => $name,
      'description[0][value]' => $description,
431 432
      'parent[0]' => $parent,
      'weight' => '0',
433
    ];
434 435

    // Create forum.
436
    $this->drupalPostForm('admin/structure/forum/add/' . $type, $edit, t('Save'));
437
    $this->assertSession()->statusCodeEquals(200);
438
    $type = ($type == 'container') ? 'forum container' : 'forum';
439
    $this->assertText(
440
      t(
441
        'Created new @type @term.',
442
        ['@term' => $name, '@type' => t($type)]
443
      ),
444
      new FormattableMarkup('@type was created', ['@type' => ucfirst($type)])
445
    );
446

447
    // Verify that the creation message contains a link to a term.
448
    $view_link = $this->xpath('//div[@class="messages"]//a[contains(@href, :href)]', [':href' => 'term/']);
449 450
    $this->assert(isset($view_link), 'The message area contains a link to a term');

451 452
    /** @var \Drupal\taxonomy\TermStorageInterface $taxonomy_term_storage */
    $taxonomy_term_storage = $this->container->get('entity_type.manager')->getStorage('taxonomy_term');
453
    // Verify forum.
454 455 456 457 458 459
    $term = $taxonomy_term_storage->loadByProperties([
      'vid' => $this->config('forum.settings')->get('vocabulary'),
      'name' => $name,
      'description__value' => $description,
    ]);
    $term = array_shift($term);
460
    $this->assertTrue(!empty($term), 'The ' . $type . ' exists in the database');
461 462

    // Verify forum hierarchy.
463 464 465
    $tid = $term->id();
    $parent_tid = $taxonomy_term_storage->loadParents($tid);
    $parent_tid = empty($parent_tid) ? 0 : array_shift($parent_tid)->id();
466
    $this->assertTrue($parent == $parent_tid, 'The ' . $type . ' is linked to its container');
467

468
    $forum = $taxonomy_term_storage->load($tid);
469
    $this->assertEqual(($type == 'forum container'), (bool) $forum->forum_container->value);
470 471 472 473 474
    return [
      'tid' => $tid,
      'name' => $term->getName(),
      'vid' => $term->bundle(),
    ];
475 476
  }

477
  /**
478
   * Deletes a forum.
479
   *
480
   * @param int $tid
481
   *   The forum ID.
482
   */
483
  public function deleteForum($tid) {
484
    // Delete the forum.
485 486
    $this->drupalGet('admin/structure/forum/edit/forum/' . $tid);
    $this->clickLink(t('Delete'));
487 488 489
    $this->assertText('Are you sure you want to delete the forum');
    $this->assertNoText('Add forum');
    $this->assertNoText('Add forum container');
490
    $this->drupalPostForm(NULL, [], t('Delete'));
491

492
    // Assert that the forum no longer exists.
493
    $this->drupalGet('forum/' . $tid);
494
    $this->assertSession()->statusCodeEquals(404);
495 496
  }

497
  /**
498
   * Runs basic tests on the indicated user.
499
   *
500
   * @param \Drupal\Core\Session\AccountInterface $user
501
   *   The logged in user.
502
   * @param bool $admin
503
   *   User has 'access administration pages' privilege.
504 505
   */
  private function doBasicTests($user, $admin) {
506
    // Log in the user.
507 508
    $this->drupalLogin($user);
    // Attempt to create forum topic under a container.
509
    $this->createForumTopic($this->forumContainer, TRUE);
510 511 512
    // Create forum node.
    $node = $this->createForumTopic($this->forum, FALSE);
    // Verify the user has access to all the forum nodes.
513
    $this->verifyForums($node, $admin);
514 515
  }

516 517 518
  /**
   * Tests a forum with a new post displays properly.
   */
519
  public function testForumWithNewPost() {
520
    // Log in as the first user.
521
    $this->drupalLogin($this->adminUser);
522
    // Create a forum container.
523
    $this->forumContainer = $this->createForum('container');
524 525 526 527 528
    // Create a forum.
    $this->forum = $this->createForum('forum');
    // Create a topic.
    $node = $this->createForumTopic($this->forum, FALSE);

529
    // Log in as a second user.
530
    $this->drupalLogin($this->postCommentUser);
531
    // Post a reply to the topic.
532
    $edit = [];
533 534
    $edit['subject[0][value]'] = $this->randomMachineName();
    $edit['comment_body[0][value]'] = $this->randomMachineName();
535
    $this->drupalPostForm('node/' . $node->id(), $edit, t('Save'));
536
    $this->assertSession()->statusCodeEquals(200);
537

538 539
    // Test replying to a comment.
    $this->clickLink('Reply');
540
    $this->assertSession()->statusCodeEquals(200);
541 542
    $this->assertFieldByName('comment_body[0][value]');

543
    // Log in as the first user.
544
    $this->drupalLogin($this->adminUser);
545 546
    // Check that forum renders properly.
    $this->drupalGet("forum/{$this->forum['tid']}");
547
    $this->assertSession()->statusCodeEquals(200);
548 549 550

    // Verify there is no unintentional HTML tag escaping.
    $this->assertNoEscaped('<', '');
551 552
  }

553
  /**
554
   * Creates a forum topic.
555
   *
556
   * @param array $forum
557
   *   A forum array.
558
   * @param bool $container
559
   *   TRUE if $forum is a container; FALSE otherwise.
560
   *
561 562
   * @return object|null
   *   The created topic node or NULL if the forum is a container.
563
   */
564
  public function createForumTopic($forum, $container = FALSE) {
565
    // Generate a random subject/body.
566 567
    $title = $this->randomMachineName(20);
    $body = $this->randomMachineName(200);
568

569
    $edit = [
570
      'title[0][value]' => $title,
571
      'body[0][value]' => $body,
572
    ];
573
    $tid = $forum['tid'];
574

575
    // Create the forum topic, preselecting the forum ID via a URL parameter.
576
    $this->drupalPostForm('node/add/forum', $edit, t('Save'), ['query' => ['forum_id' => $tid]]);
577

578 579
    $type = t('Forum topic');
    if ($container) {
580 581
      $this->assertNoText(t('@type @title has been created.', ['@type' => $type, '@title' => $title]), 'Forum topic was not created');
      $this->assertRaw(t('The item %title is a forum container, not a forum.', ['%title' => $forum['name']]), 'Error message was shown');
582 583 584
      return;
    }
    else {
585 586
      $this->assertText(t('@type @title has been created.', ['@type' => $type, '@title' => $title]), 'Forum topic was created');
      $this->assertNoRaw(t('The item %title is a forum container, not a forum.', ['%title' => $forum['name']]), 'No error message was shown');
587 588

      // Verify that the creation message contains a link to a term.
589
      $view_link = $this->xpath('//div[@class="messages"]//a[contains(@href, :href)]', [':href' => 'term/']);
590
      $this->assert(isset($view_link), 'The message area contains a link to a term');
591 592
    }

593
    // Retrieve node object, ensure that the topic was created and in the proper forum.
594
    $node = $this->drupalGetNodeByTitle($title);
595
    $this->assertTrue($node != NULL, new FormattableMarkup('Node @title was loaded', ['@title' => $title]));
596
    $this->assertEqual($node->taxonomy_forums->target_id, $tid, 'Saved forum topic was in the expected forum');
597 598

    // View forum topic.
599
    $this->drupalGet('node/' . $node->id());
600 601
    $this->assertRaw($title, 'Subject was found');
    $this->assertRaw($body, 'Body was found');
602 603 604 605 606

    return $node;
  }

  /**
607
   * Verifies that the logged in user has access to a forum node.
608
   *
609
   * @param \Drupal\Core\Entity\EntityInterface $node
610
   *   The node being checked.
611
   * @param bool $admin
612
   *   Boolean to indicate whether the user can 'access administration pages'.
613
   * @param int $response
614
   *   The expected HTTP response code.
615
   */
616
  private function verifyForums(EntityInterface $node, $admin, $response = 200) {
617 618 619 620
    $response2 = ($admin) ? 200 : 403;

    // View forum help node.
    $this->drupalGet('admin/help/forum');
621
    $this->assertSession()->statusCodeEquals($response2);
622
    if ($response2 == 200) {
623
      $this->assertTitle('Forum | Drupal');
624
      $this->assertText(t('Forum'), 'Forum help node was displayed');
625 626 627
    }

    // View forum container page.
628
    $this->verifyForumView($this->forumContainer);
629
    // View forum page.
630
    $this->verifyForumView($this->forum, $this->forumContainer);
631
    // View root forum page.
632
    $this->verifyForumView($this->rootForum);
633 634

    // View forum node.
635
    $this->drupalGet('node/' . $node->id());
636
    $this->assertSession()->statusCodeEquals(200);
637
    $this->assertTitle($node->label() . ' | Drupal');
638
    $breadcrumb_build = [
639 640
      Link::createFromRoute(t('Home'), '<front>'),
      Link::createFromRoute(t('Forums'), 'forum.index'),
641 642 643 644
      Link::createFromRoute($this->forumContainer['name'], 'forum.page', ['taxonomy_term' => $this->forumContainer['tid']]),
      Link::createFromRoute($this->forum['name'], 'forum.page', ['taxonomy_term' => $this->forum['tid']]),
    ];
    $breadcrumb = [
645
      '#theme' => 'breadcrumb',
646
      '#links' => $breadcrumb_build,
647
    ];
648
    $this->assertRaw(\Drupal::service('renderer')->renderRoot($breadcrumb), 'Breadcrumbs were displayed');
649 650

    // View forum edit node.
651
    $this->drupalGet('node/' . $node->id() . '/edit');
652
    $this->assertSession()->statusCodeEquals($response);
653
    if ($response == 200) {
654
      $this->assertTitle('Edit Forum topic ' . $node->label() . ' | Drupal');
655 656 657 658
    }

    if ($response == 200) {
      // Edit forum node (including moving it to another forum).
659
      $edit = [];
660
      $edit['title[0][value]'] = 'node/' . $node->id();
661
      $edit['body[0][value]'] = $this->randomMachineName(256);
662
      // Assume the topic is initially associated with $forum.
663
      $edit['taxonomy_forums'] = $this->rootForum['tid'];
664
      $edit['shadow'] = TRUE;
665
      $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
666
      $this->assertText(t('Forum topic @title has been updated.', ['@title' => $edit['title[0][value]']]), 'Forum node was edited');
667 668

      // Verify topic was moved to a different forum.
669 670 671 672 673 674 675 676
      $forum_tid = $this->container
        ->get('database')
        ->select('forum', 'f')
        ->fields('f', ['tid'])
        ->condition('nid', $node->id())
        ->condition('vid', $node->getRevisionId())
        ->execute()
        ->fetchField();
677
      $this->assertTrue($forum_tid == $this->rootForum['tid'], 'The forum topic is linked to a different forum');
678 679

      // Delete forum node.
680
      $this->drupalPostForm('node/' . $node->id() . '/delete', [], t('Delete'));
681
      $this->assertSession()->statusCodeEquals($response);
682
      $this->assertRaw(t('Forum topic %title has been deleted.', ['%title' => $edit['title[0][value]']]), 'Forum node was deleted');
683 684 685 686
    }
  }

  /**
687
   * Verifies the display of a forum page.
688
   *