You are here

trait TokenTestTrait in Token 8

Helper test trait with some added functions for testing.

Hierarchy

1 file declares its use of TokenTestTrait
KernelTestBase.php in tests/src/Kernel/KernelTestBase.php

File

tests/src/Functional/TokenTestTrait.php, line 13

Namespace

Drupal\Tests\token\Functional
View source
trait TokenTestTrait {
  use PathAliasTestTrait;
  function assertToken($type, array $data, $token, $expected, array $options = []) {
    return $this
      ->assertTokens($type, $data, [
      $token => $expected,
    ], $options);
  }
  function assertTokens($type, array $data, array $tokens, array $options = []) {
    $input = $this
      ->mapTokenNames($type, array_keys($tokens));
    $bubbleable_metadata = new BubbleableMetadata();
    $replacements = \Drupal::token()
      ->generate($type, $input, $data, $options, $bubbleable_metadata);
    foreach ($tokens as $name => $expected) {
      $token = $input[$name];
      if (!isset($expected)) {
        $this
          ->assertArrayNotHasKey($token, $replacements, t("Token value for @token was not generated.", [
          '@token' => $token,
        ]));
      }
      elseif (!isset($replacements[$token])) {
        $this
          ->fail(t("Token value for @token was not generated.", [
          '@type' => $type,
          '@token' => $token,
        ]));
      }
      elseif (!empty($options['regex'])) {
        $this
          ->assertEquals(1, preg_match('/^' . $expected . '$/', $replacements[$token]), t("Token value for @token was '@actual', matching regular expression pattern '@expected'.", [
          '@type' => $type,
          '@token' => $token,
          '@actual' => $replacements[$token],
          '@expected' => $expected,
        ]));
      }
      else {
        $this
          ->assertEquals($expected, $replacements[$token], t("Token value for @token was '@actual', expected value '@expected'.", [
          '@type' => $type,
          '@token' => $token,
          '@actual' => $replacements[$token],
          '@expected' => $expected,
        ]));
      }
    }
    return $replacements;
  }
  function mapTokenNames($type, array $tokens = []) {
    $return = [];
    foreach ($tokens as $token) {
      $return[$token] = "[{$type}:{$token}]";
    }
    return $return;
  }
  function assertNoTokens($type, array $data, array $tokens, array $options = []) {
    $input = $this
      ->mapTokenNames($type, $tokens);
    $bubbleable_metadata = new BubbleableMetadata();
    $replacements = \Drupal::token()
      ->generate($type, $input, $data, $options, $bubbleable_metadata);
    foreach ($tokens as $name) {
      $token = $input[$name];
      $this
        ->assertTrue(!isset($replacements[$token]), t("Token value for @token was not generated.", [
        '@type' => $type,
        '@token' => $token,
      ]));
    }
  }
  function saveAlias($source, $alias, $language = Language::LANGCODE_NOT_SPECIFIED) {
    return $this
      ->createPathAlias($source, $alias, $language);
  }
  function saveEntityAlias($entity_type, EntityInterface $entity, $alias, $language = Language::LANGCODE_NOT_SPECIFIED) {
    $uri = $entity
      ->toUrl()
      ->toArray();
    return $this
      ->saveAlias($uri['path'], $alias, $language);
  }

  /**
   * Make a page request and test for token generation.
   */
  function assertPageTokens($url, array $tokens, array $data = [], array $options = []) {
    if (empty($tokens)) {
      return TRUE;
    }
    $token_page_tokens = [
      'tokens' => $tokens,
      'data' => $data,
      'options' => $options,
    ];
    \Drupal::state()
      ->set('token_page_tokens', $token_page_tokens);
    $options += [
      'url_options' => [],
    ];
    $this
      ->drupalGet($url, $options['url_options']);
    $this
      ->refreshVariables();
    $result = \Drupal::state()
      ->get('token_page_tokens', []);
    if (!isset($result['values']) || !is_array($result['values'])) {
      return $this
        ->fail('Failed to generate tokens.');
    }
    foreach ($tokens as $token => $expected) {
      if (!isset($expected)) {
        $this
          ->assertTrue(!isset($result['values'][$token]) || $result['values'][$token] === $token, t("Token value for @token was not generated.", [
          '@token' => $token,
        ]));
      }
      elseif (!isset($result['values'][$token])) {
        $this
          ->fail(t('Failed to generate token @token.', [
          '@token' => $token,
        ]));
      }
      else {
        $this
          ->assertSame($result['values'][$token], (string) $expected, t("Token value for @token was '@actual', expected value '@expected'.", [
          '@token' => $token,
          '@actual' => $result['values'][$token],
          '@expected' => $expected,
        ]));
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PathAliasTestTrait::assertPathAliasExists protected function Asserts that a path alias exists in the storage.
PathAliasTestTrait::assertPathAliasNotExists protected function Asserts that a path alias does not exist in the storage.
PathAliasTestTrait::createPathAlias protected function Creates a new path alias.
PathAliasTestTrait::loadPathAliasByConditions protected function Gets the first result from a 'load by properties' storage call.
TokenTestTrait::assertNoTokens function
TokenTestTrait::assertPageTokens function Make a page request and test for token generation.
TokenTestTrait::assertToken function
TokenTestTrait::assertTokens function
TokenTestTrait::mapTokenNames function
TokenTestTrait::saveAlias function
TokenTestTrait::saveEntityAlias function