Problem/Motivation

#3502432: Make hook testing with kernel tests very simple made it possible to implement hook methods inside Kernel test classes, so that test hooks did not need to be implemented in separate modules. This generally works for most cases.

However, when alter hooks are implemented, and there multiple related types of alter hooks invoked (such as form_alter, form_FORM_ID_alter, etc.), and there are methods implementing multiple of these types, the the kernel test class hook implementations are not executed.

This occurs because in ModuleHandler::getCombinedListeners(), as part of the process of ordering all the related hook implementations, the implementations are filtered by installed modules. Since the "module" that is defined for Kernel test hook implementations is core, the kernel hook implementations are filtered out.

Steps to reproduce

See test class in MR 15493 for an example of a kernel test class that has implements multiple "types" of the same basic hook.

Proposed resolution

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

Issue fork drupal-3585498

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

godotislate created an issue. See original summary.

godotislate’s picture

nicxvan’s picture

Good find, I wonder if it's better to add core here $modules = array_intersect(array_keys($this->moduleList), $modules); so it's not lost in the intersect.

It's closer to what we did in getHookImplementationList and doesn't require tracking core for every alter just to handle Kernel tests.

godotislate’s picture

A lot of tests failed when I just added core to modules in the MR where I first ran into this: https://git.drupalcode.org/project/drupal/-/commit/6ac38e8e675a23e9a8c30...

I do have another idea I can try.

nicxvan’s picture

Yeah I think it would have to be in the array intersect on the left side.

godotislate’s picture

OK, this might be close to what you were asking.
Test only still fails as expected: https://git.drupalcode.org/project/drupal/-/jobs/9414907

nicxvan’s picture

Yes that is it exactly!

nicxvan’s picture

The comment doesn't exactly make sense anymore though so it could use an update. Something about adding core so if there is a kernel hook alter it will be returned.

godotislate’s picture

Did my best to reword it.

nicxvan’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs Review Queue Initiative

Thank you!

Last change was just the comment, the previous run had the test only failure. I think this is ready!

I'm not sure why this was tagged needs review queue initiative, my understanding was that tag means it was reviewed as part of that initiative which this hasn't been.

alexpott’s picture

Version: main » 11.x-dev
Status: Reviewed & tested by the community » Patch (to be ported)

Needs to implemented for 11.x - there are conflicts that look tricky to resolve.

  • alexpott committed b17f36c5 on main
    fix: #3585498 Alter hook implementations in Kernel test classes are not...

alexpott’s picture

Status: Patch (to be ported) » Reviewed & tested by the community

Did the merge conflict resolution myself.

alexpott’s picture

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

Committed and pushed 849bda6a8e9 to 11.x and 1400d62c528 to 11.3.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.

  • alexpott committed 1400d62c on 11.3.x
    fix: #3585498 Alter hook implementations in Kernel test classes are not...

  • alexpott committed 849bda6a on 11.x
    fix: #3585498 Alter hook implementations in Kernel test classes are not...