Skip to content

Commit e7bf45a

Browse files
committed
Fixed stale REQUEST_TIME during cron by refreshing before cron->run().
1 parent 4e2379e commit e7bf45a

2 files changed

Lines changed: 78 additions & 0 deletions

File tree

src/Drupal/Driver/Cores/Drupal8.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ public function nodeDelete($node) {
120120
* {@inheritdoc}
121121
*/
122122
public function runCron() {
123+
$_SERVER['REQUEST_TIME'] = time();
124+
\Drupal::request()->server->set('REQUEST_TIME', $_SERVER['REQUEST_TIME']);
123125
return \Drupal::service('cron')->run();
124126
}
125127

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace Drupal\Tests\Driver;
4+
5+
use Drupal;
6+
use Drupal\Core\CronInterface;
7+
use Drupal\Driver\Cores\Drupal8;
8+
use PHPUnit\Framework\TestCase;
9+
use Symfony\Component\DependencyInjection\ContainerBuilder;
10+
use Symfony\Component\HttpFoundation\Request;
11+
use Symfony\Component\HttpFoundation\RequestStack;
12+
13+
/**
14+
* Tests for the Drupal 8+ core driver.
15+
*/
16+
class Drupal8Test extends TestCase {
17+
18+
/**
19+
* The original REQUEST_TIME value.
20+
*
21+
* @var int
22+
*/
23+
protected $originalRequestTime;
24+
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
protected function setUp(): void {
29+
parent::setUp();
30+
$this->originalRequestTime = $_SERVER['REQUEST_TIME'] ?? NULL;
31+
}
32+
33+
/**
34+
* {@inheritdoc}
35+
*/
36+
protected function tearDown(): void {
37+
if ($this->originalRequestTime !== NULL) {
38+
$_SERVER['REQUEST_TIME'] = $this->originalRequestTime;
39+
}
40+
parent::tearDown();
41+
}
42+
43+
/**
44+
* Tests that `runCron()` refreshes `REQUEST_TIME` before running cron.
45+
*/
46+
public function testRunCronRefreshesRequestTime() {
47+
$before = time();
48+
$stale_time = $before - 60;
49+
50+
// Create a real Symfony Request with a stale REQUEST_TIME.
51+
$request = new Request();
52+
$request->server->set('REQUEST_TIME', $stale_time);
53+
$_SERVER['REQUEST_TIME'] = $stale_time;
54+
55+
// Mock the cron service.
56+
$cron = $this->createMock(CronInterface::class);
57+
$cron->method('run')->willReturn(TRUE);
58+
59+
// Wire a container with request_stack and cron service.
60+
$request_stack = new RequestStack();
61+
$request_stack->push($request);
62+
$container = new ContainerBuilder();
63+
$container->set('request_stack', $request_stack);
64+
$container->set('cron', $cron);
65+
Drupal::setContainer($container);
66+
67+
// Use __DIR__ as a dummy drupal root (runCron does not use it).
68+
$core = new Drupal8(__DIR__, 'default');
69+
$result = $core->runCron();
70+
71+
$this->assertTrue($result);
72+
$this->assertGreaterThanOrEqual($before, $_SERVER['REQUEST_TIME'], '$_SERVER[REQUEST_TIME] was not refreshed.');
73+
$this->assertGreaterThanOrEqual($before, $request->server->get('REQUEST_TIME'), 'Request server bag REQUEST_TIME was not refreshed.');
74+
}
75+
76+
}

0 commit comments

Comments
 (0)