You are here

public function ThemeTest::testThemeDataTypes in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/system/tests/src/Kernel/Theme/ThemeTest.php \Drupal\Tests\system\Kernel\Theme\ThemeTest::testThemeDataTypes()
  2. 9 core/modules/system/tests/src/Kernel/Theme/ThemeTest.php \Drupal\Tests\system\Kernel\Theme\ThemeTest::testThemeDataTypes()

Tests that ThemeManager renders the expected data types.

File

core/modules/system/tests/src/Kernel/Theme/ThemeTest.php, line 54

Class

ThemeTest
Tests low-level theme functions.

Namespace

Drupal\Tests\system\Kernel\Theme

Code

public function testThemeDataTypes() {

  // theme_test_false is an implemented theme hook so \Drupal::theme() service
  // should return a string or an object that implements MarkupInterface,
  // even though the theme function itself can return anything.
  $foos = [
    'null' => NULL,
    'false' => FALSE,
    'integer' => 1,
    'string' => 'foo',
    'empty_string' => '',
  ];
  foreach ($foos as $type => $example) {
    $output = \Drupal::theme()
      ->render('theme_test_foo', [
      'foo' => $example,
    ]);
    $this
      ->assertTrue($output instanceof MarkupInterface || is_string($output), new FormattableMarkup('\\Drupal::theme() returns an object that implements MarkupInterface or a string for data type @type.', [
      '@type' => $type,
    ]));
    if ($output instanceof MarkupInterface) {
      $this
        ->assertSame((string) $example, $output
        ->__toString());
    }
    elseif (is_string($output)) {
      $this
        ->assertSame('', $output, 'A string will be return when the theme returns an empty string.');
    }
  }

  // suggestionnotimplemented is not an implemented theme hook so \Drupal::theme() service
  // should return FALSE instead of a string.
  $output = \Drupal::theme()
    ->render([
    'suggestionnotimplemented',
  ], []);
  $this
    ->assertFalse($output, '\\Drupal::theme() returns FALSE when a hook suggestion is not implemented.');
}