View source
<?php
namespace Drupal\Tests\Core;
use Drupal\Core\Cron;
use Drupal\Core\KeyValueStore\KeyValueMemoryFactory;
use Drupal\Core\Queue\DelayedRequeueException;
use Drupal\Core\Queue\Memory;
use Drupal\Core\Queue\RequeueException;
use Drupal\Core\Queue\SuspendQueueException;
use Drupal\Core\State\State;
use Drupal\Tests\UnitTestCase;
use Prophecy\Argument;
use Prophecy\Argument\ArgumentsWildcard;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class CronTest extends UnitTestCase {
const REQUEUE_COUNT = 3;
protected $claimTime = 300;
protected $cron;
protected $queue;
protected $state;
protected function setUp() : void {
parent::setUp();
$this->state = new State(new KeyValueMemoryFactory());
$logger = $this
->prophesize('Drupal\\Core\\Logger\\LoggerChannelInterface');
$logger
->warning(Argument::exact('Attempting to re-run cron while it is already running.'))
->shouldBeCalled();
foreach (get_class_methods(LoggerInterface::class) as $logger_method) {
$logger
->{$logger_method}(Argument::cetera())
->will(function () {
\Drupal::state()
->set('cron_test.message_logged', TRUE);
});
}
$logger_factory = $this
->prophesize('Drupal\\Core\\Logger\\LoggerChannelFactoryInterface');
$logger_factory
->get(Argument::exact('cron'))
->willReturn($logger
->reveal());
$time = $this
->prophesize('Drupal\\Component\\Datetime\\TimeInterface');
\Drupal::setContainer(new ContainerBuilder());
\Drupal::getContainer()
->set('logger.factory', $logger_factory
->reveal());
\Drupal::getContainer()
->set('datetime.time', $time
->reveal());
\Drupal::getContainer()
->set('state', $this->state);
$module_handler = $this
->prophesize('Drupal\\Core\\Extension\\ModuleHandlerInterface');
$queue_factory = $this
->prophesize('Drupal\\Core\\Queue\\QueueFactory');
$queue_worker_manager = $this
->prophesize('Drupal\\Core\\Queue\\QueueWorkerManagerInterface');
$state = $this
->prophesize('Drupal\\Core\\State\\StateInterface');
$account_switcher = $this
->prophesize('Drupal\\Core\\Session\\AccountSwitcherInterface');
$lock_backend = $this
->prophesize('Drupal\\Core\\Lock\\LockBackendInterface');
$lock_backend
->acquire(Argument::exact('cron'), Argument::cetera())
->willReturn(FALSE);
$queue_worker = $this
->randomMachineName();
$queue_worker_definition = [
'id' => $queue_worker,
'cron' => [
'time' => &$this->claimTime,
],
];
$this->queue = new Memory($queue_worker);
$queue_factory
->get($queue_worker)
->willReturn($this->queue);
$queue_worker_plugin = $this
->prophesize('Drupal\\Core\\Queue\\QueueWorkerInterface');
$queue_worker_plugin
->processItem('Complete')
->willReturn();
$queue_worker_plugin
->processItem('Exception')
->willThrow(\Exception::class);
$queue_worker_plugin
->processItem('DelayedRequeueException')
->willThrow(DelayedRequeueException::class);
$queue_worker_plugin
->processItem('SuspendQueueException')
->willThrow(SuspendQueueException::class);
$queue_worker_plugin
->processItem('RequeueException')
->will(function ($args, $mock, $method) {
$method_calls = count($mock
->findProphecyMethodCalls($method
->getMethodName(), new ArgumentsWildcard($args)));
if ($method_calls < self::REQUEUE_COUNT) {
\Drupal::state()
->set('cron_test.requeue_count', $method_calls + 1);
throw new RequeueException();
}
});
$queue_worker_manager
->getDefinitions()
->willReturn([
$queue_worker => $queue_worker_definition,
]);
$queue_worker_manager
->createInstance($queue_worker)
->willReturn($queue_worker_plugin
->reveal());
$this->cron = new Cron($module_handler
->reveal(), $lock_backend
->reveal(), $queue_factory
->reveal(), $state
->reveal(), $account_switcher
->reveal(), $logger
->reveal(), $queue_worker_manager
->reveal(), $time
->reveal());
}
protected function resetTestingState() {
$this->queue
->deleteQueue();
$this->state
->set('cron_test.message_logged', FALSE);
$this->state
->set('cron_test.requeue_count', NULL);
}
public function processQueuesTestData() {
return [
[
'Complete',
'assertFalse',
0,
],
[
'Exception',
'assertTrue',
1,
],
[
'DelayedRequeueException',
'assertFalse',
1,
],
[
'SuspendQueueException',
'assertTrue',
1,
],
[
'RequeueException',
'assertFalse',
0,
],
];
}
public function testProcessQueues($item, $message_logged_assertion, $count_post_run) {
$this
->resetTestingState();
$this->queue
->createItem($item);
$this
->assertFalse($this->state
->get('cron_test.message_logged'));
$this
->assertEquals(1, $this->queue
->numberOfItems());
$this->cron
->run();
$this
->{$message_logged_assertion}($this->state
->get('cron_test.message_logged'));
$this
->assertEquals($count_post_run, $this->queue
->numberOfItems());
}
public function testRequeueException() {
$this
->resetTestingState();
$this->queue
->createItem('RequeueException');
$this->cron
->run();
$actual_requeue_count = $this->state
->get('cron_test.requeue_count');
$this
->assertIsInt($actual_requeue_count);
$this
->assertEquals(self::REQUEUE_COUNT, $actual_requeue_count);
}
}