function ForumTest::testForum

Same name in other branches
  1. 9 core/modules/forum/tests/src/Functional/ForumTest.php \Drupal\Tests\forum\Functional\ForumTest::testForum()
  2. 8.9.x core/modules/forum/tests/src/Functional/ForumTest.php \Drupal\Tests\forum\Functional\ForumTest::testForum()
  3. 11.x core/modules/forum/tests/src/Functional/ForumTest.php \Drupal\Tests\forum\Functional\ForumTest::testForum()

Tests forum functionality through the admin and user interfaces.

File

core/modules/forum/tests/src/Functional/ForumTest.php, line 139

Class

ForumTest
Tests for forum.module.

Namespace

Drupal\Tests\forum\Functional

Code

public function testForum() : void {
    // Check that the basic forum install creates a default forum topic
    $this->drupalGet('/forum');
    // Look for the "General discussion" default forum
    $this->assertSession()
        ->linkExists('General discussion');
    $this->assertSession()
        ->linkByHrefExists('/forum/1');
    // Check the presence of expected cache tags.
    $this->assertSession()
        ->responseHeaderContains('X-Drupal-Cache-Tags', 'config:forum.settings');
    $this->drupalGet(Url::fromRoute('forum.page', [
        'taxonomy_term' => 1,
    ]));
    $this->assertSession()
        ->responseHeaderContains('X-Drupal-Cache-Tags', 'config:forum.settings');
    // Do the admin tests.
    $this->doAdminTests($this->adminUser);
    // 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->assertLessThan($body['weight'], $taxonomy['weight']);
    $this->assertLessThan($comment['weight'], $body['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->assertLessThan($body['weight'], $taxonomy['weight']);
    $this->generateForumTopics();
    // Log in an unprivileged user to view the forum topics and generate an
    // active forum topics list.
    $this->drupalLogin($this->webUser);
    // Verify that this user is shown a message that they may not post content.
    $this->drupalGet('forum/' . $this->forum['tid']);
    $this->assertSession()
        ->pageTextContains('You are not allowed to post new content in the forum');
    // Log in, and do basic tests for a user with permission to edit any forum
    // content.
    $this->doBasicTests($this->editAnyTopicsUser, TRUE);
    // 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.
    $this->doBasicTests($this->editOwnTopicsUser, FALSE);
    // 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.
    $this->verifyForums($any_topics_user_node, FALSE, 403);
    // Verify that this user is shown a local task to add new forum content.
    $this->drupalGet('forum');
    $this->assertSession()
        ->linkExists('Add new Forum topic');
    $this->drupalGet('forum/' . $this->forum['tid']);
    $this->assertSession()
        ->linkExists('Add new Forum topic');
    // Log in a user with permission to edit any forum content.
    $this->drupalLogin($this->editAnyTopicsUser);
    // Verify that this user can edit forum content authored by another user.
    $this->verifyForums($own_topics_user_node, TRUE);
    // Verify the topic and post counts on the forum page.
    $this->drupalGet('forum');
    // Find the table row for the forum that has new posts. This cannot be
    // reliably identified by any CSS selector or its position in the table,
    // so look for an element with the "New posts" title and traverse up the
    // document tree until we get to the table row that contains it.
    $row = $this->assertSession()
        ->elementExists('css', '[title="New posts"]');
    while ($row && $row->getTagName() !== 'tr') {
        $row = $row->getParent();
    }
    $this->assertNotEmpty($row);
    $cells = $row->findAll('css', 'td');
    $this->assertCount(4, $cells);
    // Topics cell contains number of topics (6), number of unread topics (also
    // 6), and the forum name.
    $this->assertEquals('6 6 new posts in forum ' . $this->forum['name'], $cells[1]->getText(), 'Number of topics found.');
    // Verify total number of posts in forum.
    $this->assertEquals('6', $cells[2]->getText(), 'Number of posts found.');
    // Test loading multiple forum nodes on the front page.
    $this->drupalLogin($this->drupalCreateUser([
        'administer content types',
        'create forum content',
        'post comments',
    ]));
    $this->drupalGet('admin/structure/types/manage/forum');
    $this->submitForm([
        'options[promote]' => 'promote',
    ], 'Save');
    $this->createForumTopic($this->forum, FALSE);
    $this->createForumTopic($this->forum, FALSE);
    $this->drupalGet('node');
    // Test adding a comment to a forum topic.
    $node = $this->createForumTopic($this->forum, FALSE);
    $edit = [];
    $edit['comment_body[0][value]'] = $this->randomMachineName();
    $this->drupalGet('node/' . $node->id());
    $this->submitForm($edit, 'Save');
    $this->assertSession()
        ->statusCodeEquals(200);
    // Test editing a forum topic that has a comment.
    $this->drupalLogin($this->editAnyTopicsUser);
    $this->drupalGet('forum/' . $this->forum['tid']);
    $this->drupalGet('node/' . $node->id() . '/edit');
    $this->submitForm([], 'Save');
    $this->assertSession()
        ->statusCodeEquals(200);
    // Test the root forum page title change.
    $this->drupalGet('forum');
    $this->assertSession()
        ->responseHeaderContains('X-Drupal-Cache-Tags', 'config:taxonomy.vocabulary.' . $this->forum['vid']);
    $this->assertSession()
        ->titleEquals('Forums | Drupal');
    $vocabulary = Vocabulary::load($this->forum['vid']);
    $vocabulary->set('name', 'Discussions');
    $vocabulary->save();
    $this->drupalGet('forum');
    $this->assertSession()
        ->titleEquals('Discussions | Drupal');
    // Test anonymous action link.
    $this->drupalLogout();
    $this->drupalGet('forum/' . $this->forum['tid']);
    $this->assertSession()
        ->linkExists('Log in to post new content in the forum.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.