View source
<?php
namespace Drupal\Tests\node\Functional\Views;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
class BulkFormAccessTest extends NodeTestBase {
protected static $modules = [
'node_test_views',
'node_access_test',
];
protected $defaultTheme = 'stark';
public static $testViews = [
'test_node_bulk_form',
];
protected $accessHandler;
protected function setUp($import_test_views = TRUE, $modules = [
'node_test_views',
]) : void {
parent::setUp($import_test_views, $modules);
$this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
$this->accessHandler = \Drupal::entityTypeManager()
->getAccessControlHandler('node');
node_access_test_add_field(NodeType::load('article'));
node_access_rebuild();
\Drupal::state()
->set('node_access_test.private', TRUE);
}
public function testNodeEditAccess() {
$author = $this
->drupalCreateUser();
$node = $this
->drupalCreateNode([
'type' => 'article',
'private' => [
[
'value' => TRUE,
],
],
'uid' => $author
->id(),
]);
$account = $this
->drupalCreateUser([
'node test view',
]);
$this
->drupalLogin($account);
$this
->assertTrue($node
->isPublished(), 'Node is initially published.');
$this
->assertFalse($this->accessHandler
->access($node, 'update', $account), 'The node may not be edited.');
$edit = [
'node_bulk_form[0]' => TRUE,
'action' => 'node_unpublish_action',
];
$this
->drupalGet('test-node-bulk-form');
$this
->submitForm($edit, 'Apply to selected items');
$this
->assertSession()
->pageTextContains("No access to execute Unpublish content on the Content {$node->label()}.");
$node = Node::load($node
->id());
$this
->assertTrue($node
->isPublished(), 'The node is still published.');
$account = $this
->drupalCreateUser([
'administer nodes',
'node test view',
]);
$this
->drupalLogin($account);
$this
->assertTrue($node
->isPublished(), 'Node is initially published.');
$this
->assertFalse($node
->access('update', $account), 'The node may not be edited.');
$this
->assertTrue($node->status
->access('edit', $account), 'The node status can be edited.');
$edit = [
'node_bulk_form[0]' => TRUE,
'action' => 'node_unpublish_action',
];
$this
->drupalGet('test-node-bulk-form');
$this
->submitForm($edit, 'Apply to selected items');
$this
->assertSession()
->pageTextNotContains("Unpublish content was applied to 1 item.");
$node = Node::load($node
->id());
$this
->assertTrue($node
->isPublished(), 'The node is still published.');
$this
->assertNotEmpty($this
->cssSelect('#views-form-test-node-bulk-form-page-1'));
$edit = [
'node_bulk_form[0]' => TRUE,
'action' => 'node_delete_action',
];
$this
->drupalGet('test-node-bulk-form');
$this
->submitForm($edit, 'Apply to selected items');
$this
->assertSession()
->pageTextContains("No access to execute Delete content on the Content {$node->label()}.");
$this
->assertNotEmpty($this
->cssSelect('#views-form-test-node-bulk-form-page-1'));
}
public function testNodeDeleteAccess() {
$author = $this
->drupalCreateUser();
$private_node = $this
->drupalCreateNode([
'type' => 'article',
'private' => [
[
'value' => TRUE,
],
],
'uid' => $author
->id(),
]);
$account = $this
->drupalCreateUser([
'access content',
'administer nodes',
'delete own article content',
'node test view',
]);
$own_node = $this
->drupalCreateNode([
'type' => 'article',
'private' => [
[
'value' => TRUE,
],
],
'uid' => $account
->id(),
]);
$this
->drupalLogin($account);
$this
->assertFalse($this->accessHandler
->access($private_node, 'delete', $account), 'The private node may not be deleted.');
$this
->assertTrue($this->accessHandler
->access($own_node, 'delete', $account), 'The own node may be deleted.');
$edit = [
'node_bulk_form[0]' => TRUE,
'node_bulk_form[1]' => TRUE,
'action' => 'node_delete_action',
];
$this
->drupalGet('test-node-bulk-form');
$this
->submitForm($edit, 'Apply to selected items');
$this
->submitForm([], 'Delete');
$private_node = Node::load($private_node
->id());
$this
->assertNotNull($private_node, 'The private node has not been deleted.');
$own_node = Node::load($own_node
->id());
$this
->assertNull($own_node, 'The own node is deleted.');
}
}