Verified Commit e55cf5ca authored by Dave Long's avatar Dave Long
Browse files

Issue #3352944 by Spokje: Fix PHPStan L1 errors "#pre_render/#lazy_builder...

Issue #3352944 by Spokje: Fix PHPStan L1 errors "#pre_render/#lazy_builder callback '\Foo\Bar' at key 'n' is not trusted."
parent cc5e3477
Loading
Loading
Loading
Loading
+3 −10
Original line number Diff line number Diff line
@@ -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;
}
+28 −0
Original line number Diff line number Diff line
<?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;
  }

}
+0 −15
Original line number Diff line number Diff line
@@ -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
+3 −2
Original line number Diff line number Diff line
@@ -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'],
      ],
    ]);
  }
+6 −0
Original line number Diff line number Diff line
@@ -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',
      ],