View source
<?php
namespace Drupal\node\Tests;
use Drupal\node\Entity\NodeType;
class NodeAccessBaseTableTest extends NodeTestBase {
public static $modules = array(
'node_access_test',
'views',
);
protected $profile = 'standard';
protected $nodesByUser;
protected $publicTid;
protected $privateTid;
protected $webUser;
protected $nidsVisible;
protected function setUp() {
parent::setUp();
node_access_test_add_field(NodeType::load('article'));
node_access_rebuild();
\Drupal::state()
->set('node_access_test.private', TRUE);
}
function testNodeAccessBasic() {
$num_simple_users = 2;
$simple_users = array();
$this->nodesByUser = array();
$titles = [];
$private_nodes = [];
for ($i = 0; $i < $num_simple_users; $i++) {
$simple_users[$i] = $this
->drupalCreateUser(array(
'access content',
'create article content',
));
}
foreach ($simple_users as $this->webUser) {
$this
->drupalLogin($this->webUser);
foreach (array(
0 => 'Public',
1 => 'Private',
) as $is_private => $type) {
$edit = array(
'title[0][value]' => t('@private_public Article created by @user', array(
'@private_public' => $type,
'@user' => $this->webUser
->getUsername(),
)),
);
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
->drupalPostForm('node/add/article', $edit, t('Save'));
$node = $this
->drupalGetNodeByTitle($edit['title[0][value]']);
$this
->assertEqual($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;
}
}
$this->publicTid = db_query('SELECT tid FROM {taxonomy_term_field_data} WHERE name = :name AND default_langcode = 1', array(
':name' => 'public',
))
->fetchField();
$this->privateTid = db_query('SELECT tid FROM {taxonomy_term_field_data} WHERE name = :name AND default_langcode = 1', array(
':name' => 'private',
))
->fetchField();
$this
->assertTrue($this->publicTid, 'Public tid was found');
$this
->assertTrue($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
->assertResponse($should_be_visible ? 200 : 403, strtr('A %private node by user %uid is %visible for user %current_uid.', array(
'%private' => $is_private ? 'private' : 'public',
'%uid' => $uid,
'%visible' => $should_be_visible ? 'visible' : 'not visible',
'%current_uid' => $this->webUser
->id(),
)));
}
}
$this
->assertTaxonomyPage(FALSE);
}
$access_user = $this
->drupalCreateUser(array(
'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
->assertResponse(200);
}
}
$this
->assertTaxonomyPage(TRUE);
}
protected function assertTaxonomyPage($is_admin) {
foreach (array(
$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
->assertTrue(preg_match('|node/(\\d+)$|', (string) $link['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
->assertIdentical(isset($this->nidsVisible[$nid]), $should_be_visible, strtr('A %private node by user %uid is %visible for user %current_uid on the %tid_is_private page.', array(
'%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',
)));
}
}
}
}
}