You are here

class FieldWidgetThirdPartySettingsFormEventTest in Hook Event Dispatcher 8.2

Same name and namespace in other branches
  1. 3.x modules/field_event_dispatcher/tests/src/Unit/Field/FieldWidgetThirdPartySettingsFormEventTest.php \Drupal\Tests\field_event_dispatcher\Unit\Field\FieldWidgetThirdPartySettingsFormEventTest

Class FieldWidgetThirdPartySettingsFormEventTest.

@group field_event_dispatcher

Hierarchy

Expanded class hierarchy of FieldWidgetThirdPartySettingsFormEventTest

File

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

Namespace

Drupal\Tests\field_event_dispatcher\Unit\Field
View source
class FieldWidgetThirdPartySettingsFormEventTest extends TestCase {

  /**
   * The manager.
   *
   * @var \Drupal\Tests\hook_event_dispatcher\Unit\HookEventDispatcherManagerSpy
   */
  private $manager;

  /**
   * Sets up the test.
   */
  public function setUp() : void {
    $builder = new ContainerBuilder();
    $this->manager = new HookEventDispatcherManagerSpy();
    $builder
      ->set('hook_event_dispatcher.manager', $this->manager);
    $builder
      ->compile();
    Drupal::setContainer($builder);
  }

  /**
   * FieldWidgetThirdPartySettingsFormEvent get method return type test.
   *
   * This tests that the various methods for getting event properties return
   * correct types. We don't actually have to do the comparisons ourselves,
   * because all the methods have explicit return types defined so PHP will
   * throw an exception if there's a mismatch when we call them.
   */
  public function testGetMethodReturnTypes() : void {
    $this->manager
      ->setEventCallbacks([
      HookEventDispatcherInterface::FIELD_WIDGET_THIRD_PARTY_SETTINGS_FORM => static function (FieldWidgetThirdPartySettingsFormEvent $event) {
        $event
          ->getPlugin();
        $event
          ->getFieldDefinition();
        $event
          ->getFormMode();
        $event
          ->getForm();
        $event
          ->getFormState();
        $event
          ->getElements();
      },
    ]);
    $testFieldDefinition = new BaseFieldDefinition();
    $testPlugin = new StringTextfieldWidget('test_widget', [], $testFieldDefinition, [], []);
    $testFormState = new FormState();

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

    // So that PHPUnit doesn't mark this as a "risky test" because of no
    // assertions.
    self::assertTrue(TRUE);
  }

  /**
   * FieldWidgetThirdPartySettingsFormEvent adding elements test.
   *
   * This tests adding third-party form elements.
   */
  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);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FieldWidgetThirdPartySettingsFormEventTest::$manager private property The manager.
FieldWidgetThirdPartySettingsFormEventTest::setUp public function Sets up the test.
FieldWidgetThirdPartySettingsFormEventTest::testAddingElements public function FieldWidgetThirdPartySettingsFormEvent adding elements test.
FieldWidgetThirdPartySettingsFormEventTest::testGetMethodReturnTypes public function FieldWidgetThirdPartySettingsFormEvent get method return type test.