You are here

trait RadioactivityFunctionTestTrait in Radioactivity 8.3

Same name and namespace in other branches
  1. 4.0.x tests/src/Traits/RadioactivityFunctionTestTrait.php \Drupal\Tests\radioactivity\Traits\RadioactivityFunctionTestTrait

Radioactivity functional test trait.

Hierarchy

3 files declare their use of RadioactivityFunctionTestTrait
RadioactivityFieldTypeTest.php in tests/src/Kernel/RadioactivityFieldTypeTest.php
RadioactivityFunctionalJavascriptTestBase.php in tests/src/FunctionalJavascript/RadioactivityFunctionalJavascriptTestBase.php
RadioactivityValueFormatterTest.php in tests/src/Kernel/RadioactivityValueFormatterTest.php

File

tests/src/Traits/RadioactivityFunctionTestTrait.php, line 15

Namespace

Drupal\Tests\radioactivity\Traits
View source
trait RadioactivityFunctionTestTrait {

  /**
   * The entity type.
   *
   * @var string
   */
  protected $entityType = 'entity_test';

  /**
   * The entity type bundle.
   *
   * @var string
   */
  protected $entityBundle = 'entity_test';

  /**
   * Adds a Count type energy field to the content type.
   *
   * @param string $fieldName
   *   Field machine name.
   * @param int|float $defaultEnergy
   *   Default energy level.
   * @param int $cardinality
   *   Field cardinality.
   */
  public function addCountEnergyField($fieldName, $defaultEnergy = 0, $cardinality = 1) {
    $granularity = $halfLifeTime = $cutoff = 0;
    $this
      ->createEnergyField($fieldName, 'count', TRUE, $defaultEnergy, $granularity, $halfLifeTime, $cutoff, $cardinality);
  }

  /**
   * Adds a Linear type energy field to the content type.
   *
   * @param string $fieldName
   *   Field machine name.
   * @param int|float $defaultEnergy
   *   Field energy when the entity is created.
   * @param int $granularity
   *   Energy decay granularity.
   * @param int|float $cutoff
   *   Energy cut off value.
   * @param int $cardinality
   *   Field cardinality.
   */
  public function addLinearEnergyField($fieldName, $defaultEnergy = 0, $granularity = 900, $cutoff = 10, $cardinality = 1) {
    $halfLifeTime = 0;
    $this
      ->createEnergyField($fieldName, 'linear', TRUE, $defaultEnergy, $granularity, $halfLifeTime, $cutoff, $cardinality);
  }

  /**
   * Adds a Decay type energy field to the content type.
   *
   * @param string $fieldName
   *   Field machine name.
   * @param int|float $defaultEnergy
   *   Field energy when the entity is created.
   * @param int $granularity
   *   Energy decay granularity.
   * @param int $halfLifeTime
   *   Half-life time.
   * @param int|float $cutoff
   *   Energy cut off value.
   * @param int $cardinality
   *   Field cardinality.
   */
  public function addDecayEnergyField($fieldName, $defaultEnergy = 0, $granularity = 0, $halfLifeTime = 43200, $cutoff = 10, $cardinality = 1) {
    $this
      ->createEnergyField($fieldName, 'decay', TRUE, $defaultEnergy, $granularity, $halfLifeTime, $cutoff, $cardinality);
  }

  /**
   * Adds an radioactivity energy field to the content type.
   *
   * @param string $fieldName
   *   Field machine name.
   * @param string $profile
   *   Profile type.
   * @param bool $required
   *   Required input.
   * @param int|float $defaultEnergy
   *   Field energy when the entity is created.
   * @param int $granularity
   *   Energy decay granularity.
   * @param int $halfLifeTime
   *   Half life time.
   * @param int|float $cutoff
   *   Energy cut off value.
   * @param int $cardinality
   *   Field cardinality.
   */
  protected function createEnergyField($fieldName, $profile, $required = FALSE, $defaultEnergy = 0, $granularity = 900, $halfLifeTime = 43200, $cutoff = 10, $cardinality = 1) {
    FieldStorageConfig::create([
      'entity_type' => $this->entityType,
      'type' => 'radioactivity',
      'field_name' => $fieldName,
      'cardinality' => $cardinality,
      'settings' => [
        'profile' => $profile,
        'granularity' => $granularity,
        'halflife' => $halfLifeTime,
        'cutoff' => $cutoff,
      ],
    ])
      ->save();
    FieldConfig::create([
      'entity_type' => $this->entityType,
      'bundle' => $this->entityBundle,
      'field_name' => $fieldName,
      'required' => $required,
      'default_value' => [
        [
          'energy' => $defaultEnergy,
          'timestamp' => 0,
        ],
      ],
    ])
      ->save();
  }

  /**
   * Creates an energy field formatter.
   *
   * @param string $fieldName
   *   Field machine name.
   */
  protected function createEnergyFormDisplay($fieldName) {
    $entityFormDisplay = EntityFormDisplay::load('entity_test.entity_test.default');
    $entityFormDisplay
      ->setComponent($fieldName, [
      'type' => 'radioactivity_energy',
    ]);
    $entityFormDisplay
      ->save();
  }

  /**
   * Creates an emitter field formatter.
   *
   * @param string $fieldName
   *   Field machine name.
   * @param int|float $energy
   *   The energy to emit.
   * @param string $display
   *   The field display type.
   *
   * @return \Drupal\Core\Entity\Entity\EntityViewDisplay
   *   The entity view display object.
   */
  protected function createEmitterViewDisplay($fieldName, $energy = 10, $display = 'raw') {
    $entity_view_display = EntityViewDisplay::create([
      'targetEntityType' => $this->entityType,
      'bundle' => $this->entityBundle,
      'mode' => 'default',
      'status' => TRUE,
    ]);
    $entity_view_display
      ->setComponent($fieldName, [
      'type' => 'radioactivity_emitter',
      'settings' => [
        'energy' => $energy,
        'display' => $display,
      ],
    ]);
    $entity_view_display
      ->save();
    return $entity_view_display;
  }

  /**
   * Creates an value field formatter.
   *
   * @param string $fieldName
   *   Field machine name.
   * @param int $decimals
   *   Number of decimals to display.
   *
   * @return \Drupal\Core\Entity\Entity\EntityViewDisplay
   *   The entity view display object.
   */
  protected function createValueViewDisplay($fieldName, $decimals = 0) {
    $entity_view_display = EntityViewDisplay::create([
      'targetEntityType' => $this->entityType,
      'bundle' => $this->entityBundle,
      'mode' => 'default',
    ]);
    $entity_view_display
      ->setComponent($fieldName, [
      'type' => 'radioactivity_value',
      'settings' => [
        'decimals' => $decimals,
      ],
    ]);
    $entity_view_display
      ->save();
    return $entity_view_display;
  }

  /**
   * Set the entity type.
   *
   * @param string $type
   *   The entity type.
   */
  public function setEntityType($type) {
    $this->entityType = $type;
  }

  /**
   * Set the entity bundle.
   *
   * @param string $bundle
   *   The entity bundle.
   */
  public function setEntityBundle($bundle) {
    $this->entityBundle = $bundle;
  }

  /**
   * Sets the emitter energy of a field.
   *
   * @param string $fieldName
   *   The field name.
   * @param int $energy
   *   The energy value to set.
   */
  public function setFieldEmitterEnergy($fieldName, $energy = 10) {
    $this
      ->updateFieldEmitterSettings($fieldName, [
      'energy' => $energy,
    ]);
  }

  /**
   * Sets the emitter display mode of a field.
   *
   * @param string $fieldName
   *   The field name.
   * @param bool $displayEnergy
   *   Whether to display the energy level.
   */
  public function setFieldEmitterDisplay($fieldName, $displayEnergy = FALSE) {
    $display = $displayEnergy ? 'raw' : 'none';
    $this
      ->updateFieldEmitterSettings($fieldName, [
      'display' => $display,
    ]);
  }

  /**
   * Updates the emitter field display settings.
   *
   * @param string $fieldName
   *   The field name.
   * @param array $settings
   *   Allowed keys:
   *   'energy': The energy value this field will emit when displayed.
   *   'raw':    True if the energy value is visible.
   */
  protected function updateFieldEmitterSettings($fieldName, array $settings) {
    $display = EntityViewDisplay::load('entity_test.entity_test.default');
    $component = $display
      ->getComponent($fieldName);
    foreach ($settings as $key => $value) {
      $component['settings'][$key] = $value;
    }
    $display
      ->setComponent($fieldName, $component)
      ->save();
  }

  /**
   * Creates an entity.
   *
   * @return \Drupal\Core\Entity\ContentEntityInterface
   *   The created entity.
   */
  public function createContent() {

    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    $entity = \Drupal::entityTypeManager()
      ->getStorage($this->entityType)
      ->create([
      'type' => $this->entityType,
      'title' => $this
        ->randomString(),
    ]);
    $entity
      ->save();
    return $entity;
  }

  /**
   * Assert the energy values from a field.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The host entity of the field.
   * @param string $fieldName
   *   The field to be asserted.
   * @param array|string|int $expectedValues
   *   The expected field values.
   * @param string $operator
   *   The operator to be used to compare. Allowed values: '>', '>=', '<', '<=',
   *   '=='. The actual value on the left, the expected on the right.
   * @param string $message
   *   The assertion message.
   */
  public function assertFieldEnergyValue(EntityInterface $entity, $fieldName, $expectedValues, $operator = '==', $message = '') {
    $expectedValues = is_array($expectedValues) ? $expectedValues : [
      $expectedValues,
    ];
    $actualValues = array_map(function ($item) {
      return $item['energy'];
    }, $entity
      ->get($fieldName)
      ->getValue());
    $this
      ->assertEnergyValues($fieldName, $actualValues, $expectedValues, $operator, $message);
  }

  /**
   * Assert the energy values from the page.
   *
   * @param string $fieldName
   *   The field to be asserted.
   * @param array|string|int $expectedValues
   *   The expected field values.
   * @param string $operator
   *   The operator to be used to compare. Allowed values: '>', '>=', '<', '<=',
   *   '=='. The actual value on the left, the expected on the right.
   * @param string $message
   *   The assertion message.
   */
  public function assertPageEnergyValue($fieldName, $expectedValues, $operator = '==', $message = '') {
    $expectedValues = is_array($expectedValues) ? $expectedValues : [
      $expectedValues,
    ];
    $actualValues = $this
      ->getPageEnergyValues($fieldName);
    $this
      ->assertEnergyValues($fieldName, $actualValues, $expectedValues, $operator, $message);
  }

  /**
   * Assert field energy values.
   *
   * @param string $fieldName
   *   The field to be asserted.
   * @param array $actualValues
   *   The actual field values.
   * @param array $expectedValues
   *   The expected field values.
   * @param string $operator
   *   The operator to be used to compare. Allowed values: '>', '>=', '<', '<=',
   *   '=='. The actual value on the left, the expected on the right.
   * @param string $message
   *   The assertion message.
   */
  private function assertEnergyValues($fieldName, array $actualValues, array $expectedValues, $operator = '==', $message = '') {
    if (array_diff(array_keys($actualValues), array_keys($expectedValues))) {
      throw new \RuntimeException(sprintf('Invalid number of expected values for %s.', $fieldName));
    }
    foreach ($actualValues as $key => $actual) {
      $expected = $expectedValues[$key];
      switch ($operator) {
        case '>':
          $result = $actual > $expected;
          break;
        case '>=':
          $result = $actual >= $expected;
          break;
        case '<':
          $result = $actual < $expected;
          break;
        case '<=':
          $result = $actual <= $expected;
          break;
        case '==':
        default:
          $result = $actual == $expected;
      }
      $message = $message ?: ($message = sprintf('The energy value of %s is %s, but %s expected.', $fieldName, $actual, $expected));
      $this
        ->assertTrue($result, $message);
    }
  }

  /**
   * Gets the field's energy values from the session's page.
   *
   * @param string $fieldName
   *   The name of the field to be asserted.
   *
   * @return array
   *   The field values.
   */
  public function getPageEnergyValues($fieldName) {
    $values = [];
    $fieldBaseName = substr($fieldName, 6);
    $selector = '.field--name-field-' . $fieldBaseName . ' .field__item';
    $rows = $this
      ->getSession()
      ->getPage()
      ->findAll('css', $selector);
    if ($rows) {
      foreach ($rows as $row) {
        $values[] = $row
          ->getHtml();
      }
    }
    return $values;
  }

  /**
   * Asserts the actual incident count.
   *
   * @param int $expected
   *   The expected count.
   * @param string $message
   *   The assertion message.
   */
  public function assetIncidentCount($expected, $message = '') {
    $actual = $this
      ->getIncidentCount();
    $message = $message ?: ($message = sprintf('The incident count is %s, but %s expected.', $actual, $expected));
    $this
      ->assertTrue($actual == $expected, $message);
  }

  /**
   * Gets the number of incidents from the incident storage.
   *
   * @return int
   *   The incident count.
   */
  public function getIncidentCount() {
    $storage = new DefaultIncidentStorage(\Drupal::state());
    return count($storage
      ->getIncidents());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RadioactivityFunctionTestTrait::$entityBundle protected property The entity type bundle.
RadioactivityFunctionTestTrait::$entityType protected property The entity type.
RadioactivityFunctionTestTrait::addCountEnergyField public function Adds a Count type energy field to the content type.
RadioactivityFunctionTestTrait::addDecayEnergyField public function Adds a Decay type energy field to the content type.
RadioactivityFunctionTestTrait::addLinearEnergyField public function Adds a Linear type energy field to the content type.
RadioactivityFunctionTestTrait::assertEnergyValues private function Assert field energy values.
RadioactivityFunctionTestTrait::assertFieldEnergyValue public function Assert the energy values from a field.
RadioactivityFunctionTestTrait::assertPageEnergyValue public function Assert the energy values from the page.
RadioactivityFunctionTestTrait::assetIncidentCount public function Asserts the actual incident count.
RadioactivityFunctionTestTrait::createContent public function Creates an entity.
RadioactivityFunctionTestTrait::createEmitterViewDisplay protected function Creates an emitter field formatter.
RadioactivityFunctionTestTrait::createEnergyField protected function Adds an radioactivity energy field to the content type.
RadioactivityFunctionTestTrait::createEnergyFormDisplay protected function Creates an energy field formatter.
RadioactivityFunctionTestTrait::createValueViewDisplay protected function Creates an value field formatter.
RadioactivityFunctionTestTrait::getIncidentCount public function Gets the number of incidents from the incident storage.
RadioactivityFunctionTestTrait::getPageEnergyValues public function Gets the field's energy values from the session's page.
RadioactivityFunctionTestTrait::setEntityBundle public function Set the entity bundle.
RadioactivityFunctionTestTrait::setEntityType public function Set the entity type.
RadioactivityFunctionTestTrait::setFieldEmitterDisplay public function Sets the emitter display mode of a field.
RadioactivityFunctionTestTrait::setFieldEmitterEnergy public function Sets the emitter energy of a field.
RadioactivityFunctionTestTrait::updateFieldEmitterSettings protected function Updates the emitter field display settings.