From e55cf5ca784e09547944a15148dfb3745dd73bee Mon Sep 17 00:00:00 2001 From: Dave Long <dave@longwaveconsulting.com> Date: Wed, 12 Apr 2023 14:11:53 +0100 Subject: [PATCH] Issue #3352944 by Spokje: Fix PHPStan L1 errors "#pre_render/#lazy_builder callback '\Foo\Bar' at key 'n' is not trusted." --- .../element_info_test.module | 13 ++------- .../src/ElementInfoTestNumberBuilder.php | 28 +++++++++++++++++++ core/phpstan-baseline.neon | 15 ---------- .../Core/Render/Element/WeightTest.php | 5 ++-- .../Core/Render/RendererCallbackTest.php | 6 ++++ 5 files changed, 40 insertions(+), 27 deletions(-) create mode 100644 core/modules/system/tests/modules/element_info_test/src/ElementInfoTestNumberBuilder.php 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 74e472413766..d3a6a38d9b57 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 000000000000..ae4ad3bac47c --- /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 53662dd0a03d..daa99e8c091f 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 0f03ec2d783e..44607bcf4339 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 eabbb7f1c528..4462f987993c 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', ], -- GitLab