You are here

public function RegistryTest::testSuggestionPreprocessFunctions in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Theme/RegistryTest.php \Drupal\system\Tests\Theme\RegistryTest::testSuggestionPreprocessFunctions()

Tests the theme registry with suggestions.

File

core/modules/system/src/Tests/Theme/RegistryTest.php, line 113
Contains \Drupal\system\Tests\Theme\RegistryTest.

Class

RegistryTest
Tests the behavior of the ThemeRegistry class.

Namespace

Drupal\system\Tests\Theme

Code

public function testSuggestionPreprocessFunctions() {
  $theme_handler = \Drupal::service('theme_handler');
  $theme_handler
    ->install([
    'test_theme',
  ]);
  $registry_theme = new Registry(\Drupal::root(), \Drupal::cache(), \Drupal::lock(), \Drupal::moduleHandler(), $theme_handler, \Drupal::service('theme.initialization'), 'test_theme');
  $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
      ->assertIdentical($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__meerkat']['preprocess functions'];
  $this
    ->assertIdentical($expected_preprocess_functions, $preprocess_functions, 'Suggestion implemented as a function correctly inherits preprocess functions.');
  $preprocess_functions = $registry_theme
    ->get()['theme_test_preprocess_suggestions__kitten__bearcat']['preprocess functions'];
  $this
    ->assertIdentical($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.');
}