You are here

public function ThemeSuggestionsAlterTest::testExecutionOrder in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/system/tests/src/Functional/Theme/ThemeSuggestionsAlterTest.php \Drupal\Tests\system\Functional\Theme\ThemeSuggestionsAlterTest::testExecutionOrder()
  2. 9 core/modules/system/tests/src/Functional/Theme/ThemeSuggestionsAlterTest.php \Drupal\Tests\system\Functional\Theme\ThemeSuggestionsAlterTest::testExecutionOrder()

Tests execution order of theme suggestion alter hooks.

Hook hook_theme_suggestions_alter() should fire before hook_theme_suggestions_HOOK_alter() within an extension (module or theme).

File

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

Class

ThemeSuggestionsAlterTest
Tests theme suggestion alter hooks.

Namespace

Drupal\Tests\system\Functional\Theme

Code

public function testExecutionOrder() {

  // Install our test theme and module.
  $this
    ->config('system.theme')
    ->set('default', 'test_theme')
    ->save();
  \Drupal::service('module_installer')
    ->install([
    'theme_suggestions_test',
  ]);
  $this
    ->resetAll();

  // Send two requests so that we get all the messages we've set via
  // \Drupal\Core\Messenger\MessengerInterface::addStatus().
  $this
    ->drupalGet('theme-test/suggestion-alter');

  // Ensure that the order is first by extension, then for a given extension,
  // the hook-specific one after the generic one.
  $expected_order = [
    'theme_suggestions_test_theme_suggestions_alter() executed.',
    'theme_suggestions_test_theme_suggestions_theme_test_suggestions_alter() executed.',
    'theme_test_theme_suggestions_alter() executed for theme_test_suggestions.',
    'theme_test_theme_suggestions_theme_test_suggestions_alter() executed.',
    'test_theme_theme_suggestions_alter() executed.',
    'test_theme_theme_suggestions_theme_test_suggestions_alter() executed.',
  ];
  $content = preg_replace('/\\s+/', ' ', Xss::filter($this
    ->getSession()
    ->getPage()
    ->getContent(), []));
  $order = 0;
  foreach ($expected_order as $expected_string) {
    $this
      ->assertGreaterThan($order, strpos($content, $expected_string));
    $order = strpos($content, $expected_string);
  }
}