function ThemeTest::testThemeDataTypes

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

Test 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->assertIdentical((string) $example, $output->__toString());
        }
        elseif (is_string($output)) {
            $this->assertIdentical($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->assertIdentical($output, FALSE, '\\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.