You are here

trait EntityLegalTestTrait in Entity Legal 3.0.x

Same name and namespace in other branches
  1. 8.2 tests/src/Traits/EntityLegalTestTrait.php \Drupal\Tests\entity_legal\Traits\EntityLegalTestTrait
  2. 4.0.x tests/src/Traits/EntityLegalTestTrait.php \Drupal\Tests\entity_legal\Traits\EntityLegalTestTrait

Code reusing for Entity Legal tests.

Hierarchy

2 files declare their use of EntityLegalTestTrait
EntityLegalTestBase.php in src/Tests/EntityLegalTestBase.php
RedirectMethodTest.php in tests/src/Functional/RedirectMethodTest.php

File

tests/src/Traits/EntityLegalTestTrait.php, line 10

Namespace

Drupal\Tests\entity_legal\Traits
View source
trait EntityLegalTestTrait {

  /**
   * Creates a random legal document entity.
   *
   * @param bool $require_signup
   *   Whether or not to require new users to agree.
   * @param bool $require_existing
   *   Whether or not to require existing users to agree.
   * @param array $settings
   *   Additional settings to pass through to the document.
   *
   * @return \Drupal\entity_legal\EntityLegalDocumentInterface
   *   The created legal document.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   * @throws \Drupal\Core\Entity\EntityStorageException
   */
  protected function createDocument($require_signup = FALSE, $require_existing = FALSE, array $settings = []) {

    /** @var \Drupal\entity_legal\EntityLegalDocumentInterface $entity */
    $entity = \Drupal::entityTypeManager()
      ->getStorage('entity_legal_document')
      ->create([
      'id' => $this
        ->randomMachineName(32),
      'label' => $this
        ->randomMachineName(),
      'require_signup' => (int) $require_signup,
      'require_existing' => (int) $require_existing,
      'settings' => $settings,
    ]);
    $entity
      ->save();

    // Reset permissions cache to make new document permissions available.
    $this
      ->checkPermissions([
      $entity
        ->getPermissionView(),
      $entity
        ->getPermissionExistingUser(),
    ]);
    return $entity;
  }

  /**
   * Creates a document version.
   *
   * @param \Drupal\entity_legal\EntityLegalDocumentInterface $document
   *   The document to add the version to.
   * @param bool $save_as_default
   *   Whether to save the version as the default for the document.
   *
   * @return \Drupal\entity_legal\EntityLegalDocumentVersionInterface
   *   The created legal document version.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   * @throws \Drupal\Core\Entity\EntityStorageException
   */
  protected function createDocumentVersion(EntityLegalDocumentInterface $document, $save_as_default = FALSE) {

    /** @var \Drupal\entity_legal\EntityLegalDocumentVersionInterface $entity */
    $entity = \Drupal::entityTypeManager()
      ->getStorage('entity_legal_document_version')
      ->create([
      'label' => $this
        ->randomMachineName(),
      'name' => $this
        ->randomMachineName(64),
      'document_name' => $document
        ->id(),
      'acceptance_label' => 'I agree to the <a href="[entity_legal_document:url]">document</a>',
      'entity_legal_document_text' => [
        [
          'value' => $this
            ->randomMachineName(),
        ],
      ],
    ]);
    $entity
      ->save();
    if ($save_as_default) {
      $document
        ->setPublishedVersion($entity);
      $document
        ->save();
    }
    return $entity;
  }

  /**
   * Creates an account that is able to view and re-accept a given document.
   *
   * @param \Drupal\entity_legal\EntityLegalDocumentInterface $document
   *   The legal document the user is able to view and accept.
   *
   * @return \Drupal\Core\Session\AccountInterface
   *   The user.
   */
  protected function createUserWithAcceptancePermissions(EntityLegalDocumentInterface $document) {
    $account = $this
      ->drupalCreateUser([
      $document
        ->getPermissionView(),
      $document
        ->getPermissionExistingUser(),
    ]);
    return $account;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityLegalTestTrait::createDocument protected function Creates a random legal document entity.
EntityLegalTestTrait::createDocumentVersion protected function Creates a document version.
EntityLegalTestTrait::createUserWithAcceptancePermissions protected function Creates an account that is able to view and re-accept a given document.