class WebformElementEventTest in Hook Event Dispatcher 8
Same name and namespace in other branches
- 8.2 modules/webform_event_dispatcher/tests/src/Unit/WebformElement/WebformElementEventTest.php \Drupal\Tests\webform_event_dispatcher\Unit\WebformElement\WebformElementEventTest
- 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
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\webform_event_dispatcher\Unit\WebformElement\WebformElementEventTest
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\WebformElementView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
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. | |
WebformElementEventTest:: |
private | property | The manager. | |
WebformElementEventTest:: |
public | function |
Overrides UnitTestCase:: |
|
WebformElementEventTest:: |
public | function | Test WebformElementAlterEvent. | |
WebformElementEventTest:: |
public | function | Test WebformElementInfoAlterEvent. | |
WebformElementEventTest:: |
public | function | Test WebformElementTypeAlterEvent. |