class KernelTestHooksTest

Same name and namespace in other branches
  1. main core/tests/Drupal/KernelTests/Core/Hook/KernelTestHooksTest.php \Drupal\KernelTests\Core\Hook\KernelTestHooksTest

Tests that hook implementations in Kernel test classes are executed.

Attributes

#[Group('Hook')] #[RunTestsInSeparateProcesses]

Hierarchy

Expanded class hierarchy of KernelTestHooksTest

File

core/tests/Drupal/KernelTests/Core/Hook/KernelTestHooksTest.php, line 15

Namespace

Drupal\KernelTests\Core\Hook
View source
class KernelTestHooksTest extends KernelTestBase {
  
  /**
   * Invoke hooks and alter hooks and confirm implementations are executed.
   */
  public function testHookMethodExecution() : void {
    // Invoke a hook and confirm hookTestHook() executed.
    $values = \Drupal::moduleHandler()->invokeAll('test_hook');
    $this->assertSame([
      static::class . '::hookTestHook',
    ], $values);
    // Invoke alter hooks, with multiple types of related hooks and confirm that
    // both hookTestHookAlter() and hookTestVariantHookAlter() executed.
    $alterHooks = [
      'test_hook',
      'test_variant_hook',
    ];
    \Drupal::moduleHandler()->alter($alterHooks, $values);
    $this->assertSame([
      static::class . '::hookTestHook',
      static::class . '::hookTestHookAlter',
      static::class . '::hookTestVariantHookAlter',
    ], $values);
  }
  
  /**
   * Implements hook_test_hook().
   */
  public function hookTestHook() : string {
    return __METHOD__;
  }
  
  /**
   * Implements hook_test_hook_alter().
   */
  public function hookTestHookAlter(array &$values) : void {
    $values[] = __METHOD__;
  }
  
  /**
   * Implements hook_test_variant_hook_alter().
   */
  public function hookTestVariantHookAlter(array &$values) : void {
    $values[] = __METHOD__;
  }

}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.