View source
<?php
namespace Drupal\taxonomy_permissions\Tests;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\Display\EntityFormDisplayInterface;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\taxonomy\Functional\TaxonomyTestTrait;
use Drupal\Core\Session\AccountInterface;
class TaxonomyPermissionsTestBase extends BrowserTestBase {
use TaxonomyTestTrait;
public static $modules = [
'system',
'user',
'node',
'field',
'field_ui',
'taxonomy',
'taxonomy_permissions',
];
protected $formDisplay;
protected $viewDisplay;
protected $authorizedUser;
protected $basicUser;
protected $article1;
protected $article2;
protected $vocabulary;
protected $term1;
protected $term2;
public function setUp() {
parent::setUp();
$this->vocabulary = $this
->createVocabulary();
$this->term1 = $this
->createTerm($this->vocabulary);
$perms[] = 'view terms in ' . $this->vocabulary
->id();
user_role_revoke_permissions(AccountInterface::ANONYMOUS_ROLE, $perms);
user_role_revoke_permissions(AccountInterface::AUTHENTICATED_ROLE, $perms);
$this
->drupalCreateContentType([
'type' => 'article',
]);
$this->authorizedUser = $this
->drupalCreateUser([
'create article content',
'edit any article content',
'access content',
'view terms in ' . $this->vocabulary
->id(),
]);
$this->basicUser = $this
->drupalCreateUser([
'create article content',
'edit any article content',
'access content',
]);
$field_name = 'field_term';
$this
->attachFields($field_name);
$this->article1 = $this
->createSimpleArticle('Article 1', $field_name, $this->term1
->id());
}
public function testViewTerm() {
$this
->drupalLogin($this->authorizedUser);
$this
->drupalGet("node/{$this->article1->id()}");
$this
->assertResponse(200);
$this
->assertText($this->term1
->getName());
$this
->assertLink($this->term1
->getName());
$this
->drupalLogin($this->basicUser);
$this
->drupalGet("node/{$this->article1->id()}");
$this
->assertResponse(200);
$this
->assertNoText($this->term1
->getName());
$this
->assertNoLink($this->term1
->getName());
}
public function testAccessTermPage() {
$this
->drupalLogin($this->authorizedUser);
$this
->drupalGet("taxonomy/term/{$this->term1->id()}");
$this
->assertResponse(200);
$this
->drupalLogin($this->basicUser);
$this
->drupalGet("taxonomy/term/{$this->term1->id()}");
$this
->assertResponse(403);
}
public function testAccessFormTerm() {
$this
->drupalLogin($this->authorizedUser);
$this
->drupalGet("node/{$this->article1->id()}/edit");
$this
->assertResponse(200);
$this
->assertFieldByName('field_term[0][target_id]', $this->term1
->getName() . ' (' . $this->term1
->id() . ')', 'The expected value is found in the input field');
$this
->drupalLogin($this->basicUser);
$this
->drupalGet("node/{$this->article1->id()}/edit");
$this
->assertResponse(200);
$this
->assertNoFieldByName('field_term[0][target_id]', $this->term1
->getName() . ' (' . $this->term1
->id() . ')', 'The input field taxonomy reference is not accessible');
}
protected function createField($entity_type, $bundle, $field_name, $field_label, $target_entity_type, $selection_handler = 'default', array $selection_handler_settings = [], $cardinality = 1, $user_method = 'user', $priority = 0) {
if (!FieldStorageConfig::loadByName($entity_type, $field_name)) {
FieldStorageConfig::create([
'field_name' => $field_name,
'type' => 'entity_reference',
'entity_type' => $entity_type,
'cardinality' => $cardinality,
'settings' => [
'target_type' => $target_entity_type,
],
])
->save();
}
if (!FieldConfig::loadByName($entity_type, $bundle, $field_name)) {
FieldConfig::create([
'field_name' => $field_name,
'entity_type' => $entity_type,
'bundle' => $bundle,
'label' => $field_label,
'settings' => [
'handler' => $selection_handler,
'handler_settings' => $selection_handler_settings,
],
])
->save();
}
$field = FieldConfig::loadByName($entity_type, $bundle, $field_name);
return $field;
}
protected function setFormDisplay($form_display_id, $entity_type, $bundle, $field_name, $widget_id, array $settings, $mode = 'default') {
$this->formDisplay = EntityFormDisplay::load($form_display_id);
if (!$this->formDisplay) {
EntityFormDisplay::create([
'targetEntityType' => $entity_type,
'bundle' => $bundle,
'mode' => $mode,
'status' => TRUE,
])
->save();
$this->formDisplay = EntityFormDisplay::load($form_display_id);
}
if ($this->formDisplay instanceof EntityFormDisplayInterface) {
$this->formDisplay
->setComponent($field_name, [
'type' => $widget_id,
'settings' => $settings,
])
->save();
}
}
protected function setViewDisplay($form_display_id, $entity_type, $bundle, $field_name, $formatter_id, array $settings, $mode = 'default') {
$this->viewDisplay = EntityViewDisplay::load($form_display_id);
if (!$this->viewDisplay) {
EntityViewDisplay::create([
'targetEntityType' => $entity_type,
'bundle' => $bundle,
'mode' => $mode,
'status' => TRUE,
])
->save();
$this->viewDisplay = EntityViewDisplay::load($form_display_id);
}
if ($this->viewDisplay instanceof EntityViewDisplayInterface) {
$this->viewDisplay
->setComponent($field_name, [
'type' => $formatter_id,
'settings' => $settings,
])
->save();
}
}
protected function attachFields($field_name) {
$handler_settings = [
'target_bundles' => [
$this->vocabulary
->id() => $this->vocabulary
->id(),
],
'auto_create' => FALSE,
];
$this
->createField('node', 'article', $field_name, 'Term referenced', 'taxonomy_term', 'default', $handler_settings);
$settings = [
'match_operator' => 'CONTAINS',
'size' => 30,
'placeholder' => '',
];
$this
->setFormDisplay('node.article.default', 'node', 'article', $field_name, 'entity_reference_autocomplete', $settings);
$settings = [
'link' => TRUE,
];
$this
->setViewDisplay('node.article.default', 'node', 'article', $field_name, 'entity_reference_label', $settings);
}
protected function createSimpleArticle($title, $field_name = '', $target_id = NULL) {
$values = [
'type' => 'article',
'title' => $title,
'body' => [
'value' => 'Content body for ' . $title,
],
];
if ($field_name) {
$values[$field_name] = [
'target_id' => $target_id,
];
}
return $this
->drupalCreateNode($values);
}
}