View source
<?php
namespace Drupal\Tests\forum\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\comment\Entity\Comment;
use Drupal\Tests\BrowserTestBase;
class ForumBlockTest extends BrowserTestBase {
protected static $modules = [
'forum',
'block',
];
protected $defaultTheme = 'stark';
protected $adminUser;
protected function setUp() : void {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser([
'access administration pages',
'administer blocks',
'administer nodes',
'create forum content',
'post comments',
'skip comment approval',
]);
}
public function testNewForumTopicsBlock() {
$this
->drupalLogin($this->adminUser);
$block = $this
->drupalPlaceBlock('forum_new_block');
$this
->drupalGet('');
$topics = $this
->createForumTopics();
$this
->assertSession()
->linkExists('More', 0, 'New forum topics block has a "more"-link.');
$this
->assertSession()
->linkByHrefExists('forum', 0, 'New forum topics block has a "more"-link.');
foreach ($topics as $topic) {
$this
->assertSession()
->linkExists($topic, 0, new FormattableMarkup('Forum topic @topic found in the "New forum topics" block.', [
'@topic' => $topic,
]));
}
$block
->getPlugin()
->setConfigurationValue('block_count', 2);
$block
->save();
$this
->drupalGet('');
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
->assertSession()
->pageTextNotContains($topics[$index]);
}
}
}
public function testActiveForumTopicsBlock() {
$this
->drupalLogin($this->adminUser);
$topics = $this
->createForumTopics(10);
$date = new DrupalDateTime();
for ($index = 0; $index < 5; $index++) {
$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();
}
$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.');
$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]);
}
}
$block
->getPlugin()
->setConfigurationValue('block_count', 2);
$block
->save();
$this
->drupalGet('');
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]);
}
}
}
protected function createForumTopics($count = 5) {
$topics = [];
$date = new DrupalDateTime();
$date
->modify('-24 hours');
for ($index = 0; $index < $count; $index++) {
$title = $this
->randomMachineName(20);
$body = $this
->randomMachineName(200);
$date
->modify('+1 minute');
$edit = [
'title[0][value]' => $title,
'body[0][value]' => $body,
'created[0][value][date]' => $date
->format('Y-m-d'),
'created[0][value][time]' => $date
->format('H:i:s'),
];
$this
->drupalGet('node/add/forum', [
'query' => [
'forum_id' => 1,
],
]);
$this
->submitForm($edit, 'Save');
$topics[] = $title;
}
return $topics;
}
}