Backport of #9225 to stable6

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