class FormEventTest in Hook Event Dispatcher 8
Class FormEventTest.
@package Drupal\Tests\hook_event_dispatcher\Unit\Form
@group hook_event_dispatcher
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\hook_event_dispatcher\Unit\Form\FormEventTest
Expanded class hierarchy of FormEventTest
File
- tests/
src/ Unit/ Form/ FormEventTest.php, line 20
Namespace
Drupal\Tests\hook_event_dispatcher\Unit\FormView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FormEventTest:: |
private | property | The manager. | |
FormEventTest:: |
public | function |
Overrides UnitTestCase:: |
|
FormEventTest:: |
public | function | Test FormAlterEvent. | |
FormEventTest:: |
public | function | Test FormBaseAlterEvent. | |
FormEventTest:: |
public | function | Test FormIdAlterEvent. | |
FormEventTest:: |
public | function | Test WidgetFormAlterEvent. | |
FormEventTest:: |
public | function | Test WidgetTypeFormAlterEvent. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |