function HookAlterOrderTest::testReorderAlterMissingTarget
Tests #[ReorderHook] attributes with missing target.
There are different kinds of missing target:
- The target method to be reordered or removed may not exist.
- The hook being targeted may have no implementations.
- The target method exists, but it is registered to a different hook.
The expected behavior in these cases is that the reorder or remove attribute should have no effect, and especially not cause any errors.
However, for alter hooks, the last case is a bit special.
See also
\Drupal\KernelTests\Core\Hook\HookOrderTest::testReorderMissingTarget()
\Drupal\xyz_hook_order_test\Hook\XyzMissingTargetAlter
File
-
core/
tests/ Drupal/ KernelTests/ Core/ Hook/ HookAlterOrderTest.php, line 293
Class
- HookAlterOrderTest
- Tests Hook Alter Order.
Namespace
Drupal\KernelTests\Core\HookCode
public function testReorderAlterMissingTarget() : void {
// At the beginning, the xyz_hook_order_test is not installed, so no
// reordering is applied.
// This verifies that all implementations for this test are correctly
// declared and discovered.
$this->assertAlterCallOrder([
AMissingTargetAlter::class . '::testABAlter',
BMissingTargetAlter::class . '::testABAlterReorderedFirstByXyz',
BMissingTargetAlter::class . '::testABAlterRemovedByXyz',
], 'test_ab');
$this->assertAlterCallOrder([
BMissingTargetAlter::class . '::testBAlter',
BMissingTargetAlter::class . '::testBAlterReorderedFirstByXyz',
BMissingTargetAlter::class . '::testBAlterRemovedByXyz',
], 'test_b');
$this->assertAlterCallOrder([
AMissingTargetAlter::class . '::testASupertypeAlter',
AMissingTargetAlter::class . '::testASupertypeAlterReorderedFirstForBSubtypeByXyz',
AMissingTargetAlter::class . '::testASupertypeAlterRemovedForBSubtypeByXyz',
], 'test_a_supertype');
$this->assertAlterCallOrder([
BMissingTargetAlter::class . '::testBSubtypeAlter',
], 'test_b_subtype');
$this->assertAlterCallOrder([
AMissingTargetAlter::class . '::testASupertypeAlter',
AMissingTargetAlter::class . '::testASupertypeAlterReorderedFirstForBSubtypeByXyz',
AMissingTargetAlter::class . '::testASupertypeAlterRemovedForBSubtypeByXyz',
BMissingTargetAlter::class . '::testBSubtypeAlter',
], [
'test_a_supertype',
'test_b_subtype',
]);
// Install the module that has the reorder and remove hook attributes.
$this->enableModules([
'xyz_hook_order_test',
]);
$this->assertAlterCallOrder([
BMissingTargetAlter::class . '::testABAlterReorderedFirstByXyz',
AMissingTargetAlter::class . '::testABAlter',
], 'test_ab');
$this->assertAlterCallOrder([
BMissingTargetAlter::class . '::testBAlterReorderedFirstByXyz',
BMissingTargetAlter::class . '::testBAlter',
], 'test_b');
$this->assertAlterCallOrder([
AMissingTargetAlter::class . '::testASupertypeAlter',
AMissingTargetAlter::class . '::testASupertypeAlterReorderedFirstForBSubtypeByXyz',
AMissingTargetAlter::class . '::testASupertypeAlterRemovedForBSubtypeByXyz',
], 'test_a_supertype');
$this->assertAlterCallOrder([
BMissingTargetAlter::class . '::testBSubtypeAlter',
], 'test_b_subtype');
$this->assertAlterCallOrder([
AMissingTargetAlter::class . '::testASupertypeAlter',
AMissingTargetAlter::class . '::testASupertypeAlterReorderedFirstForBSubtypeByXyz',
AMissingTargetAlter::class . '::testASupertypeAlterRemovedForBSubtypeByXyz',
BMissingTargetAlter::class . '::testBSubtypeAlter',
], [
'test_a_supertype',
'test_b_subtype',
]);
// Uninstall the B module, which contains the reorder targets.
// Currently this causes a TypeError.
$this->expectException(\TypeError::class);
$old_request = \Drupal::request();
try {
$this->disableModules([
'bbb_hook_order_test',
]);
} finally {
// Restore a request and session, to avoid error during tearDown().
/** @var \Symfony\Component\HttpFoundation\RequestStack $request_stack */
$request_stack = $this->container
->get('request_stack');
$request_stack->push($old_request);
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.