You are here

public function WidgetFormAlterEventTest::testWidgetMultivalueFormAlterEvent in Hook Event Dispatcher 8.2

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

Test WidgetMultivalueFormAlterEvent.

File

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

Class

WidgetFormAlterEventTest
Class WidgetFormAlterEventTest.

Namespace

Drupal\Tests\field_event_dispatcher\Unit\Field

Code

public function testWidgetMultivalueFormAlterEvent() : void {
  $elements = $expectedElements = [
    0 => [
      '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_MULTIVALUE_FORM_ALTER => static function (WidgetMultivalueFormAlterEvent $event) {
      $elements =& $event
        ->getElements();
      $elements[0]['other'] = 'key';
    },
  ]);
  $this->manager
    ->setMaxEventCount(2);
  field_event_dispatcher_field_widget_multivalue_form_alter($elements, $formState, $context);
  $expectedElements[0]['other'] = 'key';
  self::assertSame($expectedElements, $elements);

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