Problem/Motivation

In Drupal 11.4, Drupal\update\MailHandler was refactored to use #[AutowireServiceClosure] attributes on its constructor parameters. However, the service definition in update.services.yml was not updated to match — it remains as a bare ~ (null), with no autowire: true directive.

As a result, when the service container attempts to instantiate MailHandler, it passes zero arguments, causing:

ArgumentCountError: Too few arguments to function Drupal\update\MailHandler::__construct(), 0 passed in [container] and exactly 4 expected

Steps to reproduce

  • Install Drupal 11.4 (or 11.x-dev).
  • Configure update email notifications at /admin/reports/updates/settings — add at least one email address to the "Email addresses to notify" field.
  • Trigger instantiation of the Drupal\update\MailHandler service (e.g. by running cron when there are pending updates, or by directly invoking the update.mail_handler service from code).
  • Observe the ArgumentCountError: Too few arguments to function Drupal\update\MailHandler::__construct(), 0 passed.

Proposed resolution

Add autowire: true to the MailHandler service definition in update/update.services.yml:

Drupal\update\MailHandler:
  autowire: true

This allows Symfony's dependency injection container to automatically resolve the constructor arguments via the #[AutowireServiceClosure] attributes already present on the class.

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

Issue fork drupal-3591548

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

ltrain created an issue. See original summary.

ltrain’s picture

Status: Active » Needs review
dalin’s picture

Priority: Normal » Major

Sounds like a major issue.

For the steps to reproduce, I'd be surprised if there isn't already tests for each one of those things, so do we need a test for some of the steps combined?

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Definitely would count as a major

  • xjm committed 32ebf9e0 on 11.x
    task: #3591548 Drupal\update\MailHandler service missing autowire: true...
xjm’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

Nice find, thanks!

This definitely points to missing test coverage.

Since this is an 11.4.x major regression with a very easy hotfix (and we're tagging a beta soon), I went ahead and committed the hotfix without tests, but let's add those next please. 🙂 Leaving open for that.

xjm’s picture

Version: 11.x-dev » main

The tests would presumably be needed on main too.

smustgrave’s picture

For test coverage this only seems to be called from the cron job. Or can we just go straight to the handler and something like

  public function testServiceWiring(): void {
    $service = $this->container->get(MailHandler::class);
    $this->assertInstanceOf(MailHandler::class, $service);
  }
dww’s picture

This is an example of why Unit tests, while fast, aren't always that helpful. 😅 The only coverage we currently have of the email sending is Drupal\Tests\update\Unit\UpdateMailTest which of course mocks a bunch of stuff to make a fake container, and only tests that the text coming out of UpdateHooks::mail() matches what we expect, given some imaginary site status.

This bug would really only be seen with a Functional test that was actually trying to do end-to-end testing of a site that was out of date, and seeing what actual email the test site generates.

While I appreciate the simplicity, I think #9 is pretty contrived. 😉 I don't believe we do that for any other services.

If we're going to add test coverage for this, I think it should be a Functional test that sets up mock status/history to be out of date, triggers cron, and checks what the email that gets sent looks like. Drupal\Tests\update\Functional\UpdateMiscTest::testModulePageRunCron() is pretty darn close to that. Could probably be expanded to cover that email is actually being sent as appropriate.

smustgrave’s picture

Fully get it agree just wanted to confirm sounds like a plan if no one beats me to it may try next week

xjm’s picture

Semi-regretting my committing the hotfix without tests now. It was still probably the right decision but the tests are still missing 4 months later. 😅

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

brandonlira’s picture

Status: Needs work » Needs review

Updated the MR!15842 and resolved the previous branch/conflict issues.

I also added functional test coverage for the cron update notification path. The test configures an update notification recipient, simulates an available core update, runs cron, and asserts that an update_status_notify email is sent with the expected recipient, subject, and body.

The MR pipeline is now passing and this is ready for review. Please let me know if any adjustments are needed.

smustgrave’s picture

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

Since this wasn't really needed for main/12 so tested on my local 11.x instance
I reverted https://git.drupalcode.org/project/drupal/-/commit/32ebf9e0484ce5fb56fa6...
Applied the test and ran and got a failure

Failed asserting that actual size 0 matches expected size 1.
/var/www/html/core/modules/update/tests/src/Functional/UpdateMiscTest.php:207

Not 100% we need the test for main but for 11.x it appears to cover the bug.

  • catch committed ec082202 on main
    fix: #3591548 Drupal\update\MailHandler service missing autowire: true...
catch’s picture

Version: main » 12.0.x-dev

Commmitted/pushed to main, thanks!

Leaving open for backport after the security release freeze.

  • catch committed cac5dc3d on 12.0.x
    fix: #3591548 Drupal\update\MailHandler service missing autowire: true...

  • catch committed 8f75cc05 on 11.5.x
    fix: #3591548 Drupal\update\MailHandler service missing autowire: true...
catch’s picture

Version: 12.0.x-dev » 11.5.x-dev
Status: Reviewed & tested by the community » Fixed

Cherry-picked to 12.0.x, 11.x and 11.5.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.

  • catch committed 853976dd on 11.x
    fix: #3591548 Drupal\update\MailHandler service missing autowire: true...
catch’s picture

Version: 12.0.x-dev » 11.5.x-dev
Status: Reviewed & tested by the community » Fixed

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.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.