.../Drupal/Core/Plugin/PluginWithFormsTrait.php | 29 ++++++++++++ .../Drupal/Tests/Core/Block/BlockBaseTest.php | 35 -------------- .../Tests/Core/Plugin/PluginWithFormsTraitTest.php | 54 ++++++++++++++++++++++ 3 files changed, 83 insertions(+), 35 deletions(-) diff --git a/core/lib/Drupal/Core/Plugin/PluginWithFormsTrait.php b/core/lib/Drupal/Core/Plugin/PluginWithFormsTrait.php new file mode 100644 index 0000000..ce442f0 --- /dev/null +++ b/core/lib/Drupal/Core/Plugin/PluginWithFormsTrait.php @@ -0,0 +1,29 @@ +getPluginDefinition()['forms'][$operation])) { + return $this->getPluginDefinition()['forms'][$operation]; + } + elseif ($operation === 'configure') { + return static::class; + } + } + + /** + * {@inheritdoc} + */ + public function hasFormClass($operation) { + return !empty($this->getFormClass($operation)); + } + +} diff --git a/core/tests/Drupal/Tests/Core/Block/BlockBaseTest.php b/core/tests/Drupal/Tests/Core/Block/BlockBaseTest.php index 0e53b8f..541c0c1 100644 --- a/core/tests/Drupal/Tests/Core/Block/BlockBaseTest.php +++ b/core/tests/Drupal/Tests/Core/Block/BlockBaseTest.php @@ -3,7 +3,6 @@ namespace Drupal\Tests\Core\Block; use Drupal\block_test\Plugin\Block\TestBlockInstantiation; -use Drupal\Core\Block\BlockBase; use Drupal\Tests\UnitTestCase; /** @@ -43,38 +42,4 @@ public function testGetMachineNameSuggestion() { $this->assertEquals('uberawesome', $block_base->getMachineNameSuggestion()); } - /** - * @covers ::getFormClass - * @covers ::hasFormClass - * @dataProvider providerGetFormClass - */ - public function testGetFormClass(BlockBase $block_plugin, $operation, $expected_class) { - $this->assertSame($expected_class, $block_plugin->getFormClass($operation)); - $this->assertSame($expected_class !== NULL, $block_plugin->hasFormClass($operation)); - } - - /** - * @return array - */ - public function providerGetFormClass() { - $block_plugin_without_forms = new TestBlockInstantiation([], 'block_plugin_without_forms', [ - 'provider' => 'block_test', - ]); - // A block plugin that has a form defined for the 'poke' operation. - $block_plugin_with_forms = new TestBlockInstantiation([], 'block_plugin_with_forms', [ - 'provider' => 'block_test', - 'forms' => [ - 'poke' => static::class, - ], - ]); - return [ - 'block plugin without forms, "configure" operation' => [$block_plugin_without_forms, 'configure', TestBlockInstantiation::class], - 'block plugin without forms, "tickle" operation' => [$block_plugin_without_forms, 'tickle', NULL], - 'block plugin withut forms, "poke" operation' => [$block_plugin_without_forms, 'poke', NULL], - 'block plugin with forms, "configure" operation' => [$block_plugin_with_forms, 'configure', TestBlockInstantiation::class], - 'block plugin with forms, "tickle" operation' => [$block_plugin_with_forms, 'tickle', NULL], - 'block plugin with forms, "poke" operation' => [$block_plugin_with_forms, 'poke', static::class], - ]; - } - } diff --git a/core/tests/Drupal/Tests/Core/Plugin/PluginWithFormsTraitTest.php b/core/tests/Drupal/Tests/Core/Plugin/PluginWithFormsTraitTest.php new file mode 100644 index 0000000..2c96e14 --- /dev/null +++ b/core/tests/Drupal/Tests/Core/Plugin/PluginWithFormsTraitTest.php @@ -0,0 +1,54 @@ +assertSame($expected_class, $block_plugin->getFormClass($operation)); + $this->assertSame($expected_class !== NULL, $block_plugin->hasFormClass($operation)); + } + + /** + * @return array + */ + public function providerGetFormClass() { + $block_plugin_without_forms = new TestClass([], 'block_plugin_without_forms', [ + 'provider' => 'block_test', + ]); + // A block plugin that has a form defined for the 'poke' operation. + $block_plugin_with_forms = new TestClass([], 'block_plugin_with_forms', [ + 'provider' => 'block_test', + 'forms' => [ + 'poke' => static::class, + ], + ]); + return [ + 'block plugin without forms, "configure" operation' => [$block_plugin_without_forms, 'configure', TestClass::class], + 'block plugin without forms, "tickle" operation' => [$block_plugin_without_forms, 'tickle', NULL], + 'block plugin withut forms, "poke" operation' => [$block_plugin_without_forms, 'poke', NULL], + 'block plugin with forms, "configure" operation' => [$block_plugin_with_forms, 'configure', TestClass::class], + 'block plugin with forms, "tickle" operation' => [$block_plugin_with_forms, 'tickle', NULL], + 'block plugin with forms, "poke" operation' => [$block_plugin_with_forms, 'poke', static::class], + ]; + } + +} + +class TestClass extends PluginBase implements PluginWithFormsInterface { + use PluginWithFormsTrait; +}