You are here

public function TokenEventTest::testTokenReplacementEvent in Hook Event Dispatcher 3.x

Same name and namespace in other branches
  1. 8.2 modules/core_event_dispatcher/tests/src/Unit/Token/TokenEventTest.php \Drupal\Tests\core_event_dispatcher\Unit\Token\TokenEventTest::testTokenReplacementEvent()

Test TokenReplacementEvent.

File

modules/core_event_dispatcher/tests/src/Unit/Token/TokenEventTest.php, line 123

Class

TokenEventTest
Class TokenEventTest.

Namespace

Drupal\Tests\core_event_dispatcher\Unit\Token

Code

public function testTokenReplacementEvent() : void {
  $replacement1 = 'Replacement value 1';
  $replacement2 = 'Replacement value 2';
  $this->manager
    ->setEventCallbacks([
    HookEventDispatcherInterface::TOKEN_REPLACEMENT => static function (TokensReplacementEvent $event) use ($replacement1, $replacement2) {
      $event
        ->setReplacementValue('test_type', 'token1', $replacement1);
      $event
        ->setReplacementValue('test_type', 'token2', $replacement2);
    },
  ]);
  $type = 'test_type';
  $tokens = [
    'token1' => '[test_type:token1]',
    'token2' => '[test_type:token2]',
    'token3' => '[test_type:token3]',
  ];
  $data = [
    'test_data' => 'test!',
  ];
  $options = [
    'test_options' => 'Option value',
  ];
  $metaData = $this
    ->createMock(BubbleableMetadata::class);
  $result = core_event_dispatcher_tokens($type, $tokens, $data, $options, $metaData);
  $expectedResult = [
    '[test_type:token1]' => $replacement1,
    '[test_type:token2]' => $replacement2,
  ];

  /** @var \Drupal\core_event_dispatcher\Event\Token\TokensReplacementEvent $event */
  $event = $this->manager
    ->getRegisteredEvent(HookEventDispatcherInterface::TOKEN_REPLACEMENT);
  self::assertSame($expectedResult, $result);
  self::assertSame($type, $event
    ->getType());
  self::assertSame($tokens, $event
    ->getTokens());
  self::assertSame($data, $event
    ->getRawData());
  self::assertSame($options, $event
    ->getOptions());
  self::assertSame($metaData, $event
    ->getBubbleableMetadata());
  self::assertSame('test!', $event
    ->getData('test_data'));
  self::assertNull($event
    ->getData('none_existing'));
  self::assertFalse($event
    ->forToken('none_existing', 'token1'));
  self::assertFalse($event
    ->forToken('test_type', 'none_existing'));
}