Version 1
[yaffs-website] / web / core / modules / forum / tests / src / Functional / ForumNodeAccessTest.php
1 <?php
2
3 namespace Drupal\Tests\forum\Functional;
4
5 use Drupal\Tests\BrowserTestBase;
6 use Drupal\node\Entity\NodeType;
7
8 /**
9  * Tests forum block view for private node access.
10  *
11  * @group forum
12  */
13 class ForumNodeAccessTest extends BrowserTestBase {
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = ['node', 'comment', 'forum', 'taxonomy', 'tracker', 'node_access_test', 'block'];
21
22   protected function setUp() {
23     parent::setUp();
24     node_access_rebuild();
25     node_access_test_add_field(NodeType::load('forum'));
26     \Drupal::state()->set('node_access_test.private', TRUE);
27   }
28
29   /**
30    * Creates some users and creates a public node and a private node.
31    *
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.
34    */
35   public function testForumNodeAccess() {
36     // Create some users.
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']);
40
41     $this->drupalLogin($admin_user);
42
43     // Create a private node.
44     $private_node_title = $this->randomMachineName(20);
45     $edit = [
46       'title[0][value]' => $private_node_title,
47       'body[0][value]' => $this->randomMachineName(200),
48       'private[0][value]' => TRUE,
49     ];
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.');
53
54     // Create a public node.
55     $public_node_title = $this->randomMachineName(20);
56     $edit = [
57       'title[0][value]' => $public_node_title,
58       'body[0][value]' => $this->randomMachineName(200),
59     ];
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.');
63
64
65     // Enable the new and active forum blocks.
66     $this->drupalPlaceBlock('forum_active_block');
67     $this->drupalPlaceBlock('forum_new_block');
68
69     // Test for $access_user.
70     $this->drupalLogin($access_user);
71     $this->drupalGet('');
72
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');
76
77     // Test for $no_access_user.
78     $this->drupalLogin($no_access_user);
79     $this->drupalGet('');
80
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');
84   }
85
86 }