SiteAlertKernelTestBase.php in Site Alert 8
File
tests/src/Kernel/SiteAlertKernelTestBase.php
View source
<?php
namespace Drupal\Tests\site_alert\Kernel;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
use Drupal\KernelTests\KernelTestBase;
abstract class SiteAlertKernelTestBase extends KernelTestBase {
protected static $modules = [
'datetime_range',
'options',
'site_alert',
'text',
];
protected $alerts;
protected $entityTypeManager;
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('site_alert');
$this->entityTypeManager = $this->container
->get('entity_type.manager');
}
protected function createAlerts(array $alerts_to_create) {
$storage = $this->entityTypeManager
->getStorage('site_alert');
$format_date = function (string $time) {
return (new DrupalDateTime($time, DateTimeItemInterface::STORAGE_TIMEZONE))
->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
};
foreach ($alerts_to_create as $identifier) {
preg_match('/^([a-z]+)-([a-z]+)\\d*/', $identifier, $matches);
$values = [
'active' => $matches[2] === 'active',
'label' => $this
->randomString(),
'severity' => 'medium',
'message' => $this
->randomString(),
];
switch ($matches[1]) {
case 'past':
$values['scheduling'] = [
'value' => $format_date('-3 hours'),
'end_value' => $format_date('-2 hours'),
];
break;
case 'present':
$values['scheduling'] = [
'value' => $format_date('-1 hours'),
'end_value' => $format_date('+1 hours'),
];
break;
case 'future':
$values['scheduling'] = [
'value' => $format_date('+2 hours'),
'end_value' => $format_date('+3 hours'),
];
break;
}
$this->alerts[$identifier] = $storage
->create($values);
$this->alerts[$identifier]
->save();
}
}
}