You are here

function TokenTestTrait::assertTokens in Token 8

27 calls to TokenTestTrait::assertTokens()
ArrayTest::testArrayTokens in tests/src/Kernel/ArrayTest.php
BookTest::testBookTokens in tests/src/Kernel/BookTest.php
CommentTest::testCommentTokens in tests/src/Kernel/CommentTest.php
DateTest::testDateTokens in tests/src/Kernel/DateTest.php
EntityTest::testEntityOriginal in tests/src/Kernel/EntityTest.php
Test the [entity:original:*] tokens.

... See full list

File

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

Class

TokenTestTrait
Helper test trait with some added functions for testing.

Namespace

Drupal\Tests\token\Functional

Code

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;
}