final class WidgetFormAlterEventTest in Hook Event Dispatcher 8.2
Same name and namespace in other branches
- 3.x modules/field_event_dispatcher/tests/src/Unit/Field/WidgetFormAlterEventTest.php \Drupal\Tests\field_event_dispatcher\Unit\Field\WidgetFormAlterEventTest
Class WidgetFormAlterEventTest.
@group field_event_dispatcher
Hierarchy
- class \Drupal\Tests\field_event_dispatcher\Unit\Field\WidgetFormAlterEventTest extends \PHPUnit\Framework\TestCase
Expanded class hierarchy of WidgetFormAlterEventTest
File
- modules/
field_event_dispatcher/ tests/ src/ Unit/ Field/ WidgetFormAlterEventTest.php, line 23
Namespace
Drupal\Tests\field_event_dispatcher\Unit\FieldView source
final class WidgetFormAlterEventTest extends TestCase {
/**
* The manager.
*
* @var \Drupal\Tests\hook_event_dispatcher\Unit\HookEventDispatcherManagerSpy
*/
private $manager;
/**
* {@inheritdoc}
*/
public function setUp() : void {
$builder = new ContainerBuilder();
$this->manager = new HookEventDispatcherManagerSpy();
$builder
->set('hook_event_dispatcher.manager', $this->manager);
$builder
->compile();
Drupal::setContainer($builder);
}
/**
* Test WidgetFormAlterEvent.
*/
public function testWidgetFormAlterEvent() : void {
$element = $expectedElement = [
'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_FORM_ALTER => static function (WidgetFormAlterEvent $event) {
$element =& $event
->getElement();
$element['other'] = 'key';
},
]);
$this->manager
->setMaxEventCount(2);
field_event_dispatcher_field_widget_form_alter($element, $formState, $context);
$expectedElement['other'] = 'key';
self::assertSame($expectedElement, $element);
/** @var \Drupal\field_event_dispatcher\Event\Field\WidgetFormAlterEvent $event */
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::WIDGET_FORM_ALTER);
self::assertSame($element, $event
->getElement());
self::assertSame($formState, $event
->getFormState());
self::assertSame($context, $event
->getContext());
}
/**
* Test WidgetTypeFormAlterEvent.
*/
public function testWidgetTypeFormAlterEvent() : void {
$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);
field_event_dispatcher_field_widget_form_alter($element, $formState, $context);
/** @var \Drupal\field_event_dispatcher\Event\Field\WidgetTypeFormAlterEvent $event */
$event = $this->manager
->getRegisteredEvent("hook_event_dispatcher.widget_{$widgetType}.alter");
self::assertSame($element, $event
->getElement());
self::assertSame($formState, $event
->getFormState());
self::assertSame($context, $event
->getContext());
}
/**
* Test WidgetMultivalueFormAlterEvent.
*/
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());
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
WidgetFormAlterEventTest:: |
private | property | The manager. | |
WidgetFormAlterEventTest:: |
public | function | ||
WidgetFormAlterEventTest:: |
public | function | Test WidgetFormAlterEvent. | |
WidgetFormAlterEventTest:: |
public | function | Test WidgetMultivalueFormAlterEvent. | |
WidgetFormAlterEventTest:: |
public | function | Test WidgetTypeFormAlterEvent. |