function ThemeTest::testThemeDataTypes

Same name and namespace in other branches
  1. 11.x core/modules/system/tests/src/Kernel/Theme/ThemeTest.php \Drupal\Tests\system\Kernel\Theme\ThemeTest::testThemeDataTypes()
  2. 10 core/modules/system/tests/src/Kernel/Theme/ThemeTest.php \Drupal\Tests\system\Kernel\Theme\ThemeTest::testThemeDataTypes()
  3. 9 core/modules/system/tests/src/Kernel/Theme/ThemeTest.php \Drupal\Tests\system\Kernel\Theme\ThemeTest::testThemeDataTypes()
  4. 8.9.x 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 56

Class

ThemeTest
Tests low-level theme functions.

Namespace

Drupal\Tests\system\Kernel\Theme

Code

public function testThemeDataTypes() : void {
  // 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.
  $types = [
    'null' => NULL,
    'false' => FALSE,
    'integer' => 1,
    'string' => 'foo',
    'empty_string' => '',
  ];
  foreach ($types as $type => $example) {
    $output = \Drupal::theme()->render('theme_test_foo', [
      'foo' => $example,
    ]);
    $this->assertTrue($output instanceof MarkupInterface || is_string($output), "\\Drupal::theme() returns an object that implements MarkupInterface or a string for data 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.');
    }
  }
  // suggestion_not_implemented is not an implemented theme hook so
  // \Drupal::theme() service should return FALSE instead of a string.
  $output = \Drupal::theme()->render([
    'suggestion_not_implemented',
  ], []);
  $this->assertFalse($output, '\\Drupal::theme() returns FALSE when a hook suggestion is not implemented.');
}

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