diff options
Diffstat (limited to 'core/tests')
-rw-r--r-- | core/tests/Drupal/BuildTests/Composer/Plugin/Unpack/Functional/UnpackRecipeTest.php (renamed from core/tests/Drupal/Tests/Composer/Plugin/Unpack/Functional/UnpackRecipeTest.php) | 2 | ||||
-rw-r--r-- | core/tests/Drupal/KernelTests/Core/Render/Element/PluginAlterTest.php | 25 | ||||
-rw-r--r-- | core/tests/Drupal/KernelTests/Core/Render/Element/WeightTest.php | 3 | ||||
-rw-r--r-- | core/tests/Drupal/Tests/Core/Render/Element/HtmlTagTest.php | 3 | ||||
-rw-r--r-- | core/tests/Drupal/Tests/Core/Render/Element/ModernRenderElementTest.php | 60 | ||||
-rw-r--r-- | core/tests/Drupal/Tests/Core/Render/Element/TableSelectTest.php | 5 | ||||
-rw-r--r-- | core/tests/Drupal/Tests/Core/Theme/Icon/IconTest.php | 3 |
7 files changed, 94 insertions, 7 deletions
diff --git a/core/tests/Drupal/Tests/Composer/Plugin/Unpack/Functional/UnpackRecipeTest.php b/core/tests/Drupal/BuildTests/Composer/Plugin/Unpack/Functional/UnpackRecipeTest.php index b74349256892..1397a78cf694 100644 --- a/core/tests/Drupal/Tests/Composer/Plugin/Unpack/Functional/UnpackRecipeTest.php +++ b/core/tests/Drupal/BuildTests/Composer/Plugin/Unpack/Functional/UnpackRecipeTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Drupal\Tests\Composer\Plugin\Unpack\Functional; +namespace Drupal\BuildTests\Composer\Plugin\Unpack\Functional; use Composer\InstalledVersions; use Composer\Util\Filesystem; diff --git a/core/tests/Drupal/KernelTests/Core/Render/Element/PluginAlterTest.php b/core/tests/Drupal/KernelTests/Core/Render/Element/PluginAlterTest.php index 7ad8afa75be6..c300cd4c019f 100644 --- a/core/tests/Drupal/KernelTests/Core/Render/Element/PluginAlterTest.php +++ b/core/tests/Drupal/KernelTests/Core/Render/Element/PluginAlterTest.php @@ -23,7 +23,7 @@ class PluginAlterTest extends KernelTestBase { $info_manager = $this->container->get('plugin.manager.element_info'); $this->assertArrayHasKey('weight', $info_manager->getDefinitions()); - // @see element_info_test_element_plugin_alter() + // @see ElementInfoTestHooks::elementPluginAlter(). $this->container->get('state')->set('hook_element_plugin_alter:remove_weight', TRUE); // The definition will be cached. $this->assertArrayHasKey('weight', $info_manager->getDefinitions()); @@ -33,4 +33,27 @@ class PluginAlterTest extends KernelTestBase { $this->assertArrayNotHasKey('weight', $info_manager->getDefinitions()); } + /** + * Tests hook_element_plugin_alter(). + */ + public function testPluginClassSwap(): void { + $info_manager = $this->container->get('plugin.manager.element_info'); + $test_details = [ + '#type' => 'details', + '#title' => 'Title', + '#description' => 'Description', + '#open' => TRUE, + ]; + + // @see ElementInfoTestHooks::elementPluginAlter(). + $expected = [ + 'class' => 'Drupal\element_info_test\Render\Element\Details', + 'provider' => 'element_info_test', + 'id' => 'details', + ]; + $this->assertEquals($expected, $info_manager->getDefinitions()['details']); + \Drupal::service('renderer')->renderRoot($test_details); + $this->assertArrayHasKey('#custom', $test_details); + } + } diff --git a/core/tests/Drupal/KernelTests/Core/Render/Element/WeightTest.php b/core/tests/Drupal/KernelTests/Core/Render/Element/WeightTest.php index 00b7948f2a81..e36da16d9028 100644 --- a/core/tests/Drupal/KernelTests/Core/Render/Element/WeightTest.php +++ b/core/tests/Drupal/KernelTests/Core/Render/Element/WeightTest.php @@ -8,6 +8,7 @@ use Drupal\Core\Form\FormState; use Drupal\Core\Render\Element\Number; use Drupal\Core\Render\Element\Select; use Drupal\Core\Render\Element\Weight; +use Drupal\Core\Render\ElementInfoManagerInterface; use Drupal\element_info_test\ElementInfoTestNumberBuilder; use Drupal\KernelTests\KernelTestBase; @@ -40,7 +41,7 @@ class WeightTest extends KernelTestBase { $form_state = new FormState(); $complete_form = []; - $element_object = new Weight([], 'weight', []); + $element_object = new Weight([], 'weight', [], elementInfoManager: $this->createStub(ElementInfoManagerInterface::class)); $info = $element_object->getInfo(); $element += $info; diff --git a/core/tests/Drupal/Tests/Core/Render/Element/HtmlTagTest.php b/core/tests/Drupal/Tests/Core/Render/Element/HtmlTagTest.php index d0c09e97fc53..8490a5c0876e 100644 --- a/core/tests/Drupal/Tests/Core/Render/Element/HtmlTagTest.php +++ b/core/tests/Drupal/Tests/Core/Render/Element/HtmlTagTest.php @@ -6,6 +6,7 @@ namespace Drupal\Tests\Core\Render\Element; use Drupal\Core\Render\Markup; use Drupal\Tests\Core\Render\RendererTestBase; +use Drupal\Core\Render\ElementInfoManagerInterface; use Drupal\Core\Render\Element\HtmlTag; /** @@ -18,7 +19,7 @@ class HtmlTagTest extends RendererTestBase { * @covers ::getInfo */ public function testGetInfo(): void { - $htmlTag = new HtmlTag([], 'test', 'test'); + $htmlTag = new HtmlTag([], 'test', 'test', elementInfoManager: $this->createStub(ElementInfoManagerInterface::class)); $info = $htmlTag->getInfo(); $this->assertArrayHasKey('#pre_render', $info); $this->assertArrayHasKey('#attributes', $info); diff --git a/core/tests/Drupal/Tests/Core/Render/Element/ModernRenderElementTest.php b/core/tests/Drupal/Tests/Core/Render/Element/ModernRenderElementTest.php new file mode 100644 index 000000000000..c91b74f8c0a4 --- /dev/null +++ b/core/tests/Drupal/Tests/Core/Render/Element/ModernRenderElementTest.php @@ -0,0 +1,60 @@ +<?php + +declare(strict_types=1); + +namespace Drupal\Tests\Core\Render\Element; + +use Drupal\Component\Plugin\Factory\FactoryInterface; +use Drupal\Core\Render\Element; +use Drupal\Core\Render\Element\Textfield; +use Drupal\Core\Render\ElementInfoManager; +use Drupal\Tests\UnitTestCase; + +/** + * @coversDefaultClass \Drupal\Core\Render\Element\RenderElementBase + * @group Render + */ +class ModernRenderElementTest extends UnitTestCase { + + public function testChildren(): void { + $factory = $this->createMock(FactoryInterface::class); + $elementInfoManager = new class ($factory) extends ElementInfoManager { + + public function __construct(protected $factory) {} + + }; + $factory->expects($this->any()) + ->method('createInstance') + ->willReturnCallback(fn () => new Textfield([], '', NULL, $elementInfoManager)); + // If the type is not given ::fromRenderable presumes "form" and uses the + // plugin discovery to find which class provides the form element. This + // test does not set up discovery so some type must be provided. + $element = ['#type' => 'ignored by the mock factory']; + $elementObject = $elementInfoManager->fromRenderable($element); + for ($i = 0; $i <= 2; $i++) { + $child = [ + '#type' => 'ignored by the mock factory', + '#test' => $i, + ]; + $elementObject->addChild("test$i", $child); + // addChild() takes the $child render array by reference and stores a + // reference to it in the render object. To avoid modifying the + // previously created render object when reusing the $child variable, + // unset() it to break the reference before reassigning. + unset($child); + } + foreach ([1 => ['test0', 'test1', 'test2'], 2 => ['test0', 'test2']] as $delta => $expectedChildrenKeys) { + $i = 0; + foreach ($elementObject->getChildren() as $name => $child) { + $this->assertSame($name, "test$i"); + $this->assertSame($i, $child->test); + $i += $delta; + } + $this->assertSame(Element::children($elementObject->toRenderable()), $expectedChildrenKeys); + // The first iteration tests removing an existing child. The second + // iteration tests removing a nonexistent child. + $elementObject->removeChild('test1'); + } + } + +} diff --git a/core/tests/Drupal/Tests/Core/Render/Element/TableSelectTest.php b/core/tests/Drupal/Tests/Core/Render/Element/TableSelectTest.php index fc58c1db4efe..7acfef4ca509 100644 --- a/core/tests/Drupal/Tests/Core/Render/Element/TableSelectTest.php +++ b/core/tests/Drupal/Tests/Core/Render/Element/TableSelectTest.php @@ -7,6 +7,7 @@ namespace Drupal\Tests\Core\Render\Element; use Drupal\Core\Form\FormState; use Drupal\Core\Link; use Drupal\Core\Render\Element\Tableselect; +use Drupal\Core\Render\ElementInfoManagerInterface; use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\Core\Url; use Drupal\Tests\UnitTestCase; @@ -25,7 +26,7 @@ class TableSelectTest extends UnitTestCase { $form_state = new FormState(); $complete_form = []; - $element_object = new Tableselect([], 'table_select', []); + $element_object = new Tableselect([], 'table_select', [], elementInfoManager: $this->createStub(ElementInfoManagerInterface::class)); $info = $element_object->getInfo(); $element += $info; @@ -50,7 +51,7 @@ class TableSelectTest extends UnitTestCase { $form_state = new FormState(); $complete_form = []; - $element_object = new Tableselect([], 'table_select', []); + $element_object = new Tableselect([], 'table_select', [], elementInfoManager: $this->createStub(ElementInfoManagerInterface::class)); $info = $element_object->getInfo(); $element += $info; diff --git a/core/tests/Drupal/Tests/Core/Theme/Icon/IconTest.php b/core/tests/Drupal/Tests/Core/Theme/Icon/IconTest.php index 2c67cf9193fb..d1a6e6433407 100644 --- a/core/tests/Drupal/Tests/Core/Theme/Icon/IconTest.php +++ b/core/tests/Drupal/Tests/Core/Theme/Icon/IconTest.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Drupal\Tests\Core\Theme\Icon; use Drupal\Core\DependencyInjection\ContainerBuilder; +use Drupal\Core\Render\ElementInfoManagerInterface; use Drupal\Core\Render\Element\Icon; use Drupal\Core\Template\Attribute; use Drupal\Core\Theme\Icon\IconDefinition; @@ -41,7 +42,7 @@ class IconTest extends UnitTestCase { * Test the Icon::getInfo method. */ public function testGetInfo(): void { - $icon = new Icon([], 'test', 'test'); + $icon = new Icon([], 'test', 'test', elementInfoManager: $this->createStub(ElementInfoManagerInterface::class)); $info = $icon->getInfo(); $this->assertArrayHasKey('#pre_render', $info); |