function ForumBlockTest::createForumTopics

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

Creates a forum topic.

Return value

string The title of the newly generated topic.

2 calls to ForumBlockTest::createForumTopics()
ForumBlockTest::testActiveForumTopicsBlock in core/modules/forum/tests/src/Functional/ForumBlockTest.php
Tests the "Active forum topics" block.
ForumBlockTest::testNewForumTopicsBlock in core/modules/forum/tests/src/Functional/ForumBlockTest.php
Tests the "New forum topics" block.

File

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

Class

ForumBlockTest
Tests the forum blocks.

Namespace

Drupal\Tests\forum\Functional

Code

protected function createForumTopics($count = 5) {
    $topics = [];
    $date = new DrupalDateTime();
    $date->modify('-24 hours');
    for ($index = 0; $index < $count; $index++) {
        // Generate a random subject/body.
        $title = $this->randomMachineName(20);
        $body = $this->randomMachineName(200);
        // Forum posts are ordered by timestamp, so force a unique timestamp by
        // changing the date.
        $date->modify('+1 minute');
        $edit = [
            'title[0][value]' => $title,
            'body[0][value]' => $body,
            // Forum posts are ordered by timestamp, so force a unique timestamp by
            // adding the index.
'created[0][value][date]' => $date->format('Y-m-d'),
            'created[0][value][time]' => $date->format('H:i:s'),
        ];
        // Create the forum topic, preselecting the forum ID via a URL parameter.
        $this->drupalGet('node/add/forum', [
            'query' => [
                'forum_id' => 1,
            ],
        ]);
        $this->submitForm($edit, 'Save');
        $topics[] = $title;
    }
    return $topics;
}

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