You are here

trait AssertLinkitFilterTrait in Linkit 8.5

Provides helper methods for assertions.

Hierarchy

File

tests/src/Kernel/AssertLinkitFilterTrait.php, line 12

Namespace

Drupal\Tests\linkit\Kernel
View source
trait AssertLinkitFilterTrait {

  /**
   * The linkit filter.
   *
   * @var \Drupal\filter\Plugin\FilterInterface
   */
  protected $filter;

  /**
   * Asserts that Linkit filter correctly processes the content.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity object to check.
   * @param string $langcode
   *   The language code of the text to be filtered.
   */
  protected function assertLinkitFilter(EntityInterface $entity, $langcode = LanguageInterface::LANGCODE_SITE_DEFAULT) {
    if ($entity
      ->getEntityTypeId() === "file") {

      /** @var \Drupal\file\Entity\File $entity */
      $href = file_create_url($entity
        ->getFileUri());
    }
    else {
      $href = $entity
        ->toUrl()
        ->toString();
    }
    $input = '<a data-entity-type="' . $entity
      ->getEntityTypeId() . '" data-entity-uuid="' . $entity
      ->uuid() . '">Link text</a>';
    $expected = '<a data-entity-type="' . $entity
      ->getEntityTypeId() . '" data-entity-uuid="' . $entity
      ->uuid() . '" href="' . $href . '">Link text</a>';
    $this
      ->assertSame($expected, $this
      ->process($input, $langcode)
      ->getProcessedText());
  }

  /**
   * Asserts that Linkit filter correctly processes the content titles.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity object to check.
   * @param string $langcode
   *   The language code of the text to be filtered.
   */
  protected function assertLinkitFilterWithTitle(EntityInterface $entity, $langcode = LanguageInterface::LANGCODE_SITE_DEFAULT) {
    if ($entity
      ->getEntityTypeId() === "file") {

      /** @var \Drupal\file\Entity\File $entity */
      $href = file_create_url($entity
        ->getFileUri());
    }
    else {
      $href = $entity
        ->toUrl()
        ->toString();
    }
    $input = '<a data-entity-type="' . $entity
      ->getEntityTypeId() . '" data-entity-uuid="' . $entity
      ->uuid() . '">Link text</a>';
    $expected = '<a data-entity-type="' . $entity
      ->getEntityTypeId() . '" data-entity-uuid="' . $entity
      ->uuid() . '" href="' . $href . '" title="' . Html::decodeEntities($entity
      ->label()) . '">Link text</a>';
    $this
      ->assertSame($expected, $this
      ->process($input, $langcode)
      ->getProcessedText());
  }

  /**
   * Test helper method that wraps the filter process method.
   *
   * @param string $input
   *   The text string to be filtered.
   * @param string $langcode
   *   The language code of the text to be filtered.
   *
   * @return \Drupal\filter\FilterProcessResult
   *   The filtered text, wrapped in a FilterProcessResult object, and possibly
   *   with associated assets, cacheability metadata and placeholders.
   *
   * @see \Drupal\filter\Plugin\FilterInterface::process
   */
  protected function process($input, $langcode = LanguageInterface::LANGCODE_SITE_DEFAULT) {
    return $this->filter
      ->process($input, $langcode);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AssertLinkitFilterTrait::$filter protected property The linkit filter.
AssertLinkitFilterTrait::assertLinkitFilter protected function Asserts that Linkit filter correctly processes the content.
AssertLinkitFilterTrait::assertLinkitFilterWithTitle protected function Asserts that Linkit filter correctly processes the content titles.
AssertLinkitFilterTrait::process protected function Test helper method that wraps the filter process method.