You are here

protected function AssertContentTrait::assertThemeOutput in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/simpletest/src/AssertContentTrait.php \Drupal\simpletest\AssertContentTrait::assertThemeOutput()

Asserts themed output.

Parameters

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

string $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: use \Drupal\Component\Utility\SafeMarkup::format() to embed variables in the message text, not t(). If left blank, a default message will be displayed.

string $group: (optional) The group this message is in, which is displayed in a column in test output. Use 'Debug' to indicate this is debugging output. Do not translate this string. Defaults to 'Other'; most tests do not override this default.

Return value

bool TRUE on pass, FALSE on fail.

5 calls to AssertContentTrait::assertThemeOutput()
FunctionsTest::testImage in core/modules/system/src/Tests/Theme/FunctionsTest.php
Tests theme_image().
FunctionsTest::testItemList in core/modules/system/src/Tests/Theme/FunctionsTest.php
Tests item-list.html.twig.
FunctionsTest::testLinks in core/modules/system/src/Tests/Theme/FunctionsTest.php
Tests links.html.twig.
ThemeTest::testAttributeMerging in core/modules/system/src/Tests/Theme/ThemeTest.php
Test attribute merging.
ThemeTest::testDrupalRenderChildren in core/modules/system/src/Tests/Theme/ThemeTest.php
Tests child element rendering for 'render element' theme hooks.

File

core/modules/simpletest/src/AssertContentTrait.php, line 875
Contains \Drupal\simpletest\AssertContentTrait.

Class

AssertContentTrait
Provides test methods to assert content.

Namespace

Drupal\simpletest

Code

protected function assertThemeOutput($callback, array $variables = array(), $expected = '', $message = '', $group = 'Other') {

  /** @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);
  });
  $this
    ->verbose('<hr />' . 'Result:' . '<pre>' . Html::escape(var_export($output, TRUE)) . '</pre>' . '<hr />' . 'Expected:' . '<pre>' . Html::escape(var_export($expected, TRUE)) . '</pre>' . '<hr />' . $output);
  if (!$message) {
    $message = '%callback rendered correctly.';
  }
  $message = format_string($message, array(
    '%callback' => 'theme_' . $callback . '()',
  ));
  return $this
    ->assertIdentical($output, $expected, $message, $group);
}