Commit af52cec4 authored by dpi's avatar dpi

Added a courier maintenance form to facilitate deleting unsent message queue items.

Added web test for courier maintenance form.

Fixed #32
parent 87c5dba9
......@@ -7,3 +7,13 @@ entity.courier_email.edit_form:
route_name: entity.courier_email.edit_form
base_route: entity.courier_email.canonical
title: 'Edit'
courier.admin.settings:
route_name: courier.config
base_route: courier.config
title: 'Settings'
courier.admin.maintenance:
route_name: courier.admin.maintenance
base_route: courier.config
title: 'Maintenance'
......@@ -63,3 +63,11 @@ courier.config:
_title: 'Courier settings'
requirements:
_permission: 'administer courier'
courier.admin.maintenance:
path: '/admin/config/communication/courier/maintenance'
defaults:
_form: '\Drupal\courier\Form\CourierMaintenanceForm'
_title: 'Courier maintenance'
requirements:
_permission: 'administer courier'
<?php
/**
* @file
* Contains \Drupal\courier\Form\CourierMaintenanceForm.
*/
namespace Drupal\courier\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Form\FormStateInterface;
/**
* Courier maintenance form.
*/
class CourierMaintenanceForm extends FormBase {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Constructs a CourierMaintenanceForm object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity_type.manager')
);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'courier_admin_maintenance';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['mqi'] = [
'#type' => 'details',
'#open' => TRUE,
];
$form['mqi']['delete_age'] = [
'#title' => $this->t('Delete old messages'),
'#description' => $this->t('This is used to delete messages queue items that have gotten stuck in the queue.'),
'#type' => 'number',
'#field_prefix' => $this->t('Delete message older than'),
'#field_suffix' => $this->t('seconds'),
'#min' => 1,
'#default_value' => 60 * 60 * 24,
];
$form['mqi']['actions'] = [
'#type' => 'actions',
];
$form['mqi']['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Delete messages'),
'#button_type' => 'danger',
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$time = REQUEST_TIME - $form_state->getValue('delete_age');
$storage = $this->entityTypeManager
->getStorage('courier_message_queue_item');
$ids = $storage->getQuery()
->condition('created', $time, '<')
->execute();
$storage->delete($storage->loadMultiple($ids));
drupal_set_message($this->formatPlural(count($ids), '@count message deleted.', '@count messages deleted.'));
}
}
<?php
namespace Drupal\courier\Tests;
use Drupal\Core\Url;
use Drupal\courier\Entity\MessageQueueItem;
use Drupal\simpletest\WebTestBase;
/**
* Courier maintenance form web test.
*
* @group courier
*/
class CourierMaintenanceFormTest extends WebTestBase {
public static $modules = ['courier'];
/**
* Test message queue items are deleted.
*/
public function testMessageDelete() {
$user = $this->drupalCreateUser(['administer courier']);
$this->drupalLogin($user);
MessageQueueItem::create(['created' => REQUEST_TIME + 3600])->save();
MessageQueueItem::create(['created' => REQUEST_TIME + 3600])->save();
MessageQueueItem::create(['created' => REQUEST_TIME + 3600])->save();
MessageQueueItem::create(['created' => REQUEST_TIME - 3600])->save();
MessageQueueItem::create(['created' => REQUEST_TIME - 3600])->save();
$edit = [
'delete_age' => 60,
];
$this->drupalPostForm(Url::fromRoute('courier.admin.maintenance'), $edit, t('Delete messages'));
$this->assertRaw('2 messages deleted.');
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment