View source
<?php
namespace Drupal\node\Tests\Views;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
class BulkFormAccessTest extends NodeTestBase {
public static $modules = array(
'node_test_views',
'node_access_test',
);
public static $testViews = array(
'test_node_bulk_form',
);
protected $accessHandler;
protected function setUp() {
parent::setUp();
$this
->drupalCreateContentType(array(
'type' => 'article',
'name' => 'Article',
));
$this->accessHandler = \Drupal::entityManager()
->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(array(
'type' => 'article',
'private' => array(
array(
'value' => TRUE,
),
),
'uid' => $author
->id(),
));
$account = $this
->drupalCreateUser(array(
'node test view',
));
$this
->drupalLogin($account);
$this
->assertTrue($node
->isPublished(), 'Node is initially published.');
$this
->assertEqual(FALSE, $this->accessHandler
->access($node, 'update', $account), 'The node may not be edited.');
$edit = array(
'node_bulk_form[0]' => TRUE,
'action' => 'node_unpublish_action',
);
$this
->drupalPostForm('test-node-bulk-form', $edit, t('Apply'));
$this
->assertRaw(SafeMarkup::format('No access to execute %action on the @entity_type_label %entity_label.', [
'%action' => 'Unpublish content',
'@entity_type_label' => 'Content',
'%entity_label' => $node
->label(),
]));
$node = Node::load($node
->id());
$this
->assertTrue($node
->isPublished(), 'The node is still published.');
$account = $this
->drupalCreateUser(array(
'administer nodes',
'node test view',
));
$this
->drupalLogin($account);
$this
->assertTrue($node
->isPublished(), 'Node is initially published.');
$this
->assertEqual(FALSE, $node
->access('update', $account), 'The node may not be edited.');
$this
->assertEqual(TRUE, $node->status
->access('edit', $account), 'The node status can be edited.');
$edit = array(
'node_bulk_form[0]' => TRUE,
'action' => 'node_unpublish_action',
);
$this
->drupalPostForm('test-node-bulk-form', $edit, t('Apply'));
$node = Node::load($node
->id());
$this
->assertTrue($node
->isPublished(), 'The node is still published.');
}
public function testNodeDeleteAccess() {
$author = $this
->drupalCreateUser();
$private_node = $this
->drupalCreateNode(array(
'type' => 'article',
'private' => array(
array(
'value' => TRUE,
),
),
'uid' => $author
->id(),
));
$account = $this
->drupalCreateUser(array(
'access content',
'administer nodes',
'delete own article content',
'node test view',
));
$own_node = $this
->drupalCreateNode(array(
'type' => 'article',
'private' => array(
array(
'value' => TRUE,
),
),
'uid' => $account
->id(),
));
$this
->drupalLogin($account);
$this
->assertEqual(FALSE, $this->accessHandler
->access($private_node, 'delete', $account), 'The private node may not be deleted.');
$this
->assertEqual(TRUE, $this->accessHandler
->access($own_node, 'delete', $account), 'The own node may be deleted.');
$edit = array(
'node_bulk_form[0]' => TRUE,
'node_bulk_form[1]' => TRUE,
'action' => 'node_delete_action',
);
$this
->drupalPostForm('test-node-bulk-form', $edit, t('Apply'));
$this
->drupalPostForm(NULL, array(), t('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.');
}
}