You are here

public function SiteAlertTimeoutTest::testSiteAlertTimeouts in Site Alert 8

Tests the automatic refreshing of site alerts.

File

tests/src/FunctionalJavascript/SiteAlertTimeoutTest.php, line 19

Class

SiteAlertTimeoutTest
Tests refreshing site alerts at regular intervals by setting a timeout.

Namespace

Drupal\Tests\site_alert\FunctionalJavascript

Code

public function testSiteAlertTimeouts() {
  $this
    ->drupalPlaceBlock('site_alert_block', [
    'id' => 'site_alert_block',
    // Use a fast timeout of 1 second so we don't have to wait too long.
    'timeout' => 1,
  ]);

  // There are no alerts yet. The block should be present on the page so it
  // can display any alerts that become active, but it should be empty.
  $this
    ->drupalGet('<front>');
  $this
    ->assertSiteAlertBlockPresent();
  $this
    ->assertSiteAlertCount(0);

  // The JavaScript code that is responsible for refreshing the alerts should
  // be loaded in the page.
  $this
    ->assertJavaScriptPresent();

  // Create two site alerts: an active and an inactive one.
  SiteAlert::create([
    'active' => TRUE,
    'severity' => 'low',
    'message' => [
      'value' => 'Active alert',
      'format' => 'plain_text',
    ],
    'label' => 'Active',
  ])
    ->save();
  SiteAlert::create([
    'active' => FALSE,
    'severity' => 'medium',
    'message' => [
      'value' => 'Inactive alert',
      'format' => 'plain_text',
    ],
    'label' => 'Inactive',
  ])
    ->save();

  // Wait until the active site alert appears.
  $this
    ->assertSiteAlertAppears('Active');

  // There should be one alert on the page right now. The "inactive" alert
  // should not have appeared.
  $this
    ->assertSiteAlertCount(1);
  $this
    ->assertSiteAlertNotVisible('Inactive');

  // Create a site alert that is scheduled to appear in a few seconds.
  SiteAlert::create([
    'active' => TRUE,
    'severity' => 'high',
    'message' => [
      'value' => 'Scheduled alert',
      'format' => 'plain_text',
    ],
    'label' => 'Scheduled',
    'scheduling' => [
      'value' => (new DrupalDateTime('+2 seconds', DateTimeItemInterface::STORAGE_TIMEZONE))
        ->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT),
    ],
  ])
    ->save();

  // The scheduled alert should not be initially visible, but it should appear
  // after a few seconds.
  $this
    ->assertSiteAlertNotVisible('Scheduled alert');
  $this
    ->assertSiteAlertAppears('Scheduled alert');

  // Create a site alert that is immediately active and scheduled to disappear
  // in a few seconds.
  SiteAlert::create([
    'active' => TRUE,
    'severity' => 'low',
    'message' => [
      'value' => 'Disappearing',
      'format' => 'plain_text',
    ],
    'label' => 'Disappearing',
    'scheduling' => [
      'end_value' => (new DrupalDateTime('+3 seconds', DateTimeItemInterface::STORAGE_TIMEZONE))
        ->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT),
    ],
  ])
    ->save();
  $this
    ->assertSiteAlertAppears('Disappearing');
  $this
    ->assertSiteAlertDisappears('Disappearing');
}