View source
<?php
namespace Drupal\Tests\workbench_access\Kernel;
use Drupal;
use Drupal\KernelTests\KernelTestBase;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\Tests\user\Traits\UserCreationTrait;
use Drupal\Tests\workbench_access\Traits\WorkbenchAccessTestTrait;
use Drupal\workbench_access\Entity\AccessScheme;
use Drupal\workbench_access\WorkbenchAccessManagerInterface;
class TaxonomyAccessTest extends KernelTestBase {
use WorkbenchAccessTestTrait;
use UserCreationTrait;
protected $vocabulary;
protected $accessControlledVocabulary;
protected $nonAccessControlledVocabulary;
protected $scheme;
protected $userStorage;
protected static $modules = [
'text',
'system',
'user',
'workbench_access',
'field',
'filter',
'taxonomy',
'options',
];
protected $accessHandler;
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('taxonomy_term');
$this->accessControlledVocabulary = Vocabulary::create([
'vid' => 'tags',
'name' => 'Tags',
]);
$this->accessControlledVocabulary
->save();
$this->nonAccessControlledVocabulary = Vocabulary::create([
'vid' => 'categories',
'name' => 'Categories',
]);
$this->nonAccessControlledVocabulary
->save();
$this
->installConfig([
'filter',
'workbench_access',
]);
$this->scheme = AccessScheme::create([
'id' => 'editorial_section',
'label' => 'Editorial section',
'plural_label' => 'Editorial sections',
'scheme' => 'taxonomy',
'scheme_settings' => [
'vocabularies' => [
'workbench_access',
],
'fields' => [
[
'entity_type' => 'taxonomy_term',
'bundle' => 'tags',
'field' => 'field_workbench_access',
],
],
],
]);
$this->scheme
->save();
$this
->installEntitySchema('user');
$this
->installEntitySchema('section_association');
$this
->installSchema('system', [
'key_value',
'sequences',
]);
$this->vocabulary = $this
->setUpVocabulary();
$this->accessHandler = $this->container
->get('entity_type.manager')
->getAccessControlHandler('taxonomy_term');
$this
->setUpTaxonomyFieldForEntityType('taxonomy_term', 'tags', $this->vocabulary
->id());
$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();
$version = Drupal::VERSION;
$permissions = $this
->getPermissions();
$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('tags', $allowed_editor));
$this
->assertFalse($this->accessHandler
->createAccess('tags', $editor_with_no_access));
$this
->assertTrue($this->accessHandler
->createAccess('tags', $editor_with_bypass_access));
}
public function testEditAccess() {
$this
->createUser();
$term = Term::create([
'vid' => $this->vocabulary
->id(),
'name' => 'Some section',
]);
$term
->save();
$permissions = $this
->getPermissions();
$allowed_editor = $this
->createUser($permissions);
$allowed_editor
->save();
$this->userStorage
->addUser($this->scheme, $allowed_editor, [
$term
->id(),
]);
$editor_with_no_access = $this
->createUser($permissions);
$entity = Term::create([
'vid' => 'categories',
'name' => 'come on in',
]);
$this
->assertTrue($this->accessHandler
->access($entity, 'update', $allowed_editor));
$this
->assertTrue($this->accessHandler
->access($entity, 'update', $editor_with_no_access));
$entity1 = Term::create([
'vid' => 'tags',
'name' => 'come on in',
]);
$this
->assertTrue($this->accessHandler
->access($entity1, 'update', $allowed_editor));
$this
->assertTrue($this->accessHandler
->access($entity1, 'update', $editor_with_no_access));
$entity2 = Term::create([
'vid' => 'tags',
'name' => 'restricted',
WorkbenchAccessManagerInterface::FIELD_NAME => $term
->id(),
]);
$this
->assertTrue($this->accessHandler
->access($entity2, 'update', $allowed_editor));
$this
->assertFalse($this->accessHandler
->access($entity2, 'update', $editor_with_no_access));
$this
->config('workbench_access.settings')
->set('deny_on_empty', 1)
->save();
$entity3 = Term::create([
'vid' => 'tags',
'name' => 'restricted',
]);
$this
->assertFalse($this->accessHandler
->access($entity3, 'update', $allowed_editor));
$this
->assertFalse($this->accessHandler
->access($entity3, 'update', $editor_with_no_access));
$this->scheme
->delete();
$this->accessHandler
->resetCache();
$this
->assertTrue($this->accessHandler
->access($entity2, 'update', $editor_with_no_access));
}
private function getPermissions() {
if (substr_count(Drupal::VERSION, '8.4') > 0) {
$permissions = [
'administer taxonomy',
'edit terms in tags',
'delete terms in tags',
'edit terms in categories',
'delete terms in categories',
];
}
else {
$permissions = [
'create terms in tags',
'edit terms in tags',
'delete terms in tags',
'create terms in categories',
'edit terms in categories',
'delete terms in categories',
];
}
return $permissions;
}
}