You are here

function ForumTest::testForum in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/forum/src/Tests/ForumTest.php \Drupal\forum\Tests\ForumTest::testForum()

Tests forum functionality through the admin and user interfaces.

File

core/modules/forum/src/Tests/ForumTest.php, line 125
Contains \Drupal\forum\Tests\ForumTest.

Class

ForumTest
Create, view, edit, delete, and change forum entries and verify its consistency in the database.

Namespace

Drupal\forum\Tests

Code

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(t('<a href="' . Url::fromRoute('forum.page', [
    'taxonomy_term' => 1,
  ])
    ->toString() . '">General discussion</a>'), "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();

  // Login 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
    ->assertLink(t('Add new Forum topic'));
  $this
    ->drupalGet('forum/' . $this->forum['tid']);
  $this
    ->assertLink(t('Add new Forum topic'));

  // Login 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 = array(
    ':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]);
  $this
    ->assertEqual($topics, '6', 'Number of topics found.');

  // Verify the number of unread topics.
  $unread_topics = $this->container
    ->get('forum_manager')
    ->unreadTopics($this->forum['tid'], $this->editAnyTopicsUser
    ->id());
  $unread_topics = \Drupal::translation()
    ->formatPlural($unread_topics, '1 new post', '@count new posts');
  $xpath = $this
    ->buildXPathQuery('//tr[@id=:forum]//td[@class="forum__topics"]//a', $forum_arg);
  $this
    ->assertFieldByXPath($xpath, $unread_topics, 'Number of unread topics found.');

  // Verify that the forum name is in the unread topics text.
  $xpath = $this
    ->buildXPathQuery('//tr[@id=:forum]//em[@class="placeholder"]', $forum_arg);
  $this
    ->assertFieldByXpath($xpath, $this->forum['name'], 'Forum name found in unread topics text.');

  // Verify total number of posts in forum.
  $xpath = $this
    ->buildXPathQuery('//tr[@id=:forum]//td[@class="forum__posts"]', $forum_arg);
  $this
    ->assertFieldByXPath($xpath, '6', 'Number of posts found.');

  // Test loading multiple forum nodes on the front page.
  $this
    ->drupalLogin($this
    ->drupalCreateUser(array(
    'administer content types',
    'create forum content',
    'post comments',
  )));
  $this
    ->drupalPostForm('admin/structure/types/manage/forum', array(
    '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 = array();
  $edit['comment_body[0][value]'] = $this
    ->randomMachineName();
  $this
    ->drupalPostForm('node/' . $node
    ->id(), $edit, t('Save'));
  $this
    ->assertResponse(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', array(), t('Save'));
  $this
    ->assertResponse(200);

  // Test the root forum page title change.
  $this
    ->drupalGet('forum');
  $this
    ->assertCacheTag('config:taxonomy.vocabulary.' . $this->forum['vid']);
  $this
    ->assertTitle(t('Forums | Drupal'));
  $vocabulary = Vocabulary::load($this->forum['vid']);
  $vocabulary
    ->set('name', 'Discussions');
  $vocabulary
    ->save();
  $this
    ->drupalGet('forum');
  $this
    ->assertTitle(t('Discussions | Drupal'));

  // Test anonymous action link.
  $this
    ->drupalLogout();
  $this
    ->drupalGet('forum/' . $this->forum['tid']);
  $this
    ->assertLink(t('Log in to post new content in the forum.'));
}