You are here

AssertLinkitFilterTrait.php in Linkit 8.5

File

tests/src/Kernel/AssertLinkitFilterTrait.php
View source
<?php

namespace Drupal\Tests\linkit\Kernel;

use Drupal\Component\Utility\Html;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Language\LanguageInterface;

/**
 * Provides helper methods for assertions.
 */
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);
  }

}

Traits

Namesort descending Description
AssertLinkitFilterTrait Provides helper methods for assertions.