3 namespace Drupal\Tests\node\Functional\Views;
5 use Drupal\node\Entity\NodeType;
8 * Tests the node_access filter handler.
11 * @see \Drupal\node\Plugin\views\filter\Access
13 class FilterNodeAccessTest extends NodeTestBase {
18 * @var \Drupal\user\Entity\User[]
25 public static $modules = ['node_access_test'];
28 * Views used by this test.
32 public static $testViews = ['test_filter_node_access'];
37 protected function setUp($import_test_views = TRUE) {
38 parent::setUp($import_test_views);
40 $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
42 node_access_test_add_field(NodeType::load('article'));
44 node_access_rebuild();
45 \Drupal::state()->set('node_access_test.private', TRUE);
47 $num_simple_users = 2;
50 for ($i = 0; $i < $num_simple_users; $i++) {
51 $this->users[$i] = $this->drupalCreateUser(['access content', 'create article content']);
53 foreach ($this->users as $web_user) {
54 $this->drupalLogin($web_user);
55 foreach ([0 => 'Public', 1 => 'Private'] as $is_private => $type) {
58 'value' => $type . ' node',
59 'format' => filter_default_format(),
61 'title' => t('@private_public Article created by @user', ['@private_public' => $type, '@user' => $web_user->getUsername()]),
63 'uid' => $web_user->id(),
64 'private' => (bool) $is_private,
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.');
74 * Tests the node access filter.
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());
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());
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());
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());