View source
<?php
namespace Drupal\Tests\site_alert\FunctionalJavascript;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
use Drupal\site_alert\Entity\SiteAlert;
class SiteAlertTimeoutTest extends SiteAlertWebDriverTestBase {
public function testSiteAlertTimeouts() {
$this
->drupalPlaceBlock('site_alert_block', [
'id' => 'site_alert_block',
'timeout' => 1,
]);
$this
->drupalGet('<front>');
$this
->assertSiteAlertBlockPresent();
$this
->assertSiteAlertCount(0);
$this
->assertJavaScriptPresent();
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();
$this
->assertSiteAlertAppears('Active');
$this
->assertSiteAlertCount(1);
$this
->assertSiteAlertNotVisible('Inactive');
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();
$this
->assertSiteAlertNotVisible('Scheduled alert');
$this
->assertSiteAlertAppears('Scheduled alert');
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');
}
public function testSiteAlertTimeoutsDisabled() {
$this
->drupalPlaceBlock('site_alert_block', [
'id' => 'site_alert_block',
'timeout' => 0,
]);
$this
->drupalGet('<front>');
$this
->assertSiteAlertBlockNotPresent();
$this
->assertJavaScriptNotPresent();
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();
$this
->drupalGet('<front>');
$this
->assertSiteAlertBlockPresent();
$this
->assertSiteAlertVisible('Active alert');
$this
->assertSiteAlertNotVisible('Inactive alert');
$this
->assertSiteAlertCount(1);
$this
->assertJavaScriptNotPresent();
}
}