You are here

class FormEventTest in Hook Event Dispatcher 8

Class FormEventTest.

@package Drupal\Tests\hook_event_dispatcher\Unit\Form

@group hook_event_dispatcher

Hierarchy

Expanded class hierarchy of FormEventTest

File

tests/src/Unit/Form/FormEventTest.php, line 20

Namespace

Drupal\Tests\hook_event_dispatcher\Unit\Form
View source
class FormEventTest extends UnitTestCase {

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

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    $builder = new ContainerBuilder();
    $this->manager = new HookEventDispatcherManagerSpy();
    $builder
      ->set('hook_event_dispatcher.manager', $this->manager);
    $builder
      ->compile();
    \Drupal::setContainer($builder);
  }

  /**
   * Test FormAlterEvent.
   */
  public function testFormAlterEvent() {
    $form = [
      'test' => 'form',
    ];
    $formState = $this
      ->createMock(FormStateInterface::class);
    $formId = 'test_form';
    $this->manager
      ->setMaxEventCount(2);
    hook_event_dispatcher_form_alter($form, $formState, $formId);

    /** @var \Drupal\hook_event_dispatcher\Event\Form\FormAlterEvent $event */
    $event = $this->manager
      ->getRegisteredEvent(HookEventDispatcherInterface::FORM_ALTER);
    self::assertEquals($form, $event
      ->getForm());
    self::assertEquals($formState, $event
      ->getFormState());
    self::assertEquals($formId, $event
      ->getFormId());
    $newForm = [
      'NewForm',
    ];
    $event
      ->setForm($newForm);
    self::assertEquals($newForm, $event
      ->getForm());
  }

  /**
   * Test FormBaseAlterEvent.
   */
  public function testFormBaseAlterEvent() {
    $baseFormId = 'test_base_form';
    $form = [
      'test' => 'form',
    ];
    $buildInfo = [
      'base_form_id' => $baseFormId,
    ];
    $formState = $this
      ->createMock(FormStateInterface::class);
    $formState
      ->method('getBuildInfo')
      ->willReturn($buildInfo);
    $formId = 'test_form';
    $this->manager
      ->setMaxEventCount(3);
    hook_event_dispatcher_form_alter($form, $formState, $formId);

    /** @var \Drupal\hook_event_dispatcher\Event\Form\FormBaseAlterEvent $event */
    $event = $this->manager
      ->getRegisteredEvent("hook_event_dispatcher.form_base_{$baseFormId}.alter");
    self::assertEquals($form, $event
      ->getForm());
    self::assertEquals($formState, $event
      ->getFormState());
    self::assertEquals($formId, $event
      ->getFormId());
    self::assertEquals($baseFormId, $event
      ->getBaseFormId());
  }

  /**
   * Test FormIdAlterEvent.
   */
  public function testFormIdAlterEvent() {
    $form = [
      'test' => 'form',
    ];
    $formState = $this
      ->createMock(FormStateInterface::class);
    $formId = 'test_form';
    $this->manager
      ->setMaxEventCount(2);
    hook_event_dispatcher_form_alter($form, $formState, $formId);

    /** @var \Drupal\hook_event_dispatcher\Event\Form\FormAlterEvent $event */
    $event = $this->manager
      ->getRegisteredEvent("hook_event_dispatcher.form_{$formId}.alter");
    self::assertEquals($form, $event
      ->getForm());
    self::assertEquals($formState, $event
      ->getFormState());
    self::assertEquals($formId, $event
      ->getFormId());
  }

  /**
   * Test WidgetFormAlterEvent.
   */
  public function testWidgetFormAlterEvent() {
    $element = [
      '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
      ->setMaxEventCount(2);
    hook_event_dispatcher_field_widget_form_alter($element, $formState, $context);

    /** @var \Drupal\hook_event_dispatcher\Event\Form\WidgetFormAlterEvent $event */
    $event = $this->manager
      ->getRegisteredEvent(HookEventDispatcherInterface::WIDGET_FORM_ALTER);
    self::assertEquals($element, $event
      ->getElement());
    $this
      ->assertEquals($formState, $event
      ->getFormState());
    $this
      ->assertEquals($context, $event
      ->getContext());
    $newElement = [
      'NewElement',
    ];
    $event
      ->setElement($newElement);
    $this
      ->assertEquals($newElement, $event
      ->getElement());
  }

  /**
   * Test WidgetTypeFormAlterEvent.
   */
  public function testWidgetTypeFormAlterEvent() {
    $element = [
      '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
      ->setMaxEventCount(2);
    hook_event_dispatcher_field_widget_form_alter($element, $formState, $context);

    /** @var \Drupal\hook_event_dispatcher\Event\Form\WidgetTypeFormAlterEvent $event */
    $event = $this->manager
      ->getRegisteredEvent("hook_event_dispatcher.widget_{$widgetType}.alter");
    $this
      ->assertEquals($element, $event
      ->getElement());
    $this
      ->assertEquals($formState, $event
      ->getFormState());
    $this
      ->assertEquals($context, $event
      ->getContext());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FormEventTest::$manager private property The manager.
FormEventTest::setUp public function Overrides UnitTestCase::setUp
FormEventTest::testFormAlterEvent public function Test FormAlterEvent.
FormEventTest::testFormBaseAlterEvent public function Test FormBaseAlterEvent.
FormEventTest::testFormIdAlterEvent public function Test FormIdAlterEvent.
FormEventTest::testWidgetFormAlterEvent public function Test WidgetFormAlterEvent.
FormEventTest::testWidgetTypeFormAlterEvent public function Test WidgetTypeFormAlterEvent.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.