function HookAlterOrderTest::testReorderCrossHookAlter

Tests #[ReorderHook] targeting other hooks.

File

core/tests/Drupal/KernelTests/Core/Hook/HookAlterOrderTest.php, line 218

Class

HookAlterOrderTest
Tests Hook Alter Order.

Namespace

Drupal\KernelTests\Core\Hook

Code

public function testReorderCrossHookAlter() : void {
  $this->assertAlterCallOrder([
    // This method is reordered last only in combination with subtype hook.
ACrossHookReorderAlter::class . '::baseAlterLastIfSubtype',
    // Implementations that are not reordered appear in order of discovery.
ACrossHookReorderAlter::class . '::baseAlter',
    BCrossHookReorderAlter::class . '::baseAlter',
    // Ordering rules from #[Hook(.., order: ..)] are applied first, in
    // order of discovery.
ACrossHookReorderAlter::class . '::baseAlterLast',
    ACrossHookReorderAlter::class . '::baseAlterLastAlsoIfSubtype',
    // Ordering rules from #[ReorderHook(..)] are applied last.
BCrossHookReorderAlter::class . '::baseAlterLast',
  ], 'test_cross_hook_reorder_base');
  $this->assertAlterCallOrder([
    // This method is reordered last only in combination with base hook.
ACrossHookReorderAlter::class . '::subtypeAlterLastIfBaseHook',
    // Implementations that are not reordered appear in order of discovery.
ACrossHookReorderAlter::class . '::subtypeAlter',
    BCrossHookReorderAlter::class . '::subtypeAlter',
    // This implementation has #[Hook(.., order: Order::Last)].
ACrossHookReorderAlter::class . '::subtypeAlterLast',
  ], 'test_cross_hook_reorder_subtype');
  $this->assertAlterCallOrder([
    // Implementations that are not reordered appear in order of modules,
    // then order of hooks passed to ->alter(), then order of discovery.
    // We remove ReorderHook directives when the identifier and hook
    // targeted combination does not exist.
    // This method has a reorder targeting it, but it is using a hook that
    // does not point to this method so the reorder directive is dropped.
ACrossHookReorderAlter::class . '::baseAlterLastIfSubtype',
    ACrossHookReorderAlter::class . '::baseAlter',
    // This method has a reorder targeting it, but it is using a hook that
    // does not point to this method so the reorder directive is dropped.
ACrossHookReorderAlter::class . '::subtypeAlterLastIfBaseHook',
    ACrossHookReorderAlter::class . '::subtypeAlter',
    // These two methods appear in opposite order in the class, but appear
    // swapped, because one is for the base alter hook, the other for the
    // subtype alter hook.
BCrossHookReorderAlter::class . '::baseAlter',
    BCrossHookReorderAlter::class . '::subtypeAlter',
    // Ordering rules for the base hook are applied first.
    // At first those from #[Hook('..base..', order: ..)].
ACrossHookReorderAlter::class . '::baseAlterLast',
    // This method has a reorder targeting it, but it is using a hook that
    // does not point to this method so the reorder directive is dropped.
ACrossHookReorderAlter::class . '::baseAlterLastAlsoIfSubtype',
    BCrossHookReorderAlter::class . '::baseAlterLast',
    // Ordering rules for the subtype hook are applied last.
    // At first those from #[Hook('..subtype..', order: ..)].
ACrossHookReorderAlter::class . '::subtypeAlterLast',
  ], [
    'test_cross_hook_reorder_base',
    'test_cross_hook_reorder_subtype',
  ]);
}

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