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 WorkspaceAccessTestHooks class (core/modules/workspaces/tests/modules/workspace_access_test/src/Hook/WorkspaceAccessTestHooks.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 workspace_access_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-3581548
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:
- 3581548-move-hook-to-kernel-test
changes, plain diff MR !15235
Comments
Comment #4
amateescu commentedSince no one picked up this one... :)
Comment #5
dcam commentedThe hook was copied accurately to the Kernel test.
The change from using the state service to keyValue is in line with changes that are happening to Core overall.
The workspace_access_test module has been deleted. I grepped Core for additional instances of the string "workspace_access_test". The only remaining ones are the keyValue set statements.
The test continues to pass after the change.
I don't think there's much else to say about this. LGTM.
Comment #8
catchCommitted/pushed to main and 11.x, thanks!