You are here

public function FieldWidgetThirdPartySettingsFormEventTest::testAddingElements in Hook Event Dispatcher 3.x

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

FieldWidgetThirdPartySettingsFormEvent adding elements test.

This tests adding third-party form elements.

File

modules/field_event_dispatcher/tests/src/Unit/Field/FieldWidgetThirdPartySettingsFormEventTest.php, line 86

Class

FieldWidgetThirdPartySettingsFormEventTest
Class FieldWidgetThirdPartySettingsFormEventTest.

Namespace

Drupal\Tests\field_event_dispatcher\Unit\Field

Code

public function testAddingElements() : void {
  $elements = $expectedElements = [];
  $this->manager
    ->setEventCallbacks([
    HookEventDispatcherInterface::FIELD_WIDGET_THIRD_PARTY_SETTINGS_FORM => static function (FieldWidgetThirdPartySettingsFormEvent $event) {
      $event
        ->addElements('test_module', [
        'test' => [],
      ]);
    },
  ]);
  $testFieldDefinition = new BaseFieldDefinition();
  $testPlugin = new StringTextfieldWidget('test_widget', [], $testFieldDefinition, [], []);
  $testFormState = new FormState();

  // Run the procedural hook which should trigger the above handler.
  $elements = field_event_dispatcher_field_widget_third_party_settings_form($testPlugin, $testFieldDefinition, 'form_mode', [], $testFormState);

  /** @var \Drupal\field_event_dispatcher\Event\Field\FieldWidgetThirdPartySettingsFormEvent $event */
  $event = $this->manager
    ->getRegisteredEvent(HookEventDispatcherInterface::FIELD_WIDGET_THIRD_PARTY_SETTINGS_FORM);
  self::assertSame($elements, $event
    ->getElements());
  $expectedElements['test_module']['test'] = [];
  self::assertSame($expectedElements, $elements);
}