function ForumBlockTest::testActiveForumTopicsBlock

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

Tests the "Active forum topics" block.

File

core/modules/forum/tests/src/Functional/ForumBlockTest.php, line 95

Class

ForumBlockTest
Tests the forum blocks.

Namespace

Drupal\Tests\forum\Functional

Code

public function testActiveForumTopicsBlock() : void {
  $this->drupalLogin($this->adminUser);
  // Create 10 forum topics.
  $topics = $this->createForumTopics(10);
  // Comment on the first 5 topics.
  $date = new DrupalDateTime();
  for ($index = 0; $index < 5; $index++) {
    // Get the node from the topic title.
    $node = $this->drupalGetNodeByTitle($topics[$index]);
    $date->modify('+1 minute');
    $comment = Comment::create([
      'entity_id' => $node->id(),
      'field_name' => 'comment_forum',
      'entity_type' => 'node',
      'node_type' => 'node_type_' . $node->bundle(),
      'subject' => $this->randomString(20),
      'comment_body' => $this->randomString(256),
      'created' => $date->getTimestamp(),
    ]);
    $comment->save();
  }
  // Enable the block.
  $block = $this->drupalPlaceBlock('forum_active_block');
  $this->drupalGet('');
  $this->assertSession()
    ->linkExists('More', 0, 'Active forum topics block has a "more"-link.');
  $this->assertSession()
    ->linkByHrefExists('forum', 0, 'Active forum topics block has a "more"-link.');
  // We expect the first 5 forum topics to appear in the "Active forum topics"
  // block.
  $this->drupalGet('<front>');
  for ($index = 0; $index < 10; $index++) {
    if ($index < 5) {
      $this->assertSession()
        ->linkExists($topics[$index], 0, new FormattableMarkup('Forum topic @topic found in the "Active forum topics" block.', [
        '@topic' => $topics[$index],
      ]));
    }
    else {
      $this->assertSession()
        ->pageTextNotContains($topics[$index]);
    }
  }
  // Configure the active forum block to only show 2 topics.
  $block->getPlugin()
    ->setConfigurationValue('block_count', 2);
  $block->save();
  $this->drupalGet('');
  // We expect only the 2 forum topics with most recent comments to appear in
  // the "Active forum topics" block.
  for ($index = 0; $index < 10; $index++) {
    if (in_array($index, [
      3,
      4,
    ])) {
      $this->assertSession()
        ->linkExists($topics[$index], 0, 'Forum topic found in the "Active forum topics" block.');
    }
    else {
      $this->assertSession()
        ->pageTextNotContains($topics[$index]);
    }
  }
}

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