3 namespace Drupal\Tests\forum\Functional;
5 use Drupal\Tests\BrowserTestBase;
6 use Drupal\node\Entity\NodeType;
9 * Tests forum block view for private node access.
13 class ForumNodeAccessTest extends BrowserTestBase {
20 public static $modules = ['node', 'comment', 'forum', 'taxonomy', 'tracker', 'node_access_test', 'block'];
22 protected function setUp() {
24 node_access_rebuild();
25 node_access_test_add_field(NodeType::load('forum'));
26 \Drupal::state()->set('node_access_test.private', TRUE);
30 * Creates some users and creates a public node and a private node.
32 * Adds both active forum topics and new forum topics blocks to the sidebar.
33 * Tests to ensure private node/public node access is respected on blocks.
35 public function testForumNodeAccess() {
37 $access_user = $this->drupalCreateUser(['node test view']);
38 $no_access_user = $this->drupalCreateUser();
39 $admin_user = $this->drupalCreateUser(['access administration pages', 'administer modules', 'administer blocks', 'create forum content']);
41 $this->drupalLogin($admin_user);
43 // Create a private node.
44 $private_node_title = $this->randomMachineName(20);
46 'title[0][value]' => $private_node_title,
47 'body[0][value]' => $this->randomMachineName(200),
48 'private[0][value]' => TRUE,
50 $this->drupalPostForm('node/add/forum', $edit, t('Save'), ['query' => ['forum_id' => 1]]);
51 $private_node = $this->drupalGetNodeByTitle($private_node_title);
52 $this->assertTrue(!empty($private_node), 'New private forum node found in database.');
54 // Create a public node.
55 $public_node_title = $this->randomMachineName(20);
57 'title[0][value]' => $public_node_title,
58 'body[0][value]' => $this->randomMachineName(200),
60 $this->drupalPostForm('node/add/forum', $edit, t('Save'), ['query' => ['forum_id' => 1]]);
61 $public_node = $this->drupalGetNodeByTitle($public_node_title);
62 $this->assertTrue(!empty($public_node), 'New public forum node found in database.');
65 // Enable the new and active forum blocks.
66 $this->drupalPlaceBlock('forum_active_block');
67 $this->drupalPlaceBlock('forum_new_block');
69 // Test for $access_user.
70 $this->drupalLogin($access_user);
73 // Ensure private node and public node are found.
74 $this->assertText($private_node->getTitle(), 'Private node found in block by $access_user');
75 $this->assertText($public_node->getTitle(), 'Public node found in block by $access_user');
77 // Test for $no_access_user.
78 $this->drupalLogin($no_access_user);
81 // Ensure private node is not found but public is found.
82 $this->assertNoText($private_node->getTitle(), 'Private node not found in block by $no_access_user');
83 $this->assertText($public_node->getTitle(), 'Public node found in block by $no_access_user');