View source
<?php
namespace Drupal\Tests\forum\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\Tests\BrowserTestBase;
class ForumTest extends BrowserTestBase {
public static $modules = [
'taxonomy',
'comment',
'forum',
'node',
'block',
'menu_ui',
'help',
];
protected $defaultTheme = 'classy';
protected $adminUser;
protected $editOwnTopicsUser;
protected $editAnyTopicsUser;
protected $webUser;
protected $postCommentUser;
protected $forumContainer;
protected $forum;
protected $rootForum;
protected $nids;
protected function setUp() {
parent::setUp();
$this
->drupalPlaceBlock('system_breadcrumb_block');
$this
->drupalPlaceBlock('page_title_block');
$this->adminUser = $this
->drupalCreateUser([
'access administration pages',
'administer modules',
'administer blocks',
'administer forums',
'administer menu',
'administer taxonomy',
'create forum content',
'access comments',
]);
$this->editAnyTopicsUser = $this
->drupalCreateUser([
'access administration pages',
'create forum content',
'edit any forum content',
'delete any forum content',
]);
$this->editOwnTopicsUser = $this
->drupalCreateUser([
'create forum content',
'edit own forum content',
'delete own forum content',
]);
$this->webUser = $this
->drupalCreateUser();
$this->postCommentUser = $this
->drupalCreateUser([
'administer content types',
'create forum content',
'post comments',
'skip comment approval',
'access comments',
]);
$this
->drupalPlaceBlock('help_block', [
'region' => 'help',
]);
$this
->drupalPlaceBlock('local_actions_block');
}
public function testForum() {
$this
->drupalGet('/forum');
$this
->assertRaw(Link::createFromRoute(t('General discussion'), 'forum.page', [
'taxonomy_term' => 1,
])
->toString(), "Found the default forum at the /forum listing");
$this
->assertCacheTag('config:forum.settings');
$this
->drupalGet(Url::fromRoute('forum.page', [
'taxonomy_term' => 1,
]));
$this
->assertCacheTag('config:forum.settings');
$this
->doAdminTests($this->adminUser);
$display = EntityViewDisplay::load('node.forum.default');
$body = $display
->getComponent('body');
$comment = $display
->getComponent('comment_forum');
$taxonomy = $display
->getComponent('taxonomy_forums');
$this
->assertTrue($taxonomy['weight'] < $body['weight']);
$this
->assertTrue($body['weight'] < $comment['weight']);
$display = EntityFormDisplay::load('node.forum.default');
$body = $display
->getComponent('body');
$comment = $display
->getComponent('comment_forum');
$taxonomy = $display
->getComponent('taxonomy_forums');
$this
->assertTrue($taxonomy['weight'] < $body['weight']);
$this
->generateForumTopics();
$this
->drupalLogin($this->webUser);
$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.");
$this
->doBasicTests($this->editAnyTopicsUser, TRUE);
$any_topics_user_node = $this
->createForumTopic($this->forum, FALSE);
$this
->doBasicTests($this->editOwnTopicsUser, FALSE);
$own_topics_user_node = $this
->createForumTopic($this->forum, FALSE);
$this
->verifyForums($any_topics_user_node, FALSE, 403);
$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'));
$this
->drupalLogin($this->editAnyTopicsUser);
$this
->verifyForums($own_topics_user_node, TRUE);
$this
->drupalGet('forum');
$forum_arg = [
':forum' => 'forum-list-' . $this->forum['tid'],
];
$xpath = $this
->buildXPathQuery('//tr[@id=:forum]//td[@class="forum__topics"]', $forum_arg);
$topics = $this
->xpath($xpath);
$topics = trim($topics[0]
->getText());
$this
->assertEquals('6 6 new posts in forum ' . $this->forum['name'], $topics, 'Number of topics found.');
$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.');
$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.');
$elements = $this
->xpath('//tr[@id=:forum]//td[@class="forum__posts"]', $forum_arg);
$this
->assertEquals('6', $elements[0]
->getText(), 'Number of posts found.');
$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');
$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);
$this
->drupalLogin($this->editAnyTopicsUser);
$this
->drupalGet('forum/' . $this->forum['tid']);
$this
->drupalPostForm('node/' . $node
->id() . '/edit', [], t('Save'));
$this
->assertSession()
->statusCodeEquals(200);
$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');
$this
->drupalLogout();
$this
->drupalGet('forum/' . $this->forum['tid']);
$this
->assertSession()
->linkExists(t('Log in to post new content in the forum.'));
}
public function testAddOrphanTopic() {
$vid = $this
->config('forum.settings')
->get('vocabulary');
$tids = \Drupal::entityQuery('taxonomy_term')
->condition('vid', $vid)
->execute();
$term_storage = \Drupal::entityTypeManager()
->getStorage('taxonomy_term');
$terms = $term_storage
->loadMultiple($tids);
$term_storage
->delete($terms);
$edit = [];
$edit['title[0][value]'] = $this
->randomMachineName(10);
$edit['body[0][value]'] = $this
->randomMachineName(120);
$this
->drupalLogin($this->adminUser);
$this
->drupalPostForm('node/add/forum', $edit, t('Save'));
$nid_count = $this->container
->get('entity_type.manager')
->getStorage('node')
->getQuery()
->accessCheck(FALSE)
->count()
->execute();
$this
->assertEqual(0, $nid_count, 'A forum node was not created when missing a forum vocabulary.');
\Drupal::service('module_installer')
->install([
'forum',
]);
}
private function doAdminTests($user) {
$this
->drupalLogin($user);
$edit = [];
$this
->drupalPostForm('admin/structure/menu/manage/tools', $edit, t('Save'));
$this
->assertSession()
->statusCodeEquals(200);
$this
->editForumVocabulary();
$this->forumContainer = $this
->createForum('container');
$this
->drupalGet('admin/structure/forum');
$this
->assertText(t('Forums contain forum topics. Use containers to group related forums'));
$this
->assertSession()
->linkExists('Add forum');
$this
->assertSession()
->linkExists('Add container');
$this
->clickLink('edit container');
$this
->assertRaw('Edit container', 'Followed the link to edit the container');
$this->forum = $this
->createForum('forum', $this->forumContainer['tid']);
$this
->drupalGet('admin/structure/forum');
$this
->clickLink('edit forum');
$this
->assertRaw('Edit forum', 'Followed the link to edit the forum');
$this
->drupalGet('admin/structure/forum');
$delete_forum = $this
->createForum('forum', $this->forumContainer['tid']);
$this
->drupalPostForm('admin/structure/forum/', [], t('Save'));
$this
->assertRaw(t('The configuration options have been saved.'));
$this
->deleteForum($delete_forum['tid']);
$this->rootForum = $this
->createForum('forum');
$this
->drupalGet('admin/structure/taxonomy/manage/forums');
$this
->assertSession()
->buttonExists('Save');
$this
->assertSession()
->buttonNotExists('Delete');
$this
->drupalGet('taxonomy/term/' . $this->forumContainer['tid'] . '/edit');
$this
->assertNoField('parent[]', 'Parent field not found.');
$description = 'Use tags to group articles on similar topics into categories.';
$help = 'Enter a comma-separated list of words to describe your content.';
$vocabulary = Vocabulary::create([
'name' => 'Tags',
'description' => $description,
'vid' => 'tags',
'langcode' => \Drupal::languageManager()
->getDefaultLanguage()
->getId(),
'help' => $help,
]);
$vocabulary
->save();
$this
->drupalGet('admin/structure/taxonomy/manage/tags');
$this
->assertSession()
->buttonExists('Save');
$this
->assertSession()
->linkExists(t('Delete'));
$this
->drupalGet('admin/structure/taxonomy/manage/tags/add');
$this
->assertField('parent[]', 'Parent field found.');
$relations_widget = $this
->xpath("//details[@id='edit-relations']");
$this
->assertTrue(isset($relations_widget[0]), 'Relations widget element found.');
}
public function editForumVocabulary() {
$vid = $this
->config('forum.settings')
->get('vocabulary');
$original_vocabulary = Vocabulary::load($vid);
$edit = [
'name' => $this
->randomMachineName(10),
'description' => $this
->randomMachineName(100),
];
$this
->drupalPostForm('admin/structure/taxonomy/manage/' . $original_vocabulary
->id(), $edit, t('Save'));
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertRaw(t('Updated vocabulary %name.', [
'%name' => $edit['name'],
]), 'Vocabulary was edited');
$current_vocabulary = Vocabulary::load($vid);
$this
->assertEqual($current_vocabulary
->label(), $edit['name'], 'The name was updated');
$this
->assertEqual($current_vocabulary
->getDescription(), $edit['description'], 'The description was updated');
$current_vocabulary
->set('name', $original_vocabulary
->label());
$current_vocabulary
->set('description', $original_vocabulary
->getDescription());
$current_vocabulary
->save();
$current_vocabulary = Vocabulary::load($vid);
$this
->assertEqual($current_vocabulary
->label(), $original_vocabulary
->label(), 'The original vocabulary settings were restored');
}
public function createForum($type, $parent = 0) {
$name = $this
->randomMachineName(10);
$description = $this
->randomMachineName(100);
$edit = [
'name[0][value]' => $name,
'description[0][value]' => $description,
'parent[0]' => $parent,
'weight' => '0',
];
$this
->drupalPostForm('admin/structure/forum/add/' . $type, $edit, t('Save'));
$this
->assertSession()
->statusCodeEquals(200);
$type = $type == 'container' ? 'forum container' : 'forum';
$this
->assertText(t('Created new @type @term.', [
'@term' => $name,
'@type' => t($type),
]), new FormattableMarkup('@type was created', [
'@type' => ucfirst($type),
]));
$view_link = $this
->xpath('//div[@class="messages"]//a[contains(@href, :href)]', [
':href' => 'term/',
]);
$this
->assert(isset($view_link), 'The message area contains a link to a term');
$taxonomy_term_storage = $this->container
->get('entity_type.manager')
->getStorage('taxonomy_term');
$term = $taxonomy_term_storage
->loadByProperties([
'vid' => $this
->config('forum.settings')
->get('vocabulary'),
'name' => $name,
'description__value' => $description,
]);
$term = array_shift($term);
$this
->assertTrue(!empty($term), 'The ' . $type . ' exists in the database');
$tid = $term
->id();
$parent_tid = $taxonomy_term_storage
->loadParents($tid);
$parent_tid = empty($parent_tid) ? 0 : array_shift($parent_tid)
->id();
$this
->assertTrue($parent == $parent_tid, 'The ' . $type . ' is linked to its container');
$forum = $taxonomy_term_storage
->load($tid);
$this
->assertEqual($type == 'forum container', (bool) $forum->forum_container->value);
return [
'tid' => $tid,
'name' => $term
->getName(),
'vid' => $term
->bundle(),
];
}
public function deleteForum($tid) {
$this
->drupalGet('admin/structure/forum/edit/forum/' . $tid);
$this
->clickLink(t('Delete'));
$this
->assertText('Are you sure you want to delete the forum');
$this
->assertNoText('Add forum');
$this
->assertNoText('Add forum container');
$this
->drupalPostForm(NULL, [], t('Delete'));
$this
->drupalGet('forum/' . $tid);
$this
->assertSession()
->statusCodeEquals(404);
}
private function doBasicTests($user, $admin) {
$this
->drupalLogin($user);
$this
->createForumTopic($this->forumContainer, TRUE);
$node = $this
->createForumTopic($this->forum, FALSE);
$this
->verifyForums($node, $admin);
}
public function testForumWithNewPost() {
$this
->drupalLogin($this->adminUser);
$this->forumContainer = $this
->createForum('container');
$this->forum = $this
->createForum('forum');
$node = $this
->createForumTopic($this->forum, FALSE);
$this
->drupalLogin($this->postCommentUser);
$edit = [];
$edit['subject[0][value]'] = $this
->randomMachineName();
$edit['comment_body[0][value]'] = $this
->randomMachineName();
$this
->drupalPostForm('node/' . $node
->id(), $edit, t('Save'));
$this
->assertSession()
->statusCodeEquals(200);
$this
->clickLink('Reply');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertFieldByName('comment_body[0][value]');
$this
->drupalLogin($this->adminUser);
$this
->drupalGet("forum/{$this->forum['tid']}");
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertNoEscaped('<');
}
public function createForumTopic($forum, $container = FALSE) {
$title = $this
->randomMachineName(20);
$body = $this
->randomMachineName(200);
$edit = [
'title[0][value]' => $title,
'body[0][value]' => $body,
];
$tid = $forum['tid'];
$this
->drupalPostForm('node/add/forum', $edit, t('Save'), [
'query' => [
'forum_id' => $tid,
],
]);
$type = t('Forum topic');
if ($container) {
$this
->assertNoText(t('@type @title has been created.', [
'@type' => $type,
'@title' => $title,
]), 'Forum topic was not created');
$this
->assertRaw(t('The item %title is a forum container, not a forum.', [
'%title' => $forum['name'],
]), 'Error message was shown');
return;
}
else {
$this
->assertText(t('@type @title has been created.', [
'@type' => $type,
'@title' => $title,
]), 'Forum topic was created');
$this
->assertNoRaw(t('The item %title is a forum container, not a forum.', [
'%title' => $forum['name'],
]), 'No error message was shown');
$view_link = $this
->xpath('//div[@class="messages"]//a[contains(@href, :href)]', [
':href' => 'term/',
]);
$this
->assert(isset($view_link), 'The message area contains a link to a term');
}
$node = $this
->drupalGetNodeByTitle($title);
$this
->assertTrue($node != NULL, new FormattableMarkup('Node @title was loaded', [
'@title' => $title,
]));
$this
->assertEqual($node->taxonomy_forums->target_id, $tid, 'Saved forum topic was in the expected forum');
$this
->drupalGet('node/' . $node
->id());
$this
->assertRaw($title, 'Subject was found');
$this
->assertRaw($body, 'Body was found');
return $node;
}
private function verifyForums(EntityInterface $node, $admin, $response = 200) {
$response2 = $admin ? 200 : 403;
$this
->drupalGet('admin/help/forum');
$this
->assertSession()
->statusCodeEquals($response2);
if ($response2 == 200) {
$this
->assertTitle('Forum | Drupal');
$this
->assertText(t('Forum'), 'Forum help node was displayed');
}
$this
->verifyForumView($this->forumContainer);
$this
->verifyForumView($this->forum, $this->forumContainer);
$this
->verifyForumView($this->rootForum);
$this
->drupalGet('node/' . $node
->id());
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertTitle($node
->label() . ' | Drupal');
$breadcrumb_build = [
Link::createFromRoute(t('Home'), '<front>'),
Link::createFromRoute(t('Forums'), 'forum.index'),
Link::createFromRoute($this->forumContainer['name'], 'forum.page', [
'taxonomy_term' => $this->forumContainer['tid'],
]),
Link::createFromRoute($this->forum['name'], 'forum.page', [
'taxonomy_term' => $this->forum['tid'],
]),
];
$breadcrumb = [
'#theme' => 'breadcrumb',
'#links' => $breadcrumb_build,
];
$this
->assertRaw(\Drupal::service('renderer')
->renderRoot($breadcrumb), 'Breadcrumbs were displayed');
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->assertSession()
->statusCodeEquals($response);
if ($response == 200) {
$this
->assertTitle('Edit Forum topic ' . $node
->label() . ' | Drupal');
}
if ($response == 200) {
$edit = [];
$edit['title[0][value]'] = 'node/' . $node
->id();
$edit['body[0][value]'] = $this
->randomMachineName(256);
$edit['taxonomy_forums'] = $this->rootForum['tid'];
$edit['shadow'] = TRUE;
$this
->drupalPostForm('node/' . $node
->id() . '/edit', $edit, t('Save'));
$this
->assertText(t('Forum topic @title has been updated.', [
'@title' => $edit['title[0][value]'],
]), 'Forum node was edited');
$forum_tid = $this->container
->get('database')
->select('forum', 'f')
->fields('f', [
'tid',
])
->condition('nid', $node
->id())
->condition('vid', $node
->getRevisionId())
->execute()
->fetchField();
$this
->assertTrue($forum_tid == $this->rootForum['tid'], 'The forum topic is linked to a different forum');
$this
->drupalPostForm('node/' . $node
->id() . '/delete', [], t('Delete'));
$this
->assertSession()
->statusCodeEquals($response);
$this
->assertRaw(t('Forum topic %title has been deleted.', [
'%title' => $edit['title[0][value]'],
]), 'Forum node was deleted');
}
}
private function verifyForumView($forum, $parent = NULL) {
$this
->drupalGet('forum/' . $forum['tid']);
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertTitle($forum['name'] . ' | Drupal');
$breadcrumb_build = [
Link::createFromRoute(t('Home'), '<front>'),
Link::createFromRoute(t('Forums'), 'forum.index'),
];
if (isset($parent)) {
$breadcrumb_build[] = Link::createFromRoute($parent['name'], 'forum.page', [
'taxonomy_term' => $parent['tid'],
]);
}
$breadcrumb = [
'#theme' => 'breadcrumb',
'#links' => $breadcrumb_build,
];
$this
->assertRaw(\Drupal::service('renderer')
->renderRoot($breadcrumb), 'Breadcrumbs were displayed');
}
private function generateForumTopics() {
$this->nids = [];
for ($i = 0; $i < 5; $i++) {
$node = $this
->createForumTopic($this->forum, FALSE);
$this->nids[] = $node
->id();
}
}
}