Backport of #9225

@voxsim can you help finish the port? The code itself works, but the tests do not run. Could you have a look?