function ForumTest::testForum

Same name and namespace in other branches
  1. 10 core/modules/forum/tests/src/Functional/ForumTest.php \Drupal\Tests\forum\Functional\ForumTest::testForum()
  2. 9 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\Functional

Code

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->assertRaw(Link::createFromRoute(t('General discussion'), 'forum.page', [
    'taxonomy_term' => 1,
  ])->toString(), "Found the default forum at the /forum listing");
  // Check the presence of expected cache tags.
  $this->assertCacheTag('config:forum.settings');
  $this->drupalGet(Url::fromRoute('forum.page', [
    'taxonomy_term' => 1,
  ]));
  $this->assertCacheTag('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->assertTrue($taxonomy['weight'] < $body['weight']);
  $this->assertTrue($body['weight'] < $comment['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->assertTrue($taxonomy['weight'] < $body['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->assertText(t('You are not allowed to post new content in the forum'), "Authenticated user without permission to post forum content is shown message in local tasks to that effect.");
  // 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(t('Add new Forum topic'));
  $this->drupalGet('forum/' . $this->forum['tid']);
  $this->assertSession()
    ->linkExists(t('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->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->drupalPostForm('admin/structure/types/manage/forum', [
    'options[promote]' => 'promote',
  ], t('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->drupalPostForm('node/' . $node->id(), $edit, t('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->drupalPostForm('node/' . $node->id() . '/edit', [], t('Save'));
  $this->assertSession()
    ->statusCodeEquals(200);
  // Test the root forum page title change.
  $this->drupalGet('forum');
  $this->assertCacheTag('config:taxonomy.vocabulary.' . $this->forum['vid']);
  $this->assertTitle('Forums | Drupal');
  $vocabulary = Vocabulary::load($this->forum['vid']);
  $vocabulary->set('name', 'Discussions');
  $vocabulary->save();
  $this->drupalGet('forum');
  $this->assertTitle('Discussions | Drupal');
  // Test anonymous action link.
  $this->drupalLogout();
  $this->drupalGet('forum/' . $this->forum['tid']);
  $this->assertSession()
    ->linkExists(t('Log in to post new content in the forum.'));
}

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