diff --git a/tests/src/FunctionalJavascript/SiteAlertCacheWorkaroundTest.php b/tests/src/FunctionalJavascript/SiteAlertCacheWorkaroundTest.php index 545dba8..52b95e2 100644 --- a/tests/src/FunctionalJavascript/SiteAlertCacheWorkaroundTest.php +++ b/tests/src/FunctionalJavascript/SiteAlertCacheWorkaroundTest.php @@ -60,8 +60,14 @@ class SiteAlertCacheWorkaroundTest extends SiteAlertWebDriverTestBase { // responsible for refreshing the alerts should be loaded in the page. $this->assertJavaScriptPresent(); - // Check that the alert appears within a few seconds. Thanks to the - // workaround this will work regardless of the fact that the page is cached. + // The automatic refreshing has been disabled by setting the 'timeout' to 0, + // so the scheduled alert should not appear until we reload the page. + $this->assertSiteAlertNotAppears('Scheduled alert', 3000); + + // Reload the page. Thanks to the workaround the site alert should now + // appear regardless of the fact that the page was cached at a moment when + // no alert was visible. + $this->drupalGet(''); $this->assertSiteAlertAppears('Scheduled alert'); // Disable the alert. Now there are no more scheduled alerts, so the diff --git a/tests/src/FunctionalJavascript/SiteAlertWebDriverTestBase.php b/tests/src/FunctionalJavascript/SiteAlertWebDriverTestBase.php index fa19ff1..225b5f1 100644 --- a/tests/src/FunctionalJavascript/SiteAlertWebDriverTestBase.php +++ b/tests/src/FunctionalJavascript/SiteAlertWebDriverTestBase.php @@ -3,6 +3,7 @@ namespace Drupal\Tests\site_alert\FunctionalJavascript; use Drupal\FunctionalJavascriptTests\WebDriverTestBase; +use PHPUnit\Framework\AssertionFailedError; /** * Base class for functional JS tests for the Site Alerts module. @@ -44,6 +45,27 @@ abstract class SiteAlertWebDriverTestBase extends WebDriverTestBase { $this->assertJsCondition($condition); } + /** + * Checks that the alert with the given message does not appear on the page. + * + * @param string $message + * The message contained in the alert that is expected to appear. + * @param int $timeout + * (Optional) Timeout in milliseconds, defaults to 10000. + */ + protected function assertSiteAlertNotAppears($message, $timeout = 10000) { + $condition = 'jQuery(\'.site-alert div.text:contains("' . $message . '")\').length > 0;'; + try { + $this->assertJsCondition($condition, $timeout); + } + catch (AssertionFailedError $e) { + // The alert has not appeared, the test has passed. + return; + } + // The alert has unexpectedly appeared, the test has failed. + $this->fail(); + } + /** * Checks that the alert with the given message disappears from the page. *