function ForumIndexTest::testForumIndexStatus

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

Tests the forum index for published and unpublished nodes.

File

core/modules/forum/tests/src/Functional/ForumIndexTest.php, line 49

Class

ForumIndexTest
Tests the forum index listing.

Namespace

Drupal\Tests\forum\Functional

Code

public function testForumIndexStatus() {
    // The forum ID to use.
    $tid = 1;
    // Create a test node.
    $title = $this->randomMachineName(20);
    $edit = [
        'title[0][value]' => $title,
        'body[0][value]' => $this->randomMachineName(200),
    ];
    // Create the forum topic, preselecting the forum ID via a URL parameter.
    $this->drupalGet("forum/{$tid}");
    $this->clickLink('Add new Forum topic');
    $this->assertSession()
        ->addressEquals("node/add/forum?forum_id={$tid}");
    $this->submitForm($edit, 'Save');
    // Check that the node exists in the database.
    $node = $this->drupalGetNodeByTitle($title);
    $this->assertNotEmpty($node, 'New forum node found in database.');
    // Create a child forum.
    $edit = [
        'name[0][value]' => $this->randomMachineName(20),
        'description[0][value]' => $this->randomMachineName(200),
        'parent[0]' => $tid,
    ];
    $this->drupalGet('admin/structure/forum/add/forum');
    $this->submitForm($edit, 'Save');
    $this->assertSession()
        ->linkExists('edit forum');
    $tid_child = $tid + 1;
    // Verify that the node appears on the index.
    $this->drupalGet('forum/' . $tid);
    $this->assertSession()
        ->pageTextContains($title);
    $this->assertSession()
        ->responseHeaderContains('X-Drupal-Cache-Tags', 'node_list');
    $this->assertSession()
        ->responseHeaderContains('X-Drupal-Cache-Tags', 'config:node.type.forum');
    $this->assertSession()
        ->responseHeaderContains('X-Drupal-Cache-Tags', 'comment_list');
    $this->assertSession()
        ->responseHeaderContains('X-Drupal-Cache-Tags', 'node:' . $node->id());
    $this->assertSession()
        ->responseHeaderContains('X-Drupal-Cache-Tags', 'taxonomy_term:' . $tid);
    $this->assertSession()
        ->responseHeaderContains('X-Drupal-Cache-Tags', 'taxonomy_term:' . $tid_child);
    // Unpublish the node.
    $edit = [
        'status[value]' => FALSE,
    ];
    $this->drupalGet('node/' . $node->id() . '/edit');
    $this->submitForm($edit, 'Save');
    $this->drupalGet('node/' . $node->id());
    $this->assertSession()
        ->pageTextContains('Access denied');
    // Verify that the node no longer appears on the index.
    $this->drupalGet('forum/' . $tid);
    $this->assertSession()
        ->pageTextNotContains($title);
}

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