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:

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

Command icon 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:

Comments

dcam created an issue. See original summary.

sapnil_biswas made their first commit to this issue’s fork.

sapnil_biswas’s picture

Status: Active » Needs review

marking this for review moved the hooks to kernel

smustgrave’s picture

Would say kinda a -1 for this. Personally not a fan of mixing hooks with the test. Hard to debug later

nicxvan’s picture

How so? One nice side effect is fewer modules to be discovered during extension discovery.

dcam’s picture

Status: Needs review » Reviewed & tested by the community

The 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 @see annotations reference the correct functions.

It looks good to me.

smustgrave’s picture

Will say that @dcam spoke on the side and this seems to already have committer buy in so no issue here.

  • catch committed c6d7e6b1 on 11.x
    task: #3581547 Move hooks from link_generation_test into the Kernel test...

  • catch committed bc777a38 on main
    task: #3581547 Move hooks from link_generation_test into the Kernel test...
catch’s picture

Version: main » 11.x-dev
Status: Reviewed & tested by the community » Fixed

Committed/pushed to main and 11.x, thanks!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.