You are here

public function ThemeTest::testAttributeMerging in Drupal 8

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

Test attribute merging.

Render arrays that use a render element and templates (and hence call template_preprocess()) must ensure the attributes at different occasions are all merged correctly:

  • $variables['attributes'] as passed in to the theme hook implementation.
  • the render element's #attributes
  • any attributes set in the template's preprocessing function

File

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

Class

ThemeTest
Tests low-level theme functions.

Namespace

Drupal\Tests\system\Kernel\Theme

Code

public function testAttributeMerging() {
  $theme_test_render_element = [
    'elements' => [
      '#attributes' => [
        'data-foo' => 'bar',
      ],
    ],
    'attributes' => [
      'id' => 'bazinga',
    ],
  ];
  $this
    ->assertThemeOutput('theme_test_render_element', $theme_test_render_element, '<div id="bazinga" data-foo="bar" data-variables-are-preprocessed></div>' . "\n");
}