diff --git a/core/modules/system/tests/modules/element_info_test/element_info_test.module b/core/modules/system/tests/modules/element_info_test/element_info_test.module index 74e4724137664372b5f61fc98bc0b559914accb3..d3a6a38d9b57aa0a59cd4196fe41256cd5acf551 100644 --- a/core/modules/system/tests/modules/element_info_test/element_info_test.module +++ b/core/modules/system/tests/modules/element_info_test/element_info_test.module @@ -5,13 +5,15 @@ * Element info test. */ +use Drupal\element_info_test\ElementInfoTestNumberBuilder; + /** * Implements hook_element_info_alter(). */ function element_info_test_element_info_alter(array &$info) { $info['number'] += ['#pre_render' => []]; /* @see \Drupal\KernelTests\Core\Render\Element\WeightTest::testProcessWeightSelectMax() */ - $info['number']['#pre_render'][] = 'element_info_test_element_pre_render'; + $info['number']['#pre_render'][] = [ElementInfoTestNumberBuilder::class, 'preRender']; } /** @@ -22,12 +24,3 @@ function element_info_test_element_plugin_alter(array &$definitions) { unset($definitions['weight']); } } - -/** - * {@inheritdoc} - * - * @see \Drupal\KernelTests\Core\Render\Element\WeightTest::testProcessWeightSelectMax() - */ -function element_info_test_element_pre_render(array $element) { - return $element; -} diff --git a/core/modules/system/tests/modules/element_info_test/src/ElementInfoTestNumberBuilder.php b/core/modules/system/tests/modules/element_info_test/src/ElementInfoTestNumberBuilder.php new file mode 100644 index 0000000000000000000000000000000000000000..ae4ad3bac47c359569b8fdfa6615e45ebf9b9fae --- /dev/null +++ b/core/modules/system/tests/modules/element_info_test/src/ElementInfoTestNumberBuilder.php @@ -0,0 +1,28 @@ +<?php + +namespace Drupal\element_info_test; + +use Drupal\Core\Security\TrustedCallbackInterface; + +/** + * Provides a trusted callback to alter the element_info_test number element. + * + * @see element_info_test_element_info_alter() + */ +class ElementInfoTestNumberBuilder implements TrustedCallbackInterface { + + /** + * {@inheritdoc} + */ + public static function trustedCallbacks() { + return ['preRender']; + } + + /** + * Sets element_info_test - #pre_render callback. + */ + public static function preRender(array $element): array { + return $element; + } + +} diff --git a/core/phpstan-baseline.neon b/core/phpstan-baseline.neon index 53662dd0a03dbcaf8c4138251e8602aa81e09634..daa99e8c091f008b7b1fcc6bef366795a2456b39 100644 --- a/core/phpstan-baseline.neon +++ b/core/phpstan-baseline.neon @@ -3380,11 +3380,6 @@ parameters: count: 1 path: tests/Drupal/KernelTests/Core/KeyValueStore/GarbageCollectionTest.php - - - message: "#^\\#pre_render callback 'element_info_test…' at key '1' is not trusted\\.$#" - count: 1 - path: tests/Drupal/KernelTests/Core/Render/Element/WeightTest.php - - message: "#^@covers value drupal_rewrite_settings\\(\\) references an invalid class or function\\.$#" count: 1 @@ -3830,16 +3825,6 @@ parameters: count: 1 path: tests/Drupal/Tests/Core/Plugin/TestPluginManager.php - - - message: "#^\\#lazy_builder callback '\\\\\\\\Drupal\\\\\\\\Tests\\\\\\\\Core…' at key '0' is not trusted\\.$#" - count: 1 - path: tests/Drupal/Tests/Core/Render/RendererCallbackTest.php - - - - message: "#^\\#pre_render callback '\\\\\\\\Drupal\\\\\\\\Tests\\\\\\\\Core…' at key '0' is not trusted\\.$#" - count: 1 - path: tests/Drupal/Tests/Core/Render/RendererCallbackTest.php - - message: "#^Missing call to parent\\:\\:setUp\\(\\) method\\.$#" count: 1 diff --git a/core/tests/Drupal/KernelTests/Core/Render/Element/WeightTest.php b/core/tests/Drupal/KernelTests/Core/Render/Element/WeightTest.php index 0f03ec2d783e96d94d21d725b6a206afeddf668c..44607bcf4339b386db9c8ad65c6de33dbee7db59 100644 --- a/core/tests/Drupal/KernelTests/Core/Render/Element/WeightTest.php +++ b/core/tests/Drupal/KernelTests/Core/Render/Element/WeightTest.php @@ -6,6 +6,7 @@ use Drupal\Core\Render\Element\Number; use Drupal\Core\Render\Element\Select; use Drupal\Core\Render\Element\Weight; +use Drupal\element_info_test\ElementInfoTestNumberBuilder; use Drupal\KernelTests\KernelTestBase; /** @@ -117,8 +118,8 @@ public function testProcessWeightSelectMax() { '#pre_render' => [ [Number::class, 'preRenderNumber'], // The custom callback is appended. - /* @see element_info_test_element_info_alter() */ - 'element_info_test_element_pre_render', + /* @see \Drupal\element_info_test\ElementInfoTestNumberBuilder::preRender */ + [ElementInfoTestNumberBuilder::class, 'preRender'], ], ]); } diff --git a/core/tests/Drupal/Tests/Core/Render/RendererCallbackTest.php b/core/tests/Drupal/Tests/Core/Render/RendererCallbackTest.php index eabbb7f1c52823299e977936854f692a1135c391..4462f987993c4fa99d1944ff530d0269d03dabaf 100644 --- a/core/tests/Drupal/Tests/Core/Render/RendererCallbackTest.php +++ b/core/tests/Drupal/Tests/Core/Render/RendererCallbackTest.php @@ -42,6 +42,9 @@ public function testCallback(array $render_array, $expected_deprecation) { public function providerTestCallback() { return [ 'Procedural function pre render' => [ + // We specifically test an untrusted callback here. We need to let + // PHPStan ignore it. + // @phpstan-ignore-next-line ['#pre_render' => ['\Drupal\Tests\Core\Render\callback'], '#type' => 'container'], 'Render #pre_render callbacks must be methods of a class that implements \Drupal\Core\Security\TrustedCallbackInterface or be an anonymous function. The callback was \Drupal\Tests\Core\Render\callback. See https://www.drupal.org/node/2966725', ], @@ -60,6 +63,9 @@ public function providerTestCallback() { 'Render #access_callback callbacks must be methods of a class that implements \Drupal\Core\Security\TrustedCallbackInterface or be an anonymous function. The callback was Drupal\Tests\Core\Render\RendererCallbackTest::renderCallback. See https://www.drupal.org/node/2966725', ], 'Procedural function lazy builder' => [ + // We specifically test an untrusted callback here. We need to let + // PHPStan ignore it. + // @phpstan-ignore-next-line ['#lazy_builder' => ['\Drupal\Tests\Core\Render\callback', []]], 'Render #lazy_builder callbacks must be methods of a class that implements \Drupal\Core\Security\TrustedCallbackInterface or be an anonymous function. The callback was \Drupal\Tests\Core\Render\callback. See https://www.drupal.org/node/2966725', ],