diff --git a/core/modules/system/src/Controller/BatchController.php b/core/modules/system/src/Controller/BatchController.php index ccafd85fd9f670109a2e144d175400920625c09d..9a0e8f0bc2f3d835a99a09b696140b344e947807 100644 --- a/core/modules/system/src/Controller/BatchController.php +++ b/core/modules/system/src/Controller/BatchController.php @@ -75,4 +75,15 @@ public function batchPage(Request $request) { } } + /** + * The _title_callback for the system.batch_page.normal route. + * + * @return string + * The page title. + */ + public function batchPageTitle() { + $current_set = _batch_current_set(); + return !empty($current_set['title']) ? $current_set['title'] : ''; + } + } diff --git a/core/modules/system/src/Tests/Batch/PageTest.php b/core/modules/system/src/Tests/Batch/PageTest.php index 1dd76281907d811716f41eee93b9f94b51864212..ad5d83ab07698f91f1e99f24cbfa257797aa23e1 100644 --- a/core/modules/system/src/Tests/Batch/PageTest.php +++ b/core/modules/system/src/Tests/Batch/PageTest.php @@ -47,4 +47,18 @@ function testBatchProgressPageTheme() { // page. $this->assertEqual(batch_test_stack(), array('seven'), 'A progressive batch correctly uses the theme of the page that started the batch.'); } + + /** + * Tests that the batch API progress page shows the title correctly. + */ + function testBatchProgressPageTitle() { + // Visit an administrative page that runs a test batch, and check that the + // title shown during batch execution (which the batch callback function + // saved as a variable) matches the theme used on the administrative page. + $this->drupalGet('batch-test/test-title'); + // The stack should contain the title shown on the progress page. + $this->assertEqual(batch_test_stack(), ['Batch Test'], 'The batch title is shown on the batch page.'); + $this->assertText('Redirection successful.', 'Redirection after batch execution is correct.'); + } + } diff --git a/core/modules/system/system.routing.yml b/core/modules/system/system.routing.yml index 798cc899de3baf7c5fca507d84017842dac8d1a5..2bb9e62c836a210552cf69a41763682babd6d545 100644 --- a/core/modules/system/system.routing.yml +++ b/core/modules/system/system.routing.yml @@ -426,6 +426,7 @@ system.batch_page.html: path: '/batch' defaults: _controller: '\Drupal\system\Controller\BatchController::batchPage' + _title_callback: '\Drupal\system\Controller\BatchController::batchPageTitle' requirements: _access: 'TRUE' _format: 'html' diff --git a/core/modules/system/tests/modules/batch_test/batch_test.module b/core/modules/system/tests/modules/batch_test/batch_test.module index 3cf5f20cc72504e5de77ace31f8d29320aa04fd3..e3d1c953a83f9d0189981d1be2e8766d6233743a 100644 --- a/core/modules/system/tests/modules/batch_test/batch_test.module +++ b/core/modules/system/tests/modules/batch_test/batch_test.module @@ -166,6 +166,21 @@ function _batch_test_theme_callback() { batch_test_stack($theme); } +/** + * Tests the title on the progress page by performing a batch callback. + */ +function _batch_test_title_callback() { + // Because drupalGet() steps through the full progressive batch before + // returning control to the test function, we cannot test that the correct + // title is being used on the batch processing page by viewing that page + // directly. Instead, we save the title being used in a variable here, so + // that it can be loaded and inspected in the thread running the test. + $request = \Drupal::request(); + $route_match = \Drupal::routeMatch(); + $title = \Drupal::service('title_resolver')->getTitle($request, $route_match->getRouteObject()); + batch_test_stack($title); +} + /** * Helper function: Stores or retrieves traced execution data. */ diff --git a/core/modules/system/tests/modules/batch_test/batch_test.routing.yml b/core/modules/system/tests/modules/batch_test/batch_test.routing.yml index 67362896fdf2dbeff11ae4db02b8ac386e43ff7e..638d6fe9494da61db59c6e870e07a85dad1cb88a 100644 --- a/core/modules/system/tests/modules/batch_test/batch_test.routing.yml +++ b/core/modules/system/tests/modules/batch_test/batch_test.routing.yml @@ -70,3 +70,10 @@ batch_test.test_theme: _controller: '\Drupal\batch_test\Controller\BatchTestController::testThemeBatch' requirements: _access: 'TRUE' + +batch_test.test_title: + path: '/batch-test/test-title' + defaults: + _controller: '\Drupal\batch_test\Controller\BatchTestController::testTitleBatch' + requirements: + _access: 'TRUE' diff --git a/core/modules/system/tests/modules/batch_test/src/Controller/BatchTestController.php b/core/modules/system/tests/modules/batch_test/src/Controller/BatchTestController.php index df3a67249ab2030f9408fd529ceb3983baec0681..19ea6e9bf346609e04760000190d551218fc0763 100644 --- a/core/modules/system/tests/modules/batch_test/src/Controller/BatchTestController.php +++ b/core/modules/system/tests/modules/batch_test/src/Controller/BatchTestController.php @@ -113,4 +113,22 @@ public function testThemeBatch() { return batch_process('batch-test/redirect'); } + /** + * Runs a batch for testing the title shown on the progress page. + * + * @return \Symfony\Component\HttpFoundation\RedirectResponse|null + * A redirect response if the batch is progressive. No return value otherwise. + */ + public function testTitleBatch() { + batch_test_stack(NULL, TRUE); + $batch = [ + 'title' => 'Batch Test', + 'operations' => [ + ['_batch_test_title_callback', []], + ], + ]; + batch_set($batch); + return batch_process('batch-test/redirect'); + } + }