2b320ae4b6dfde17118310d8128ee9b3d45b7a71
[yaffs-website] / web / core / modules / tracker / tests / src / Functional / TrackerNodeAccessTest.php
1 <?php
2
3 namespace Drupal\Tests\tracker\Functional;
4
5 use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
6 use Drupal\comment\Tests\CommentTestTrait;
7 use Drupal\node\Entity\NodeType;
8 use Drupal\Tests\BrowserTestBase;
9
10 /**
11  * Tests for private node access on /tracker.
12  *
13  * @group tracker
14  */
15 class TrackerNodeAccessTest extends BrowserTestBase {
16
17   use CommentTestTrait;
18
19   /**
20    * Modules to enable.
21    *
22    * @var array
23    */
24   public static $modules = ['node', 'comment', 'tracker', 'node_access_test'];
25
26   protected function setUp() {
27     parent::setUp();
28     node_access_rebuild();
29     $this->drupalCreateContentType(['type' => 'page']);
30     node_access_test_add_field(NodeType::load('page'));
31     $this->addDefaultCommentField('node', 'page', 'comment', CommentItemInterface::OPEN);
32     \Drupal::state()->set('node_access_test.private', TRUE);
33   }
34
35   /**
36    * Ensure private node on /tracker is only visible to users with permission.
37    */
38   public function testTrackerNodeAccess() {
39     // Create user with node test view permission.
40     $access_user = $this->drupalCreateUser(['node test view', 'access user profiles']);
41
42     // Create user without node test view permission.
43     $no_access_user = $this->drupalCreateUser(['access user profiles']);
44
45     $this->drupalLogin($access_user);
46
47     // Create some nodes.
48     $private_node = $this->drupalCreateNode([
49       'title' => t('Private node test'),
50       'private' => TRUE,
51     ]);
52     $public_node = $this->drupalCreateNode([
53       'title' => t('Public node test'),
54       'private' => FALSE,
55     ]);
56
57     // User with access should see both nodes created.
58     $this->drupalGet('activity');
59     $this->assertText($private_node->getTitle(), 'Private node is visible to user with private access.');
60     $this->assertText($public_node->getTitle(), 'Public node is visible to user with private access.');
61     $this->drupalGet('user/' . $access_user->id() . '/activity');
62     $this->assertText($private_node->getTitle(), 'Private node is visible to user with private access.');
63     $this->assertText($public_node->getTitle(), 'Public node is visible to user with private access.');
64
65     // User without access should not see private node.
66     $this->drupalLogin($no_access_user);
67     $this->drupalGet('activity');
68     $this->assertNoText($private_node->getTitle(), 'Private node is not visible to user without private access.');
69     $this->assertText($public_node->getTitle(), 'Public node is visible to user without private access.');
70     $this->drupalGet('user/' . $access_user->id() . '/activity');
71     $this->assertNoText($private_node->getTitle(), 'Private node is not visible to user without private access.');
72     $this->assertText($public_node->getTitle(), 'Public node is visible to user without private access.');
73   }
74
75 }