Verified Commit 098c4f89 authored by Dave Long's avatar Dave Long
Browse files

fix: #2879023 Batch is dependent on the order of query string parameters

By: hideaway
By: blacklabel_tom
By: dawehner
By: rbayliss
By: bvoynick
By: nicxvan
By: john cook
By: y.sa
By: m4olivei
By: dries
By: eduardo morales alberti
By: doka
(cherry picked from commit 2c6cf436)
parent 084ca800
Loading
Loading
Loading
Loading
Loading
+6 −2
Changes for core/misc/batch.js: 6 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -19,7 +19,9 @@
      function updateCallback(progress, status, pb) {
        if (progress === '100') {
          pb.stopMonitoring();
          window.location = `${batch.uri}&op=finished`;
          const url = new URL(batch.uri, window.location.origin);
          url.searchParams.set('op', 'finished');
          window.location = url.toString();
        }
      }

@@ -36,7 +38,9 @@
          errorCallback,
        );
        progressBar.setProgress(-1, batch.initMessage);
        progressBar.startMonitoring(`${batch.uri}&op=do`, 10);
        const url = new URL(batch.uri, window.location.origin);
        url.searchParams.set('op', 'do');
        progressBar.startMonitoring(url.toString(), 10);
        // Remove HTML from no-js progress bar.
        $progress.empty();
        // Append the JS progressbar element.
+43 −0
Changes for core/modules/system/tests/modules/batch_test/src/BatchTestCallbacks.php: 43 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -127,6 +127,24 @@ public function callback8(bool $throw_exception): void {
    }
  }

  /**
   * Implements callback_batch_operation().
   *
   * Performs a simple batch operation.
   */
  public function callback9(array &$context): void {
    if (!isset($context['sandbox']['count'])) {
      $context['sandbox']['count'] = 0;
    }
    // No-op, but ensure the batch takes a couple iterations.
    // Batch needs time to run for the test, so sleep a bit.
    usleep(500);

    $context['sandbox']['count']++;
    $context['finished'] = $context['sandbox']['count'] / 5;
    $context['results'][] = \Drupal::request()->getUri();
  }

  /**
   * Implements callback_batch_operation().
   *
@@ -268,4 +286,29 @@ public function finished7($success, $results, $operations, $elapsed): void {
    $this->finishedHelper(7, $success, $results, $operations, $elapsed);
  }

  /**
   * Implements callback_batch_finished().
   *
   * Triggers 'finished' callback for batch 9.
   */
  public function finished9($success, $results, $operations, $elapsed): void {
    // Use item list template to render the messages.
    $message = [
      '#type' => 'inline_template',
      '#template' => 'Finished Url: <span class="batch-finished-url">{{ finished_url }}</span><br/ >Batch URLs: {{ urls }}',
      '#context' => [
        'finished_url' => \Drupal::request()->getUri(),
        'urls' => [
          '#theme' => 'item_list',
          '#items' => $results,
          '#attributes' => [
            'class' => ['batch-urls'],
          ],
        ],
      ],
    ];

    \Drupal::messenger()->addStatus(\Drupal::service('renderer')->renderInIsolation($message));
  }

}
+10 −0
Changes for core/modules/system/tests/modules/batch_test/src/BatchTestDefinitions.php: 10 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -188,4 +188,14 @@ public function batch8(): array {
    return $batch_builder->toArray() + ['batch_test_id' => 'batch8'];
  }

  /**
   * Batch 9: Displays URLs.
   */
  public function batch9(): array {
    $batch_builder = (new BatchBuilder())
      ->addOperation(BatchTestCallbacks::class . ':callback9')
      ->setFinishCallback(BatchTestCallbacks::class . ':finished9');
    return $batch_builder->toArray();
  }

}
+1 −0
Changes for core/modules/system/tests/modules/batch_test/src/Form/BatchTestSimpleForm.php: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -39,6 +39,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
        'batch6' => 'batch 6',
        'batch7' => 'batch 7',
        'batch8' => 'batch 8',
        'batch9' => 'batch 9',
      ],
      '#multiple' => TRUE,
    ];
+18 −0
Changes for core/modules/system/tests/src/FunctionalJavascript/Batch/ProcessingTest.php: 18 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -41,4 +41,22 @@ public function testLinkToErrorPageAppears(): void {
    $this->assertSession()->pageTextContains('Redirection successful.');
  }

  /**
   * Tests that JS batches replace the op in the URL and do not add to it.
   */
  public function testBatchUrls(): void {
    $edit = ['batch' => 'batch9'];
    $this->drupalGet('batch-test');
    $this->submitForm($edit, 'Submit');
    $this->assertSession()->waitForElementVisible('css', '.batch-urls');
    $batch_url_list = $this->assertSession()->elementExists('css', '.batch-urls');
    $batch_url_list_items = $batch_url_list->findAll('css', 'li');
    $this->assertCount(5, $batch_url_list_items);
    foreach ($batch_url_list_items as $batch_url_list_item) {
      $this->assertStringEndsWith('/batch?_format=json&id=1&op=do', $batch_url_list_item->getText());
    }
    $batch_finished_url = $this->assertSession()->elementExists('css', '.batch-finished-url');
    $this->assertStringEndsWith('/batch?id=1&op=finished', $batch_finished_url->getText());
  }

}