AutosaveFormTestBase.php in Autosave Form 8
File
tests/src/FunctionalJavascript/AutosaveFormTestBase.php
View source
<?php
namespace Drupal\Tests\autosave_form\FunctionalJavascript;
use Behat\Mink\Exception\ResponseTextException;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
abstract class AutosaveFormTestBase extends WebDriverTestBase {
protected static $modules = [
'autosave_form',
'autosave_form_test',
];
protected $defaultTheme = 'classy';
protected $profile = 'testing';
protected $webUser;
protected $interval = 2000;
protected function setUp() {
parent::setUp();
$this
->prepareSetUp();
}
protected function prepareSetUp() {
\Drupal::configFactory()
->getEditable('autosave_form.settings')
->set('interval', $this->interval)
->save();
$this->webUser = $this
->drupalCreateUser($this
->getUserPermissions());
$this
->prepareUser();
$this
->drupalLogin($this->webUser);
}
protected function prepareUser() {
}
protected function reloadPageAndRestore($path, $last_autosave_timestamp) {
$this
->logHtmlOutput(__FUNCTION__ . ' before reload');
$this
->drupalGet($path);
$this
->logHtmlOutput(__FUNCTION__ . ' after reload');
$this
->assertAutosaveResumeDiscardMessageIsShown(TRUE, $last_autosave_timestamp);
$this
->pressAutosaveRestoreButton();
$this
->waitForAutosaveResumeButtonToDisappear();
}
protected function assertAutosaveResumeDiscardMessageIsShown($excepted, $last_autosave_timestamp, $timeout = 30) {
$date = \Drupal::service('date.formatter')
->format((int) $last_autosave_timestamp, 'custom', 'M d, Y H:i');
$message = (string) t('A version of this page you were editing at @date was saved as a draft. Do you want to resume editing or discard it?', [
'@date' => $date,
]);
if ($excepted) {
while ($timeout > 0) {
$timeout--;
try {
$this
->assertSession()
->pageTextContains($message);
break;
} catch (ResponseTextException $e) {
if ($timeout <= 0) {
throw $e;
}
}
if ($timeout > 0) {
sleep(1);
}
}
}
else {
sleep($timeout);
$this
->assertSession()
->pageTextNotContains($message);
}
}
protected function waitForAutosaveSubmits($count) {
$state = \Drupal::state();
$state
->resetCache();
$start_count = $state
->get('autosave_submit_count', 0);
$system_slowness_factor = 5;
$timeout = $this->interval / 1000 * $system_slowness_factor * $count;
$deadline_time = time() + $timeout;
while (TRUE) {
$state
->resetCache();
$current_count = $state
->get('autosave_submit_count', 0);
if ($current_count >= $start_count + $count) {
return TRUE;
}
if (time() >= $deadline_time) {
return FALSE;
}
usleep(100000);
}
}
protected function assertAutosaveFormLibraryLoaded($loaded) {
$dom_loaded = $this
->getSession()
->evaluateScript('Drupal.hasOwnProperty("autosaveForm")');
$this
->assertEquals($loaded, $dom_loaded);
}
protected function pressAutosaveRestoreButton() {
$page = $this
->getSession()
->getPage();
$restore_button = $page
->find('css', '.autosave-form-resume-button');
$this
->assertNotEmpty($restore_button);
$restore_button
->press();
}
protected function waitForAutosaveResumeButtonToDisappear($timeout = 30000) {
$restored = $this
->waitForElementToDisappear('css', '.autosave-form-resume-button', $timeout);
$this
->logHtmlOutput(__FUNCTION__ . ' after resume button disappears');
$this
->assertTrue($restored);
return $restored;
}
protected function waitForElementToDisappear($selector, $locator, $timeout = 10000) {
$page = $this
->getSession()
->getPage();
$result = $page
->waitFor($timeout / 1000, function () use ($page, $selector, $locator) {
$element = $page
->find($selector, $locator);
if (empty($element)) {
return TRUE;
}
return FALSE;
});
return $result;
}
protected function logHtmlOutput($debug_text = NULL) {
if ($this->htmlOutputEnabled) {
$html_output = 'Current URL: ' . $this
->getSession()
->getCurrentUrl();
if ($debug_text) {
$html_output .= '<hr />' . $debug_text;
}
$html_output .= '<hr />' . $this
->getSession()
->getPage()
->getContent();
$html_output .= $this
->getHtmlOutputHeaders();
$this
->htmlOutput($html_output);
}
}
protected function assertAutosaveIsRunning($running) {
$script = <<<EndOfScript
(function () {
if (typeof Drupal.autosaveForm !== 'undefined') {
return Drupal.autosaveForm.autosaveFormRunning;
}
else {
return FALSE;
}
})();
EndOfScript;
$is_running = $this
->getSession()
->evaluateScript($script);
$this
->assertEquals($running, $is_running);
}
protected abstract function getUserPermissions();
}