You are here

trait RngTestTrait in RNG - Events and Registrations 8.2

Same name and namespace in other branches
  1. 8 src/Tests/RngTestTrait.php \Drupal\rng\Tests\RngTestTrait
  2. 3.x src/Tests/RngTestTrait.php \Drupal\rng\Tests\RngTestTrait

Hierarchy

3 files declare their use of RngTestTrait
RngBrowserTestBase.php in tests/src/Functional/RngBrowserTestBase.php
RngKernelTestBase.php in tests/src/Kernel/RngKernelTestBase.php
RngViewsTest.php in tests/src/Kernel/Views/RngViewsTest.php

File

src/Tests/RngTestTrait.php, line 17

Namespace

Drupal\rng\Tests
View source
trait RngTestTrait {

  /**
   * Create and save a registration type entity.
   *
   * @return \Drupal\rng\Entity\RegistrationTypeInterface
   *   A registration type entity
   */
  protected function createRegistrationType() {
    $registration_type = RegistrationType::create([
      'id' => 'registration_type_a',
      'label' => 'Registration Type A',
      'description' => 'Description for registration type a',
    ]);
    $registration_type
      ->save();
    return $registration_type;
  }

  /**
   * Creates an event type config.
   *
   * @param string $entity_type_id
   *   An entity type ID.
   * @param string $bundle
   *   An entity type bundle.
   * @param array $values
   *   Optional values for the event type.
   *
   * @return \Drupal\rng\Entity\EventTypeInterface
   *   An event type config.
   */
  protected function createEventType($entity_type_id, $bundle, $values = []) {
    $event_type = RngEventType::create($values + [
      'label' => 'Event Type A',
      'entity_type' => $entity_type_id,
      'bundle' => $bundle,
      'mirror_operation_to_event_manage' => 'update',
    ]);
    $event_type
      ->setIdentityTypeReference('user', 'user', TRUE);
    $event_type
      ->setDefaultRegistrantType('registrant');
    $event_type
      ->save();
    return $event_type;
  }

  /**
   * Create an event.
   *
   * @return \Drupal\rng\EventMetaInterface
   */
  protected function createEvent($values = []) {
    $event = EntityTest::create($values + [
      EventManagerInterface::FIELD_REGISTRATION_TYPE => $this->registrationType
        ->id(),
      EventManagerInterface::FIELD_STATUS => TRUE,
      EventManagerInterface::FIELD_ALLOW_DUPLICATE_REGISTRANTS => 0,
    ]);
    $event
      ->save();
    return $this->eventManager
      ->getMeta($event);
  }

  /**
   * Create a registration and add an identity as a registrant.
   *
   * @param \Drupal\Core\Entity\EntityInterface $event
   *   An event entity.
   * @param \Drupal\rng\Entity\RegistrationTypeInterface $registration_type
   *   A registration type.
   * @param \Drupal\Core\Entity\EntityInterface[] $identities
   *   An array of identities.
   *
   * @return \Drupal\rng\Entity\RegistrationInterface
   *   A saved registration
   */
  protected function createRegistration(EntityInterface $event, RegistrationTypeInterface $registration_type, array $identities) {
    $registration = Registration::create([
      'type' => $registration_type
        ->id(),
    ]);
    foreach ($identities as $identity) {
      $registration
        ->addIdentity($identity);
    }
    $registration
      ->setEvent($event)
      ->save();
    return $registration;
  }

  /**
   * Create rules for an event type.
   *
   * @param array $roles
   *   An array of role ID to add access.
   * @param array $operations
   *   An array of operations. Value is boolean whether to grant, key can be
   *   any of 'create', 'view', 'update', 'delete'.
   */
  protected function createUserRoleRules($roles = [], $operations = []) {
    $rule = EventTypeRule::create([
      'trigger' => 'rng_event.register',
      'entity_type' => 'entity_test',
      'bundle' => 'entity_test',
      'machine_name' => 'user_role',
    ]);
    $rule
      ->setCondition('role', [
      'id' => 'rng_user_role',
      'roles' => $roles,
    ]);
    $rule
      ->setAction('registration_operations', [
      'id' => 'registration_operations',
      'configuration' => [
        'operations' => $operations,
      ],
    ]);
    $rule
      ->save();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RngTestTrait::createEvent protected function Create an event.
RngTestTrait::createEventType protected function Creates an event type config.
RngTestTrait::createRegistration protected function Create a registration and add an identity as a registrant. 1
RngTestTrait::createRegistrationType protected function Create and save a registration type entity.
RngTestTrait::createUserRoleRules protected function Create rules for an event type.