FieldWidgetThirdPartySettingsFormEventTest.php in Hook Event Dispatcher 8.2
File
modules/field_event_dispatcher/tests/src/Unit/Field/FieldWidgetThirdPartySettingsFormEventTest.php
View source
<?php
namespace Drupal\Tests\field_event_dispatcher\Unit\Field;
use Drupal;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\Plugin\Field\FieldWidget\StringTextfieldWidget;
use Drupal\Core\Form\FormState;
use Drupal\field_event_dispatcher\Event\Field\FieldWidgetThirdPartySettingsFormEvent;
use Drupal\hook_event_dispatcher\HookEventDispatcherInterface;
use Drupal\Tests\hook_event_dispatcher\Unit\HookEventDispatcherManagerSpy;
use PHPUnit\Framework\TestCase;
use function field_event_dispatcher_field_widget_third_party_settings_form;
class FieldWidgetThirdPartySettingsFormEventTest extends TestCase {
private $manager;
public function setUp() : void {
$builder = new ContainerBuilder();
$this->manager = new HookEventDispatcherManagerSpy();
$builder
->set('hook_event_dispatcher.manager', $this->manager);
$builder
->compile();
Drupal::setContainer($builder);
}
public function testGetMethodReturnTypes() : void {
$this->manager
->setEventCallbacks([
HookEventDispatcherInterface::FIELD_WIDGET_THIRD_PARTY_SETTINGS_FORM => static function (FieldWidgetThirdPartySettingsFormEvent $event) {
$event
->getPlugin();
$event
->getFieldDefinition();
$event
->getFormMode();
$event
->getForm();
$event
->getFormState();
$event
->getElements();
},
]);
$testFieldDefinition = new BaseFieldDefinition();
$testPlugin = new StringTextfieldWidget('test_widget', [], $testFieldDefinition, [], []);
$testFormState = new FormState();
field_event_dispatcher_field_widget_third_party_settings_form($testPlugin, $testFieldDefinition, 'form_mode', [], $testFormState);
self::assertTrue(TRUE);
}
public function testAddingElements() : void {
$elements = $expectedElements = [];
$this->manager
->setEventCallbacks([
HookEventDispatcherInterface::FIELD_WIDGET_THIRD_PARTY_SETTINGS_FORM => static function (FieldWidgetThirdPartySettingsFormEvent $event) {
$event
->addElements('test_module', [
'test' => [],
]);
},
]);
$testFieldDefinition = new BaseFieldDefinition();
$testPlugin = new StringTextfieldWidget('test_widget', [], $testFieldDefinition, [], []);
$testFormState = new FormState();
$elements = field_event_dispatcher_field_widget_third_party_settings_form($testPlugin, $testFieldDefinition, 'form_mode', [], $testFormState);
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::FIELD_WIDGET_THIRD_PARTY_SETTINGS_FORM);
self::assertSame($elements, $event
->getElements());
$expectedElements['test_module']['test'] = [];
self::assertSame($expectedElements, $elements);
}
}