You are here

public function TokenEventTest::testTokenInfoEvent in Hook Event Dispatcher 8

Test TokenInfoEvent.

File

tests/src/Unit/Token/TokenEventTest.php, line 45

Class

TokenEventTest
Class TokenEventTest.

Namespace

Drupal\Tests\hook_event_dispatcher\Unit\Token

Code

public function testTokenInfoEvent() {
  $types = [
    TokenType::create('test_type', 'Test type')
      ->setDescription('Test type desc'),
    TokenType::create('other_type', 'Other type')
      ->setDescription('Other type!')
      ->setNeedsData('test_data'),
  ];
  $tokens = [
    Token::create('test_type', 'test_token1', 'Test name 1')
      ->setDescription('Test description 1'),
    Token::create('test_type', 'test_token2', 'Test name 2')
      ->setDescription('Test description 2'),
    Token::create('other_type', 'test_token3', 'Test name 3'),
    Token::create('dynamic_type', 'test_token4', 'Test name 4')
      ->setDynamic(TRUE),
  ];
  $this->manager
    ->setEventCallbacks([
    HookEventDispatcherInterface::TOKEN_INFO => function (TokensInfoEvent $event) use ($types, $tokens) {
      foreach ($types as $type) {
        $event
          ->addTokenType($type);
      }
      foreach ($tokens as $token) {
        $event
          ->addToken($token);
      }
    },
  ]);
  $result = hook_event_dispatcher_token_info();
  $expectedTypes = [
    'test_type' => [
      'name' => 'Test type',
      'description' => 'Test type desc',
      'needs-data' => NULL,
    ],
    'other_type' => [
      'name' => 'Other type',
      'description' => 'Other type!',
      'needs-data' => 'test_data',
    ],
  ];
  $expectedTokens = [
    'test_type' => [
      'test_token1' => [
        'name' => 'Test name 1',
        'description' => 'Test description 1',
        'dynamic' => FALSE,
      ],
      'test_token2' => [
        'name' => 'Test name 2',
        'description' => 'Test description 2',
        'dynamic' => FALSE,
      ],
    ],
    'other_type' => [
      'test_token3' => [
        'name' => 'Test name 3',
        'description' => NULL,
        'dynamic' => FALSE,
      ],
    ],
    'dynamic_type' => [
      'test_token4' => [
        'name' => 'Test name 4',
        'description' => NULL,
        'dynamic' => TRUE,
      ],
    ],
  ];

  /** @var \Drupal\hook_event_dispatcher\Event\Token\TokensInfoEvent $event */
  $event = $this->manager
    ->getRegisteredEvent(HookEventDispatcherInterface::TOKEN_INFO);
  self::assertEquals($expectedTypes, $result['types']);
  self::assertEquals($expectedTokens, $result['tokens']);
  self::assertEquals($expectedTypes, $event
    ->getTokenTypes());
  self::assertEquals($expectedTokens, $event
    ->getTokens());
}