Unverified Commit 5b3a0c37 authored by Alex Pott's avatar Alex Pott
Browse files

fix: #3576648 Skip automated cron on all CLI commands

By: mstrelan
By: godotislate
(cherry picked from commit 1bcdac07)
parent ed5ff2b3
Loading
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -30,6 +30,9 @@ public function __construct(
   *   The Event to process.
   */
  public function onTerminate(TerminateEvent $event): void {
    if (PHP_SAPI === 'cli') {
      return;
    }
    $interval = $this->configFactory->get('automated_cron.settings')->get('interval');
    if ($interval > 0) {
      $cron_next = $this->state->get('system.cron_last', 0) + $interval;
+49 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\Tests\automated_cron\Functional;

use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\WaitTerminateTestTrait;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;

/**
 * Tests for automated_cron.
 */
#[Group('automated_cron')]
#[RunTestsInSeparateProcesses]
class AutomatedCronTest extends BrowserTestBase {

  use WaitTerminateTestTrait;

  /**
   * {@inheritdoc}
   */
  protected static $modules = ['automated_cron'];

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

  /**
   * Tests that automated cron runs cron on an HTTP request.
   *
   * @legacy-covers \Drupal\automated_cron\EventSubscriber\AutomatedCron::onTerminate
   */
  public function testRunsCronOnHttpRequest(): void {
    // Set automated_cron interval and times.
    // Any interval > 0 should work.
    $this->config('automated_cron.settings')->set('interval', 10800)->save();
    \Drupal::state()->delete('system.cron_last');

    $this->assertNull(\Drupal::state()->get('system.cron_last'));
    $this->assertNotNull($_SERVER['REQUEST_TIME']);
    $this->setWaitForTerminate();
    $this->drupalGet('/user/login');
    $this->assertGreaterThanOrEqual($_SERVER['REQUEST_TIME'], \Drupal::state()->get('system.cron_last'));
  }

}
+4 −5
Original line number Diff line number Diff line
@@ -23,11 +23,11 @@ class AutomatedCronTest extends KernelTestBase {
  protected static $modules = ['automated_cron'];

  /**
   * Tests that automated cron runs cron on an HTTP request.
   * Tests that automated cron does not run cron on a CLI request.
   *
   * @legacy-covers \Drupal\automated_cron\EventSubscriber\AutomatedCron::onTerminate
   */
  public function testRunsCronOnHttpRequest(): void {
  public function testCronDoesNotRunOnCliRequest(): void {
    // Set automated_cron interval and times.
    // Any interval > 0 should work.
    $this->config('automated_cron.settings')->set('interval', 10800)->save();
@@ -36,13 +36,12 @@ public function testRunsCronOnHttpRequest(): void {
    // Cron uses `$_SERVER['REQUEST_TIME']` to set `system.cron_last`
    // because there is no request stack, so we set the request time
    // to the same.
    $expected = $_SERVER['REQUEST_TIME'];
    $request->server->set('REQUEST_TIME', $expected);
    $request->server->set('REQUEST_TIME', $_SERVER['REQUEST_TIME']);

    // Invoke `AutomatedCron::onTerminate` and check result.
    $this->assertNull($this->container->get('state')->get('system.cron_last'));
    $this->container->get('kernel')->terminate($request, new Response());
    $this->assertEquals($expected, $this->container->get('state')->get('system.cron_last'));
    $this->assertNull($this->container->get('state')->get('system.cron_last'));
  }

}