You are here

public function ThemeSuggestionsAlterTest::testSpecificSuggestionsAlter in Drupal 9

Same name and namespace in other branches
  1. 8 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
    ->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'));
}