You are here

trait WorkbenchAccessTestTrait in Workbench Access 8

Contains helper classes for tests to set up various configuration.

Hierarchy

23 files declare their use of WorkbenchAccessTestTrait
AccessByRoleFormTest.php in tests/src/Functional/AccessByRoleFormTest.php
AccessByUserFormTest.php in tests/src/Functional/AccessByUserFormTest.php
AccessCacheTest.php in tests/src/Functional/AccessCacheTest.php
AddSchemeFormTest.php in tests/src/Functional/AddSchemeFormTest.php
AssignUserFormTest.php in tests/src/Functional/AssignUserFormTest.php

... See full list

File

tests/src/Traits/WorkbenchAccessTestTrait.php, line 17

Namespace

Drupal\Tests\workbench_access\Traits
View source
trait WorkbenchAccessTestTrait {
  use EntityReferenceTestTrait;

  /**
   * Set up a content type with workbench access enabled.
   *
   * @return \Drupal\node\Entity\NodeType
   *   The node type entity.
   */
  public function setUpContentType() {
    $node_type = $this
      ->createContentType([
      'type' => 'page',
    ]);
    return $node_type;
  }

  /**
   * Create a test vocabulary.
   *
   * @return \Drupal\taxonomy\Entity\Vocabulary
   *   The vocabulary entity.
   */
  public function setUpVocabulary() {
    $vocab = Vocabulary::create([
      'vid' => 'workbench_access',
      'name' => 'Test Vocabulary',
    ]);
    $vocab
      ->save();
    return $vocab;
  }

  /**
   * Sets up a taxonomy field on a given entity type and bundle.
   *
   * @param string $entity_type_id
   *   Entity type ID.
   * @param string $bundle
   *   Bundle ID.
   * @param string $vocabulary_id
   *   Vocabulary ID.
   * @param string $field_name
   *   Field machine name.
   * @param string $title
   *   Field display title.
   * @param int $cardinality
   *   Indicates the number of values to save. -1 is unlimited.
   * @param $field_type
   *   The type of field widget to enable: options_select|options_buttons
   *
   * @return field
   *   The created field entity.
   */
  protected function setUpTaxonomyFieldForEntityType($entity_type_id, $bundle, $vocabulary_id, $field_name = WorkbenchAccessManagerInterface::FIELD_NAME, $title = 'Section', $cardinality = 1, $field_type = 'options_select') {

    // Create an instance of the access field on the bundle.
    $handler_id = 'workbench_access:taxonomy_term:editorial_section';
    if (!AccessScheme::load('editorial_section')) {

      // The scheme doesn't exist yet so there is no plugin yet.
      $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);

    // Set the field to display as a dropdown on the form.
    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);
  }

  /**
   * Sets up a user with an editor role that has access to content.
   *
   * @return \Drupal\user\Entity\User
   *   The user entity.
   */
  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);
  }

  /**
   * Sets up a user with an editor role that has access to content.
   *
   * @param array $additional_permissions
   *   Array of additional permissions beyond 'access administration pages' and
   *   'assign workbench access'.
   *
   * @return \Drupal\user\Entity\User
   *   The user entity.
   */
  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);
  }

  /**
   * Sets up a user with a given role and saves it.
   *
   * @param string $rid
   *   The role id.
   *
   * @return \Drupal\user\Entity\User
   *   The user entity.
   */
  protected function createUserWithRole($rid) {
    $user = $this
      ->createUser();
    $user
      ->addRole($rid);
    $user
      ->save();
    return $user;
  }

  /**
   * Sets up a taxonomy scheme for a given node type.
   *
   * @param \Drupal\node\Entity\NodeType $node_type
   *   Node type to set it up for.
   * @param \Drupal\taxonomy\Entity\Vocabulary $vocab
   *   The vocab to use for the scheme.
   * @param string $id
   *   Scheme ID.
   *
   * @return \Drupal\workbench_access\Entity\AccessSchemeInterface
   *   Created scheme.
   */
  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;
  }

  /**
   * Sets up a menu scheme for a given node type.
   *
   * @param array $node_type_ids
   *   Node type.
   * @param array $menu_ids
   *   Menu IDs.
   * @param string $id
   *   Scheme ID.
   *
   * @return \Drupal\workbench_access\Entity\AccessSchemeInterface
   *   Created 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;
  }

  /**
   * Assert that unprivileged users cannot access admin pages.
   */
  protected function assertThatUnprivilegedUsersCannotAccessAdminPages() {
    $this
      ->drupalGet(Url::fromRoute('entity.access_scheme.collection'));
    $assert = $this
      ->assertSession();
    $assert
      ->statusCodeEquals(403);
  }

  /**
   * Assert that admin can create an access scheme.
   *
   * @param string $scheme_id
   *   Scheme plugin ID.
   *
   * @return \Drupal\workbench_access\Entity\AccessSchemeInterface
   *   Created scheme.
   */
  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');

    /** @var \Drupal\workbench_access\Entity\AccessSchemeInterface $scheme */
    $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;
  }

  /**
   * Loads the given scheme.
   *
   * @param string $scheme_id
   *   Scheme ID.
   *
   * @return \Drupal\workbench_access\Entity\AccessSchemeInterface
   *   Unchanged scheme.
   */
  protected function loadUnchangedScheme($scheme_id) {
    return $this->container
      ->get('entity_type.manager')
      ->getStorage('access_scheme')
      ->loadUnchanged($scheme_id);
  }

  /**
   * Sets the field type.
   *
   * @param string $entity_type_id
   *   Entity type ID.
   * @param string $bundle
   *   Bundle ID.
   * @param $type
   *   The field type to use.
   * @param string $field_name
   *   Field machine name.
   */
  protected function setFieldType($entity_type_id, $bundle, $type = 'options_select', $field_name = WorkbenchAccessManagerInterface::FIELD_NAME) {

    // Set the field to display as a dropdown on the form.
    $form_display = EntityFormDisplay::load("{$entity_type_id}.{$bundle}.default");
    $form_display
      ->setComponent($field_name, [
      'type' => $type,
    ]);
    $form_display
      ->save();
  }

  /**
   * Checks if we are using Drupal 8 or 9.
   *
   * @return bool
   *   TRUE if Drupal 8, FALSE otherwise.
   */
  public function isDrupal8() {
    $version = (int) substr(\Drupal::VERSION, 0, 1);
    if ($version < 9) {
      return TRUE;
    }
    return FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityReferenceTestTrait::createEntityReferenceField protected function Creates a field of an entity reference field storage on the specified bundle.
WorkbenchAccessTestTrait::assertCreatingAnAccessSchemeAsAdmin protected function Assert that admin can create an access scheme.
WorkbenchAccessTestTrait::assertThatUnprivilegedUsersCannotAccessAdminPages protected function Assert that unprivileged users cannot access admin pages.
WorkbenchAccessTestTrait::createUserWithRole protected function Sets up a user with a given role and saves it.
WorkbenchAccessTestTrait::isDrupal8 public function Checks if we are using Drupal 8 or 9.
WorkbenchAccessTestTrait::loadUnchangedScheme protected function Loads the given scheme.
WorkbenchAccessTestTrait::setFieldType protected function Sets the field type.
WorkbenchAccessTestTrait::setUpAdminUser protected function Sets up a user with an editor role that has access to content.
WorkbenchAccessTestTrait::setUpContentType public function Set up a content type with workbench access enabled.
WorkbenchAccessTestTrait::setUpEditorUser public function Sets up a user with an editor role that has access to content.
WorkbenchAccessTestTrait::setUpMenuScheme public function Sets up a menu scheme for a given node type.
WorkbenchAccessTestTrait::setUpTaxonomyFieldForEntityType protected function Sets up a taxonomy field on a given entity type and bundle.
WorkbenchAccessTestTrait::setUpTaxonomyScheme public function Sets up a taxonomy scheme for a given node type.
WorkbenchAccessTestTrait::setUpVocabulary public function Create a test vocabulary.