You are here

public function FieldFormatterInfoAlterEventTest::testInfoAlter in Hook Event Dispatcher 8.2

Same name and namespace in other branches
  1. 3.x modules/field_event_dispatcher/tests/src/Unit/Field/FieldFormatterInfoAlterEventTest.php \Drupal\Tests\field_event_dispatcher\Unit\Field\FieldFormatterInfoAlterEventTest::testInfoAlter()

FieldFormatterInfoAlterEvent array alter test.

This tests altering the field formatter type definitions array.

File

modules/field_event_dispatcher/tests/src/Unit/Field/FieldFormatterInfoAlterEventTest.php, line 43

Class

FieldFormatterInfoAlterEventTest
Class FieldFormatterInfoAlterEventTest.

Namespace

Drupal\Tests\field_event_dispatcher\Unit\Field

Code

public function testInfoAlter() {
  $info = $expectedInfo = [
    'image' => [
      'id' => 'image',
      'class' => 'Drupal\\image\\Plugin\\Field\\FieldFormatter\\ImageFormatter',
    ],
  ];
  $this->manager
    ->setEventCallbacks([
    HookEventDispatcherInterface::FIELD_FORMATTER_INFO_ALTER => static function (FieldFormatterInfoAlterEvent $event) {
      $info =& $event
        ->getInfo();
      $info['image']['class'] = 'Drupal\\another_module\\Plugin\\Field\\FieldFormatter\\ImageFormatter';
    },
  ]);

  // Run the procedural hook which should trigger the above handler.
  field_event_dispatcher_field_formatter_info_alter($info);

  /** @var \Drupal\field_event_dispatcher\Event\Field\FieldFormatterInfoAlterEvent $event */
  $event = $this->manager
    ->getRegisteredEvent(HookEventDispatcherInterface::FIELD_FORMATTER_INFO_ALTER);
  self::assertSame($info, $event
    ->getInfo());
  $expectedInfo['image']['class'] = 'Drupal\\another_module\\Plugin\\Field\\FieldFormatter\\ImageFormatter';
  self::assertSame($expectedInfo, $info);
}