NodeViewPermissionsTest.php in Node View Permissions 8
File
tests/src/Functional/NodeViewPermissionsTest.php
View source
<?php
namespace Drupal\Tests\node_view_permissions\Functional;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
use Symfony\Component\HttpFoundation\Response;
class NodeViewPermissionsTest extends BrowserTestBase {
public static $modules = [
'node_view_permissions',
];
protected $defaultTheme = 'stark';
public function setUp() {
parent::setUp();
$this
->drupalCreateContentType([
'type' => 'article',
]);
node_access_rebuild();
}
public function testViewOwn() {
$user1 = $this
->drupalCreateUser([
'view own article content',
]);
$user2 = $this
->drupalCreateUser([
'view own article content',
]);
$node = $this
->drupalCreateNode([
'type' => 'article',
'uid' => $user1
->id(),
]);
$lookup = [
[
$user1,
Response::HTTP_OK,
],
[
$user2,
Response::HTTP_FORBIDDEN,
],
];
foreach ($lookup as $i) {
list($user, $expected) = $i;
$this
->drupalLogin($user);
$this
->drupalGet(Url::fromRoute('entity.node.canonical', [
'node' => $node
->id(),
]));
$this
->assertSession()
->statusCodeEquals($expected);
}
}
public function testViewAny() {
$user1 = $this
->drupalCreateUser([
'view any article content',
]);
$user2 = $this
->drupalCreateUser([
'view any article content',
]);
$node = $this
->drupalCreateNode([
'type' => 'article',
'uid' => $user1
->id(),
]);
foreach ([
$user1,
$user2,
] as $user) {
$this
->drupalLogin($user);
$this
->drupalGet(Url::fromRoute('entity.node.canonical', [
'node' => $node
->id(),
]));
$this
->assertSession()
->statusCodeEquals(Response::HTTP_OK);
}
}
}