View source
<?php
namespace Drupal\Tests\workbench_access\Traits;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Url;
use Drupal\field\Entity\FieldConfig;
use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
use Drupal\node\Entity\NodeType;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\workbench_access\Entity\AccessScheme;
use Drupal\workbench_access\WorkbenchAccessManagerInterface;
trait WorkbenchAccessTestTrait {
use EntityReferenceTestTrait;
public function setUpContentType() {
$node_type = $this
->createContentType([
'type' => 'page',
]);
return $node_type;
}
public function setUpVocabulary() {
$vocab = Vocabulary::create([
'vid' => 'workbench_access',
'name' => 'Test Vocabulary',
]);
$vocab
->save();
return $vocab;
}
protected function setUpTaxonomyFieldForEntityType($entity_type_id, $bundle, $vocabulary_id, $field_name = WorkbenchAccessManagerInterface::FIELD_NAME, $title = 'Section', $cardinality = 1, $field_type = 'options_select') {
$handler_id = 'workbench_access:taxonomy_term:editorial_section';
if (!AccessScheme::load('editorial_section')) {
$handler_id = 'default:taxonomy_term';
}
$field = $this
->createEntityReferenceField($entity_type_id, $bundle, $field_name, $title, 'taxonomy_term', $handler_id, [
'target_bundles' => [
$vocabulary_id => $vocabulary_id,
],
], $cardinality);
if (!($form_display = EntityFormDisplay::load("{$entity_type_id}.{$bundle}.default"))) {
$form_display = EntityFormDisplay::create([
'targetEntityType' => $entity_type_id,
'bundle' => $bundle,
'mode' => 'default',
'status' => TRUE,
]);
}
$form_display
->setComponent($field_name, [
'type' => $field_type,
]);
$form_display
->save();
return FieldConfig::loadByName($entity_type_id, $bundle, $field_name);
}
public function setUpEditorUser() {
$editor_rid = $this
->createRole([
'access administration pages',
'create page content',
'edit any page content',
'administer menu',
'delete any page content',
'use workbench access',
], 'editor');
return $this
->createUserWithRole($editor_rid);
}
protected function setUpAdminUser(array $additional_permissions = []) {
$admin_rid = $this
->createRole(array_merge($additional_permissions, [
'access administration pages',
'assign workbench access',
]), 'admin');
return $this
->createUserWithRole($admin_rid);
}
protected function createUserWithRole($rid) {
$user = $this
->createUser();
$user
->addRole($rid);
$user
->save();
return $user;
}
public function setUpTaxonomyScheme(NodeType $node_type, Vocabulary $vocab, $id = 'editorial_section') {
$scheme = AccessScheme::create([
'id' => $id,
'label' => ucfirst(str_replace('_', ' ', $id)),
'plural_label' => ucfirst(str_replace('_', ' ', $id)) . 's',
'scheme' => 'taxonomy',
'scheme_settings' => [
'vocabularies' => [
$vocab
->id(),
],
'fields' => [
[
'entity_type' => 'node',
'field' => WorkbenchAccessManagerInterface::FIELD_NAME,
'bundle' => $node_type
->id(),
],
],
],
]);
$scheme
->save();
return $scheme;
}
public function setUpMenuScheme(array $node_type_ids, array $menu_ids, $id = 'editorial_section') {
$scheme = AccessScheme::create([
'id' => $id,
'label' => ucfirst(str_replace('_', ' ', $id)),
'plural_label' => ucfirst(str_replace('_', ' ', $id)) . 's',
'scheme' => 'menu',
'scheme_settings' => [
'menus' => $menu_ids,
'bundles' => $node_type_ids,
],
]);
$scheme
->save();
return $scheme;
}
protected function assertThatUnprivilegedUsersCannotAccessAdminPages() {
$this
->drupalGet(Url::fromRoute('entity.access_scheme.collection'));
$assert = $this
->assertSession();
$assert
->statusCodeEquals(403);
}
protected function assertCreatingAnAccessSchemeAsAdmin($scheme_id = 'taxonomy') {
$this
->drupalLogin($this->admin);
$this
->drupalGet(Url::fromRoute('entity.access_scheme.collection'));
$assert = $this
->assertSession();
$assert
->statusCodeEquals(200);
$this
->clickLink('Add Access scheme');
$this
->submitForm([
'label' => 'Section',
'plural_label' => 'Sections',
'id' => 'editorial_section',
'scheme' => $scheme_id,
], 'Save');
$scheme = $this
->loadUnchangedScheme('editorial_section');
$this
->assertEquals('Section', $scheme
->label());
$this
->assertEquals('Sections', $scheme
->getPluralLabel());
$this
->assertEquals($scheme
->toUrl('edit-form')
->setAbsolute()
->toString(), $this
->getSession()
->getCurrentUrl());
return $scheme;
}
protected function loadUnchangedScheme($scheme_id) {
return $this->container
->get('entity_type.manager')
->getStorage('access_scheme')
->loadUnchanged($scheme_id);
}
protected function setFieldType($entity_type_id, $bundle, $type = 'options_select', $field_name = WorkbenchAccessManagerInterface::FIELD_NAME) {
$form_display = EntityFormDisplay::load("{$entity_type_id}.{$bundle}.default");
$form_display
->setComponent($field_name, [
'type' => $type,
]);
$form_display
->save();
}
public function isDrupal8() {
$version = (int) substr(\Drupal::VERSION, 0, 1);
if ($version < 9) {
return TRUE;
}
return FALSE;
}
}