View source
<?php
namespace Drupal\Tests\entity_share_cron\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\entity_share_cron\EntityShareCronServiceInterface;
class HookCronTest extends KernelTestBase {
public static $modules = [
'serialization',
'jsonapi',
'entity_share_client',
'entity_share_cron',
];
protected $config;
protected $queue;
public function testEnqueueChannels() {
$this->config
->set('cron_interval', 1)
->set('cron_last_run', -99999);
$this->config
->set('remotes', [
'remote1' => [
'enabled' => FALSE,
'channels' => [
'channel1' => [
'enabled' => FALSE,
'url' => 'url1',
],
],
],
'remote2' => [
'enabled' => TRUE,
'channels' => [
'channel2' => [
'enabled' => TRUE,
'url' => 'url2',
],
'channel3' => [
'enabled' => FALSE,
'url' => 'url3',
],
'channel4' => [
'enabled' => TRUE,
'url' => 'url4',
],
],
],
'remote3' => [
'enabled' => FALSE,
],
'remote4' => [
'enabled' => TRUE,
'channels' => [
'channel5' => [
'enabled' => TRUE,
'url' => 'url5',
],
],
],
]);
$this->config
->save();
\entity_share_cron_cron();
$this
->assertEquals(3, $this->queue
->numberOfItems());
$item = $this->queue
->claimItem();
$this
->assertEquals('remote2', $item->data['remote_id']);
$this
->assertEquals('channel2', $item->data['channel_id']);
$this
->assertEquals('url2', $item->data['channel_info']['url']);
$item = $this->queue
->claimItem();
$this
->assertEquals('remote2', $item->data['remote_id']);
$this
->assertEquals('channel4', $item->data['channel_id']);
$this
->assertEquals('url4', $item->data['channel_info']['url']);
$item = $this->queue
->claimItem();
$this
->assertEquals('remote4', $item->data['remote_id']);
$this
->assertEquals('channel5', $item->data['channel_id']);
$this
->assertEquals('url5', $item->data['channel_info']['url']);
$last_run = $this->config
->get('cron_last_run');
$this
->assertGreaterThan(-99999, $last_run);
$this
->assertLessThanOrEqual(time(), $last_run);
}
public function testNoRemotesEnabled() {
$this->config
->set('cron_interval', 1)
->set('cron_last_run', -99999);
$this->config
->set('remotes', [
'remote1' => [
'enabled' => FALSE,
'channels' => [
'channel1' => [
'enabled' => FALSE,
'url' => 'url1',
],
],
],
'remote2' => [
'enabled' => FALSE,
],
]);
$this->config
->save();
\entity_share_cron_cron();
$this
->assertEquals(0, $this->queue
->numberOfItems());
$last_run = $this->config
->get('cron_last_run');
$this
->assertGreaterThan(-99999, $last_run);
$this
->assertLessThanOrEqual(time(), $last_run);
}
public function testRecentExecution() {
$last_run = time();
$this->config
->set('cron_interval', 9999999)
->set('cron_last_run', $last_run);
$this->config
->set('remotes', [
'remote1' => [
'enabled' => TRUE,
'channels' => [
'channel1' => [
'enabled' => TRUE,
'url' => 'url1',
],
],
],
]);
$this->config
->save();
\entity_share_cron_cron();
$this
->assertEquals(0, $this->queue
->numberOfItems());
$this
->assertEquals($last_run, $this->config
->get('cron_last_run'));
}
protected function setUp() {
parent::setUp();
$this
->installConfig([
'serialization',
'jsonapi',
'entity_share_client',
'entity_share_cron',
]);
$this->config = \Drupal::configFactory()
->getEditable('entity_share_cron.settings');
$this->queue = \Drupal::queue(EntityShareCronServiceInterface::PENDING_QUEUE_NAME);
}
}