View source
<?php
namespace Drupal\Tests\workbench_access\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\taxonomy\Entity\Term;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\Tests\user\Traits\UserCreationTrait;
use Drupal\Tests\workbench_access\Traits\WorkbenchAccessTestTrait;
use Drupal\workbench_access\WorkbenchAccessManagerInterface;
class NodeAccessTest extends KernelTestBase {
use WorkbenchAccessTestTrait;
use NodeCreationTrait;
use ContentTypeCreationTrait;
use UserCreationTrait;
protected $vocabulary;
protected static $modules = [
'node',
'text',
'system',
'user',
'workbench_access',
'field',
'filter',
'taxonomy',
'options',
];
protected $accessHandler;
protected $scheme;
protected $userStorage;
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('node');
$this
->installConfig([
'filter',
'node',
'workbench_access',
]);
$this
->installEntitySchema('user');
$this
->installEntitySchema('taxonomy_term');
$this
->installEntitySchema('section_association');
$this
->installSchema('system', [
'key_value',
'sequences',
]);
$node_type = $this
->createContentType([
'type' => 'page',
]);
$this
->createContentType([
'type' => 'article',
]);
$this->vocabulary = $this
->setUpVocabulary();
$this->accessHandler = $this->container
->get('entity_type.manager')
->getAccessControlHandler('node');
$this
->setUpTaxonomyFieldForEntityType('node', $node_type
->id(), $this->vocabulary
->id());
$this->scheme = $this
->setUpTaxonomyScheme($node_type, $this->vocabulary);
$this->userStorage = \Drupal::service('workbench_access.user_section_storage');
}
public function testCreateAccess() {
$this
->createUser();
$term = Term::create([
'vid' => $this->vocabulary
->id(),
'name' => 'Some section',
]);
$term
->save();
$permissions = [
'create page content',
'edit any page content',
'access content',
'delete any page content',
'administer nodes',
];
$allowed_editor = $this
->createUser($permissions);
$allowed_editor
->save();
$this->userStorage
->addUser($this->scheme, $allowed_editor, [
$term
->id(),
]);
$editor_with_no_access = $this
->createUser($permissions);
$permissions[] = 'bypass workbench access';
$editor_with_bypass_access = $this
->createUser($permissions);
$this
->assertTrue($this->accessHandler
->createAccess('page', $allowed_editor));
$this
->assertFalse($this->accessHandler
->createAccess('page', $editor_with_no_access));
$this
->assertTrue($this->accessHandler
->createAccess('page', $editor_with_bypass_access));
}
public function testEditAccess() {
$this
->createUser();
$term = Term::create([
'vid' => $this->vocabulary
->id(),
'name' => 'Some section',
]);
$term
->save();
$permissions = [
'create page content',
'edit any page content',
'access content',
'delete any page content',
];
$allowed_editor = $this
->createUser($permissions);
$allowed_editor
->save();
$this->userStorage
->addUser($this->scheme, $allowed_editor, [
$term
->id(),
]);
$editor_with_no_access = $this
->createUser($permissions);
$node1 = $this
->createNode([
'type' => 'page',
'title' => 'foo',
]);
$this
->assertTrue($this->accessHandler
->access($node1, 'update', $allowed_editor));
$this
->assertTrue($this->accessHandler
->access($node1, 'update', $editor_with_no_access));
$this
->assertTrue($this->accessHandler
->access($node1, 'delete', $allowed_editor));
$this
->assertTrue($this->accessHandler
->access($node1, 'delete', $editor_with_no_access));
$node2 = $this
->createNode([
'type' => 'page',
'title' => 'bar',
WorkbenchAccessManagerInterface::FIELD_NAME => $term
->id(),
]);
$this
->assertTrue($this->accessHandler
->access($node2, 'update', $allowed_editor));
$this
->assertFalse($this->accessHandler
->access($node2, 'update', $editor_with_no_access));
$this
->assertTrue($this->accessHandler
->access($node2, 'delete', $allowed_editor));
$this
->assertFalse($this->accessHandler
->access($node2, 'delete', $editor_with_no_access));
$this
->config('workbench_access.settings')
->set('deny_on_empty', 1)
->save();
$node3 = $this
->createNode([
'type' => 'page',
'title' => 'baz',
]);
$this
->assertFalse($this->accessHandler
->access($node3, 'update', $allowed_editor));
$this
->assertFalse($this->accessHandler
->access($node3, 'update', $editor_with_no_access));
$this
->assertFalse($this->accessHandler
->access($node3, 'delete', $allowed_editor));
$this
->assertFalse($this->accessHandler
->access($node3, 'delete', $editor_with_no_access));
}
}