View source
<?php
namespace Drupal\Tests\Core\Render;
use Drupal\Core\Security\UntrustedCallbackException;
class RendererCallbackTest extends RendererTestBase {
protected function setUp() : void {
parent::setUp();
$this->controllerResolver
->expects($this
->any())
->method('getControllerFromDefinition')
->willReturnArgument(0);
}
public function testCallback(array $render_array, $expected_deprecation) {
$this
->expectException(UntrustedCallbackException::class);
$this
->expectExceptionMessage($expected_deprecation);
$this->renderer
->renderRoot($render_array);
}
public function providerTestCallback() {
return [
'Procedural function pre render' => [
[
'#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',
],
'Static object method post render' => [
[
'#post_render' => [
'\\Drupal\\Tests\\Core\\Render\\RendererCallbackTest::renderCallback',
],
'#type' => 'container',
],
'Render #post_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\\RendererCallbackTest::renderCallback. See https://www.drupal.org/node/2966725',
],
'Object method access callback' => [
[
'#access_callback' => [
$this,
'renderCallback',
],
'#type' => 'container',
],
'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' => [
[
'#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',
],
'Invokable object access callback' => [
[
'#access_callback' => $this,
'#type' => 'container',
],
'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. See https://www.drupal.org/node/2966725',
],
];
}
public static function renderCallback($element = []) {
return $element;
}
public function __invoke($element = []) {
return $element;
}
}
function callback($element = []) {
return $element;
}