function ThemeSuggestionsAlterTest::testSpecificSuggestionsAlter

Same name and namespace in other branches
  1. 9 core/modules/system/tests/src/Functional/Theme/ThemeSuggestionsAlterTest.php \Drupal\Tests\system\Functional\Theme\ThemeSuggestionsAlterTest::testSpecificSuggestionsAlter()
  2. 8.9.x 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 102

Class

ThemeSuggestionsAlterTest
Tests theme suggestion alter hooks.

Namespace

Drupal\Tests\system\Functional\Theme

Code

public function testSpecificSuggestionsAlter() : void {
  // Test that the default template is rendered.
  $this->drupalGet('theme-test/specific-suggestion-alter');
  $this->assertSession()
    ->pageTextContains('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->assertSession()
    ->pageTextContains('Template matching the specific theme call.');
  $this->assertSession()
    ->pageTextContains('theme_test_specific_suggestions__variant');
  // 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->assertSession()
    ->pageTextContains('Template overridden based on suggestion alter hook determined by the base hook.');
  $raw_content = $this->getSession()
    ->getPage()
    ->getContent();
  // Verify that a specific theme call is added to the suggestions array
  // before the suggestions alter hook.
  $this->assertLessThan(strpos($raw_content, 'theme_test_specific_suggestions__variant__foo'), strpos($raw_content, 'theme_test_specific_suggestions__variant'));
}

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