You are here

class WebformElementEventTest in Hook Event Dispatcher 8

Same name and namespace in other branches
  1. 8.2 modules/webform_event_dispatcher/tests/src/Unit/WebformElement/WebformElementEventTest.php \Drupal\Tests\webform_event_dispatcher\Unit\WebformElement\WebformElementEventTest
  2. 3.x modules/webform_event_dispatcher/tests/src/Unit/WebformElement/WebformElementEventTest.php \Drupal\Tests\webform_event_dispatcher\Unit\WebformElement\WebformElementEventTest

Class WebformElementEventTest.

@package Drupal\Tests\webform_event_dispatcher\Unit\Element

@group hook_event_dispatcher

Hierarchy

Expanded class hierarchy of WebformElementEventTest

File

modules/webform_event_dispatcher/tests/src/Unit/WebformElement/WebformElementEventTest.php, line 20

Namespace

Drupal\Tests\webform_event_dispatcher\Unit\WebformElement
View source
class WebformElementEventTest 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();
    $this->manager
      ->setMaxEventCount(2);
    $builder
      ->set('hook_event_dispatcher.manager', $this->manager);
    $builder
      ->compile();
    \Drupal::setContainer($builder);
  }

  /**
   * Test WebformElementInfoAlterEvent.
   */
  public function testWebformElementInfoAlterEvent() {
    $definitions = [
      'textfield' => [
        'id' => 'textfield',
      ],
    ];
    $alters = [
      'textfield' => [
        '#test' => 'test',
      ],
    ];
    $expectedDefinitions = array_merge_recursive($definitions, $alters);

    // Create event subscriber to alter element info.
    $this->manager
      ->setEventCallbacks([
      'hook_event_dispatcher.webform.element.info.alter' => function (WebformElementInfoAlterEvent $event) {
        $definitions =& $event
          ->getDefinitions();
        $definitions['textfield']['#test'] = 'test';
      },
    ]);
    webform_event_dispatcher_webform_element_info_alter($definitions);

    /** @var \Drupal\webform_event_dispatcher\Event\WebformElement\WebformElementInfoAlterEvent $event */
    $event = $this->manager
      ->getRegisteredEvent('hook_event_dispatcher.webform.element.info.alter');
    self::assertInstanceOf(WebformElementInfoAlterEvent::class, $event);
    self::assertSame($expectedDefinitions, $event
      ->getDefinitions());
  }

  /**
   * Test WebformElementAlterEvent.
   */
  public function testWebformElementAlterEvent() {
    $element = [
      '#type' => 'textfield',
    ];
    $alters = [
      '#test' => 'test',
    ];
    $expectedElement = array_merge($element, $alters);
    $formState = $this
      ->createMock(FormStateInterface::class);
    $context = [
      'form' => [
        '#webform_id' => 'test_form',
      ],
    ];

    // Create event subscriber to alter element.
    $this->manager
      ->setEventCallbacks([
      'hook_event_dispatcher.webform.element.alter' => function (WebformElementAlterEvent $event) {
        $element =& $event
          ->getElement();
        $element['#test'] = 'test';
      },
    ]);
    webform_event_dispatcher_webform_element_alter($element, $formState, $context);

    /** @var \Drupal\webform_event_dispatcher\Event\WebformElement\WebformElementAlterEvent $event */
    $event = $this->manager
      ->getRegisteredEvent('hook_event_dispatcher.webform.element.alter');
    self::assertInstanceOf(WebformElementAlterEvent::class, $event);
    self::assertSame($expectedElement, $event
      ->getElement());
    self::assertSame($formState, $event
      ->getFormState());
    self::assertSame($context, $event
      ->getContext());
  }

  /**
   * Test WebformElementTypeAlterEvent.
   */
  public function testWebformElementTypeAlterEvent() {
    $elementType = 'textfield';
    $element = [
      '#type' => $elementType,
    ];
    $alters = [
      '#test' => 'test',
    ];
    $expectedElement = array_merge($element, $alters);
    $formState = $this
      ->createMock(FormStateInterface::class);
    $context = [
      'form' => [
        '#webform_id' => 'test_form',
      ],
    ];

    // Create event subscriber to alter element of given type.
    $this->manager
      ->setEventCallbacks([
      "hook_event_dispatcher.webform.element_{$elementType}.alter" => function (WebformElementAlterEvent $event) {
        $element =& $event
          ->getElement();
        $element['#test'] = 'test';
      },
    ]);
    webform_event_dispatcher_webform_element_alter($element, $formState, $context);

    /** @var \Drupal\webform_event_dispatcher\Event\WebformElement\WebformElementTypeAlterEvent $event */
    $event = $this->manager
      ->getRegisteredEvent("hook_event_dispatcher.webform.element_{$elementType}.alter");
    self::assertInstanceOf(WebformElementTypeAlterEvent::class, $event);
    self::assertSame($expectedElement, $event
      ->getElement());
    self::assertSame($elementType, $event
      ->getElementType());
    self::assertSame($formState, $event
      ->getFormState());
    self::assertSame($context, $event
      ->getContext());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.
WebformElementEventTest::$manager private property The manager.
WebformElementEventTest::setUp public function Overrides UnitTestCase::setUp
WebformElementEventTest::testWebformElementAlterEvent public function Test WebformElementAlterEvent.
WebformElementEventTest::testWebformElementInfoAlterEvent public function Test WebformElementInfoAlterEvent.
WebformElementEventTest::testWebformElementTypeAlterEvent public function Test WebformElementTypeAlterEvent.