function AssertContentTrait::assertThemeOutput

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/AssertContentTrait.php \Drupal\KernelTests\AssertContentTrait::assertThemeOutput()
  2. 8.9.x core/tests/Drupal/KernelTests/AssertContentTrait.php \Drupal\KernelTests\AssertContentTrait::assertThemeOutput()
  3. 10 core/tests/Drupal/KernelTests/AssertContentTrait.php \Drupal\KernelTests\AssertContentTrait::assertThemeOutput()

Asserts themed output.

Parameters

string $callback: The name of the theme hook to invoke; e.g. 'links' for links.html.twig.

array $variables: An array of variables to pass to the theme function.

string $expected: The expected themed output string.

string $message: (optional) A message to display with the assertion. Do not translate messages with t(). Use double quotes and embed variables directly in message text, or use sprintf() if necessary. Avoid the use of \Drupal\Component\Render\FormattableMarkup unless you cast the object to a string. If left blank, a default message will be displayed.

6 calls to AssertContentTrait::assertThemeOutput()
FunctionsTest::testImage in core/modules/system/tests/src/Kernel/Theme/FunctionsTest.php
Tests theme_image().
FunctionsTest::testIndexedKeyedLinks in core/modules/system/tests/src/Kernel/Theme/FunctionsTest.php
Tests links.html.twig using links with indexed keys.
FunctionsTest::testItemList in core/modules/system/tests/src/Kernel/Theme/FunctionsTest.php
Tests item-list.html.twig.
FunctionsTest::testLinks in core/modules/system/tests/src/Kernel/Theme/FunctionsTest.php
Tests links.html.twig.
ThemeTest::testAttributeMerging in core/modules/system/tests/src/Kernel/Theme/ThemeTest.php
Tests attribute merging.

... See full list

File

core/tests/Drupal/KernelTests/AssertContentTrait.php, line 759

Class

AssertContentTrait
Provides test methods to assert content.

Namespace

Drupal\KernelTests

Code

protected function assertThemeOutput($callback, array $variables = [], $expected = '', $message = '') {
    
    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = \Drupal::service('renderer');
    // The string cast is necessary because theme functions return
    // MarkupInterface objects. This means we can assert that $expected
    // matches the theme output without having to worry about 0 == ''.
    $output = (string) $renderer->executeInRenderContext(new RenderContext(), function () use ($callback, $variables) {
        return \Drupal::theme()->render($callback, $variables);
    });
    if (!$message) {
        $message = '%callback rendered correctly.';
    }
    $message = new FormattableMarkup($message, [
        '%callback' => 'theme_' . $callback . '()',
    ]);
    $this->assertSame($expected, $output, (string) $message);
}

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