Skip to content

Commit

Permalink
Issue #3028621 by jungle, John Cook: BatchBuilder included files fails
Browse files Browse the repository at this point in the history
(cherry picked from commit ad50981aaa21093e406afa64738106a4bfd9e91e)
  • Loading branch information
larowlan committed Jun 24, 2020
1 parent 713bbfc commit d659fe9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
6 changes: 6 additions & 0 deletions lib/Drupal/Core/Batch/BatchBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,18 @@ public function setErrorMessage($message) {
* The path should be relative to base_path(), and thus should be built using
* drupal_get_path(). Defaults to {module_name}.module.
*
* The file needs to be set before using ::addOperation(),
* ::setFinishCallback(), or any other function that uses callbacks from the
* file. This is so that PHP knows about the included functions.
*
* @param string $filename
* The path to the file.
*
* @return $this
*/
public function setFile($filename) {
include_once $filename;

$this->file = $filename;
return $this;
}
Expand Down
15 changes: 12 additions & 3 deletions tests/Drupal/Tests/Core/Batch/BatchBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,20 @@ public function testSetErrorMessage() {
* @covers ::setFile
*/
public function testSetFile() {
$filename = dirname(__DIR__, 6) . '/core/modules/system/tests/modules/batch_test/batch_test.callbacks.inc';
$this->assertIsNotCallable('_batch_test_callback_1');
$this->assertIsNotCallable('_batch_test_finished_1');

$batch = (new BatchBuilder())
->setFile('filename.php')
->setFile($filename)
->setFinishCallback('_batch_test_finished_1')
->addOperation('_batch_test_callback_1', [])
->toArray();

$this->assertEquals('filename.php', $batch['file']);
$this->assertEquals($filename, $batch['file']);
$this->assertEquals([['_batch_test_callback_1', []]], $batch['operations']);
$this->assertEquals('_batch_test_finished_1', $batch['finished']);
$this->assertIsCallable('_batch_test_callback_1');
$this->assertIsCallable('_batch_test_finished_1');
}

/**
Expand Down

0 comments on commit d659fe9

Please sign in to comment.