You are here

public function RegistryTest::testSuggestionPreprocessFunctions in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Theme/RegistryTest.php \Drupal\KernelTests\Core\Theme\RegistryTest::testSuggestionPreprocessFunctions()

Tests the theme registry with suggestions.

File

core/tests/Drupal/KernelTests/Core/Theme/RegistryTest.php, line 109

Class

RegistryTest
Tests the behavior of the ThemeRegistry class.

Namespace

Drupal\KernelTests\Core\Theme

Code

public function testSuggestionPreprocessFunctions() {
  $theme_handler = \Drupal::service('theme_handler');
  \Drupal::service('theme_installer')
    ->install([
    'test_theme',
  ]);
  $extension_list = $this->container
    ->get('extension.list.module');
  assert($extension_list instanceof ModuleExtensionList);
  $registry_theme = new Registry($this->root, \Drupal::cache(), \Drupal::lock(), \Drupal::moduleHandler(), $theme_handler, \Drupal::service('theme.initialization'), 'test_theme', NULL, $extension_list);
  $registry_theme
    ->setThemeManager(\Drupal::theme());
  $suggestions = [
    '__kitten',
    '__flamingo',
  ];
  $expected_preprocess_functions = [
    'template_preprocess',
    'theme_test_preprocess_theme_test_preprocess_suggestions',
  ];
  $suggestion = '';
  $hook = 'theme_test_preprocess_suggestions';
  do {
    $hook .= "{$suggestion}";
    $expected_preprocess_functions[] = "test_theme_preprocess_{$hook}";
    $preprocess_functions = $registry_theme
      ->get()[$hook]['preprocess functions'];
    $this
      ->assertSame($expected_preprocess_functions, $preprocess_functions, "{$hook} has correct preprocess functions.");
  } while ($suggestion = array_shift($suggestions));
  $expected_preprocess_functions = [
    'template_preprocess',
    'theme_test_preprocess_theme_test_preprocess_suggestions',
    'test_theme_preprocess_theme_test_preprocess_suggestions',
    'test_theme_preprocess_theme_test_preprocess_suggestions__kitten',
  ];
  $preprocess_functions = $registry_theme
    ->get()['theme_test_preprocess_suggestions__kitten__bearcat']['preprocess functions'];
  $this
    ->assertSame($expected_preprocess_functions, $preprocess_functions, 'Suggestion implemented as a template correctly inherits preprocess functions.');
  $this
    ->assertTrue(isset($registry_theme
    ->get()['theme_test_preprocess_suggestions__kitten__meerkat__tarsier__moose']), 'Preprocess function with an unimplemented lower-level suggestion is added to the registry.');
}