Version 1
[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             'value' => $type . ' node',
59             'format' => filter_default_format(),
60           ]],
61           'title' => t('@private_public Article created by @user', ['@private_public' => $type, '@user' => $web_user->getUsername()]),
62           'type' => 'article',
63           'uid' => $web_user->id(),
64           'private' => (bool) $is_private,
65         ];
66
67         $node = $this->drupalCreateNode($settings);
68         $this->assertEqual($is_private, (int) $node->private->value, 'The private status of the node was properly set in the node_access_test table.');
69       }
70     }
71   }
72
73   /**
74    * Tests the node access filter.
75    */
76   public function testFilterNodeAccess() {
77     $this->drupalLogin($this->users[0]);
78     $this->drupalGet('test_filter_node_access');
79     // Test that the private node of the current user is shown.
80     $this->assertText('Private Article created by ' . $this->users[0]->getUsername());
81     // Test that the private node of the other use isn't shown.
82     $this->assertNoText('Private Article created by ' . $this->users[1]->getUsername());
83     // Test that both public nodes are shown.
84     $this->assertText('Public Article created by ' . $this->users[0]->getUsername());
85     $this->assertText('Public Article created by ' . $this->users[1]->getUsername());
86
87     // Switch users and test the other private node is shown.
88     $this->drupalLogin($this->users[1]);
89     $this->drupalGet('test_filter_node_access');
90     // Test that the private node of the current user is shown.
91     $this->assertText('Private Article created by ' . $this->users[1]->getUsername());
92     // Test that the private node of the other use isn't shown.
93     $this->assertNoText('Private Article created by ' . $this->users[0]->getUsername());
94
95     // Test that a user with administer nodes permission can't see all nodes.
96     $administer_nodes_user = $this->drupalCreateUser(['access content', 'administer nodes']);
97     $this->drupalLogin($administer_nodes_user);
98     $this->drupalGet('test_filter_node_access');
99     $this->assertNoText('Private Article created by ' . $this->users[0]->getUsername());
100     $this->assertNoText('Private Article created by ' . $this->users[1]->getUsername());
101     $this->assertText('Public Article created by ' . $this->users[0]->getUsername());
102     $this->assertText('Public Article created by ' . $this->users[1]->getUsername());
103
104     // Test that a user with bypass node access can see all nodes.
105     $bypass_access_user = $this->drupalCreateUser(['access content', 'bypass node access']);
106     $this->drupalLogin($bypass_access_user);
107     $this->drupalGet('test_filter_node_access');
108     $this->assertText('Private Article created by ' . $this->users[0]->getUsername());
109     $this->assertText('Private Article created by ' . $this->users[1]->getUsername());
110     $this->assertText('Public Article created by ' . $this->users[0]->getUsername());
111     $this->assertText('Public Article created by ' . $this->users[1]->getUsername());
112   }
113
114 }