Problem/Motivation
In the past, if we wanted to test the behavior of a hook function, then we needed to implement that hook inside of a "test module." Test modules are typically located in core/modules/*/tests/modules/ directories.
#3502432: Make hook testing with kernel tests very simple provided an alternative to creating test modules for Kernel tests. Now hooks may be created as functions directly in Kernel test classes. This reduces the amount of boilerplate necessary for testing hooks, couples the hook function tightly with its related test, and gives us an opportunity to remove some test modules from Core.
The LinkGenerationTestHooks class (core/modules/system/tests/modules/link_generation_test/src/Hook/LinkGenerationTestHooks.php) has a hook function that appears to only be used by a Kernel test. Move the hook function into its related test.
See the following for helpful information on making this change:
- The change record about Kernel test hooks
- #3502432: Make hook testing with kernel tests very simple
- This commit contains an example of a moved hook function from a similar issue
Proposed resolution
- Search for the test class or classes that use the link_generation_test module using any method you prefer, for example grep or an IDE's search.
- Verify that the hook is only used by one or more Kernel tests. Comment if you believe this is not true.
- Copy the hook function from the test module into the Kernel test class(es).
- Delete the hook function. If the Hook class is empty, delete the entire class. If the test module has no other code, delete the entire module.
Remaining tasks
User interface changes
Introduced terminology
API changes
Data model changes
Release notes snippet
Issue fork drupal-3581547
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
- 3581547-move-hooks-from
compare
- main
changes, plain diff MR !15254
Comments
Comment #4
sapnil_biswas commentedmarking this for review moved the hooks to kernel
Comment #5
smustgrave commentedWould say kinda a -1 for this. Personally not a fan of mixing hooks with the test. Hard to debug later
Comment #6
nicxvan commentedHow so? One nice side effect is fewer modules to be discovered during extension discovery.
Comment #7
dcam commentedThe hook was copied accurately from the former class to the test.
The test passes on GitLab and on my local environment.
The link_generation_test module has been deleted.
I grepped Core for the string "link_generation_test". All remaining instances are from the state set/get calls in the test.
The new
@seeannotations reference the correct functions.It looks good to me.
Comment #8
smustgrave commentedWill say that @dcam spoke on the side and this seems to already have committer buy in so no issue here.
Comment #11
catchCommitted/pushed to main and 11.x, thanks!