Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / node / tests / src / Functional / Views / FilterNodeAccessTest.php
1 <?php
2
3 namespace Drupal\Tests\node\Functional\Views;
4
5 use Drupal\node\Entity\NodeType;
6
7 /**
8  * Tests the node_access filter handler.
9  *
10  * @group node
11  * @see \Drupal\node\Plugin\views\filter\Access
12  */
13 class FilterNodeAccessTest extends NodeTestBase {
14
15   /**
16    * An array of users.
17    *
18    * @var \Drupal\user\Entity\User[]
19    */
20   protected $users;
21
22   /**
23    * {@inheritdoc}
24    */
25   public static $modules = ['node_access_test'];
26
27   /**
28    * Views used by this test.
29    *
30    * @var array
31    */
32   public static $testViews = ['test_filter_node_access'];
33
34   /**
35    * {@inheritdoc}
36    */
37   protected function setUp($import_test_views = TRUE) {
38     parent::setUp($import_test_views);
39
40     $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
41
42     node_access_test_add_field(NodeType::load('article'));
43
44     node_access_rebuild();
45     \Drupal::state()->set('node_access_test.private', TRUE);
46
47     $num_simple_users = 2;
48     $this->users = [];
49
50     for ($i = 0; $i < $num_simple_users; $i++) {
51       $this->users[$i] = $this->drupalCreateUser(['access content', 'create article content']);
52     }
53     foreach ($this->users as $web_user) {
54       $this->drupalLogin($web_user);
55       foreach ([0 => 'Public', 1 => 'Private'] as $is_private => $type) {
56         $settings = [
57           'body' => [
58             [
59               'value' => $type . ' node',
60               'format' => filter_default_format(),
61             ],
62           ],
63           'title' => t('@private_public Article created by @user', ['@private_public' => $type, '@user' => $web_user->getUsername()]),
64           'type' => 'article',
65           'uid' => $web_user->id(),
66           'private' => (bool) $is_private,
67         ];
68
69         $node = $this->drupalCreateNode($settings);
70         $this->assertEqual($is_private, (int) $node->private->value, 'The private status of the node was properly set in the node_access_test table.');
71       }
72     }
73   }
74
75   /**
76    * Tests the node access filter.
77    */
78   public function testFilterNodeAccess() {
79     $this->drupalLogin($this->users[0]);
80     $this->drupalGet('test_filter_node_access');
81     // Test that the private node of the current user is shown.
82     $this->assertText('Private Article created by ' . $this->users[0]->getUsername());
83     // Test that the private node of the other use isn't shown.
84     $this->assertNoText('Private Article created by ' . $this->users[1]->getUsername());
85     // Test that both public nodes are shown.
86     $this->assertText('Public Article created by ' . $this->users[0]->getUsername());
87     $this->assertText('Public Article created by ' . $this->users[1]->getUsername());
88
89     // Switch users and test the other private node is shown.
90     $this->drupalLogin($this->users[1]);
91     $this->drupalGet('test_filter_node_access');
92     // Test that the private node of the current user is shown.
93     $this->assertText('Private Article created by ' . $this->users[1]->getUsername());
94     // Test that the private node of the other use isn't shown.
95     $this->assertNoText('Private Article created by ' . $this->users[0]->getUsername());
96
97     // Test that a user with administer nodes permission can't see all nodes.
98     $administer_nodes_user = $this->drupalCreateUser(['access content', 'administer nodes']);
99     $this->drupalLogin($administer_nodes_user);
100     $this->drupalGet('test_filter_node_access');
101     $this->assertNoText('Private Article created by ' . $this->users[0]->getUsername());
102     $this->assertNoText('Private Article created by ' . $this->users[1]->getUsername());
103     $this->assertText('Public Article created by ' . $this->users[0]->getUsername());
104     $this->assertText('Public Article created by ' . $this->users[1]->getUsername());
105
106     // Test that a user with bypass node access can see all nodes.
107     $bypass_access_user = $this->drupalCreateUser(['access content', 'bypass node access']);
108     $this->drupalLogin($bypass_access_user);
109     $this->drupalGet('test_filter_node_access');
110     $this->assertText('Private Article created by ' . $this->users[0]->getUsername());
111     $this->assertText('Private Article created by ' . $this->users[1]->getUsername());
112     $this->assertText('Public Article created by ' . $this->users[0]->getUsername());
113     $this->assertText('Public Article created by ' . $this->users[1]->getUsername());
114   }
115
116 }