Issue #2903183 by amateescu, jkovell, dawehner: Don't run cron after updating cron settings

8.5.x
Nathaniel Catchpole 2017-08-22 16:14:51 +09:00
parent 64ffc1ff4d
commit 0846b231ef
2 changed files with 22 additions and 7 deletions

View File

@ -16,6 +16,7 @@ use Drupal\Core\Form\ConfigFormBaseTrait;
* Configure cron settings for this site. * Configure cron settings for this site.
*/ */
class CronForm extends FormBase { class CronForm extends FormBase {
use ConfigFormBaseTrait; use ConfigFormBaseTrait;
/** /**
@ -104,6 +105,7 @@ class CronForm extends FormBase {
$form['run'] = [ $form['run'] = [
'#type' => 'submit', '#type' => 'submit',
'#value' => t('Run cron'), '#value' => t('Run cron'),
'#submit' => ['::runCron'],
]; ];
$status = '<p>' . $this->t('Last run: %time ago.', ['%time' => $this->dateFormatter->formatTimeDiffSince($this->state->get('system.cron_last'))]) . '</p>'; $status = '<p>' . $this->t('Last run: %time ago.', ['%time' => $this->dateFormatter->formatTimeDiffSince($this->state->get('system.cron_last'))]) . '</p>';
$form['status'] = [ $form['status'] = [
@ -145,22 +147,25 @@ class CronForm extends FormBase {
} }
/** /**
* Runs cron and reloads the page. * {@inheritdoc}
*/ */
public function submitForm(array &$form, FormStateInterface $form_state) { public function submitForm(array &$form, FormStateInterface $form_state) {
$this->config('system.cron') $this->config('system.cron')
->set('logging', $form_state->getValue('logging')) ->set('logging', $form_state->getValue('logging'))
->save(); ->save();
drupal_set_message(t('The configuration options have been saved.')); drupal_set_message(t('The configuration options have been saved.'));
}
// Run cron manually from Cron form. /**
* Form submission handler for running cron manually.
*/
public function runCron(array &$form, FormStateInterface $form_state) {
if ($this->cron->run()) { if ($this->cron->run()) {
drupal_set_message(t('Cron ran successfully.')); drupal_set_message($this->t('Cron ran successfully.'));
} }
else { else {
drupal_set_message(t('Cron run failed.'), 'error'); drupal_set_message($this->t('Cron run failed.'), 'error');
} }
} }
} }

View File

@ -105,9 +105,19 @@ class CronRunTest extends WebTestBase {
// the time will start at 1 January 1970. // the time will start at 1 January 1970.
$this->assertNoText('years'); $this->assertNoText('years');
$this->drupalPostForm(NULL, [], t('Save configuration')); $cron_last = time() - 200;
$this->assertText(t('The configuration options have been saved.')); \Drupal::state()->set('system.cron_last', $cron_last);
$this->drupalPostForm(NULL, [], 'Save configuration');
$this->assertText('The configuration options have been saved.');
$this->assertUrl('admin/config/system/cron'); $this->assertUrl('admin/config/system/cron');
// Check that cron does not run when saving the configuration form.
$this->assertEqual($cron_last, \Drupal::state()->get('system.cron_last'), 'Cron does not run when saving the configuration form.');
// Check that cron runs when triggered manually.
$this->drupalPostForm(NULL, [], 'Run cron');
$this->assertTrue($cron_last < \Drupal::state()->get('system.cron_last'), 'Cron runs when triggered manually.');
} }
/** /**