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 111

when 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

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

acbramley created an issue. See original summary.

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

nicxvan’s picture

Not 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.

nicxvan’s picture

Status: Active » Needs review

The approach needs review.

acbramley’s picture

Looking 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?

nicxvan’s picture

I think I saw each one call to array so isn't it implicitly testing that?

acbramley’s picture

I mean keeping some of the old style callbacks in addOperation and setFinishedCallback

voleger’s picture

I'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.

nicxvan’s picture

Status: Needs review » Needs work

You'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!

acbramley’s picture

I can revert one or two of them so we have coverage of both ways.

That's what I was hoping :)

nicxvan’s picture

Status: Needs work » Needs review

I reverted a couple!

nicxvan’s picture

Status: Needs review » Needs work

This is a real failure

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

berdir’s picture

Status: Needs work » Needs review

The reverted/updated definitions still had a : in the method names in the array, fixed that.

nicxvan’s picture

Oh 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

berdir’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community

Went 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

nicxvan’s picture

This should probably be backported to 11.3 too I think.

berdir’s picture

I'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.

nicxvan’s picture

Fair 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.

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

longwave’s picture

Status: Reviewed & tested by the community » Needs work

I 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.

nicxvan’s picture

Is there a reason the BetchTestDefinitions tests are not enough? We do have this:

public function batch1(): array {
    // Ensure the batch takes at least two iterations.
    $total = 10;
    $sleep = (int) (1000000 / $total) * 2;

    $batch_builder = (new BatchBuilder())
      ->setFinishCallback(BatchTestCallbacks::class . ':finished1');

    for ($i = 1; $i <= $total; $i++) {
      $batch_builder->addOperation(BatchTestCallbacks::class . ':callback1', [$i, $sleep]);
    }

    return $batch_builder->toArray() + ['batch_test_id' => 'batch1'];
  }

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.

longwave’s picture

Mix 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.

nicxvan’s picture

Fair enough, will do!

nicxvan’s picture

Status: Needs work » Needs review

I pushed up some additional test cases to check the various forms.

acbramley’s picture

Status: Needs review » Reviewed & tested by the community

New test coverage is looking good!

alexpott’s picture

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

Committed and pushed bec4e5cafcc to main and 668beff98d2 to 11.x and 07c059cad23 to 11.3.x. Thanks!

Nice fix.

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 07c059ca on 11.3.x
    fix: #3575096 Remove callable type from BatchBuilder ::addOperation and...

  • alexpott committed 668beff9 on 11.x
    fix: #3575096 Remove callable type from BatchBuilder ::addOperation and...

  • alexpott committed bec4e5ca on main
    fix: #3575096 Remove callable type from BatchBuilder ::addOperation and...

Status: Fixed » Closed (fixed)

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