class RendererCallbackTest

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Render/RendererCallbackTest.php \Drupal\Tests\Core\Render\RendererCallbackTest
  2. 8.9.x core/tests/Drupal/Tests/Core/Render/RendererCallbackTest.php \Drupal\Tests\Core\Render\RendererCallbackTest
  3. 11.x core/tests/Drupal/Tests/Core/Render/RendererCallbackTest.php \Drupal\Tests\Core\Render\RendererCallbackTest

@coversDefaultClass \Drupal\Core\Render\Renderer @group Render

Hierarchy

Expanded class hierarchy of RendererCallbackTest

File

core/tests/Drupal/Tests/Core/Render/RendererCallbackTest.php, line 13

Namespace

Drupal\Tests\Core\Render
View source
class RendererCallbackTest extends RendererTestBase {
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        $this->callableResolver
            ->expects($this->any())
            ->method('getCallableFromDefinition')
            ->willReturnArgument(0);
    }
    
    /**
     * Tests the expected deprecations are triggered by Renderer::doCallback().
     *
     * @param array $render_array
     *   The render array with a callback.
     * @param string $expected_deprecation
     *   The expected deprecation message triggered whilst rendering.
     *
     * @dataProvider providerTestCallback
     */
    public function testCallback(array $render_array, string $expected_deprecation) : void {
        $this->expectException(UntrustedCallbackException::class);
        $this->expectExceptionMessage($expected_deprecation);
        $this->renderer
            ->renderRoot($render_array);
    }
    
    /**
     * Data provider for testCallback().
     */
    public static function providerTestCallback() : array {
        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',
            ],
            'Static object method post render' => [
                // We specifically test an invalid callback here. We need to let PHPStan
                // ignore it.
                // @phpstan-ignore-next-line
[
                    '#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' => [
                // We specifically test an invalid callback here. We need to let PHPStan
                // ignore it.
                // @phpstan-ignore-next-line
[
                    '#access_callback' => [
                        new static('test'),
                        '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' => [
                // 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',
            ],
            'Invokable object access callback' => [
                [
                    '#access_callback' => new static('test'),
                    '#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',
            ],
        ];
    }
    
    /**
     * A test render callback.
     */
    public static function renderCallback($element = []) {
        return $element;
    }
    
    /**
     * Implements magic method as a render callback.
     */
    public function __invoke($element = []) {
        return $element;
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
RandomGeneratorTrait::randomStringValidate Deprecated public function Callback for random string validation.
RendererCallbackTest::providerTestCallback public static function Data provider for testCallback().
RendererCallbackTest::renderCallback public static function A test render callback.
RendererCallbackTest::setUp protected function Overrides RendererTestBase::setUp
RendererCallbackTest::testCallback public function Tests the expected deprecations are triggered by Renderer::doCallback().
RendererCallbackTest::__invoke public function Implements magic method as a render callback.
RendererTestBase::$cacheContextsManager protected property
RendererTestBase::$cacheFactory protected property
RendererTestBase::$callableResolver protected property The mocked controller resolver.
RendererTestBase::$currentUserRole protected property The simulated "current" user role, for use in tests with cache contexts.
RendererTestBase::$datetimeTime protected property System time service.
RendererTestBase::$elementInfo protected property The mocked element info.
RendererTestBase::$memoryCache protected property
RendererTestBase::$placeholderGenerator protected property The tested placeholder generator. 1
RendererTestBase::$renderCache protected property The tested render cache.
RendererTestBase::$renderer protected property The tested renderer.
RendererTestBase::$rendererConfig protected property The mocked renderer configuration.
RendererTestBase::$requestStack protected property
RendererTestBase::$themeManager protected property The mocked theme manager.
RendererTestBase::assertRenderCacheItem protected function Asserts a render cache item.
RendererTestBase::randomContextValue protected static function Generates a random context value for the placeholder tests.
RendererTestBase::setUpMemoryCache protected function Sets up a memory-based render cache back-end.
RendererTestBase::setUpRequest protected function Sets up a request object on the request stack.
RendererTestBase::setUpUnusedCache protected function Sets up a render cache back-end that is asserted to be never used.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUpBeforeClass public static function
UnitTestCase::__get public function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.