function ForumBlockTest::testNewForumTopicsBlock

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

Tests the "New forum topics" block.

File

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

Class

ForumBlockTest
Tests the forum blocks.

Namespace

Drupal\Tests\forum\Functional

Code

public function testNewForumTopicsBlock() {
    $this->drupalLogin($this->adminUser);
    // Enable the new forum topics block.
    $block = $this->drupalPlaceBlock('forum_new_block');
    $this->drupalGet('');
    // Create 5 forum topics.
    $topics = $this->createForumTopics();
    $this->assertSession()
        ->linkExists(t('More'), 0, 'New forum topics block has a "more"-link.');
    $this->assertLinkByHref('forum', 0, 'New forum topics block has a "more"-link.');
    // We expect all 5 forum topics to appear in the "New forum topics" block.
    foreach ($topics as $topic) {
        $this->assertSession()
            ->linkExists($topic, 0, new FormattableMarkup('Forum topic @topic found in the "New forum topics" block.', [
            '@topic' => $topic,
        ]));
    }
    // Configure the new forum topics block to only show 2 topics.
    $block->getPlugin()
        ->setConfigurationValue('block_count', 2);
    $block->save();
    $this->drupalGet('');
    // We expect only the 2 most recent forum topics to appear in the "New forum
    // topics" block.
    for ($index = 0; $index < 5; $index++) {
        if (in_array($index, [
            3,
            4,
        ])) {
            $this->assertSession()
                ->linkExists($topics[$index], 0, new FormattableMarkup('Forum topic @topic found in the "New forum topics" block.', [
                '@topic' => $topics[$index],
            ]));
        }
        else {
            $this->assertNoText($topics[$index], new FormattableMarkup('Forum topic @topic not found in the "New forum topics" block.', [
                '@topic' => $topics[$index],
            ]));
        }
    }
}

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