You are here

public function RegistryTest::providerTestPostProcessExtension in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Core/Theme/RegistryTest.php \Drupal\Tests\Core\Theme\RegistryTest::providerTestPostProcessExtension()
  2. 9 core/tests/Drupal/Tests/Core/Theme/RegistryTest.php \Drupal\Tests\Core\Theme\RegistryTest::providerTestPostProcessExtension()

Provides test data to ::testPostProcessExtension().

File

core/tests/Drupal/Tests/Core/Theme/RegistryTest.php, line 220

Class

RegistryTest
@coversDefaultClass \Drupal\Core\Theme\Registry @group Theme

Namespace

Drupal\Tests\Core\Theme

Code

public function providerTestPostProcessExtension() {

  // This is test data for unit testing
  // \Drupal\Core\Theme\Registry::postProcessExtension(), not what happens
  // before it. Therefore, for all test data:
  // - Explicitly defined hooks also come with explicitly defined preprocess
  //   functions, because those are collected in
  //   \Drupal\Core\Theme\Registry::processExtension().
  // - Explicitly defined hooks that set a 'base hook' also have
  //   'incomplete preprocess functions' set to TRUE, since that is done in
  //   \Drupal\Core\Theme\Registry::processExtension().
  $data = [];

  // Test the discovery of suggestions via the presence of preprocess
  // functions that follow the "__" naming pattern.
  $data['base_hook_with_auto-discovered_suggestions'] = [
    'defined_functions' => [
      'test_preprocess_test_hook__suggestion',
      'test_preprocess_test_hook__suggestion__another',
    ],
    'hooks' => [
      'test_hook' => [
        'preprocess functions' => [
          'explicit_preprocess_test_hook',
        ],
      ],
    ],
    'expected' => [
      'test_hook' => [
        'preprocess functions' => [
          'explicit_preprocess_test_hook',
        ],
      ],
      'test_hook__suggestion' => [
        'preprocess functions' => [
          'explicit_preprocess_test_hook',
          'test_preprocess_test_hook__suggestion',
        ],
        'base hook' => 'test_hook',
      ],
      'test_hook__suggestion__another' => [
        'preprocess functions' => [
          'explicit_preprocess_test_hook',
          'test_preprocess_test_hook__suggestion',
          'test_preprocess_test_hook__suggestion__another',
        ],
        'base hook' => 'test_hook',
      ],
    ],
  ];

  // Test that suggestions following the "__" naming pattern can also be
  // explicitly defined in hook_theme(), such as 'field__node__title' defined
  // in node_theme().
  $data['base_hook_with_explicit_suggestions'] = [
    'defined_functions' => [],
    'hooks' => [
      'test_hook' => [
        'preprocess functions' => [
          'explicit_preprocess_test_hook',
        ],
      ],
      'test_hook__suggestion__another' => [
        'base hook' => 'test_hook',
        'preprocess functions' => [
          'explicit_preprocess_test_hook__suggestion__another',
        ],
        'incomplete preprocess functions' => TRUE,
      ],
    ],
    'expected' => [
      'test_hook' => [
        'preprocess functions' => [
          'explicit_preprocess_test_hook',
        ],
      ],
      'test_hook__suggestion__another' => [
        'preprocess functions' => [
          'explicit_preprocess_test_hook',
          'explicit_preprocess_test_hook__suggestion__another',
        ],
        'base hook' => 'test_hook',
      ],
    ],
  ];

  // Same as above, but also test that a preprocess function for an
  // intermediary suggestion level gets discovered.
  $data['base_hook_with_explicit_suggestions_and_intermediary_preprocess_function'] = [
    'defined_functions' => [
      'test_preprocess_test_hook__suggestion',
    ],
    'hooks' => [
      'test_hook' => [
        'preprocess functions' => [
          'explicit_preprocess_test_hook',
        ],
      ],
      'test_hook__suggestion__another' => [
        'base hook' => 'test_hook',
        'preprocess functions' => [
          'explicit_preprocess_test_hook__suggestion__another',
        ],
        'incomplete preprocess functions' => TRUE,
      ],
    ],
    'expected' => [
      'test_hook' => [
        'preprocess functions' => [
          'explicit_preprocess_test_hook',
        ],
      ],
      'test_hook__suggestion' => [
        'preprocess functions' => [
          'explicit_preprocess_test_hook',
          'test_preprocess_test_hook__suggestion',
        ],
        'base hook' => 'test_hook',
      ],
      'test_hook__suggestion__another' => [
        'preprocess functions' => [
          'explicit_preprocess_test_hook',
          'test_preprocess_test_hook__suggestion',
          'explicit_preprocess_test_hook__suggestion__another',
        ],
        'base hook' => 'test_hook',
      ],
    ],
  ];

  // Test that hooks not following the "__" naming pattern can explicitly
  // specify a base hook, such as is done in
  // \Drupal\Core\Layout\LayoutPluginManager::getThemeImplementations().
  $data['child_hook_without_magic_naming'] = [
    'defined_functions' => [],
    'hooks' => [
      'test_hook' => [
        'preprocess functions' => [
          'explicit_preprocess_test_hook',
        ],
      ],
      'child_hook' => [
        'base hook' => 'test_hook',
        'preprocess functions' => [
          'explicit_preprocess_child_hook',
        ],
        'incomplete preprocess functions' => TRUE,
      ],
    ],
    'expected' => [
      'test_hook' => [
        'preprocess functions' => [
          'explicit_preprocess_test_hook',
        ],
      ],
      'child_hook' => [
        'preprocess functions' => [
          'explicit_preprocess_test_hook',
          'explicit_preprocess_child_hook',
        ],
        'base hook' => 'test_hook',
      ],
    ],
  ];

  // Same as above, but also test that such child hooks can also be extended
  // with magically named suggestions.
  $data['child_hook_with_suggestions'] = [
    'defined_functions' => [
      'test_preprocess_child_hook__suggestion',
      'test_preprocess_child_hook__suggestion__another',
    ],
    'hooks' => [
      'test_hook' => [
        'preprocess functions' => [
          'explicit_preprocess_test_hook',
        ],
      ],
      'child_hook' => [
        'base hook' => 'test_hook',
        'preprocess functions' => [
          'explicit_preprocess_child_hook',
        ],
        'incomplete preprocess functions' => TRUE,
      ],
    ],
    'expected' => [
      'test_hook' => [
        'preprocess functions' => [
          'explicit_preprocess_test_hook',
        ],
      ],
      'child_hook' => [
        'preprocess functions' => [
          'explicit_preprocess_test_hook',
          'explicit_preprocess_child_hook',
        ],
        'base hook' => 'test_hook',
      ],
      'child_hook__suggestion' => [
        'preprocess functions' => [
          'explicit_preprocess_test_hook',
          'explicit_preprocess_child_hook',
          'test_preprocess_child_hook__suggestion',
        ],
        'base hook' => 'test_hook',
      ],
      'child_hook__suggestion__another' => [
        'preprocess functions' => [
          'explicit_preprocess_test_hook',
          'explicit_preprocess_child_hook',
          'test_preprocess_child_hook__suggestion',
          'test_preprocess_child_hook__suggestion__another',
        ],
        'base hook' => 'test_hook',
      ],
    ],
  ];

  // Test that a suggestion following the "__" naming pattern can specify a
  // different base hook than what is implied by that pattern. Ensure that
  // preprocess functions from both the naming pattern and from 'base hook'
  // are collected.
  $data['suggestion_with_alternate_base_hook'] = [
    'defined_functions' => [
      'test_preprocess_test_hook__suggestion',
    ],
    'hooks' => [
      'test_hook' => [
        'preprocess functions' => [
          'explicit_preprocess_test_hook',
        ],
      ],
      'alternate_base_hook' => [
        'preprocess functions' => [
          'explicit_preprocess_alternate_base_hook',
        ],
      ],
      'test_hook__suggestion__another' => [
        'base hook' => 'alternate_base_hook',
        'preprocess functions' => [
          'explicit_preprocess_test_hook__suggestion__another',
        ],
        'incomplete preprocess functions' => TRUE,
      ],
    ],
    'expected' => [
      'test_hook' => [
        'preprocess functions' => [
          'explicit_preprocess_test_hook',
        ],
      ],
      'alternate_base_hook' => [
        'preprocess functions' => [
          'explicit_preprocess_alternate_base_hook',
        ],
      ],
      'test_hook__suggestion' => [
        'preprocess functions' => [
          'explicit_preprocess_test_hook',
          'test_preprocess_test_hook__suggestion',
        ],
        'base hook' => 'test_hook',
      ],
      'test_hook__suggestion__another' => [
        'preprocess functions' => [
          'explicit_preprocess_alternate_base_hook',
          'explicit_preprocess_test_hook',
          'test_preprocess_test_hook__suggestion',
          'explicit_preprocess_test_hook__suggestion__another',
        ],
        'base hook' => 'alternate_base_hook',
      ],
    ],
  ];

  // Test when a base hook is missing.
  $data['missing_base_hook'] = [
    'defined_functions' => [],
    'hooks' => [
      'child_hook' => [
        'base hook' => 'test_hook',
        'preprocess functions' => [
          'explicit_preprocess_child_hook',
        ],
        'incomplete preprocess functions' => TRUE,
      ],
    ],
    'expected' => [
      'child_hook' => [
        'preprocess functions' => [
          'explicit_preprocess_child_hook',
        ],
        'base hook' => 'test_hook',
      ],
    ],
  ];
  return $data;
}