public function ForumTest::testForum in Drupal 9
Same name and namespace in other branches
- 8 core/modules/forum/tests/src/Functional/ForumTest.php \Drupal\Tests\forum\Functional\ForumTest::testForum()
Tests forum functionality through the admin and user interfaces.
File
- core/
modules/ forum/ tests/ src/ Functional/ ForumTest.php, line 134
Class
- ForumTest
- Tests for forum.module.
Namespace
Drupal\Tests\forum\FunctionalCode
public function testForum() {
// Check that the basic forum install creates a default forum topic
$this
->drupalGet('/forum');
// Look for the "General discussion" default forum
$this
->assertSession()
->linkExists('General discussion');
$this
->assertSession()
->linkByHrefExists('/forum/1');
// Check the presence of expected cache tags.
$this
->assertSession()
->responseHeaderContains('X-Drupal-Cache-Tags', 'config:forum.settings');
$this
->drupalGet(Url::fromRoute('forum.page', [
'taxonomy_term' => 1,
]));
$this
->assertSession()
->responseHeaderContains('X-Drupal-Cache-Tags', 'config:forum.settings');
// Do the admin tests.
$this
->doAdminTests($this->adminUser);
// Check display order.
$display = EntityViewDisplay::load('node.forum.default');
$body = $display
->getComponent('body');
$comment = $display
->getComponent('comment_forum');
$taxonomy = $display
->getComponent('taxonomy_forums');
// Assert field order is body » taxonomy » comments.
$this
->assertLessThan($body['weight'], $taxonomy['weight']);
$this
->assertLessThan($comment['weight'], $body['weight']);
// Check form order.
$display = EntityFormDisplay::load('node.forum.default');
$body = $display
->getComponent('body');
$comment = $display
->getComponent('comment_forum');
$taxonomy = $display
->getComponent('taxonomy_forums');
// Assert category comes before body in order.
$this
->assertLessThan($body['weight'], $taxonomy['weight']);
$this
->generateForumTopics();
// Log in an unprivileged user to view the forum topics and generate an
// active forum topics list.
$this
->drupalLogin($this->webUser);
// Verify that this user is shown a message that they may not post content.
$this
->drupalGet('forum/' . $this->forum['tid']);
$this
->assertSession()
->pageTextContains('You are not allowed to post new content in the forum');
// Log in, and do basic tests for a user with permission to edit any forum
// content.
$this
->doBasicTests($this->editAnyTopicsUser, TRUE);
// Create a forum node authored by this user.
$any_topics_user_node = $this
->createForumTopic($this->forum, FALSE);
// Log in, and do basic tests for a user with permission to edit only its
// own forum content.
$this
->doBasicTests($this->editOwnTopicsUser, FALSE);
// Create a forum node authored by this user.
$own_topics_user_node = $this
->createForumTopic($this->forum, FALSE);
// Verify that this user cannot edit forum content authored by another user.
$this
->verifyForums($any_topics_user_node, FALSE, 403);
// Verify that this user is shown a local task to add new forum content.
$this
->drupalGet('forum');
$this
->assertSession()
->linkExists('Add new Forum topic');
$this
->drupalGet('forum/' . $this->forum['tid']);
$this
->assertSession()
->linkExists('Add new Forum topic');
// Log in a user with permission to edit any forum content.
$this
->drupalLogin($this->editAnyTopicsUser);
// Verify that this user can edit forum content authored by another user.
$this
->verifyForums($own_topics_user_node, TRUE);
// Verify the topic and post counts on the forum page.
$this
->drupalGet('forum');
// Verify row for testing forum.
$forum_arg = [
':forum' => 'forum-list-' . $this->forum['tid'],
];
// Topics cell contains number of topics and number of unread topics.
$xpath = $this
->assertSession()
->buildXPathQuery('//tr[@id=:forum]//td[@class="forum__topics"]', $forum_arg);
$topics = $this
->xpath($xpath);
$topics = trim($topics[0]
->getText());
// The extracted text contains the number of topics (6) and new posts
// (also 6) in this table cell.
$this
->assertEquals('6 6 new posts in forum ' . $this->forum['name'], $topics, 'Number of topics found.');
// Verify the number of unread topics.
$elements = $this
->xpath('//tr[@id=:forum]//td[@class="forum__topics"]//a', $forum_arg);
$this
->assertStringStartsWith('6 new posts', $elements[0]
->getText(), 'Number of unread topics found.');
// Verify that the forum name is in the unread topics text.
$elements = $this
->xpath('//tr[@id=:forum]//em[@class="placeholder"]', $forum_arg);
$this
->assertStringContainsString($this->forum['name'], $elements[0]
->getText(), 'Forum name found in unread topics text.');
// Verify total number of posts in forum.
$elements = $this
->xpath('//tr[@id=:forum]//td[@class="forum__posts"]', $forum_arg);
$this
->assertEquals('6', $elements[0]
->getText(), 'Number of posts found.');
// Test loading multiple forum nodes on the front page.
$this
->drupalLogin($this
->drupalCreateUser([
'administer content types',
'create forum content',
'post comments',
]));
$this
->drupalGet('admin/structure/types/manage/forum');
$this
->submitForm([
'options[promote]' => 'promote',
], 'Save content type');
$this
->createForumTopic($this->forum, FALSE);
$this
->createForumTopic($this->forum, FALSE);
$this
->drupalGet('node');
// Test adding a comment to a forum topic.
$node = $this
->createForumTopic($this->forum, FALSE);
$edit = [];
$edit['comment_body[0][value]'] = $this
->randomMachineName();
$this
->drupalGet('node/' . $node
->id());
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->statusCodeEquals(200);
// Test editing a forum topic that has a comment.
$this
->drupalLogin($this->editAnyTopicsUser);
$this
->drupalGet('forum/' . $this->forum['tid']);
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->submitForm([], 'Save');
$this
->assertSession()
->statusCodeEquals(200);
// Test the root forum page title change.
$this
->drupalGet('forum');
$this
->assertSession()
->responseHeaderContains('X-Drupal-Cache-Tags', 'config:taxonomy.vocabulary.' . $this->forum['vid']);
$this
->assertSession()
->titleEquals('Forums | Drupal');
$vocabulary = Vocabulary::load($this->forum['vid']);
$vocabulary
->set('name', 'Discussions');
$vocabulary
->save();
$this
->drupalGet('forum');
$this
->assertSession()
->titleEquals('Discussions | Drupal');
// Test anonymous action link.
$this
->drupalLogout();
$this
->drupalGet('forum/' . $this->forum['tid']);
$this
->assertSession()
->linkExists('Log in to post new content in the forum.');
}