View source
<?php
namespace Drupal\Tests\forum\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\node\Entity\NodeType;
class ForumNodeAccessTest extends BrowserTestBase {
public static $modules = [
'node',
'comment',
'forum',
'taxonomy',
'tracker',
'node_access_test',
'block',
];
protected $defaultTheme = 'stark';
protected function setUp() {
parent::setUp();
node_access_rebuild();
node_access_test_add_field(NodeType::load('forum'));
\Drupal::state()
->set('node_access_test.private', TRUE);
}
public function testForumNodeAccess() {
$access_user = $this
->drupalCreateUser([
'node test view',
]);
$no_access_user = $this
->drupalCreateUser();
$admin_user = $this
->drupalCreateUser([
'access administration pages',
'administer modules',
'administer blocks',
'create forum content',
]);
$this
->drupalLogin($admin_user);
$private_node_title = $this
->randomMachineName(20);
$edit = [
'title[0][value]' => $private_node_title,
'body[0][value]' => $this
->randomMachineName(200),
'private[0][value]' => TRUE,
];
$this
->drupalPostForm('node/add/forum', $edit, t('Save'), [
'query' => [
'forum_id' => 1,
],
]);
$private_node = $this
->drupalGetNodeByTitle($private_node_title);
$this
->assertTrue(!empty($private_node), 'New private forum node found in database.');
$public_node_title = $this
->randomMachineName(20);
$edit = [
'title[0][value]' => $public_node_title,
'body[0][value]' => $this
->randomMachineName(200),
];
$this
->drupalPostForm('node/add/forum', $edit, t('Save'), [
'query' => [
'forum_id' => 1,
],
]);
$public_node = $this
->drupalGetNodeByTitle($public_node_title);
$this
->assertTrue(!empty($public_node), 'New public forum node found in database.');
$this
->drupalPlaceBlock('forum_active_block');
$this
->drupalPlaceBlock('forum_new_block');
$this
->drupalLogin($access_user);
$this
->drupalGet('');
$this
->assertText($private_node
->getTitle(), 'Private node found in block by $access_user');
$this
->assertText($public_node
->getTitle(), 'Public node found in block by $access_user');
$this
->drupalLogin($no_access_user);
$this
->drupalGet('');
$this
->assertNoText($private_node
->getTitle(), 'Private node not found in block by $no_access_user');
$this
->assertText($public_node
->getTitle(), 'Public node found in block by $no_access_user');
}
}