You are here

protected function ContentEntityAutosaveFormTestBase::doTestAutosaveAfterFormValidationFail in Autosave Form 8

Tests the autosave message not being shown on reload after validation fail.

1 call to ContentEntityAutosaveFormTestBase::doTestAutosaveAfterFormValidationFail()
ContentEntityAutosaveFormTestBase::testAutosaveForms in tests/src/FunctionalJavascript/ContentEntity/ContentEntityAutosaveFormTestBase.php
Tests autosave.

File

tests/src/FunctionalJavascript/ContentEntity/ContentEntityAutosaveFormTestBase.php, line 222

Class

ContentEntityAutosaveFormTestBase
Base test class for testing autosave support for entity forms.

Namespace

Drupal\Tests\autosave_form\FunctionalJavascript\ContentEntity

Code

protected function doTestAutosaveAfterFormValidationFail() {

  // Create a test entity and ensure that the required field is not filled in
  // order to trigger a validation error on entity form submission.
  $entity = $this
    ->createTestEntity();

  // Disable the HTML5 validation as it prevents the form submission when a
  // required field is empty, which however we want to do on purpose to test
  // how autosave_form behaves when a form is returned with validation errors,
  // but for that it has to be submitted first.
  \Drupal::state()
    ->set('disable_html5_validation', TRUE);
  $entity_form_edit_url = $entity
    ->toUrl('edit-form');
  $this
    ->drupalGet($entity_form_edit_url);

  // Assure that the initial autosave submission for gathering initial input
  // has run.
  $this
    ->assertTrue($this
    ->waitForAutosaveSubmits(1));

  // Make the first change to trigger an autosave state creation, but do not
  // fill the required field.
  $this
    ->alterTitleField();

  // Ensure a validation fail will occur.
  $this
    ->emptyRequiredFieldTestAutosaveAfterFormValidationFail();

  // Ensure an autosave state is saved.
  $this
    ->assertTrue($this
    ->waitForAutosaveSubmits(2));
  $before_submission_autosave_entries = $this
    ->getCountAutosaveEntries($entity
    ->id());
  $this
    ->assertTrue($before_submission_autosave_entries > 0);

  // Submit the form.
  $this
    ->saveForm();

  // Do not prevent the HTML5 validation anymore.
  \Drupal::state()
    ->delete('disable_html5_validation');
  $this
    ->logHtmlOutput(__FUNCTION__ . ' after validation fail.');

  // Ensure the validation fail message is shown.
  $error_messages = $this
    ->getSession()
    ->getPage()
    ->find('css', '.messages--error');
  $this
    ->assertNotNull($error_messages);

  // Ensure that the autosave resume/discard message is not shown.
  $this
    ->assertAutosaveResumeDiscardMessageIsShown(FALSE, $this
    ->getLastAutosaveTimestamp($entity
    ->id()));

  // Ensure that autosave submissions are running.
  $this
    ->assertTrue($this
    ->waitForAutosaveSubmits(2));

  // Ensure no further autosave states are being created without changes.
  $this
    ->assertEquals($before_submission_autosave_entries, $this
    ->getCountAutosaveEntries($entity
    ->id()));
}