Problem/Motivation
From https://www.drupal.org/project/drupal/issues/3539919#comment-16481523:
I'm trying to follow the CR to update #3533299: Deprecate node access rebuild functions to use the callable syntax but I get
TypeError: Drupal\Core\Batch\BatchBuilder::addOperation(): Argument #1 ($callback) must be of type callable, string given, called in /data/app/core/modules/node/src/NodeAccessRebuild.php on line 111when using
->addOperation(NodeAccessRebuild::class . ':batchOperation')Looking at the commits here it doesn't look like we have test coverage for the BatchBuilder approach?
Steps to reproduce
Try to use callable syntax from https://www.drupal.org/node/3570904 with the BatchBuilder class
Proposed resolution
Replace callable type with callable|string
Remaining tasks
Decide on new type
Update type
Add test coverage
User interface changes
N/A
Introduced terminology
N/A
API changes
Type change in BatchBuilder methods
Data model changes
N/A
Release notes snippet
N/A
Issue fork drupal-3575096
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
Comment #3
nicxvan commentedNot sure if we want to just expand it to include strings or remove it entirely.
For now I expanded it so we could convert some.
I did the ones that only check the literal type and the ones actually calling the batch.
Comment #5
nicxvan commentedThe approach needs review.
Comment #6
acbramley commentedLooking good, I think expanding is probably the right approach?
For the test coverage - do we have any remaining with the array based callbacks? Should we keep coverage for both?
Comment #7
nicxvan commentedI think I saw each one call to array so isn't it implicitly testing that?
Comment #8
acbramley commentedI mean keeping some of the old style callbacks in
addOperationandsetFinishedCallbackComment #9
volegerI'm not sure about updating existing tests to use the format service:method notation. I expect to see new case usage alongside existing test cases.
Comment #10
nicxvan commentedYou're right I think we need coverage of both ways, but I don't think we need brand new tests. I can revert one or two of them so we have coverage of both ways.
We'll have good coverage since we test so many cases already and we won't slow down the actual runtime.
I set it to needs work so I can do that, great idea!
Comment #11
acbramley commentedThat's what I was hoping :)
Comment #12
nicxvan commentedI reverted a couple!
Comment #13
nicxvan commentedThis is a real failure
Comment #15
berdirThe reverted/updated definitions still had a : in the method names in the array, fixed that.
Comment #16
nicxvan commentedOh thank you! I forgot to circle back to this when I got in front of my computer.
I've reviewed your changes and they look like what I expected.
If you want to review my changes I think we can co rtbc this
Comment #17
berdirWent through the MR again, looks good to me, my changes essentially just correct the revert back to HEAD and do not actually show up in the MR anymore.
This unblocks further cleanup in #3533299: Deprecate node access rebuild functions
Comment #18
nicxvan commentedThis should probably be backported to 11.3 too I think.
Comment #19
berdirI'd argue against that. There are workarounds (not using batch builder, using static methods, ...) and this is a type change, even if it's for a class that nobody is supposed to subclass, but who knows, in theory it would break a subclass overriding that method.
Comment #20
nicxvan commentedFair enough, I won't push back too hard, but this enables the intent of adding callable resolver which is why I thought it should be backported.
I'm happy as long as it gets into main.
Comment #22
longwaveI think it would be clearer if BatchBuilderTest explicitly tested the
[$class, $method]format as well for the two methods in question as well as the two string formats.Comment #23
nicxvan commentedIs there a reason the BetchTestDefinitions tests are not enough? We do have this:
Also I'm not quite sure what you mean by two string formats?
Do mean this:
->setFinishCallback(static::class . ':finishedCallback')and this:
->setFinishCallback('Drupal\Tests\Core\Batch\BatchBuilderTest:finishedCallback')Or do you mean something else, because that does not seem to work.
Comment #24
longwaveMix and matching in BatchTestDefinitions has the possibility of being lost or refactored in the future to be consistent (and ideally, it should consistently use the recommended best practice now if there is one, so copy-pasters and LLMs can learn from it).
Testing each type of callable (array, single colon string, double colon string) in the actual method unit tests shows explicit intent of the test coverage and is less likely to be lost.
Comment #25
nicxvan commentedFair enough, will do!
Comment #26
nicxvan commentedI pushed up some additional test cases to check the various forms.
Comment #27
acbramley commentedNew test coverage is looking good!
Comment #28
alexpottCommitted and pushed bec4e5cafcc to main and 668beff98d2 to 11.x and 07c059cad23 to 11.3.x. Thanks!
Nice fix.