3 namespace Drupal\Tests\forum\Functional;
5 use Drupal\Core\Datetime\DrupalDateTime;
6 use Drupal\comment\Entity\Comment;
7 use Drupal\Tests\BrowserTestBase;
10 * Tests the forum blocks.
14 class ForumBlockTest extends BrowserTestBase {
21 public static $modules = ['forum', 'block'];
24 * A user with various administrative privileges.
28 protected function setUp() {
32 $this->adminUser = $this->drupalCreateUser([
33 'access administration pages',
36 'create forum content',
38 'skip comment approval',
43 * Tests the "New forum topics" block.
45 public function testNewForumTopicsBlock() {
46 $this->drupalLogin($this->adminUser);
48 // Enable the new forum topics block.
49 $block = $this->drupalPlaceBlock('forum_new_block');
52 // Create 5 forum topics.
53 $topics = $this->createForumTopics();
56 $this->assertLink(t('More'), 0, 'New forum topics block has a "more"-link.');
57 $this->assertLinkByHref('forum', 0, 'New forum topics block has a "more"-link.');
59 // We expect all 5 forum topics to appear in the "New forum topics" block.
60 foreach ($topics as $topic) {
61 $this->assertLink($topic, 0, format_string('Forum topic @topic found in the "New forum topics" block.', ['@topic' => $topic]));
64 // Configure the new forum topics block to only show 2 topics.
65 $block->getPlugin()->setConfigurationValue('block_count', 2);
69 // We expect only the 2 most recent forum topics to appear in the "New forum
71 for ($index = 0; $index < 5; $index++) {
72 if (in_array($index, [3, 4])) {
73 $this->assertLink($topics[$index], 0, format_string('Forum topic @topic found in the "New forum topics" block.', ['@topic' => $topics[$index]]));
76 $this->assertNoText($topics[$index], format_string('Forum topic @topic not found in the "New forum topics" block.', ['@topic' => $topics[$index]]));
82 * Tests the "Active forum topics" block.
84 public function testActiveForumTopicsBlock() {
85 $this->drupalLogin($this->adminUser);
87 // Create 10 forum topics.
88 $topics = $this->createForumTopics(10);
90 // Comment on the first 5 topics.
91 $date = new DrupalDateTime();
92 for ($index = 0; $index < 5; $index++) {
93 // Get the node from the topic title.
94 $node = $this->drupalGetNodeByTitle($topics[$index]);
95 $date->modify('+1 minute');
96 $comment = Comment::create([
97 'entity_id' => $node->id(),
98 'field_name' => 'comment_forum',
99 'entity_type' => 'node',
100 'node_type' => 'node_type_' . $node->bundle(),
101 'subject' => $this->randomString(20),
102 'comment_body' => $this->randomString(256),
103 'created' => $date->getTimestamp(),
109 $block = $this->drupalPlaceBlock('forum_active_block');
110 $this->drupalGet('');
111 $this->assertLink(t('More'), 0, 'Active forum topics block has a "more"-link.');
112 $this->assertLinkByHref('forum', 0, 'Active forum topics block has a "more"-link.');
114 // We expect the first 5 forum topics to appear in the "Active forum topics"
116 $this->drupalGet('<front>');
117 for ($index = 0; $index < 10; $index++) {
119 $this->assertLink($topics[$index], 0, format_string('Forum topic @topic found in the "Active forum topics" block.', ['@topic' => $topics[$index]]));
122 $this->assertNoText($topics[$index], format_string('Forum topic @topic not found in the "Active forum topics" block.', ['@topic' => $topics[$index]]));
126 // Configure the active forum block to only show 2 topics.
127 $block->getPlugin()->setConfigurationValue('block_count', 2);
130 $this->drupalGet('');
132 // We expect only the 2 forum topics with most recent comments to appear in
133 // the "Active forum topics" block.
134 for ($index = 0; $index < 10; $index++) {
135 if (in_array($index, [3, 4])) {
136 $this->assertLink($topics[$index], 0, 'Forum topic found in the "Active forum topics" block.');
139 $this->assertNoText($topics[$index], 'Forum topic not found in the "Active forum topics" block.');
145 * Creates a forum topic.
148 * The title of the newly generated topic.
150 protected function createForumTopics($count = 5) {
152 $date = new DrupalDateTime();
153 $date->modify('-24 hours');
155 for ($index = 0; $index < $count; $index++) {
156 // Generate a random subject/body.
157 $title = $this->randomMachineName(20);
158 $body = $this->randomMachineName(200);
159 // Forum posts are ordered by timestamp, so force a unique timestamp by
160 // changing the date.
161 $date->modify('+1 minute');
164 'title[0][value]' => $title,
165 'body[0][value]' => $body,
166 // Forum posts are ordered by timestamp, so force a unique timestamp by
168 'created[0][value][date]' => $date->format('Y-m-d'),
169 'created[0][value][time]' => $date->format('H:i:s'),
172 // Create the forum topic, preselecting the forum ID via a URL parameter.
173 $this->drupalPostForm('node/add/forum', $edit, t('Save and publish'), ['query' => ['forum_id' => 1]]);