You are here

public function WidgetFormAlterEventTest::testWidgetFormAlterEvent in Hook Event Dispatcher 3.x

Same name and namespace in other branches
  1. 8.2 modules/field_event_dispatcher/tests/src/Unit/Field/WidgetFormAlterEventTest.php \Drupal\Tests\field_event_dispatcher\Unit\Field\WidgetFormAlterEventTest::testWidgetFormAlterEvent()

Test WidgetFormAlterEvent.

File

modules/field_event_dispatcher/tests/src/Unit/Field/WidgetFormAlterEventTest.php, line 46

Class

WidgetFormAlterEventTest
Class WidgetFormAlterEventTest.

Namespace

Drupal\Tests\field_event_dispatcher\Unit\Field

Code

public function testWidgetFormAlterEvent() : void {
  $element = $expectedElement = [
    'widget' => 'element',
  ];
  $formState = $this
    ->createMock(FormStateInterface::class);
  $items = $this
    ->createMock(FieldItemListInterface::class);
  $definition = $this
    ->createMock(FieldDefinitionInterface::class);
  $widgetType = 'widget_type';
  $definition
    ->method('getType')
    ->willReturn($widgetType);
  $items
    ->method('getFieldDefinition')
    ->willReturn($definition);
  $context = [
    'items' => $items,
  ];
  $this->manager
    ->setEventCallbacks([
    HookEventDispatcherInterface::WIDGET_FORM_ALTER => static function (WidgetFormAlterEvent $event) {
      $element =& $event
        ->getElement();
      $element['other'] = 'key';
    },
  ]);
  $this->manager
    ->setMaxEventCount(2);
  field_event_dispatcher_field_widget_form_alter($element, $formState, $context);
  $expectedElement['other'] = 'key';
  self::assertSame($expectedElement, $element);

  /** @var \Drupal\field_event_dispatcher\Event\Field\WidgetFormAlterEvent $event */
  $event = $this->manager
    ->getRegisteredEvent(HookEventDispatcherInterface::WIDGET_FORM_ALTER);
  self::assertSame($element, $event
    ->getElement());
  self::assertSame($formState, $event
    ->getFormState());
  self::assertSame($context, $event
    ->getContext());
}