SchedulerLightweightCronTest.php in Scheduler 8
File
tests/src/Functional/SchedulerLightweightCronTest.php
View source
<?php
namespace Drupal\Tests\scheduler\Functional;
use Drupal\Core\Url;
class SchedulerLightweightCronTest extends SchedulerBrowserTestBase {
protected function setUp() : void {
parent::setUp();
$this->routeCronForm = Url::fromRoute('scheduler.cron_form');
}
public function testLightweightCronRun() {
$this
->drupalGet('scheduler/cron');
$this
->assertSession()
->statusCodeEquals(404);
$key = substr(md5(rand()), 0, 20);
$this
->drupalGet('scheduler/cron/' . $key);
$this
->assertSession()
->statusCodeEquals(403);
$config = $this
->config('scheduler.settings');
$key = $config
->get('lightweight_cron_access_key');
$this
->drupalGet('scheduler/cron/' . $key);
$this
->assertSession()
->statusCodeEquals(204);
}
public function testLightweightCronSettingsForm() {
$this
->drupalLogin($this->adminUser);
$this
->drupalGet($this->routeCronForm);
$key_xpath = $this
->xpath('//input[@id="edit-lightweight-access-key"]/@value');
$key = $key_xpath[0]
->getText();
$this
->assertNotEmpty($key, 'The default lightweight cron key field should not be empty');
$this
->assertEquals(20, strlen($key), 'The default lightweight cron key string length should be 20');
$this
->drupalPostForm($this->routeCronForm, [], 'Generate new random key');
$new_key_xpath = $this
->xpath('//input[@id="edit-lightweight-access-key"]/@value');
$new_key = $new_key_xpath[0]
->getText();
$this
->assertNotEmpty($new_key, 'The lightweight cron key field should not be empty after generating a new key');
$this
->assertEquals(20, strlen($new_key), 'The new lightweight cron key string length should be 20');
$this
->assertNotEquals($new_key, $key, 'The new lightweight cron key should be different from the previous key.');
$this
->drupalPostForm($this->routeCronForm, [], "Run Scheduler's lightweight cron now");
$this
->assertSession()
->pageTextContains('Lightweight cron run completed.');
$this
->drupalPostForm($this->routeCronForm, [
'lightweight_access_key' => '',
], 'Save configuration');
$this
->assertSession()
->pageTextContains('Lightweight cron access key field is required.');
$this
->assertSession()
->pageTextNotContains('The configuration options have been saved.');
}
}