function ThemeSuggestionsAlterTest::testSpecificSuggestionsAlter

Same name in other branches
  1. 9 core/modules/system/tests/src/Functional/Theme/ThemeSuggestionsAlterTest.php \Drupal\Tests\system\Functional\Theme\ThemeSuggestionsAlterTest::testSpecificSuggestionsAlter()
  2. 10 core/modules/system/tests/src/Functional/Theme/ThemeSuggestionsAlterTest.php \Drupal\Tests\system\Functional\Theme\ThemeSuggestionsAlterTest::testSpecificSuggestionsAlter()
  3. 11.x core/modules/system/tests/src/Functional/Theme/ThemeSuggestionsAlterTest.php \Drupal\Tests\system\Functional\Theme\ThemeSuggestionsAlterTest::testSpecificSuggestionsAlter()

Tests that theme suggestion alter hooks work for specific theme calls.

File

core/modules/system/tests/src/Functional/Theme/ThemeSuggestionsAlterTest.php, line 96

Class

ThemeSuggestionsAlterTest
Tests theme suggestion alter hooks.

Namespace

Drupal\Tests\system\Functional\Theme

Code

public function testSpecificSuggestionsAlter() {
    // Test that the default template is rendered.
    $this->drupalGet('theme-test/specific-suggestion-alter');
    $this->assertText('Template for testing specific theme calls.');
    $this->config('system.theme')
        ->set('default', 'test_theme')
        ->save();
    // Test a specific theme call similar to '#theme' => 'node__article'.
    $this->drupalGet('theme-test/specific-suggestion-alter');
    $this->assertText('Template matching the specific theme call.');
    $this->assertText('theme_test_specific_suggestions__variant', 'Specific theme call is added to the suggestions array.');
    // Ensure that the base hook is used to determine the suggestion alter hook.
    \Drupal::service('module_installer')->install([
        'theme_suggestions_test',
    ]);
    $this->resetAll();
    $this->drupalGet('theme-test/specific-suggestion-alter');
    $this->assertText('Template overridden based on suggestion alter hook determined by the base hook.');
    $raw_content = $this->getSession()
        ->getPage()
        ->getContent();
    $this->assertTrue(strpos($raw_content, 'theme_test_specific_suggestions__variant') < strpos($raw_content, 'theme_test_specific_suggestions__variant__foo'), 'Specific theme call is added to the suggestions array before the suggestions alter hook.');
}

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