View source
<?php
namespace Drupal\Tests\node\Functional;
use Drupal\node\Entity\NodeType;
class NodeAccessBaseTableTest extends NodeTestBase {
protected static $modules = [
'node_access_test',
'views',
];
protected $defaultTheme = 'stark';
protected $profile = 'standard';
protected $nodesByUser;
protected $publicTid;
protected $privateTid;
protected $webUser;
protected $nidsVisible;
protected function setUp() : void {
parent::setUp();
node_access_test_add_field(NodeType::load('article'));
node_access_rebuild();
\Drupal::state()
->set('node_access_test.private', TRUE);
}
public function testNodeAccessBasic() {
$num_simple_users = 2;
$simple_users = [];
$this->nodesByUser = [];
$titles = [];
$private_nodes = [];
for ($i = 0; $i < $num_simple_users; $i++) {
$simple_users[$i] = $this
->drupalCreateUser([
'access content',
'create article content',
]);
}
foreach ($simple_users as $this->webUser) {
$this
->drupalLogin($this->webUser);
foreach ([
0 => 'Public',
1 => 'Private',
] as $is_private => $type) {
$edit = [
'title[0][value]' => t('@private_public Article created by @user', [
'@private_public' => $type,
'@user' => $this->webUser
->getAccountName(),
]),
];
if ($is_private) {
$edit['private[0][value]'] = TRUE;
$edit['body[0][value]'] = 'private node';
$edit['field_tags[target_id]'] = 'private';
}
else {
$edit['body[0][value]'] = 'public node';
$edit['field_tags[target_id]'] = 'public';
}
$this
->drupalGet('node/add/article');
$this
->submitForm($edit, 'Save');
$node = $this
->drupalGetNodeByTitle($edit['title[0][value]']);
$this
->assertEquals($is_private, (int) $node->private->value, 'The private status of the node was properly set in the node_access_test table.');
if ($is_private) {
$private_nodes[] = $node
->id();
}
$titles[$node
->id()] = $edit['title[0][value]'];
$this->nodesByUser[$this->webUser
->id()][$node
->id()] = $is_private;
}
}
$public_tids = \Drupal::entityQuery('taxonomy_term')
->accessCheck(FALSE)
->condition('name', 'public')
->condition('default_langcode', 1)
->execute();
$this->publicTid = reset($public_tids);
$private_tids = \Drupal::entityQuery('taxonomy_term')
->accessCheck(FALSE)
->condition('name', 'private')
->condition('default_langcode', 1)
->execute();
$this->privateTid = reset($private_tids);
$this
->assertNotEmpty($this->publicTid, 'Public tid was found');
$this
->assertNotEmpty($this->privateTid, 'Private tid was found');
foreach ($simple_users as $this->webUser) {
$this
->drupalLogin($this->webUser);
foreach ($this->nodesByUser as $uid => $data) {
foreach ($data as $nid => $is_private) {
$this
->drupalGet('node/' . $nid);
if ($is_private) {
$should_be_visible = $uid == $this->webUser
->id();
}
else {
$should_be_visible = TRUE;
}
$this
->assertSession()
->statusCodeEquals($should_be_visible ? 200 : 403);
}
}
$this
->assertTaxonomyPage(FALSE);
}
$access_user = $this
->drupalCreateUser([
'access content',
'create article content',
'node test view',
'search content',
]);
$this
->drupalLogin($access_user);
foreach ($this->nodesByUser as $private_status) {
foreach ($private_status as $nid => $is_private) {
$this
->drupalGet('node/' . $nid);
$this
->assertSession()
->statusCodeEquals(200);
}
}
$this
->assertTaxonomyPage(TRUE);
node_access_rebuild();
foreach ($this->nodesByUser as $private_status) {
foreach ($private_status as $nid => $is_private) {
$this
->drupalGet('node/' . $nid);
$this
->assertSession()
->statusCodeEquals(200);
}
}
$this
->assertTaxonomyPage(TRUE);
}
protected function assertTaxonomyPage($is_admin) {
foreach ([
$this->publicTid,
$this->privateTid,
] as $tid_is_private => $tid) {
$this
->drupalGet("taxonomy/term/{$tid}");
$this->nidsVisible = [];
foreach ($this
->xpath("//a[text()='Read more']") as $link) {
$this
->assertEquals(1, preg_match('|node/(\\d+)$|', $link
->getAttribute('href'), $matches), 'Read more points to a node');
$this->nidsVisible[$matches[1]] = TRUE;
}
foreach ($this->nodesByUser as $uid => $data) {
foreach ($data as $nid => $is_private) {
$should_be_visible = $tid_is_private == $is_private;
if (!$is_admin && $tid_is_private) {
$should_be_visible = $should_be_visible && $uid == $this->webUser
->id();
}
$this
->assertSame($should_be_visible, isset($this->nidsVisible[$nid]), strtr('A %private node by user %uid is %visible for user %current_uid on the %tid_is_private page.', [
'%private' => $is_private ? 'private' : 'public',
'%uid' => $uid,
'%visible' => isset($this->nidsVisible[$nid]) ? 'visible' : 'not visible',
'%current_uid' => $this->webUser
->id(),
'%tid_is_private' => $tid_is_private ? 'private' : 'public',
]));
}
}
}
}
}