View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\forum\Functional;
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 {
protected static $modules = [
'taxonomy',
'comment',
'forum',
'node',
'block',
'menu_ui',
'help',
];
protected $defaultTheme = 'starterkit_theme';
protected $adminUser;
protected $editOwnTopicsUser;
protected $editAnyTopicsUser;
protected $webUser;
protected $postCommentUser;
protected $forumContainer;
protected $forum;
protected $rootForum;
protected $nids;
protected function setUp() : void {
parent::setUp();
$this
->drupalPlaceBlock('system_breadcrumb_block');
$this
->drupalPlaceBlock('page_title_block');
$this->adminUser = $this
->drupalCreateUser([
'access administration pages',
'access help pages',
'administer modules',
'administer blocks',
'administer forums',
'administer menu',
'administer taxonomy',
'create forum content',
'access comments',
]);
$this->editAnyTopicsUser = $this
->drupalCreateUser([
'access administration pages',
'access help 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
->assertSession()
->linkExists('General discussion');
$this
->assertSession()
->linkByHrefExists('/forum/1');
$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');
$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
->assertLessThan($body['weight'], $taxonomy['weight']);
$this
->assertLessThan($comment['weight'], $body['weight']);
$display = EntityFormDisplay::load('node.forum.default');
$body = $display
->getComponent('body');
$comment = $display
->getComponent('comment_forum');
$taxonomy = $display
->getComponent('taxonomy_forums');
$this
->assertLessThan($body['weight'], $taxonomy['weight']);
$this
->generateForumTopics();
$this
->drupalLogin($this->webUser);
$this
->drupalGet('forum/' . $this->forum['tid']);
$this
->assertSession()
->pageTextContains('You are not allowed to post new content in the forum');
$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('Add new Forum topic');
$this
->drupalGet('forum/' . $this->forum['tid']);
$this
->assertSession()
->linkExists('Add new Forum topic');
$this
->drupalLogin($this->editAnyTopicsUser);
$this
->verifyForums($own_topics_user_node, TRUE);
$this
->drupalGet('forum');
$row = $this
->assertSession()
->elementExists('css', '[title="New posts"]');
while ($row && $row
->getTagName() !== 'tr') {
$row = $row
->getParent();
}
$this
->assertNotEmpty($row);
$cells = $row
->findAll('css', 'td');
$this
->assertCount(4, $cells);
$this
->assertEquals('6 6 new posts in forum ' . $this->forum['name'], $cells[1]
->getText(), 'Number of topics found.');
$this
->assertEquals('6', $cells[2]
->getText(), 'Number of posts found.');
$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');
$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
->drupalGet('node/' . $node
->id());
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalLogin($this->editAnyTopicsUser);
$this
->drupalGet('forum/' . $this->forum['tid']);
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->submitForm([], 'Save');
$this
->assertSession()
->statusCodeEquals(200);
$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');
$this
->drupalLogout();
$this
->drupalGet('forum/' . $this->forum['tid']);
$this
->assertSession()
->linkExists('Log in to post new content in the forum.');
}
public function testAddOrphanTopic() {
$vid = $this
->config('forum.settings')
->get('vocabulary');
$tids = \Drupal::entityQuery('taxonomy_term')
->accessCheck(FALSE)
->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
->drupalGet('node/add/forum');
$this
->submitForm($edit, 'Save');
$nid_count = $this->container
->get('entity_type.manager')
->getStorage('node')
->getQuery()
->accessCheck(FALSE)
->count()
->execute();
$this
->assertEquals(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
->drupalGet('admin/structure/menu/manage/tools');
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->statusCodeEquals(200);
$this
->editForumVocabulary();
$this->forumContainer = $this
->createForum('container');
$this
->drupalGet('admin/structure/forum');
$this
->assertSession()
->pageTextContains('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
->assertSession()
->pageTextContains('Edit container');
$this->forum = $this
->createForum('forum', $this->forumContainer['tid']);
$this
->drupalGet('admin/structure/forum');
$this
->clickLink('edit forum');
$this
->assertSession()
->pageTextContains('Edit forum');
$this
->drupalGet('admin/structure/forum');
$delete_forum = $this
->createForum('forum', $this->forumContainer['tid']);
$this
->drupalGet('admin/structure/forum/');
$this
->submitForm([], 'Save');
$this
->assertSession()
->pageTextContains('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
->assertSession()
->fieldNotExists('parent[]');
$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('Delete');
$this
->drupalGet('admin/structure/taxonomy/manage/tags/add');
$this
->assertSession()
->fieldExists('parent[]');
$this
->assertSession()
->elementExists('xpath', "//details[@id='edit-relations']");
}
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
->drupalGet('admin/structure/taxonomy/manage/' . $original_vocabulary
->id());
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains("Updated vocabulary {$edit['name']}.");
$current_vocabulary = Vocabulary::load($vid);
$this
->assertEquals($edit['name'], $current_vocabulary
->label(), 'The name was updated');
$this
->assertEquals($edit['description'], $current_vocabulary
->getDescription(), '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
->assertEquals($original_vocabulary
->label(), $current_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
->drupalGet('admin/structure/forum/add/' . $type);
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->statusCodeEquals(200);
$type = $type == 'container' ? 'forum container' : 'forum';
$this
->assertSession()
->pageTextContains('Created new ' . $type . ' ' . $name . '.');
$this
->assertSession()
->elementExists('xpath', '//div[@data-drupal-messages]//a[contains(@href, "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
->assertNotEmpty($term, "The forum type '{$type}' should exist 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
->assertSame($parent, $parent_tid, 'The ' . $type . ' is linked to its container');
$forum = $taxonomy_term_storage
->load($tid);
$this
->assertEquals($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('Delete');
$this
->assertSession()
->pageTextContains('Are you sure you want to delete the forum');
$this
->assertSession()
->pageTextNotContains('Add forum');
$this
->assertSession()
->pageTextNotContains('Add forum container');
$this
->submitForm([], '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
->drupalGet('node/' . $node
->id());
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->statusCodeEquals(200);
$this
->clickLink('Add new comment');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->fieldExists('comment_body[0][value]');
$this
->drupalLogin($this->adminUser);
$this
->drupalGet("forum/{$this->forum['tid']}");
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->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
->drupalGet('node/add/forum', [
'query' => [
'forum_id' => $tid,
],
]);
$this
->submitForm($edit, 'Save');
if ($container) {
$this
->assertSession()
->pageTextNotContains("Forum topic {$title} has been created.");
$this
->assertSession()
->pageTextContains("The item {$forum['name']} is a forum container, not a forum.");
return;
}
else {
$this
->assertSession()
->pageTextContains("Forum topic {$title} has been created.");
$this
->assertSession()
->pageTextNotContains("The item {$forum['name']} is a forum container, not a forum.");
$this
->assertSession()
->elementExists('xpath', '//div[@data-drupal-messages]//a[contains(@href, "node/")]');
}
$node = $this
->drupalGetNodeByTitle($title);
$this
->assertNotNull($node, "Node {$title} was loaded");
$this
->assertEquals($tid, $node->taxonomy_forums->target_id, 'Saved forum topic was in the expected forum');
$this
->drupalGet('node/' . $node
->id());
$this
->assertSession()
->pageTextContains($title);
$this
->assertSession()
->pageTextContains($body);
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
->assertSession()
->titleEquals('Forum | Drupal');
$this
->assertSession()
->pageTextContains('Forum');
}
$this
->verifyForumView($this->forumContainer);
$this
->verifyForumView($this->forum, $this->forumContainer);
$this
->verifyForumView($this->rootForum);
$this
->drupalGet('node/' . $node
->id());
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->titleEquals($node
->label() . ' | Drupal');
$breadcrumb_build = [
Link::createFromRoute('Home', '<front>'),
Link::createFromRoute('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
->assertSession()
->responseContains(\Drupal::service('renderer')
->renderRoot($breadcrumb));
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->assertSession()
->statusCodeEquals($response);
if ($response == 200) {
$this
->assertSession()
->titleEquals('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
->drupalGet('node/' . $node
->id() . '/edit');
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains('Forum topic ' . $edit['title[0][value]'] . ' has been updated.');
$forum_tid = $this->container
->get('database')
->select('forum', 'f')
->fields('f', [
'tid',
])
->condition('nid', $node
->id())
->condition('vid', $node
->getRevisionId())
->execute()
->fetchField();
$this
->assertSame($this->rootForum['tid'], $forum_tid, 'The forum topic is linked to a different forum');
$this
->drupalGet('node/' . $node
->id() . '/delete');
$this
->submitForm([], 'Delete');
$this
->assertSession()
->statusCodeEquals($response);
$this
->assertSession()
->pageTextContains("Forum topic {$edit['title[0][value]']} has been deleted.");
}
}
private function verifyForumView($forum, $parent = NULL) {
$this
->drupalGet('forum/' . $forum['tid']);
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->titleEquals($forum['name'] . ' | Drupal');
$breadcrumb_build = [
Link::createFromRoute('Home', '<front>'),
Link::createFromRoute('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
->assertSession()
->responseContains(\Drupal::service('renderer')
->renderRoot($breadcrumb));
}
private function generateForumTopics() {
$this->nids = [];
for ($i = 0; $i < 5; $i++) {
$node = $this
->createForumTopic($this->forum, FALSE);
$this->nids[] = $node
->id();
}
}
public function testForumTopicButton() {
$this
->drupalLogin($this->adminUser);
$forum_container = $this
->createForum('container');
$this
->drupalGet('forum/' . $forum_container['tid']);
$this
->assertSession()
->linkNotExists('Add new Forum topic');
$forum = $this
->createForum('forum');
$this
->drupalGet('forum/' . $forum['tid']);
$this
->assertSession()
->linkExists('Add new Forum topic');
}
}