View source
<?php
namespace Drupal\Tests\thunder\FunctionalJavascript\Integration;
use Drupal\Tests\thunder\FunctionalJavascript\ThunderFormFieldTestTrait;
use Drupal\Tests\thunder\FunctionalJavascript\ThunderJavascriptTestBase;
use Drupal\Tests\thunder\FunctionalJavascript\ThunderParagraphsTestTrait;
class AutosaveFormTest extends ThunderJavascriptTestBase {
use ThunderFormFieldTestTrait;
use ThunderParagraphsTestTrait;
protected function setUp() {
parent::setUp();
\Drupal::configFactory()
->getEditable('autosave_form.settings')
->set('interval', 2000)
->save();
}
public function testAutosaveInExistingEntity() {
$this
->drupalGet('node/7/edit');
$page = $this
->getSession()
->getPage();
$this
->makeFormChanges();
$this
->drupalGet('node/7/edit');
$this
->pressRejectButton();
$this
->assertEquals([
5,
], $page
->findField('field_tags[]')
->getValue());
$this
->assertEquals('Come to DrupalCon New Orleans', $page
->findField('title[0][value]')
->getValue());
$this
->assertEmpty($page
->find('css', '.form-item-field-paragraphs-3-subform-field-text-0-value'));
$this
->makeFormChanges();
$this
->drupalGet('node/7/edit');
$this
->pressRestoreButton();
$this
->assertEquals([
5,
'$ID:Tag2',
], $page
->findField('field_tags[]')
->getValue());
$this
->assertEquals('New title', $page
->findField('title[0][value]')
->getValue());
$this
->assertNotEmpty($page
->find('css', '.form-item-field-paragraphs-3-subform-field-text-0-value'));
$this
->clickSave();
$this
->assertSession()
->pageTextContains('New title is scheduled to be published');
$this
->assertSession()
->pageTextContains('Awesome quote');
}
protected function pressRestoreButton() {
$page = $this
->getSession()
->getPage();
$this
->assertSession()
->waitForText('A version of this page you were editing at');
$restore_button = $page
->find('css', '.autosave-form-resume-button');
$restore_button
->press();
}
protected function pressRejectButton() {
$page = $this
->getSession()
->getPage();
$this
->assertSession()
->waitForText('A version of this page you were editing at');
$reject_button = $page
->find('css', '.autosave-form-reject-button');
$reject_button
->press();
}
protected function makeFormChanges() {
$page = $this
->getSession()
->getPage();
$this
->expandAllTabs();
$this
->addTextParagraph('field_paragraphs', 'Awesome quote', 'quote');
$startTimestamp = strtotime('-2 days');
$endTimestamp = strtotime('+1 day');
$fieldValues = [
'title[0][value]' => 'New title',
'field_tags[]' => [
[
5,
'Drupal',
],
'Tag2',
],
'publish_on[0][value][date]' => date('Y-m-d', $startTimestamp),
'publish_on[0][value][time]' => date('H:i:s', $startTimestamp),
'unpublish_on[0][value][date]' => date('Y-m-d', $endTimestamp),
'unpublish_on[0][value][time]' => date('H:i:s', $endTimestamp),
'publish_state[0]' => 'published',
'unpublish_state[0]' => 'unpublished',
];
$this
->setFieldValues($page, $fieldValues);
sleep(3);
}
}