You are here

function TokenTestTrait::assertPageTokens in Token 8

Make a page request and test for token generation.

1 call to TokenTestTrait::assertPageTokens()
TokenCurrentPageTest::testCurrentPageTokens in tests/src/Functional/TokenCurrentPageTest.php

File

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

Class

TokenTestTrait
Helper test trait with some added functions for testing.

Namespace

Drupal\Tests\token\Functional

Code

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,
      ]));
    }
  }
}