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\FieldFormatter\BasicStringFormatter;
use Drupal\Core\Form\FormState;
use Drupal\field_event_dispatcher\Event\Field\FieldFormatterThirdPartySettingsFormEvent;
use Drupal\hook_event_dispatcher\HookEventDispatcherInterface;
use Drupal\Tests\hook_event_dispatcher\Unit\HookEventDispatcherManagerSpy;
use PHPUnit\Framework\TestCase;
use function field_event_dispatcher_field_formatter_third_party_settings_form;
class FieldFormatterThirdPartySettingsFormEventTest 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_FORMATTER_THIRD_PARTY_SETTINGS_FORM => static function (FieldFormatterThirdPartySettingsFormEvent $event) {
$event
->getPlugin();
$event
->getFieldDefinition();
$event
->getViewMode();
$event
->getForm();
$event
->getFormState();
$event
->getElements();
},
]);
$testFieldDefinition = new BaseFieldDefinition();
$testPlugin = new BasicStringFormatter('test_formatter', [], $testFieldDefinition, [], 'label', 'view_mode', []);
$testFormState = new FormState();
field_event_dispatcher_field_formatter_third_party_settings_form($testPlugin, $testFieldDefinition, 'view_mode', [], $testFormState);
self::assertTrue(TRUE);
}
public function testAddingElements() : void {
$elements = $expectedElements = [];
$this->manager
->setEventCallbacks([
HookEventDispatcherInterface::FIELD_FORMATTER_THIRD_PARTY_SETTINGS_FORM => static function (FieldFormatterThirdPartySettingsFormEvent $event) {
$event
->addElements('test_module', [
'test' => [],
]);
},
]);
$testFieldDefinition = new BaseFieldDefinition();
$testPlugin = new BasicStringFormatter('test_formatter', [], $testFieldDefinition, [], 'label', 'view_mode', []);
$testFormState = new FormState();
$elements = field_event_dispatcher_field_formatter_third_party_settings_form($testPlugin, $testFieldDefinition, 'view_mode', [], $testFormState);
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::FIELD_FORMATTER_THIRD_PARTY_SETTINGS_FORM);
self::assertSame($elements, $event
->getElements());
$expectedElements['test_module']['test'] = [];
self::assertSame($expectedElements, $elements);
}
}