You are here

protected function SiteAlertKernelTestBase::createAlerts in Site Alert 8

Creates site alert entities for the given identifiers.

Parameters

array $alerts_to_create: An array of identifiers indicating which site alerts to create. Each identifier starts with the type of alert to create (one of 'active', 'inactive', 'past', 'current' or 'future'), a hyphen, whether or not the alert is active, and optionally ending in a number if multiple instances of the same type should be created.

3 calls to SiteAlertKernelTestBase::createAlerts()
GetAlertsTest::testGetActiveAlertIds in tests/src/Kernel/GetAlertsTest.php
Tests the retrieval of active alert IDs.
GetAlertsTest::testGetActiveAlerts in tests/src/Kernel/GetAlertsTest.php
Tests the retrieval of active alerts.
SiteAlertStorageTest::testGetMaxAge in tests/src/Kernel/SiteAlertStorageTest.php
Tests the retrieval of the cache max age.

File

tests/src/Kernel/SiteAlertKernelTestBase.php, line 60

Class

SiteAlertKernelTestBase
Base class for kernel tests for the Site Alerts module.

Namespace

Drupal\Tests\site_alert\Kernel

Code

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();
  }
}